Showing the changes to interface/src/SerialInterface.cpp (1 of 3 files). Show all
Unified · Split
| @@ -15,6 +15,12 @@ · 14 unmodified lines | ||
| 15 | 15 | // |
| 16 | 16 | |
| 17 | 17 | #include "SerialInterface.h" |
| 18 | +#include <dirent.h> | |
| 19 | +#include <sys/time.h> | |
| 20 | +#ifdef __APPLE__ | |
| 21 | +#include <regex.h> | |
| 22 | +#endif | |
| 23 | + | |
| 18 | 24 | |
| 19 | 25 | int serial_fd; |
| 20 | 26 | const int MAX_BUFFER = 255; |
| @@ -23,12 +29,48 @@ int serial_buffer_pos = 0; · 2 unmodified lines | ||
| 23 | 29 | int samples_total = 0; |
| 24 | 30 | |
| 25 | 31 | const int ZERO_OFFSET = 2048; |
| 32 | +const short NO_READ_MAXIMUM = 10; | |
| 33 | + | |
| 34 | +SerialInterface::SerialInterface() { | |
| 35 | + active = false; | |
| 36 | + noReadCount = 0; | |
| 37 | +} | |
| 26 | 38 | |
| 27 | −// Init the serial port to the specified values | |
| 28 | −int SerialInterface::init(char * portname, int baud) | |
| 39 | +void SerialInterface::pair() { | |
| 40 | + // look for a matching gyro setup | |
| 41 | + DIR *devDir; | |
| 42 | + struct dirent *entry; | |
| 43 | + int matchStatus; | |
| 44 | + regex_t regex; | |
| 45 | + | |
| 46 | +#ifdef __APPLE__ | |
| 47 | + // for now this only works on OS X, where the usb serial shows up as /dev/tty.usb* | |
| 48 | + if((devDir = opendir("/dev"))) { | |
| 49 | + while((entry = readdir(devDir))) { | |
| 50 | + regcomp(®ex, "tty\\.usb", REG_EXTENDED|REG_NOSUB); | |
| 51 | + matchStatus = regexec(®ex, entry->d_name, (size_t) 0, NULL, 0); | |
| 52 | + if (matchStatus == 0) { | |
| 53 | + char *serialPortname = new char[100]; | |
| 54 | + sprintf(serialPortname, "/dev/%s", entry->d_name); | |
| 55 | + | |
| 56 | + init(serialPortname, 115200); | |
| 57 | + | |
| 58 | + delete [] serialPortname; | |
| 59 | + } | |
| 60 | + regfree(®ex); | |
| 61 | + } | |
| 62 | + closedir(devDir); | |
| 63 | + } | |
| 64 | +#endif | |
| 65 | +} | |
| 66 | + | |
| 67 | +// Init the serial port to the specified values | |
| 68 | +int SerialInterface::init(char* portname, int baud) | |
| 29 | 69 | { |
| 30 | 70 | serial_fd = open(portname, O_RDWR | O_NOCTTY | O_NDELAY); |
| 31 | 71 | |
| 72 | + printf("Attemping to open serial interface: %s\n", portname); | |
| 73 | + | |
| 32 | 74 | if (serial_fd == -1) return -1; // Failed to open port |
| 33 | 75 | |
| 34 | 76 | struct termios options; |
| @@ -68,9 +110,12 @@ int SerialInterface::init(char * portname, int baud) · 33 unmodified lines | ||
| 68 | 110 | for (int i = 1; i < MAX_BUFFER; i++) { |
| 69 | 111 | serial_buffer[i] = ' '; |
| 70 | 112 | } |
| 71 | − | |
| 72 | − | |
| 73 | − return 0; // Init serial port was a success | |
| 113 | + | |
| 114 | + printf("Serial interface opened!\n"); | |
| 115 | + | |
| 116 | + active = true; | |
| 117 | + | |
| 118 | + return 0; | |
| 74 | 119 | } |
| 75 | 120 | |
| 76 | 121 | // Reset Trailing averages to the current measurement |
| @@ -133,8 +178,12 @@ void SerialInterface::readData() { · 56 unmodified lines | ||
| 133 | 178 | const float AVG_RATE[] = {0.01, 0.01, 0.01, 0.01, 0.01, 0.01}; |
| 134 | 179 | char bufchar[1]; |
| 135 | 180 | |
| 181 | + bool atLeastOneRead = false; | |
| 182 | + | |
| 136 | 183 | while (read(serial_fd, &bufchar, 1) > 0) |
| 137 | 184 | { |
| 185 | + atLeastOneRead = true; | |
| 186 | + | |
| 138 | 187 | //std::cout << bufchar[0]; |
| 139 | 188 | serial_buffer[serial_buffer_pos] = bufchar[0]; |
| 140 | 189 | serial_buffer_pos++; |
| @@ -169,6 +218,28 @@ void SerialInterface::readData() { · 28 unmodified lines | ||
| 169 | 218 | serial_buffer_pos = 0; |
| 170 | 219 | } |
| 171 | 220 | } |
| 221 | + | |
| 222 | + if (!atLeastOneRead) { | |
| 223 | + noReadCount++; | |
| 224 | + std::cout << "#" << noReadCount << " blank read from serial.\n"; | |
| 225 | + if (noReadCount >= NO_READ_MAXIMUM) { | |
| 226 | + disconnectSerial(); | |
| 227 | + } | |
| 228 | + } | |
| 229 | +} | |
| 230 | + | |
| 231 | +void SerialInterface::disconnectSerial() { | |
| 232 | + std::cout << "Reached maximum blank read count. Shutting down serial.\n"; | |
| 233 | + | |
| 234 | + active = false; | |
| 235 | + noReadCount = 0; | |
| 236 | + | |
| 237 | + // Clear the measured and average channel data | |
| 238 | + for (int i = 1; i < NUM_CHANNELS; i++) | |
| 239 | + { | |
| 240 | + lastMeasured[i] = 0; | |
| 241 | + trailingAverage[i] = 0.0; | |
| 242 | + } | |
| 172 | 243 | } |
| 173 | 244 | |
| 174 | 245 | |