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
1717 #include "SerialInterface.h"
1818
1919 int serial_fd;
20−const int MAX_BUFFER = 100;
20+const int MAX_BUFFER = 255;
2121 char serial_buffer[MAX_BUFFER];
2222 int serial_buffer_pos = 0;
2323 int samples_total = 0;
@@ -64,6 +64,11 @@ int SerialInterface::init(char * portname, int baud) · 40 unmodified lines
6464 lastMeasured[i] = 0;
6565 trailingAverage[i] = 0.0;
6666 }
67+ // Clear serial input buffer
68+ for (int i = 1; i < MAX_BUFFER; i++) {
69+ serial_buffer[i] = ' ';
70+ }
71+
6772
6873 return 0; // Init serial port was a success
6974 }
@@ -127,42 +132,40 @@ void SerialInterface::readData() { · 57 unmodified lines
127132 // If the sensor rate is 100Hz, 0.001 will make the long term average a 10-second average
128133 const float AVG_RATE[] = {0.01, 0.01, 0.01, 0.01, 0.01, 0.01};
129134 char bufchar[1];
130− while (read(serial_fd, bufchar, 1) > 0)
135+
136+ while (read(serial_fd, &bufchar, 1) > 0)
131137 {
138+ //std::cout << bufchar[0];
132139 serial_buffer[serial_buffer_pos] = bufchar[0];
133140 serial_buffer_pos++;
134141 // Have we reached end of a line of input?
135142 if ((bufchar[0] == '\n') || (serial_buffer_pos >= MAX_BUFFER))
136143 {
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+
163167 }
164− // Clear rest of string for printing onscreen
165− while(serial_buffer_pos++ < MAX_BUFFER) serial_buffer[serial_buffer_pos] = ' ';
168+ samples_total++;
166169 serial_buffer_pos = 0;
167170 }
168171 }