2018-05-25 22:59:36 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "AP_RangeFinder_Benewake.h"
|
2019-11-01 04:03:17 -03:00
|
|
|
|
2022-03-12 06:37:29 -04:00
|
|
|
#if AP_RANGEFINDER_BENEWAKE_ENABLED
|
|
|
|
|
2019-11-01 04:03:17 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2018-05-25 22:59:36 -03:00
|
|
|
#include <AP_HAL/utility/sparse-endian.h>
|
|
|
|
|
2019-11-01 04:03:17 -03:00
|
|
|
#include <ctype.h>
|
|
|
|
|
2018-05-25 22:59:36 -03:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
#define BENEWAKE_FRAME_HEADER 0x59
|
|
|
|
#define BENEWAKE_FRAME_LENGTH 9
|
2018-10-23 02:23:44 -03:00
|
|
|
#define BENEWAKE_DIST_MAX_CM 32768
|
2018-11-07 21:04:03 -04:00
|
|
|
#define BENEWAKE_OUT_OF_RANGE_ADD_CM 100
|
2018-05-25 22:59:36 -03:00
|
|
|
|
|
|
|
// format of serial packets received from benewake lidar
|
|
|
|
//
|
|
|
|
// Data Bit Definition Description
|
|
|
|
// ------------------------------------------------
|
|
|
|
// byte 0 Frame header 0x59
|
|
|
|
// byte 1 Frame header 0x59
|
|
|
|
// byte 2 DIST_L Distance (in cm) low 8 bits
|
|
|
|
// byte 3 DIST_H Distance (in cm) high 8 bits
|
|
|
|
// byte 4 STRENGTH_L Strength low 8 bits
|
2019-10-14 06:30:48 -03:00
|
|
|
// bute 4 (TF03) (Reserved)
|
2018-05-25 22:59:36 -03:00
|
|
|
// byte 5 STRENGTH_H Strength high 8 bits
|
2019-10-14 06:30:48 -03:00
|
|
|
// bute 5 (TF03) (Reserved)
|
2018-05-25 22:59:36 -03:00
|
|
|
// byte 6 (TF02) SIG Reliability in 8 levels, 7 & 8 means reliable
|
|
|
|
// byte 6 (TFmini) Distance Mode 0x02 for short distance (mm), 0x07 for long distance (cm)
|
2019-10-14 06:30:48 -03:00
|
|
|
// byte 6 (TF03) (Reserved)
|
2018-05-25 22:59:36 -03:00
|
|
|
// byte 7 (TF02 only) TIME Exposure time in two levels 0x03 and 0x06
|
|
|
|
// byte 8 Checksum Checksum byte, sum of bytes 0 to bytes 7
|
|
|
|
|
2021-10-18 02:45:33 -03:00
|
|
|
// distance returned in reading_m, signal_ok is set to true if sensor reports a strong signal
|
|
|
|
bool AP_RangeFinder_Benewake::get_reading(float &reading_m)
|
2018-05-25 22:59:36 -03:00
|
|
|
{
|
|
|
|
if (uart == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float sum_cm = 0;
|
|
|
|
uint16_t count = 0;
|
2018-11-07 21:04:03 -04:00
|
|
|
uint16_t count_out_of_range = 0;
|
2018-05-25 22:59:36 -03:00
|
|
|
|
|
|
|
// read any available lines from the lidar
|
2023-11-28 18:16:22 -04:00
|
|
|
for (auto j=0; j<8192; j++) {
|
|
|
|
uint8_t c;
|
|
|
|
if (!uart->read(c)) {
|
|
|
|
break;
|
2018-11-18 21:31:24 -04:00
|
|
|
}
|
2018-05-25 22:59:36 -03:00
|
|
|
// if buffer is empty and this byte is 0x59, add to buffer
|
|
|
|
if (linebuf_len == 0) {
|
|
|
|
if (c == BENEWAKE_FRAME_HEADER) {
|
|
|
|
linebuf[linebuf_len++] = c;
|
|
|
|
}
|
|
|
|
} else if (linebuf_len == 1) {
|
|
|
|
// if buffer has 1 element and this byte is 0x59, add it to buffer
|
|
|
|
// if not clear the buffer
|
|
|
|
if (c == BENEWAKE_FRAME_HEADER) {
|
|
|
|
linebuf[linebuf_len++] = c;
|
|
|
|
} else {
|
|
|
|
linebuf_len = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// add character to buffer
|
|
|
|
linebuf[linebuf_len++] = c;
|
|
|
|
// if buffer now has 9 items try to decode it
|
|
|
|
if (linebuf_len == BENEWAKE_FRAME_LENGTH) {
|
|
|
|
// calculate checksum
|
2018-11-14 02:35:56 -04:00
|
|
|
uint8_t checksum = 0;
|
2018-05-25 22:59:36 -03:00
|
|
|
for (uint8_t i=0; i<BENEWAKE_FRAME_LENGTH-1; i++) {
|
2018-11-18 21:31:24 -04:00
|
|
|
checksum += linebuf[i];
|
2018-05-25 22:59:36 -03:00
|
|
|
}
|
|
|
|
// if checksum matches extract contents
|
2018-11-18 21:31:24 -04:00
|
|
|
if (checksum == linebuf[BENEWAKE_FRAME_LENGTH-1]) {
|
2018-11-08 08:34:35 -04:00
|
|
|
// calculate distance
|
2018-05-25 22:59:36 -03:00
|
|
|
uint16_t dist = ((uint16_t)linebuf[3] << 8) | linebuf[2];
|
2022-09-05 01:27:53 -03:00
|
|
|
if (dist >= BENEWAKE_DIST_MAX_CM || dist == uint16_t(model_dist_max_cm())) {
|
|
|
|
// this reading is out of range. Note that we
|
|
|
|
// consider getting exactly the model dist max
|
|
|
|
// is out of range. This fixes an issue with
|
|
|
|
// the TF03 which can give exactly 18000 cm
|
|
|
|
// when out of range
|
2018-11-08 08:34:35 -04:00
|
|
|
count_out_of_range++;
|
2019-11-01 04:03:17 -03:00
|
|
|
} else if (!has_signal_byte()) {
|
2018-11-08 08:34:35 -04:00
|
|
|
// no signal byte from TFmini so add distance to sum
|
|
|
|
sum_cm += dist;
|
|
|
|
count++;
|
|
|
|
} else {
|
|
|
|
// TF02 provides signal reliability (good = 7 or 8)
|
|
|
|
if (linebuf[6] >= 7) {
|
|
|
|
// add distance to sum
|
2018-05-25 22:59:36 -03:00
|
|
|
sum_cm += dist;
|
|
|
|
count++;
|
2018-11-07 21:04:03 -04:00
|
|
|
} else {
|
2018-11-08 08:34:35 -04:00
|
|
|
// this reading is out of range
|
|
|
|
count_out_of_range++;
|
2018-05-25 22:59:36 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// clear buffer
|
|
|
|
linebuf_len = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-07 21:04:03 -04:00
|
|
|
if (count > 0) {
|
|
|
|
// return average distance of readings
|
2021-10-18 02:45:33 -03:00
|
|
|
reading_m = (sum_cm * 0.01f) / count;
|
2018-11-07 21:04:03 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count_out_of_range > 0) {
|
2018-11-13 22:51:54 -04:00
|
|
|
// if only out of range readings return larger of
|
|
|
|
// driver defined maximum range for the model and user defined max range + 1m
|
2021-10-18 02:45:33 -03:00
|
|
|
reading_m = MAX(model_dist_max_cm(), max_distance_cm() + BENEWAKE_OUT_OF_RANGE_ADD_CM) * 0.01f;
|
2018-11-07 21:04:03 -04:00
|
|
|
return true;
|
2018-05-25 22:59:36 -03:00
|
|
|
}
|
2018-11-07 21:04:03 -04:00
|
|
|
|
|
|
|
// no readings so return false
|
|
|
|
return false;
|
2018-05-25 22:59:36 -03:00
|
|
|
}
|
2022-03-12 06:37:29 -04:00
|
|
|
|
|
|
|
#endif // AP_RANGEFINDER_BENEWAKE_ENABLED
|