Cleaned up Beaglebone sensors/actuators/PWM code
[matches/MCTX3420.git] / BBB code / GetData_real.c
1 char adc_dir = "/sys/devices/platform/tsc/ain";
2 char pin_dir = "/sys/class/gpio/gpio";
3
4 /** Open an ADC and return the voltage value from it
5 *       @param adc_num - ADC number, ranges from 0 to 7 on the Beaglebone
6         @return the converted voltage value if successful
7 */
8
9 //TODO: create a function to lookup the ADC or pin number instead of manually
10 //              specifying it here (so we can keep all the numbers in one place)
11
12 int OpenAnalog(int adc_num)
13 {
14         char adc_path[40];
15         snprintf(adc_path, sizeof(adc_path), "%s%d", adc_dir, adc_num);         //construct ADC path
16         int sensor = open(adc_path, O_RDONLY);
17         char buffer[128];                                                               //I think ADCs are only 12 bits (0-4096)
18         int read = read(sensor, buffer, sizeof(buffer); //buffer can probably be smaller
19         if (read != -1) {
20                 buffer[read] = NULL;
21                 int value = atoi(buffer);
22                 double convert = (value/4096) * 1000;           //random conversion factor, will be different for each sensor
23                 //lseek(sensor, 0, 0);
24                 close(sensor);
25                 return convert;
26         }
27         else {
28                 perror("Failed to get value from sensor");
29                 close(sensor);
30                 return -1;
31         }
32 }
33
34 /** Open a digital pin and return the value from it
35 *       @param pin_num - pin number, specified by electronics team
36         @return 1 or 0 if reading was successful
37 */
38
39 int OpenDigital(int pin_num)
40 {
41         char pin_path[40];
42         snprintf(pin_path, sizeof(pin_path), "%s%d%s", pin_dir, pin_num, "/value");     //construct pin path
43         int pin = open(pin_path, O_RDONLY);
44         char ch;
45         lseek(fd, 0, SEEK_SET);
46         int read = read(pin, &ch, sizeof(ch);
47         if (read != -1) {
48                 if (ch != '0') {
49                         close(pin);
50                         return 1;
51                 }
52                 else {
53                         close(pin);
54                         return 0;
55                 }
56         else {
57                 perror("Failed to get value from pin");
58                 close(pin);
59                 return -1;
60         }
61 }
62
63 /**
64  * Read a DataPoint from a Sensor; block until value is read
65  * @param id - The ID of the sensor
66  * @param d - DataPoint to set
67  * @returns True if the DataPoint was different from the most recently recorded.
68  */
69 bool Sensor_Read(Sensor * s, DataPoint * d)
70 {
71         
72         // Set time stamp
73         struct timeval t;
74         gettimeofday(&t, NULL);
75         d->time_stamp = TIMEVAL_DIFF(t, g_options.start_time);
76
77         // Read value based on Sensor Id
78         switch (s->id)
79         {
80                 case ANALOG_TEST0:
81                         d->value = OpenAnalog(0);       //ADC #0 on the Beaglebone
82                         break;
83                 case ANALOG_TEST1:
84                 {
85                         d->value = OpenAnalog(1);
86                         break;
87                 }
88                 case ANALOG_FAIL0:
89                         d->value = (double)(rand() % 6) * -( rand() % 2) / ( rand() % 100 + 1);
90                         //Gives a value between -5 and 5
91                         break;
92                 case DIGITAL_TEST0:
93                         d->value = openDigital(0);      //replace 0 with correct pin number
94                         break;
95                 case DIGITAL_TEST1:
96                         d->value = openDigital(1);      //replace 1 with correct pin number
97                         break;
98                 case DIGITAL_FAIL0:
99                         if( rand() % 100 > 98)
100                                 d->value = 2;
101                         d->value = rand() % 2; 
102                         //Gives 0 or 1 or a 2 every 1/100 times
103                         break;
104                 default:
105                         Fatal("Unknown sensor id: %d", s->id);
106                         break;
107         }       
108         usleep(100000); // simulate delay in sensor polling
109
110         // Perform sanity check based on Sensor's ID and the DataPoint
111         Sensor_CheckData(s->id, d->value);
112
113         // Update latest DataPoint if necessary
114         bool result = (d->value != s->newest_data.value);
115         if (result)
116         {
117                 s->newest_data.time_stamp = d->time_stamp;
118                 s->newest_data.value = d->value;
119         }
120         return result;
121 }

UCC git Repository :: git.ucc.asn.au