X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=BBB%20code%2FGetData_real.c;h=c724e6255f4f3a9d45237bd41b56d280f591c66e;hb=c160e49480a1b7771bb00df7404a417a4892d736;hp=2192772818ba7006208e36bc3ab2d26cbe9b08a2;hpb=1a5cb99a6e794b4d51815aad67a73ae3f3c5b659;p=matches%2FMCTX3420.git diff --git a/BBB code/GetData_real.c b/BBB code/GetData_real.c index 2192772..c724e62 100644 --- a/BBB code/GetData_real.c +++ b/BBB code/GetData_real.c @@ -1,93 +1,61 @@ -DataPoint * GetData(int sensor_id, DataPoint * d) -{ - //TODO: We should ensure the time is *never* allowed to change on the server if we use gettimeofday - // Another way people might think of getting the time is to count CPU cycles with clock() - // But this will not work because a) CPU clock speed may change on some devices (RPi?) and b) It counts cycles used by all threads - gettimeofday(&(d->time_stamp), NULL); +#include "gpio.h" + +/** + * Read a DataPoint from a Sensor; block until value is read + * @param id - The ID of the sensor + * @param d - DataPoint to set + * @returns True if the DataPoint was different from the most recently recorded. + */ +bool Sensor_Read(Sensor * s, DataPoint * d) +{ - switch (sensor_id) + // Set time stamp + struct timeval t; + gettimeofday(&t, NULL); + d->time_stamp = TIMEVAL_DIFF(t, g_options.start_time); + + // Read value based on Sensor Id + switch (s->id) { - //TODO: test buffer size on actual hardware - // maybe map the sensor path to an array/structure that can be looked up via sensor_id? - // not sure about the best place to do unit conversions, especially for nonlinear sensors - case SENSOR_TEST0: + case ANALOG_TEST0: + d->value = ADCRead(0); //ADC #0 on the Beaglebone + break; + case ANALOG_TEST1: { - int sensor = open("/sys/devices/platform/tsc/ain0", O_RDONLY); //need unique path for each sensor ADC, check path in documentation - char buffer[128]; //ADCs on Beaglebone are 12 bits? - int read = read(sensor, buffer, sizeof(buffer); - if (read != -1) { - buffer[read] = NULL; //string returned by read is not null terminated - int value = atoi(buffer); - double convert = (value/4096) * 1800; //sample conversion from ADC input to 'true value' - d->value = convert; - lseek(sensor, 0, 0); //after read string must be rewound to start of file using lseek - } - else { - perror("Failed to get value from sensor"); - } - close(sensor); + d->value = ADCRead(1); break; } - case SENSOR_TEST1: - int sensor = open("/sys/devices/platform/tsc/ain1", O_RDONLY); - char buffer[128]; - int read = read(sensor, buffer, sizeof(buffer); - if (read != -1) { - buffer[read] = NULL; - int value = atoi(buffer); - double convert = (value/4096) * 1800; - d->value = convert; - lseek(sensor, 0, 0); - } - else { - perror("Failed to get value from sensor"); - } - close(sensor); - break; + case ANALOG_FAIL0: + d->value = (double)(rand() % 6) * -( rand() % 2) / ( rand() % 100 + 1); + //Gives a value between -5 and 5 break; - } - //TODO: think about a better way to address individual pins - // i.e. pass an int to a separate function that builds the correct filename - // doesn't really matter if the pins we're using are fixed though case DIGITAL_TEST0: - int fd = open("/sys/class/gpio/gpio17/value", O_RDONLY) //again need the right addresses for each pin - char ch; //just one character for binary info - lseek(fd, 0, SEEK_SET); - int read = read(fd, &ch, sizeof(ch); - if (read != -1) { - if (ch != '0') { - d->value = 1; - } - else { - d->value = 0; - } - } - else { - perror("Failed to get value from pin"); - } + d->value = pinRead(0); //replace 0 with correct pin number break; case DIGITAL_TEST1: - int fd = open("/sys/class/gpio/gpio23/value", O_RDONLY) - char ch; - lseek(fd, 0, SEEK_SET); - int read = read(fd, &ch, sizeof(ch); - if (read != -1) { - if (ch != '0') { - d->value = 1; - } - else { - d->value = 0; - } - } - else { - perror("Failed to get value from pin"); - } + d->value = pinRead(1); //replace 1 with correct pin number + break; + case DIGITAL_FAIL0: + if( rand() % 100 > 98) + d->value = 2; + d->value = rand() % 2; + //Gives 0 or 1 or a 2 every 1/100 times break; default: - Fatal("Unknown sensor id: %d", sensor_id); + Fatal("Unknown sensor id: %d", s->id); break; } usleep(100000); // simulate delay in sensor polling - return d; + // Perform sanity check based on Sensor's ID and the DataPoint + Sensor_CheckData(s->id, d->value); + + // Update latest DataPoint if necessary + bool result = (d->value != s->newest_data.value); + if (result) + { + s->newest_data.time_stamp = d->time_stamp; + s->newest_data.value = d->value; + } + return result; } \ No newline at end of file