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