Showing the changes to interface/src/SerialInterface.cpp (1 of 2 files). Show all
Unified · Split
| @@ -6,12 +6,12 @@ · 5 unmodified lines | |||
| 6 | // | 6 | // |
| 7 | // Channels are received in the following order (integer 0-4096 based on voltage 0-3.3v) | 7 | // Channels are received in the following order (integer 0-4096 based on voltage 0-3.3v) |
| 8 | // | 8 | // |
| 9 | // AIN 15: Pitch Gyro (nodding your head 'yes') | 9 | // 0 - AIN 15: Pitch Gyro (nodding your head 'yes') |
| 10 | // AIN 16: Yaw Gyro (shaking your head 'no') | 10 | // 1 - AIN 16: Yaw Gyro (shaking your head 'no') |
| 11 | // AIN 17: Roll Gyro (looking quizzical, tilting your head) | 11 | // 2 - AIN 17: Roll Gyro (looking quizzical, tilting your head) |
| 12 | // AIN 18: Lateral acceleration (moving from side-to-side in front of your monitor) | 12 | // 3 - AIN 18: Lateral acceleration (moving from side-to-side in front of your monitor) |
| 13 | // AIN 19: Up/Down acceleration (sitting up/ducking in front of your monitor) | 13 | // 4 - AIN 19: Up/Down acceleration (sitting up/ducking in front of your monitor) |
| 14 | // AIN 20: Forward/Back acceleration (Toward or away from your monitor) | 14 | // 5 - AIN 20: Forward/Back acceleration (Toward or away from your monitor) |
| 15 | // | 15 | // |
| 16 | 16 | ||
| 17 | #include "SerialInterface.h" | 17 | #include "SerialInterface.h" |
| @@ -21,14 +21,15 @@ · 3 unmodified lines | |||
| 21 | #include <sys/time.h> | 21 | #include <sys/time.h> |
| 22 | #endif | 22 | #endif |
| 23 | 23 | ||
| 24 | int serial_fd; | 24 | int serialFd; |
| 25 | const int MAX_BUFFER = 255; | 25 | const int MAX_BUFFER = 255; |
| 26 | char serial_buffer[MAX_BUFFER]; | 26 | char serialBuffer[MAX_BUFFER]; |
| 27 | int serial_buffer_pos = 0; | 27 | int serialBufferPos = 0; |
| 28 | 28 | ||
| 29 | const int ZERO_OFFSET = 2048; | 29 | const int ZERO_OFFSET = 2048; |
| 30 | const short NO_READ_MAXIMUM_MSECS = 3000; | 30 | const short NO_READ_MAXIMUM_MSECS = 3000; |
| 31 | const short SAMPLES_TO_DISCARD = 100; | 31 | const short SAMPLES_TO_DISCARD = 100; // Throw out the first few samples |
| 32 | const int GRAVITY_SAMPLES = 200; // Use the first samples to compute gravity vector | ||
| 32 | 33 | ||
| 33 | void SerialInterface::pair() { | 34 | void SerialInterface::pair() { |
| 34 | 35 | ||
| @@ -48,7 +49,7 @@ void SerialInterface::pair() { · 13 unmodified lines | |||
| 48 | char *serialPortname = new char[100]; | 49 | char *serialPortname = new char[100]; |
| 49 | sprintf(serialPortname, "/dev/%s", entry->d_name); | 50 | sprintf(serialPortname, "/dev/%s", entry->d_name); |
| 50 | 51 | ||
| 51 | init(serialPortname, 115200); | 52 | initializePort(serialPortname, 115200); |
| 52 | 53 | ||
| 53 | delete [] serialPortname; | 54 | delete [] serialPortname; |
| 54 | } | 55 | } |
| @@ -60,18 +61,20 @@ void SerialInterface::pair() { · 5 unmodified lines | |||
| 60 | 61 | ||
| 61 | } | 62 | } |
| 62 | 63 | ||
| 63 | // Init the serial port to the specified values | 64 | // Connect to the serial port |
| 64 | int SerialInterface::init(char* portname, int baud) | 65 | int SerialInterface::initializePort(char* portname, int baud) |
| 65 | { | 66 | { |
| 66 | #ifdef __APPLE__ | 67 | #ifdef __APPLE__ |
| 67 | serial_fd = open(portname, O_RDWR | O_NOCTTY | O_NDELAY); | 68 | serialFd = open(portname, O_RDWR | O_NOCTTY | O_NDELAY); |
| 68 | 69 | ||
| 69 | printf("Attemping to open serial interface: %s\n", portname); | 70 | printf("Opening SerialUSB %s: ", portname); |
| 70 | |||
| 71 | if (serial_fd == -1) return -1; // Failed to open port | ||
| 72 | 71 | ||
| 72 | if (serialFd == -1) { | ||
| 73 | printf("Failed.\n"); | ||
| 74 | return -1; // Failed to open port | ||
| 75 | } | ||
| 73 | struct termios options; | 76 | struct termios options; |
| 74 | tcgetattr(serial_fd,&options); | 77 | tcgetattr(serialFd,&options); |
| 75 | switch(baud) | 78 | switch(baud) |
| 76 | { | 79 | { |
| 77 | case 9600: cfsetispeed(&options,B9600); | 80 | case 9600: cfsetispeed(&options,B9600); |
| @@ -95,10 +98,10 @@ int SerialInterface::init(char* portname, int baud) · 17 unmodified lines | |||
| 95 | options.c_cflag &= ~CSTOPB; | 98 | options.c_cflag &= ~CSTOPB; |
| 96 | options.c_cflag &= ~CSIZE; | 99 | options.c_cflag &= ~CSIZE; |
| 97 | options.c_cflag |= CS8; | 100 | options.c_cflag |= CS8; |
| 98 | tcsetattr(serial_fd,TCSANOW,&options); | 101 | tcsetattr(serialFd,TCSANOW,&options); |
| 99 | 102 | ||
| 100 | 103 | ||
| 101 | printf("Serial interface opened!\n"); | 104 | printf("Connected.\n"); |
| 102 | resetSerial(); | 105 | resetSerial(); |
| 103 | active = true; | 106 | active = true; |
| 104 | #endif | 107 | #endif |
| @@ -126,22 +129,17 @@ void SerialInterface::renderLevels(int width, int height) { · 21 unmodified lines | |||
| 126 | glBegin(GL_LINES); | 129 | glBegin(GL_LINES); |
| 127 | glVertex2f(disp_x, height*0.95); | 130 | glVertex2f(disp_x, height*0.95); |
| 128 | glVertex2f(disp_x, height*(0.25 + 0.75f*getValue(i)/4096)); | 131 | glVertex2f(disp_x, height*(0.25 + 0.75f*getValue(i)/4096)); |
| 132 | glColor4f(1, 0, 0, 1); | ||
| 133 | glVertex2f(disp_x - 3, height*(0.25 + 0.75f*getValue(i)/4096)); | ||
| 134 | glVertex2f(disp_x, height*(0.25 + 0.75f*getValue(i)/4096)); | ||
| 129 | glEnd(); | 135 | glEnd(); |
| 130 | // Trailing Average value | 136 | // Trailing Average value |
| 131 | glColor4f(1, 1, 0, 1); | ||
| 132 | glBegin(GL_LINES); | 137 | glBegin(GL_LINES); |
| 133 | glVertex2f(disp_x + 2, height*0.95); | 138 | glColor4f(1, 1, 1, 1); |
| 134 | glVertex2f(disp_x + 2, height*(0.25 + 0.75f*getTrailingValue(i)/4096)); | 139 | glVertex2f(disp_x, height*(0.25 + 0.75f*getTrailingValue(i)/4096)); |
| 140 | glVertex2f(disp_x + 4, height*(0.25 + 0.75f*getTrailingValue(i)/4096)); | ||
| 135 | glEnd(); | 141 | glEnd(); |
| 136 | 142 | ||
| 137 | /* | ||
| 138 | glColor3f(1,0,0); | ||
| 139 | glBegin(GL_LINES); | ||
| 140 | glLineWidth(4.0); | ||
| 141 | glVertex2f(disp_x - 10, height*0.5 - getValue(i)/4096); | ||
| 142 | glVertex2f(disp_x + 10, height*0.5 - getValue(i)/4096); | ||
| 143 | glEnd(); | ||
| 144 | */ | ||
| 145 | sprintf(val, "%d", getValue(i)); | 143 | sprintf(val, "%d", getValue(i)); |
| 146 | drawtext(disp_x-GAP/2, (height*0.95)+2, 0.08, 90, 1.0, 0, val, 0, 1, 0); | 144 | drawtext(disp_x-GAP/2, (height*0.95)+2, 0.08, 90, 1.0, 0, val, 0, 1, 0); |
| 147 | 145 | ||
| @@ -162,20 +160,20 @@ void SerialInterface::renderLevels(int width, int height) { · 14 unmodified lines | |||
| 162 | } | 160 | } |
| 163 | void SerialInterface::readData() { | 161 | void SerialInterface::readData() { |
| 164 | #ifdef __APPLE__ | 162 | #ifdef __APPLE__ |
| 165 | // This array sets the rate of trailing averaging for each channel. | 163 | // This array sets the rate of trailing averaging for each channel: |
| 166 | // If the sensor rate is 100Hz, 0.001 will make the long term average a 10-second average | 164 | // If the sensor rate is 100Hz, 0.001 will make the long term average a 10-second average |
| 167 | const float AVG_RATE[] = {0.01, 0.01, 0.01, 0.01, 0.01, 0.01}; | 165 | const float AVG_RATE[] = {0.002, 0.002, 0.002, 0.002, 0.002, 0.002}; |
| 168 | char bufchar[1]; | 166 | char bufchar[1]; |
| 169 | 167 | ||
| 170 | int initialSamples = totalSamples; | 168 | int initialSamples = totalSamples; |
| 171 | 169 | ||
| 172 | while (read(serial_fd, &bufchar, 1) > 0) { | 170 | while (read(serialFd, &bufchar, 1) > 0) { |
| 173 | //std::cout << bufchar[0]; | 171 | //std::cout << bufchar[0]; |
| 174 | serial_buffer[serial_buffer_pos] = bufchar[0]; | 172 | serialBuffer[serialBufferPos] = bufchar[0]; |
| 175 | serial_buffer_pos++; | 173 | serialBufferPos++; |
| 176 | // Have we reached end of a line of input? | 174 | // Have we reached end of a line of input? |
| 177 | if ((bufchar[0] == '\n') || (serial_buffer_pos >= MAX_BUFFER)) { | 175 | if ((bufchar[0] == '\n') || (serialBufferPos >= MAX_BUFFER)) { |
| 178 | std::string serialLine(serial_buffer, serial_buffer_pos-1); | 176 | std::string serialLine(serialBuffer, serialBufferPos-1); |
| 179 | //std::cout << serialLine << "\n"; | 177 | //std::cout << serialLine << "\n"; |
| 180 | int spot; | 178 | int spot; |
| 181 | //int channel = 0; | 179 | //int channel = 0; |
| @@ -191,6 +189,7 @@ void SerialInterface::readData() { · 9 unmodified lines | |||
| 191 | serialLine = serialLine.substr(spot+1, serialLine.length() - spot - 1); | 189 | serialLine = serialLine.substr(spot+1, serialLine.length() - spot - 1); |
| 192 | } | 190 | } |
| 193 | 191 | ||
| 192 | // Update Trailing Averages | ||
| 194 | for (int i = 0; i < NUM_CHANNELS; i++) { | 193 | for (int i = 0; i < NUM_CHANNELS; i++) { |
| 195 | if (totalSamples > SAMPLES_TO_DISCARD) { | 194 | if (totalSamples > SAMPLES_TO_DISCARD) { |
| 196 | trailingAverage[i] = (1.f - AVG_RATE[i])*trailingAverage[i] + | 195 | trailingAverage[i] = (1.f - AVG_RATE[i])*trailingAverage[i] + |
| @@ -200,8 +199,21 @@ void SerialInterface::readData() { · 3 unmodified lines | |||
| 200 | } | 199 | } |
| 201 | 200 | ||
| 202 | } | 201 | } |
| 202 | |||
| 203 | // Use a set of initial samples to compute gravity | ||
| 204 | if (totalSamples < GRAVITY_SAMPLES) { | ||
| 205 | gravity.x += lastMeasured[ACCEL_X]; | ||
| 206 | gravity.y += lastMeasured[ACCEL_Y]; | ||
| 207 | gravity.z += lastMeasured[ACCEL_Z]; | ||
| 208 | } | ||
| 209 | if (totalSamples == GRAVITY_SAMPLES) { | ||
| 210 | gravity = glm::normalize(gravity); | ||
| 211 | std::cout << "gravity: " << gravity.x << "," << | ||
| 212 | gravity.y << "," << gravity.z << "\n"; | ||
| 213 | } | ||
| 214 | |||
| 203 | totalSamples++; | 215 | totalSamples++; |
| 204 | serial_buffer_pos = 0; | 216 | serialBufferPos = 0; |
| 205 | } | 217 | } |
| 206 | } | 218 | } |
| 207 | 219 | ||
| @@ -210,7 +222,7 @@ void SerialInterface::readData() { · 2 unmodified lines | |||
| 210 | gettimeofday(&now, NULL); | 222 | gettimeofday(&now, NULL); |
| 211 | 223 | ||
| 212 | if (diffclock(&lastGoodRead, &now) > NO_READ_MAXIMUM_MSECS) { | 224 | if (diffclock(&lastGoodRead, &now) > NO_READ_MAXIMUM_MSECS) { |
| 213 | std::cout << "No data coming over serial. Shutting down SerialInterface.\n"; | 225 | std::cout << "No data - Shutting down SerialInterface.\n"; |
| 214 | resetSerial(); | 226 | resetSerial(); |
| 215 | } | 227 | } |
| 216 | } else { | 228 | } else { |
| @@ -223,6 +235,7 @@ void SerialInterface::resetSerial() { · 6 unmodified lines | |||
| 223 | #ifdef __APPLE__ | 235 | #ifdef __APPLE__ |
| 224 | active = false; | 236 | active = false; |
| 225 | totalSamples = 0; | 237 | totalSamples = 0; |
| 238 | gravity = glm::vec3(0,-1,0); | ||
| 226 | 239 | ||
| 227 | gettimeofday(&lastGoodRead, NULL); | 240 | gettimeofday(&lastGoodRead, NULL); |
| 228 | 241 | ||
| @@ -233,7 +246,7 @@ void SerialInterface::resetSerial() { · 4 unmodified lines | |||
| 233 | } | 246 | } |
| 234 | // Clear serial input buffer | 247 | // Clear serial input buffer |
| 235 | for (int i = 1; i < MAX_BUFFER; i++) { | 248 | for (int i = 1; i < MAX_BUFFER; i++) { |
| 236 | serial_buffer[i] = ' '; | 249 | serialBuffer[i] = ' '; |
| 237 | } | 250 | } |
| 238 | #endif | 251 | #endif |
| 239 | } | 252 | } |