tfmini : restructure parsing logic to always publish latest measurement

This commit is contained in:
Mohammed Kabir
2018-08-24 16:15:27 -04:00
committed by Lorenz Meier
parent 3afa018954
commit 38bae7d401
3 changed files with 49 additions and 35 deletions
+30 -16
View File
@@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
* *
* Copyright (c) 2017 PX4 Development Team. All rights reserved. * Copyright (c) 2017-2018 PX4 Development Team. All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@@ -37,6 +37,7 @@
* @author Greg Hulands * @author Greg Hulands
* @author Ayush Gaud <ayush.gaud@gmail.com> * @author Ayush Gaud <ayush.gaud@gmail.com>
* @author Christoph Tobler <christoph@px4.io> * @author Christoph Tobler <christoph@px4.io>
* @author Mohammed Kabir <mhkabir@mit.edu>
* *
* Driver for the Benewake TFmini laser rangefinder series * Driver for the Benewake TFmini laser rangefinder series
*/ */
@@ -58,6 +59,9 @@
#include <math.h> #include <math.h>
#include <unistd.h> #include <unistd.h>
#include <termios.h> #include <termios.h>
#ifdef __PX4_CYGWIN
#include <asm/socket.h>
#endif
#include <perf/perf_counter.h> #include <perf/perf_counter.h>
#include <systemlib/err.h> #include <systemlib/err.h>
@@ -517,8 +521,6 @@ TFMINI::read(device::file_t *filp, char *buffer, size_t buflen)
int int
TFMINI::collect() TFMINI::collect()
{ {
int ret;
perf_begin(_sample_perf); perf_begin(_sample_perf);
/* clear buffer if last read was too long ago */ /* clear buffer if last read was too long ago */
@@ -528,6 +530,19 @@ TFMINI::collect()
char readbuf[sizeof(_linebuf)]; char readbuf[sizeof(_linebuf)];
unsigned readlen = sizeof(readbuf) - 1; unsigned readlen = sizeof(readbuf) - 1;
int ret = 0;
float distance_m = -1.0f;
/* Check the number of bytes available in the buffer*/
int bytes_available = 0;
::ioctl(_fd, FIONREAD, (unsigned long)&bytes_available);
if(!bytes_available) {
return -EAGAIN;
}
/* parse entire buffer */
do {
/* read from the sensor (uart buffer) */ /* read from the sensor (uart buffer) */
ret = ::read(_fd, &readbuf[0], readlen); ret = ::read(_fd, &readbuf[0], readlen);
@@ -538,33 +553,32 @@ TFMINI::collect()
/* only throw an error if we time out */ /* only throw an error if we time out */
if (read_elapsed > (_conversion_interval * 2)) { if (read_elapsed > (_conversion_interval * 2)) {
/* flush anything in RX buffer */
tcflush(_fd, TCIFLUSH);
return ret; return ret;
} else { } else {
return -EAGAIN; return -EAGAIN;
} }
} else if (ret == 0) {
return -EAGAIN;
} }
_last_read = hrt_absolute_time(); _last_read = hrt_absolute_time();
float distance_m = -1.0f; /* parse buffer */
bool valid = false;
for (int i = 0; i < ret; i++) { for (int i = 0; i < ret; i++) {
if (OK == tfmini_parser(readbuf[i], _linebuf, &_linebuf_index, &_parse_state, &distance_m)) { tfmini_parse(readbuf[i], _linebuf, &_linebuf_index, &_parse_state, &distance_m);
valid = true;
}
} }
if (!valid) { /* bytes left to parse */
bytes_available -= ret;
} while (bytes_available > 0);
/* no valid measurement after parsing buffer */
if (distance_m < 0.0f) {
return -EAGAIN; return -EAGAIN;
} }
DEVICE_DEBUG("val (float): %8.4f, raw: %s, valid: %s", (double)distance_m, _linebuf, ((valid) ? "OK" : "NO")); /* publish most recent valid measurement from buffer */
struct distance_sensor_s report; struct distance_sensor_s report;
report.timestamp = hrt_absolute_time(); report.timestamp = hrt_absolute_time();
@@ -63,7 +63,7 @@ const char *parser_state[] = {
}; };
#endif #endif
int tfmini_parser(char c, char *parserbuf, unsigned *parserbuf_index, enum TFMINI_PARSE_STATE *state, float *dist) int tfmini_parse(char c, char *parserbuf, unsigned *parserbuf_index, enum TFMINI_PARSE_STATE *state, float *dist)
{ {
int ret = -1; int ret = -1;
//char *end; //char *end;
@@ -69,4 +69,4 @@ enum TFMINI_PARSE_STATE {
TFMINI_PARSE_STATE6_GOT_CHECKSUM TFMINI_PARSE_STATE6_GOT_CHECKSUM
}; };
int tfmini_parser(char c, char *parserbuf, unsigned *parserbuf_index, enum TFMINI_PARSE_STATE *state, float *dist); int tfmini_parse(char c, char *parserbuf, unsigned *parserbuf_index, enum TFMINI_PARSE_STATE *state, float *dist);