Showing the changes to interface/src/SerialInterface.cpp (1 of 1 file). Show all
Unified · Split
| @@ -17,7 +17,7 @@ · 16 unmodified lines | |||
| 17 | #include "SerialInterface.h" | 17 | #include "SerialInterface.h" |
| 18 | 18 | ||
| 19 | int serial_fd; | 19 | int serial_fd; |
| 20 | const int MAX_BUFFER = 100; | 20 | const int MAX_BUFFER = 255; |
| 21 | char serial_buffer[MAX_BUFFER]; | 21 | char serial_buffer[MAX_BUFFER]; |
| 22 | int serial_buffer_pos = 0; | 22 | int serial_buffer_pos = 0; |
| 23 | int samples_total = 0; | 23 | int samples_total = 0; |
| @@ -64,6 +64,11 @@ int SerialInterface::init(char * portname, int baud) · 40 unmodified lines | |||
| 64 | lastMeasured[i] = 0; | 64 | lastMeasured[i] = 0; |
| 65 | trailingAverage[i] = 0.0; | 65 | trailingAverage[i] = 0.0; |
| 66 | } | 66 | } |
| 67 | // Clear serial input buffer | ||
| 68 | for (int i = 1; i < MAX_BUFFER; i++) { | ||
| 69 | serial_buffer[i] = ' '; | ||
| 70 | } | ||
| 71 | |||
| 67 | 72 | ||
| 68 | return 0; // Init serial port was a success | 73 | return 0; // Init serial port was a success |
| 69 | } | 74 | } |
| @@ -127,42 +132,40 @@ void SerialInterface::readData() { · 57 unmodified lines | |||
| 127 | // If the sensor rate is 100Hz, 0.001 will make the long term average a 10-second average | 132 | // If the sensor rate is 100Hz, 0.001 will make the long term average a 10-second average |
| 128 | const float AVG_RATE[] = {0.01, 0.01, 0.01, 0.01, 0.01, 0.01}; | 133 | const float AVG_RATE[] = {0.01, 0.01, 0.01, 0.01, 0.01, 0.01}; |
| 129 | char bufchar[1]; | 134 | char bufchar[1]; |
| 130 | while (read(serial_fd, bufchar, 1) > 0) | 135 | |
| 136 | while (read(serial_fd, &bufchar, 1) > 0) | ||
| 131 | { | 137 | { |
| 138 | //std::cout << bufchar[0]; | ||
| 132 | serial_buffer[serial_buffer_pos] = bufchar[0]; | 139 | serial_buffer[serial_buffer_pos] = bufchar[0]; |
| 133 | serial_buffer_pos++; | 140 | serial_buffer_pos++; |
| 134 | // Have we reached end of a line of input? | 141 | // Have we reached end of a line of input? |
| 135 | if ((bufchar[0] == '\n') || (serial_buffer_pos >= MAX_BUFFER)) | 142 | if ((bufchar[0] == '\n') || (serial_buffer_pos >= MAX_BUFFER)) |
| 136 | { | 143 | { |
| 137 | // At end - Extract value from string to variables | 144 | std::string serialLine(serial_buffer, serial_buffer_pos-1); |
| 138 | if (serial_buffer[0] != 'p') | 145 | //std::cout << serialLine << "\n"; |
| 139 | { | 146 | int spot; |
| 140 | // NOTE: This is stupid, need to rewrite to properly scan for NUM_CHANNELS + 2 | 147 | //int channel = 0; |
| 141 | // | 148 | std::string val; |
| 142 | sscanf(serial_buffer, "%d %d %d %d %d %d %d %d", | 149 | for (int i = 0; i < NUM_CHANNELS + 2; i++) { |
| 143 | &lastMeasured[0], | 150 | spot = serialLine.find_first_of(" ", 0); |
| 144 | &lastMeasured[1], | 151 | if (spot != std::string::npos) { |
| 145 | &lastMeasured[2], | 152 | val = serialLine.substr(0,spot); |
| 146 | &lastMeasured[3], | 153 | //std::cout << val << "\n"; |
| 147 | &lastMeasured[4], | 154 | if (i < NUM_CHANNELS) lastMeasured[i] = atoi(val.c_str()); |
| 148 | &lastMeasured[5], | 155 | else samplesAveraged = atoi(val.c_str()); |
| 149 | &samplesAveraged, | 156 | } else LED = atoi(serialLine.c_str()); |
| 150 | &LED | 157 | serialLine = serialLine.substr(spot+1, serialLine.length() - spot - 1); |
| 151 | ); | 158 | } |
| 152 | for (int i = 0; i < NUM_CHANNELS; i++) | 159 | //std::cout << LED << "\n"; |
| 153 | { | 160 | |
| 154 | if (samples_total > 0) | 161 | for (int i = 0; i < NUM_CHANNELS; i++) { |
| 155 | trailingAverage[i] = (1.f - AVG_RATE[i])*trailingAverage[i] + | 162 | if (samples_total > 0) |
| 156 | AVG_RATE[i]*(float)lastMeasured[i]; | 163 | trailingAverage[i] = (1.f - AVG_RATE[i])*trailingAverage[i] + |
| 157 | else | 164 | AVG_RATE[i]*(float)lastMeasured[i]; |
| 158 | { | 165 | else trailingAverage[i] = (float)lastMeasured[i]; |
| 159 | trailingAverage[i] = (float)lastMeasured[i]; | 166 | |
| 160 | } | ||
| 161 | } | ||
| 162 | samples_total++; | ||
| 163 | } | 167 | } |
| 164 | // Clear rest of string for printing onscreen | 168 | samples_total++; |
| 165 | while(serial_buffer_pos++ < MAX_BUFFER) serial_buffer[serial_buffer_pos] = ' '; | ||
| 166 | serial_buffer_pos = 0; | 169 | serial_buffer_pos = 0; |
| 167 | } | 170 | } |
| 168 | } | 171 | } |