don't kill serial when polling too quickly

This commit is contained in:
Stephen Birarda 2013-02-18 10:37:00 -08:00
parent fd73b8de82
commit 9774dd5874
2 changed files with 14 additions and 15 deletions

View file

@ -27,7 +27,7 @@ char serial_buffer[MAX_BUFFER];
int serial_buffer_pos = 0; int serial_buffer_pos = 0;
const int ZERO_OFFSET = 2048; const int ZERO_OFFSET = 2048;
const short NO_READ_MAXIMUM = 10; const short NO_READ_MAXIMUM_MSECS = 3000;
const short SAMPLES_TO_DISCARD = 100; const short SAMPLES_TO_DISCARD = 100;
void SerialInterface::pair() { void SerialInterface::pair() {
@ -166,14 +166,12 @@ void SerialInterface::readData() {
int initialSamples = totalSamples; int initialSamples = totalSamples;
while (read(serial_fd, &bufchar, 1) > 0) while (read(serial_fd, &bufchar, 1) > 0) {
{
//std::cout << bufchar[0]; //std::cout << bufchar[0];
serial_buffer[serial_buffer_pos] = bufchar[0]; serial_buffer[serial_buffer_pos] = bufchar[0];
serial_buffer_pos++; serial_buffer_pos++;
// Have we reached end of a line of input? // Have we reached end of a line of input?
if ((bufchar[0] == '\n') || (serial_buffer_pos >= MAX_BUFFER)) if ((bufchar[0] == '\n') || (serial_buffer_pos >= MAX_BUFFER)) {
{
std::string serialLine(serial_buffer, serial_buffer_pos-1); std::string serialLine(serial_buffer, serial_buffer_pos-1);
//std::cout << serialLine << "\n"; //std::cout << serialLine << "\n";
int spot; int spot;
@ -204,26 +202,27 @@ void SerialInterface::readData() {
} }
} }
if (initialSamples == totalSamples) { if (initialSamples == totalSamples) {
noReadCount++; timeval now;
std::cout << "#" << noReadCount << " blank read from serial.\n"; gettimeofday(&now, NULL);
if (noReadCount >= NO_READ_MAXIMUM) { if (diffclock(&lastGoodRead, &now) > NO_READ_MAXIMUM_MSECS) {
std::cout << "No data coming over serial. Shutting down SerialInterface.\n";
resetSerial(); resetSerial();
} }
} else {
gettimeofday(&lastGoodRead, NULL);
} }
} }
void SerialInterface::resetSerial() { void SerialInterface::resetSerial() {
std::cout << "Reached maximum blank read count. Shutting down serial.\n";
active = false; active = false;
noReadCount = 0;
totalSamples = 0; totalSamples = 0;
gettimeofday(&lastGoodRead, NULL);
// Clear the measured and average channel data // Clear the measured and average channel data
for (int i = 0; i < NUM_CHANNELS; i++) for (int i = 0; i < NUM_CHANNELS; i++) {
{
lastMeasured[i] = 0; lastMeasured[i] = 0;
trailingAverage[i] = 0.0; trailingAverage[i] = 0.0;
} }

View file

@ -49,8 +49,8 @@ private:
float trailingAverage[NUM_CHANNELS]; float trailingAverage[NUM_CHANNELS];
int samplesAveraged; int samplesAveraged;
int LED; int LED;
int noReadCount;
int totalSamples; int totalSamples;
timeval lastGoodRead;
}; };
#endif #endif