mirror of
https://github.com/HifiExperiments/overte.git
synced 2025-07-22 17:03:18 +02:00
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.
This commit is contained in:
parent
2446e80c6c
commit
12e4e15b21
1 changed files with 33 additions and 30 deletions
|
@ -17,7 +17,7 @@
|
||||||
#include "SerialInterface.h"
|
#include "SerialInterface.h"
|
||||||
|
|
||||||
int serial_fd;
|
int serial_fd;
|
||||||
const int MAX_BUFFER = 100;
|
const int MAX_BUFFER = 255;
|
||||||
char serial_buffer[MAX_BUFFER];
|
char serial_buffer[MAX_BUFFER];
|
||||||
int serial_buffer_pos = 0;
|
int serial_buffer_pos = 0;
|
||||||
int samples_total = 0;
|
int samples_total = 0;
|
||||||
|
@ -64,6 +64,11 @@ int SerialInterface::init(char * portname, int baud)
|
||||||
lastMeasured[i] = 0;
|
lastMeasured[i] = 0;
|
||||||
trailingAverage[i] = 0.0;
|
trailingAverage[i] = 0.0;
|
||||||
}
|
}
|
||||||
|
// Clear serial input buffer
|
||||||
|
for (int i = 1; i < MAX_BUFFER; i++) {
|
||||||
|
serial_buffer[i] = ' ';
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return 0; // Init serial port was a success
|
return 0; // Init serial port was a success
|
||||||
}
|
}
|
||||||
|
@ -127,42 +132,40 @@ void SerialInterface::readData() {
|
||||||
// If the sensor rate is 100Hz, 0.001 will make the long term average a 10-second average
|
// If the sensor rate is 100Hz, 0.001 will make the long term average a 10-second average
|
||||||
const float AVG_RATE[] = {0.01, 0.01, 0.01, 0.01, 0.01, 0.01};
|
const float AVG_RATE[] = {0.01, 0.01, 0.01, 0.01, 0.01, 0.01};
|
||||||
char bufchar[1];
|
char bufchar[1];
|
||||||
while (read(serial_fd, bufchar, 1) > 0)
|
|
||||||
|
while (read(serial_fd, &bufchar, 1) > 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))
|
||||||
{
|
{
|
||||||
// At end - Extract value from string to variables
|
std::string serialLine(serial_buffer, serial_buffer_pos-1);
|
||||||
if (serial_buffer[0] != 'p')
|
//std::cout << serialLine << "\n";
|
||||||
{
|
int spot;
|
||||||
// NOTE: This is stupid, need to rewrite to properly scan for NUM_CHANNELS + 2
|
//int channel = 0;
|
||||||
//
|
std::string val;
|
||||||
sscanf(serial_buffer, "%d %d %d %d %d %d %d %d",
|
for (int i = 0; i < NUM_CHANNELS + 2; i++) {
|
||||||
&lastMeasured[0],
|
spot = serialLine.find_first_of(" ", 0);
|
||||||
&lastMeasured[1],
|
if (spot != std::string::npos) {
|
||||||
&lastMeasured[2],
|
val = serialLine.substr(0,spot);
|
||||||
&lastMeasured[3],
|
//std::cout << val << "\n";
|
||||||
&lastMeasured[4],
|
if (i < NUM_CHANNELS) lastMeasured[i] = atoi(val.c_str());
|
||||||
&lastMeasured[5],
|
else samplesAveraged = atoi(val.c_str());
|
||||||
&samplesAveraged,
|
} else LED = atoi(serialLine.c_str());
|
||||||
&LED
|
serialLine = serialLine.substr(spot+1, serialLine.length() - spot - 1);
|
||||||
);
|
|
||||||
for (int i = 0; i < NUM_CHANNELS; i++)
|
|
||||||
{
|
|
||||||
if (samples_total > 0)
|
|
||||||
trailingAverage[i] = (1.f - AVG_RATE[i])*trailingAverage[i] +
|
|
||||||
AVG_RATE[i]*(float)lastMeasured[i];
|
|
||||||
else
|
|
||||||
{
|
|
||||||
trailingAverage[i] = (float)lastMeasured[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
samples_total++;
|
|
||||||
}
|
}
|
||||||
// Clear rest of string for printing onscreen
|
//std::cout << LED << "\n";
|
||||||
while(serial_buffer_pos++ < MAX_BUFFER) serial_buffer[serial_buffer_pos] = ' ';
|
|
||||||
|
for (int i = 0; i < NUM_CHANNELS; i++) {
|
||||||
|
if (samples_total > 0)
|
||||||
|
trailingAverage[i] = (1.f - AVG_RATE[i])*trailingAverage[i] +
|
||||||
|
AVG_RATE[i]*(float)lastMeasured[i];
|
||||||
|
else trailingAverage[i] = (float)lastMeasured[i];
|
||||||
|
|
||||||
|
}
|
||||||
|
samples_total++;
|
||||||
serial_buffer_pos = 0;
|
serial_buffer_pos = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue