History

don't kill serial when polling too quickly

Stephen Birarda committed 13 years ago

Commit 9774dd51 parent fd73b8d2 files · +14 −15Browse files

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

Unified · Split

interface/src/SerialInterface.cpp+13−14
@@ -27,7 +27,7 @@ char serial_buffer[MAX_BUFFER]; · 26 unmodified lines
27int serial_buffer_pos = 0;27int serial_buffer_pos = 0;
28 28
29const int ZERO_OFFSET = 2048;29const int ZERO_OFFSET = 2048;
30const short NO_READ_MAXIMUM = 10;30const short NO_READ_MAXIMUM_MSECS = 3000;
31const short SAMPLES_TO_DISCARD = 100;31const short SAMPLES_TO_DISCARD = 100;
32 32
33void SerialInterface::pair() {33void SerialInterface::pair() {
@@ -166,14 +166,12 @@ void SerialInterface::readData() { · 132 unmodified lines
166 166
167 int initialSamples = totalSamples;167 int initialSamples = totalSamples;
168 168
169 while (read(serial_fd, &bufchar, 1) > 0)169 while (read(serial_fd, &bufchar, 1) > 0) {
170 {
171 //std::cout << bufchar[0];170 //std::cout << bufchar[0];
172 serial_buffer[serial_buffer_pos] = bufchar[0];171 serial_buffer[serial_buffer_pos] = bufchar[0];
173 serial_buffer_pos++;172 serial_buffer_pos++;
174 // Have we reached end of a line of input?173 // Have we reached end of a line of input?
175 if ((bufchar[0] == '\n') || (serial_buffer_pos >= MAX_BUFFER))174 if ((bufchar[0] == '\n') || (serial_buffer_pos >= MAX_BUFFER)) {
176 {
177 std::string serialLine(serial_buffer, serial_buffer_pos-1);175 std::string serialLine(serial_buffer, serial_buffer_pos-1);
178 //std::cout << serialLine << "\n";176 //std::cout << serialLine << "\n";
179 int spot;177 int spot;
@@ -204,26 +202,27 @@ void SerialInterface::readData() { · 24 unmodified lines
204 }202 }
205 }203 }
206 204
207 if (initialSamples == totalSamples) {205 if (initialSamples == totalSamples) {
208 noReadCount++;206 timeval now;
209 std::cout << "#" << noReadCount << " blank read from serial.\n";207 gettimeofday(&now, NULL);
210 208
211 if (noReadCount >= NO_READ_MAXIMUM) {209 if (diffclock(&lastGoodRead, &now) > NO_READ_MAXIMUM_MSECS) {
210 std::cout << "No data coming over serial. Shutting down SerialInterface.\n";
212 resetSerial();211 resetSerial();
213 }212 }
213 } else {
214 gettimeofday(&lastGoodRead, NULL);
214 }215 }
215}216}
216 217
217void SerialInterface::resetSerial() {218void SerialInterface::resetSerial() {
218 std::cout << "Reached maximum blank read count. Shutting down serial.\n";
219
220 active = false;219 active = false;
221 noReadCount = 0;
222 totalSamples = 0;220 totalSamples = 0;
223 221
222 gettimeofday(&lastGoodRead, NULL);
223
224 // Clear the measured and average channel data224 // Clear the measured and average channel data
225 for (int i = 0; i < NUM_CHANNELS; i++)225 for (int i = 0; i < NUM_CHANNELS; i++) {
226 {
227 lastMeasured[i] = 0;226 lastMeasured[i] = 0;
228 trailingAverage[i] = 0.0;227 trailingAverage[i] = 0.0;
229 }228 }