Hubo Library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
GetAnalogInput.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <pthread.h>
4 #include <assert.h>
5 
6 #include "../hubolib.h"
7 
8 using namespace HuboLib;
9 
10 /*
11 Compile and link:
12  g++ GetAnalogInput.cpp -L../ -lhubo -lpthread -lrt -o GetAnalogInput
13 Run:
14  sudo ./GetAnalogInput
15 Purpose:
16  Reads the analog input as specified in the command line and returns the success of the operation.
17 */
18 
19 void BoostThreadPriority();
20 
21 int main(int argc, char* argv[])
22 {
23  // Lets have this program finish as soon as possible.
25 
26  if (argc != 2)
27  {
28  printf ("Usage: \n GetAnalogInput <analog input to read>\n example: GetAnalogInput 7 // this will read analog input 7 and return the result\n");
29  return 0;
30  }
31 
32  int channel = 0;
33  if (sscanf (argv[1], "%d", &channel) != 1)
34  {
35  printf ("Analog input could not be determined!\n");
36  return 0;
37  }
38 
39  // Initialize the library once in your program.
40  if (!Initialize())
41  {
42  printf ("Error: Initialize\n");
43  return 0;
44  }
45 
46  // Set the cycle time to 2ms (50Hz).
47  Set_Cycle_Time(20);
48 
49  // Define channel 0 to be read (set to 1) all other channels (set to 0) are not read from the ADC.
50  unsigned short overSampling[MAX_MCP3x08_CHANNELS] = { 1, 1, 1, 1, 1, 1, 1, 1 };
51  Set_MCP3x08_Oversampling (overSampling);
52 
53  // Wait as long until we have valid values in the buffer.
55 
56  unsigned long adcCount;
57  double volt;
58  // Read channel 0 and output its value.
59  if (Get_AI_Channel (channel, adcCount, volt))
60  printf ("%ld\n", adcCount);
61  else
62  printf ("-1\n");
63 
64  // Free library resources.
65  Uninitialize();
66 
67  return 1;
68 }
69 
71 {
72  // Scheduling params.
73  sched_param param;
74  int policy;
75  int ret;
76  pthread_t threadHandle = pthread_self();
77 
78  // Get current scheduling parameters of thread.
79  ret = pthread_getschedparam (threadHandle, &policy, &param);
80  assert (ret == 0);
81 
82  // Set scheduling parameters of thread to real time values (FIFO scheduling type and max prio).
83  policy = SCHED_FIFO; // SCHED_RR;
84  param.sched_priority = sched_get_priority_max(policy); // New max priority for new scheduling concept.
85  ret = pthread_setschedparam(threadHandle, policy, &param);
86  assert (ret == 0);
87 }
88 
89 
bool Wait_For_MCP3x08_Buffered_Values()
Waits until the input buffer of the MCP3x08 are initialized from the hardware.
#define MAX_MCP3x08_CHANNELS
Definition: hubolib.h:39
int main(int argc, char *argv[])
bool Initialize()
Initializes the library.
bool Set_Cycle_Time(long cycleTime)
Sets the backgrounds threads polling interval in ms.
bool Get_AI_Channel(int channel, unsigned long &count, double &volt)
Get the buffered and oversampled data from the MCP3x08.
bool Set_MCP3x08_Oversampling(unsigned short overSampling[MAX_MCP3x08_CHANNELS])
Specifies the ADC channels to be sampled as well as the number they get oversampled.
void Uninitialize()
Releases any resources bound to the library.
void BoostThreadPriority()