History

It's entirely possible that the serialInterface reader is a tiny bit better, but I'm really not sure. :) Fixed bug with reading from middle of serial string on startup causing hang.

Philip Rosedale committed 13 years ago

Commit 12e4e151 parent 2446e801 file · +33 −30Browse files

Showing the changes to interface/src/SerialInterface.cpp (1 of 1 file). Show all

Unified · Split

interface/src/SerialInterface.cpp+33−30
@@ -17,7 +17,7 @@ · 16 unmodified lines
17#include "SerialInterface.h"17#include "SerialInterface.h"
18 18
19int serial_fd;19int serial_fd;
20const int MAX_BUFFER = 100;20const int MAX_BUFFER = 255;
21char serial_buffer[MAX_BUFFER];21char serial_buffer[MAX_BUFFER];
22int serial_buffer_pos = 0;22int serial_buffer_pos = 0;
23int samples_total = 0;23int 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 success73 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 average132 // 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 variables144 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 + 2147 //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 &LED157 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 else164 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 onscreen168 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 }