Skip to content

Commit

Permalink
AP_RangeFinder: use boolean uartdriver read method
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbarker committed Nov 29, 2023
1 parent 41f61da commit 9394a7b
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 49 deletions.
7 changes: 3 additions & 4 deletions libraries/AP_RangeFinder/AP_RangeFinder_BLPing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ bool AP_RangeFinder_BLPing::get_reading(float &reading_m)
} averageStruct;

// read any available lines from the lidar
int16_t nbytes = uart->available();
while (nbytes-- > 0) {
const int16_t b = uart->read();
if (b < 0) {
for (auto i=0; i<8192; i++) {
uint8_t b;
if (!uart->read(b)) {
break;
}
if (protocol.parse_byte(b) == PingProtocol::MessageId::DISTANCE_SIMPLE) {
Expand Down
10 changes: 4 additions & 6 deletions libraries/AP_RangeFinder/AP_RangeFinder_Benewake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,11 @@ bool AP_RangeFinder_Benewake::get_reading(float &reading_m)
uint16_t count_out_of_range = 0;

// read any available lines from the lidar
int16_t nbytes = uart->available();
while (nbytes-- > 0) {
int16_t r = uart->read();
if (r < 0) {
continue;
for (auto j=0; j<8192; j++) {
uint8_t c;
if (!uart->read(c)) {
break;
}
uint8_t c = (uint8_t)r;
// if buffer is empty and this byte is 0x59, add to buffer
if (linebuf_len == 0) {
if (c == BENEWAKE_FRAME_HEADER) {
Expand Down
7 changes: 3 additions & 4 deletions libraries/AP_RangeFinder/AP_RangeFinder_Lanbao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ bool AP_RangeFinder_Lanbao::get_reading(float &reading_m)
// format is: [ 0xA5 | 0x5A | distance-MSB-mm | distance-LSB-mm | crc16 ]

// read any available lines from the lidar
int16_t nbytes = uart->available();
while (nbytes-- > 0) {
int16_t b = uart->read();
if (b == -1) {
for (auto i=0; i<8192; i++) {
uint8_t b;
if (!uart->read(b)) {
break;
}
if (buf_len == 0 && b != 0xA5) {
Expand Down
11 changes: 5 additions & 6 deletions libraries/AP_RangeFinder/AP_RangeFinder_LeddarVu8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,12 @@ bool AP_RangeFinder_LeddarVu8::get_reading(float &reading_m)
bool latest_dist_valid = false;

// read any available characters from the lidar
int16_t nbytes = uart->available();
while (nbytes-- > 0) {
int16_t r = uart->read();
if (r < 0) {
continue;
for (auto i=0; i<8192; i++) {
uint8_t b;
if (!uart->read(b)) {
break;
}
if (parse_byte((uint8_t)r, latest_dist_valid, latest_dist_cm)) {
if (parse_byte(b, latest_dist_valid, latest_dist_cm)) {
if (latest_dist_valid) {
sum_cm += latest_dist_cm;
count++;
Expand Down
9 changes: 5 additions & 4 deletions libraries/AP_RangeFinder/AP_RangeFinder_LightWareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ bool AP_RangeFinder_LightWareSerial::get_reading(float &reading_m)
const int16_t distance_cm_max = max_distance_cm();

// read any available lines from the lidar
int16_t nbytes = uart->available();
while (nbytes-- > 0) {
char c = uart->read();

for (auto i=0; i<8192; i++) {
uint8_t c;
if (!uart->read(c)) {
break;
}
// use legacy protocol
if (protocol_state == ProtocolState::UNKNOWN || protocol_state == ProtocolState::LEGACY) {
if (c == '\r') {
Expand Down
8 changes: 5 additions & 3 deletions libraries/AP_RangeFinder/AP_RangeFinder_MaxsonarSerialLV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ bool AP_RangeFinder_MaxsonarSerialLV::get_reading(float &reading_m)
}

int32_t sum = 0;
int16_t nbytes = uart->available();
uint16_t count = 0;

while (nbytes-- > 0) {
char c = uart->read();
for (auto i=0; i<8192; i++) {
uint8_t c;
if (!uart->read(c)) {
break;
}
if (c == '\r') {
linebuf[linebuf_len] = 0;
sum += (int)atoi(linebuf);
Expand Down
8 changes: 5 additions & 3 deletions libraries/AP_RangeFinder/AP_RangeFinder_NMEA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ bool AP_RangeFinder_NMEA::get_reading(float &reading_m)
// read any available lines from the lidar
float sum = 0.0f;
uint16_t count = 0;
int16_t nbytes = uart->available();
while (nbytes-- > 0) {
char c = uart->read();
for (auto i=0; i<8192; i++) {
uint8_t c;
if (!uart->read(c)) {
break;
}
if (decode(c)) {
sum += _distance_m;
count++;
Expand Down
10 changes: 4 additions & 6 deletions libraries/AP_RangeFinder/AP_RangeFinder_TeraRanger_Serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,11 @@ bool AP_RangeFinder_TeraRanger_Serial::get_reading(float &reading_m)
uint16_t bad_read = 0;

// read any available lines from the lidar
int16_t nbytes = uart->available();
while (nbytes-- > 0) {
int16_t r = uart->read();
if (r < 0) {
continue;
for (auto i=0; i<8192; i++) {
uint8_t c;
if (!uart->read(c)) {
break;
}
uint8_t c = (uint8_t)r;
// if buffer is empty and this byte is 0x57, add to buffer
if (linebuf_len == 0) {
if (c == FRAME_HEADER) {
Expand Down
20 changes: 10 additions & 10 deletions libraries/AP_RangeFinder/AP_RangeFinder_USD1_Serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ bool AP_RangeFinder_USD1_Serial::detect_version(void)
uint8_t count = 0;

// read any available data from USD1_Serial
int16_t nbytes = uart->available();

while (nbytes-- > 0) {
uint8_t c = uart->read();

for (auto i=0; i<8192; i++) {
uint8_t c;
if (!uart->read(c)) {
break;
}
if (((c == USD1_HDR_V0) || (c == USD1_HDR)) && !hdr_found) {
byte1 = c;
hdr_found = true;
Expand Down Expand Up @@ -121,11 +121,11 @@ bool AP_RangeFinder_USD1_Serial::get_reading(float &reading_m)
uint16_t count = 0;
bool hdr_found = false;

int16_t nbytes = uart->available();

while (nbytes-- > 0) {
uint8_t c = uart->read();

for (auto i=0; i<8192; i++) {
uint8_t c;
if (!uart->read(c)) {
break;
}
if ((c == _header) && !hdr_found) {
// located header byte
_linebuf_len = 0;
Expand Down
8 changes: 5 additions & 3 deletions libraries/AP_RangeFinder/AP_RangeFinder_Wasp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ bool AP_RangeFinder_Wasp::get_reading(float &reading_m) {
// read any available lines from the lidar
float sum = 0;
uint16_t count = 0;
int16_t nbytes = uart->available();
while (nbytes-- > 0) {
char c = uart->read();
for (auto i=0; i<8192; i++) {
uint8_t c;
if (!uart->read(c)) {
break;
}
if (c == '\n') {
linebuf[linebuf_len] = 0;
linebuf_len = 0;
Expand Down

0 comments on commit 9394a7b

Please sign in to comment.