2013-12-02 07:02:56 -04: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_PulsedLightLRF.h"
|
2016-07-12 18:07:27 -03:00
|
|
|
|
2022-03-12 06:37:29 -04:00
|
|
|
#if AP_RANGEFINDER_PULSEDLIGHTLRF_ENABLED
|
|
|
|
|
2016-04-14 17:00:38 -03:00
|
|
|
#include <utility>
|
2016-11-11 17:58:46 -04:00
|
|
|
#include <stdio.h>
|
2013-12-02 07:02:56 -04:00
|
|
|
|
2016-07-12 18:07:27 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2016-07-12 18:08:19 -03:00
|
|
|
#include <AP_HAL/utility/sparse-endian.h>
|
2016-07-12 18:07:27 -03:00
|
|
|
|
2013-12-02 07:02:56 -04:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2016-11-11 17:58:46 -04:00
|
|
|
/* LL40LS Registers addresses */
|
2018-07-27 07:22:26 -03:00
|
|
|
#define LL40LS_MEASURE_REG 0x00 /* Measure range register */
|
|
|
|
#define LL40LS_SIG_COUNT_VAL 0x02
|
|
|
|
#define LL40LS_DISTHIGH_REG 0x0F /* High byte of distance register, auto increment */
|
|
|
|
#define LL40LS_COUNT 0x11
|
|
|
|
#define LL40LS_HW_VERSION 0x41
|
|
|
|
#define LL40LS_INTERVAL 0x45
|
|
|
|
#define LL40LS_SW_VERSION 0x4f
|
2016-11-11 17:58:46 -04:00
|
|
|
|
|
|
|
// bit values
|
2018-07-27 07:22:26 -03:00
|
|
|
#define LL40LS_MSRREG_RESET 0x00 /* reset to power on defaults */
|
|
|
|
#define LL40LS_AUTO_INCREMENT 0x80
|
|
|
|
#define LL40LS_COUNT_CONTINUOUS 0xff
|
|
|
|
#define LL40LS_MSRREG_ACQUIRE 0x04 /* Value to initiate a measurement, varies based on sensor revision */
|
2016-11-11 17:58:46 -04:00
|
|
|
|
|
|
|
// i2c address
|
|
|
|
#define LL40LS_ADDR 0x62
|
|
|
|
|
2017-08-07 00:41:01 -03:00
|
|
|
AP_RangeFinder_PulsedLightLRF::AP_RangeFinder_PulsedLightLRF(uint8_t bus,
|
2017-03-01 00:22:32 -04:00
|
|
|
RangeFinder::RangeFinder_State &_state,
|
2019-02-03 22:21:58 -04:00
|
|
|
AP_RangeFinder_Params &_params,
|
2019-11-01 00:03:14 -03:00
|
|
|
RangeFinder::Type _rftype)
|
2018-07-04 11:22:17 -03:00
|
|
|
: AP_RangeFinder_Backend(_state, _params)
|
2016-11-11 17:58:46 -04:00
|
|
|
, _dev(hal.i2c_mgr->get_device(bus, LL40LS_ADDR))
|
2017-03-01 00:22:32 -04:00
|
|
|
, rftype(_rftype)
|
2013-12-02 07:02:56 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-07-11 19:58:29 -03:00
|
|
|
/*
|
2014-06-27 00:39:52 -03:00
|
|
|
detect if a PulsedLight rangefinder is connected. We'll detect by
|
2016-11-11 17:58:46 -04:00
|
|
|
look for the version registers
|
2014-06-27 00:39:52 -03:00
|
|
|
*/
|
2017-08-07 00:41:01 -03:00
|
|
|
AP_RangeFinder_Backend *AP_RangeFinder_PulsedLightLRF::detect(uint8_t bus,
|
2017-03-01 00:22:32 -04:00
|
|
|
RangeFinder::RangeFinder_State &_state,
|
2018-07-04 11:22:17 -03:00
|
|
|
AP_RangeFinder_Params &_params,
|
2019-11-01 00:03:14 -03:00
|
|
|
RangeFinder::Type rftype)
|
2013-12-02 07:02:56 -04:00
|
|
|
{
|
2016-04-14 17:00:38 -03:00
|
|
|
AP_RangeFinder_PulsedLightLRF *sensor
|
2024-05-26 22:24:14 -03:00
|
|
|
= NEW_NOTHROW AP_RangeFinder_PulsedLightLRF(bus, _state, _params, rftype);
|
2016-11-11 17:58:46 -04:00
|
|
|
if (!sensor ||
|
|
|
|
!sensor->init()) {
|
2016-04-14 17:00:38 -03:00
|
|
|
delete sensor;
|
|
|
|
return nullptr;
|
2014-06-27 00:39:52 -03:00
|
|
|
}
|
2016-04-14 17:00:38 -03:00
|
|
|
return sensor;
|
2013-12-02 07:02:56 -04:00
|
|
|
}
|
|
|
|
|
2016-11-11 17:58:46 -04:00
|
|
|
/*
|
|
|
|
called at 50Hz
|
|
|
|
*/
|
2017-01-13 15:26:14 -04:00
|
|
|
void AP_RangeFinder_PulsedLightLRF::timer(void)
|
2013-12-02 07:02:56 -04:00
|
|
|
{
|
2016-11-11 17:58:46 -04:00
|
|
|
if (check_reg_counter++ == 10) {
|
|
|
|
check_reg_counter = 0;
|
|
|
|
if (!_dev->check_next_register()) {
|
|
|
|
// re-send the acquire. this handles the case of power
|
|
|
|
// cycling while running in continuous mode
|
|
|
|
_dev->write_register(LL40LS_MEASURE_REG, LL40LS_MSRREG_ACQUIRE);
|
|
|
|
}
|
2013-12-02 07:02:56 -04:00
|
|
|
}
|
|
|
|
|
2016-11-11 17:58:46 -04:00
|
|
|
switch (phase) {
|
|
|
|
case PHASE_COLLECT: {
|
|
|
|
be16_t val;
|
|
|
|
// read the high and low byte distance registers
|
|
|
|
if (_dev->read_registers(LL40LS_DISTHIGH_REG | LL40LS_AUTO_INCREMENT, (uint8_t*)&val, sizeof(val))) {
|
2017-08-08 02:54:09 -03:00
|
|
|
uint16_t _distance_cm = be16toh(val);
|
2016-11-19 20:31:36 -04:00
|
|
|
// remove momentary spikes
|
2017-08-08 02:54:09 -03:00
|
|
|
if (abs(_distance_cm - last_distance_cm) < 100) {
|
2021-10-18 02:45:33 -03:00
|
|
|
state.distance_m = _distance_cm * 0.01f;
|
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();
|
2016-11-19 20:31:36 -04:00
|
|
|
update_status();
|
|
|
|
}
|
2017-08-08 02:54:09 -03:00
|
|
|
last_distance_cm = _distance_cm;
|
2016-11-11 17:58:46 -04:00
|
|
|
} else {
|
2019-11-01 02:10:52 -03:00
|
|
|
set_status(RangeFinder::Status::NoData);
|
2016-11-11 17:58:46 -04:00
|
|
|
}
|
|
|
|
if (!v2_hardware) {
|
|
|
|
// for v2 hw we use continuous mode
|
|
|
|
phase = PHASE_MEASURE;
|
|
|
|
}
|
2018-07-27 07:22:26 -03:00
|
|
|
if (!v3hp_hardware) {
|
|
|
|
// for v3hp hw we start PHASE_MEASURE immediately after PHASE_COLLECT
|
|
|
|
break;
|
|
|
|
}
|
2016-11-11 17:58:46 -04:00
|
|
|
}
|
2018-10-28 04:01:46 -03:00
|
|
|
FALLTHROUGH;
|
2018-07-27 07:22:26 -03:00
|
|
|
case PHASE_MEASURE:
|
|
|
|
if (_dev->write_register(LL40LS_MEASURE_REG, LL40LS_MSRREG_ACQUIRE)) {
|
|
|
|
phase = PHASE_COLLECT;
|
|
|
|
}
|
|
|
|
break;
|
2016-11-11 17:58:46 -04:00
|
|
|
}
|
2013-12-02 07:02:56 -04:00
|
|
|
}
|
|
|
|
|
2016-07-11 19:58:29 -03:00
|
|
|
|
2016-11-11 17:58:46 -04:00
|
|
|
/*
|
|
|
|
a table of settings for a lidar
|
|
|
|
*/
|
|
|
|
struct settings_table {
|
2016-11-11 22:55:30 -04:00
|
|
|
uint8_t reg;
|
|
|
|
uint8_t value;
|
2016-11-11 17:58:46 -04:00
|
|
|
};
|
2013-12-02 07:02:56 -04:00
|
|
|
|
2016-11-11 17:58:46 -04:00
|
|
|
/*
|
|
|
|
register setup table for V1 Lidar
|
|
|
|
*/
|
|
|
|
static const struct settings_table settings_v1[] = {
|
2016-11-11 22:55:30 -04:00
|
|
|
{ LL40LS_MEASURE_REG, LL40LS_MSRREG_RESET },
|
2016-11-11 17:58:46 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
register setup table for V2 Lidar
|
|
|
|
*/
|
|
|
|
static const struct settings_table settings_v2[] = {
|
2016-11-11 22:55:30 -04:00
|
|
|
{ LL40LS_INTERVAL, 0x28 }, // 0x28 == 50Hz
|
|
|
|
{ LL40LS_COUNT, LL40LS_COUNT_CONTINUOUS },
|
|
|
|
{ LL40LS_MEASURE_REG, LL40LS_MSRREG_ACQUIRE },
|
2016-11-11 17:58:46 -04:00
|
|
|
};
|
2016-07-11 19:58:29 -03:00
|
|
|
|
2018-07-27 07:22:26 -03:00
|
|
|
/*
|
|
|
|
register setup table for V3HP Lidar
|
|
|
|
*/
|
|
|
|
static const struct settings_table settings_v3hp[] = {
|
|
|
|
{ LL40LS_SIG_COUNT_VAL, 0x80 }, // 0x80 = 128 acquisitions
|
|
|
|
};
|
|
|
|
|
2016-11-11 17:58:46 -04:00
|
|
|
/*
|
|
|
|
initialise the sensor to required settings
|
|
|
|
*/
|
|
|
|
bool AP_RangeFinder_PulsedLightLRF::init(void)
|
|
|
|
{
|
2020-01-18 17:42:34 -04:00
|
|
|
if (!_dev) {
|
2014-06-27 00:39:52 -03:00
|
|
|
return false;
|
2013-12-02 07:02:56 -04:00
|
|
|
}
|
2020-01-18 17:42:34 -04:00
|
|
|
_dev->get_semaphore()->take_blocking();
|
2016-11-11 22:55:30 -04:00
|
|
|
_dev->set_retries(3);
|
2016-11-11 17:58:46 -04:00
|
|
|
|
2016-12-08 21:58:57 -04:00
|
|
|
// LidarLite needs split transfers
|
|
|
|
_dev->set_split_transfers(true);
|
|
|
|
|
2019-11-01 00:03:14 -03:00
|
|
|
if (rftype == RangeFinder::Type::PLI2CV3) {
|
2017-03-01 00:22:32 -04:00
|
|
|
v2_hardware = true;
|
2019-11-01 00:03:14 -03:00
|
|
|
} else if (rftype == RangeFinder::Type::PLI2CV3HP) {
|
2018-07-27 07:22:26 -03:00
|
|
|
v3hp_hardware = true;
|
2017-03-01 00:22:32 -04:00
|
|
|
} else {
|
|
|
|
// auto-detect v1 vs v2
|
|
|
|
if (!(_dev->read_registers(LL40LS_HW_VERSION, &hw_version, 1) &&
|
|
|
|
hw_version > 0 &&
|
|
|
|
_dev->read_registers(LL40LS_SW_VERSION, &sw_version, 1) &&
|
|
|
|
sw_version > 0)) {
|
|
|
|
printf("PulsedLightI2C: bad version 0x%02x 0x%02x\n", (unsigned)hw_version, (unsigned)sw_version);
|
|
|
|
// invalid version information
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
v2_hardware = (hw_version >= 0x15);
|
2016-11-11 17:58:46 -04:00
|
|
|
}
|
|
|
|
|
2016-11-11 22:55:30 -04:00
|
|
|
const struct settings_table *table;
|
|
|
|
uint8_t num_settings;
|
2016-11-11 17:58:46 -04:00
|
|
|
|
2016-11-11 22:55:30 -04:00
|
|
|
if (v2_hardware) {
|
|
|
|
table = settings_v2;
|
|
|
|
num_settings = sizeof(settings_v2) / sizeof(settings_table);
|
2016-11-11 17:58:46 -04:00
|
|
|
phase = PHASE_COLLECT;
|
2018-07-27 07:22:26 -03:00
|
|
|
} else if (v3hp_hardware) {
|
|
|
|
table = settings_v3hp;
|
|
|
|
num_settings = sizeof(settings_v3hp) / sizeof(settings_table);
|
|
|
|
phase = PHASE_MEASURE;
|
2016-11-11 22:55:30 -04:00
|
|
|
} else {
|
|
|
|
table = settings_v1;
|
|
|
|
num_settings = sizeof(settings_v1) / sizeof(settings_table);
|
2016-11-11 17:58:46 -04:00
|
|
|
phase = PHASE_MEASURE;
|
2016-11-11 22:55:30 -04:00
|
|
|
}
|
2016-11-11 17:58:46 -04:00
|
|
|
|
|
|
|
_dev->setup_checked_registers(num_settings);
|
|
|
|
|
2016-11-11 22:55:30 -04:00
|
|
|
for (uint8_t i = 0; i < num_settings; i++) {
|
2017-03-01 00:22:32 -04:00
|
|
|
if (!_dev->write_register(table[i].reg, table[i].value, true)) {
|
|
|
|
goto failed;
|
|
|
|
}
|
2016-11-11 17:58:46 -04:00
|
|
|
}
|
2013-12-02 07:02:56 -04:00
|
|
|
|
2018-07-27 07:22:26 -03:00
|
|
|
printf("Found LidarLite device=0x%x v2=%d v3hp=%d\n", _dev->get_bus_id(), (int)v2_hardware, (int)v3hp_hardware);
|
2017-03-01 00:22:32 -04:00
|
|
|
|
|
|
|
_dev->get_semaphore()->give();
|
|
|
|
|
2016-11-11 17:58:46 -04:00
|
|
|
_dev->register_periodic_callback(20000,
|
2017-01-13 15:26:14 -04:00
|
|
|
FUNCTOR_BIND_MEMBER(&AP_RangeFinder_PulsedLightLRF::timer, void));
|
2014-06-27 00:39:52 -03:00
|
|
|
return true;
|
2017-03-01 00:22:32 -04:00
|
|
|
|
|
|
|
failed:
|
|
|
|
_dev->get_semaphore()->give();
|
|
|
|
return false;
|
2014-06-27 00:39:52 -03:00
|
|
|
}
|
|
|
|
|
2022-03-12 06:37:29 -04:00
|
|
|
#endif // AP_RANGEFINDER_PULSEDLIGHTLRF_ENABLED
|