2015-09-07 02:32:06 -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_HAL/AP_HAL.h>
|
|
|
|
#include "AP_RangeFinder_LightWareSerial.h"
|
|
|
|
#include <AP_SerialManager/AP_SerialManager.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2019-07-27 03:05:42 -03:00
|
|
|
#define LIGHTWARE_DIST_MAX_CM 10000
|
|
|
|
#define LIGHTWARE_OUT_OF_RANGE_ADD_CM 100
|
|
|
|
|
2015-09-07 02:32:06 -03:00
|
|
|
/*
|
|
|
|
The constructor also initialises the rangefinder. Note that this
|
|
|
|
constructor is not called until detect() returns true, so we
|
|
|
|
already know that we should setup the rangefinder
|
|
|
|
*/
|
2017-08-07 00:41:01 -03:00
|
|
|
AP_RangeFinder_LightWareSerial::AP_RangeFinder_LightWareSerial(RangeFinder::RangeFinder_State &_state,
|
2019-02-03 22:21:58 -04:00
|
|
|
AP_RangeFinder_Params &_params,
|
2018-02-23 08:11:47 -04:00
|
|
|
uint8_t serial_instance) :
|
2018-07-04 11:22:17 -03:00
|
|
|
AP_RangeFinder_Backend(_state, _params)
|
2015-09-07 02:32:06 -03:00
|
|
|
{
|
2019-07-09 03:14:52 -03:00
|
|
|
const AP_SerialManager &serial_manager = AP::serialmanager();
|
2018-02-27 12:01:19 -04:00
|
|
|
uart = serial_manager.find_serial(AP_SerialManager::SerialProtocol_Rangefinder, serial_instance);
|
2015-09-07 02:32:06 -03:00
|
|
|
if (uart != nullptr) {
|
2018-02-27 12:01:19 -04:00
|
|
|
uart->begin(serial_manager.find_baudrate(AP_SerialManager::SerialProtocol_Rangefinder, serial_instance));
|
2015-09-07 02:32:06 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
detect if a Lightware rangefinder is connected. We'll detect by
|
|
|
|
trying to take a reading on Serial. If we get a result the sensor is
|
|
|
|
there.
|
|
|
|
*/
|
2019-07-09 03:14:52 -03:00
|
|
|
bool AP_RangeFinder_LightWareSerial::detect(uint8_t serial_instance)
|
2015-09-07 02:32:06 -03:00
|
|
|
{
|
2019-07-09 03:14:52 -03:00
|
|
|
return AP::serialmanager().find_serial(AP_SerialManager::SerialProtocol_Rangefinder, serial_instance) != nullptr;
|
2015-09-07 02:32:06 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// read - return last value measured by sensor
|
|
|
|
bool AP_RangeFinder_LightWareSerial::get_reading(uint16_t &reading_cm)
|
|
|
|
{
|
|
|
|
if (uart == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-27 03:05:42 -03:00
|
|
|
float sum = 0; // sum of all readings taken
|
|
|
|
uint16_t valid_count = 0; // number of valid readings
|
|
|
|
uint16_t invalid_count = 0; // number of invalid readings
|
|
|
|
|
2015-09-07 02:32:06 -03:00
|
|
|
// read any available lines from the lidar
|
|
|
|
int16_t nbytes = uart->available();
|
|
|
|
while (nbytes-- > 0) {
|
|
|
|
char c = uart->read();
|
|
|
|
if (c == '\r') {
|
|
|
|
linebuf[linebuf_len] = 0;
|
2019-07-27 03:05:42 -03:00
|
|
|
const float dist = (float)atof(linebuf);
|
|
|
|
if (!is_negative(dist)) {
|
|
|
|
sum += dist;
|
|
|
|
valid_count++;
|
|
|
|
} else {
|
|
|
|
invalid_count++;
|
|
|
|
}
|
2015-09-07 02:32:06 -03:00
|
|
|
linebuf_len = 0;
|
2019-07-27 03:05:42 -03:00
|
|
|
} else if (isdigit(c) || c == '.' || c == '-') {
|
2015-09-07 02:32:06 -03:00
|
|
|
linebuf[linebuf_len++] = c;
|
|
|
|
if (linebuf_len == sizeof(linebuf)) {
|
|
|
|
// too long, discard the line
|
|
|
|
linebuf_len = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-17 17:19:50 -04:00
|
|
|
uint32_t now = AP_HAL::millis();
|
|
|
|
if (last_init_ms == 0 ||
|
|
|
|
(now - last_init_ms > 1000 &&
|
|
|
|
now - state.last_reading_ms > 1000)) {
|
|
|
|
// send enough serial transitions to trigger LW20 into serial
|
|
|
|
// mode. It starts in dual I2C/serial mode, and wants to see
|
|
|
|
// enough transitions to switch into serial mode.
|
|
|
|
uart->write("www\r\n");
|
|
|
|
last_init_ms = now;
|
|
|
|
} else {
|
|
|
|
uart->write('d');
|
|
|
|
}
|
2015-09-07 02:32:06 -03:00
|
|
|
|
2019-07-27 03:05:42 -03:00
|
|
|
// return average of all valid readings
|
|
|
|
if (valid_count > 0) {
|
|
|
|
reading_cm = 100 * sum / valid_count;
|
|
|
|
return true;
|
2015-09-07 02:32:06 -03:00
|
|
|
}
|
2019-07-27 03:05:42 -03:00
|
|
|
|
|
|
|
// all readings were invalid so return out-of-range-high value
|
|
|
|
if (invalid_count > 0) {
|
|
|
|
reading_cm = MIN(MAX(LIGHTWARE_DIST_MAX_CM, max_distance_cm() + LIGHTWARE_OUT_OF_RANGE_ADD_CM), UINT16_MAX);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// no readings so return false
|
|
|
|
return false;
|
2015-09-07 02:32:06 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
update the state of the sensor
|
|
|
|
*/
|
|
|
|
void AP_RangeFinder_LightWareSerial::update(void)
|
|
|
|
{
|
|
|
|
if (get_reading(state.distance_cm)) {
|
|
|
|
// update range_valid state based on distance measured
|
AP_RangeFinder: support last_reading_ms
Benewake, LeddarOne, LightWareSerial, MAVLink, MaxsonarI2CXL, MaxsonarSerialLV, NMEA, PX4_PWM, uLanding and Wasp already stored the last read time so for these drivers, this change just moves that storage to the state structure
analog, BBB_PRU, Bebop, LightWareI2C, PulsedLightLRF, TeraRangerI2C, VL53L0X did not store the last read time so this was added
2018-08-27 04:02:51 -03:00
|
|
|
state.last_reading_ms = AP_HAL::millis();
|
2015-09-07 02:32:06 -03:00
|
|
|
update_status();
|
AP_RangeFinder: support last_reading_ms
Benewake, LeddarOne, LightWareSerial, MAVLink, MaxsonarI2CXL, MaxsonarSerialLV, NMEA, PX4_PWM, uLanding and Wasp already stored the last read time so for these drivers, this change just moves that storage to the state structure
analog, BBB_PRU, Bebop, LightWareI2C, PulsedLightLRF, TeraRangerI2C, VL53L0X did not store the last read time so this was added
2018-08-27 04:02:51 -03:00
|
|
|
} else if (AP_HAL::millis() - state.last_reading_ms > 200) {
|
2015-09-07 02:32:06 -03:00
|
|
|
set_status(RangeFinder::RangeFinder_NoData);
|
|
|
|
}
|
|
|
|
}
|