2015-08-28 06:52:34 -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_LightWareI2C.h"
|
2016-07-12 18:51:50 -03:00
|
|
|
|
2016-05-24 15:51:54 -03:00
|
|
|
#include <utility>
|
2015-08-28 06:52:34 -03:00
|
|
|
|
2016-07-12 18:51:50 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include <AP_HAL/utility/sparse-endian.h>
|
|
|
|
|
2015-08-28 06:52:34 -03:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2016-07-12 18:51:50 -03:00
|
|
|
/*
|
2016-07-11 20:03:44 -03:00
|
|
|
The constructor also initializes the rangefinder. Note that this
|
2015-08-28 06:52:34 -03:00
|
|
|
constructor is not called until detect() returns true, so we
|
|
|
|
already know that we should setup the rangefinder
|
|
|
|
*/
|
2018-07-04 11:22:17 -03:00
|
|
|
AP_RangeFinder_LightWareI2C::AP_RangeFinder_LightWareI2C(RangeFinder::RangeFinder_State &_state, AP_RangeFinder_Params &_params, AP_HAL::OwnPtr<AP_HAL::I2CDevice> dev)
|
|
|
|
: AP_RangeFinder_Backend(_state, _params)
|
2016-12-06 16:02:58 -04:00
|
|
|
, _dev(std::move(dev)) {}
|
2015-08-28 06:52:34 -03:00
|
|
|
|
2016-07-12 18:51:50 -03:00
|
|
|
/*
|
2015-08-28 06:52:34 -03:00
|
|
|
detect if a Lightware rangefinder is connected. We'll detect by
|
|
|
|
trying to take a reading on I2C. If we get a result the sensor is
|
|
|
|
there.
|
|
|
|
*/
|
2018-07-04 11:22:17 -03:00
|
|
|
AP_RangeFinder_Backend *AP_RangeFinder_LightWareI2C::detect(RangeFinder::RangeFinder_State &_state, AP_RangeFinder_Params &_params, AP_HAL::OwnPtr<AP_HAL::I2CDevice> dev)
|
2015-08-28 06:52:34 -03:00
|
|
|
{
|
2018-10-25 23:39:48 -03:00
|
|
|
if (!dev) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-05-24 15:51:54 -03:00
|
|
|
AP_RangeFinder_LightWareI2C *sensor
|
2018-07-04 11:22:17 -03:00
|
|
|
= new AP_RangeFinder_LightWareI2C(_state, _params, std::move(dev));
|
2016-05-24 15:51:54 -03:00
|
|
|
|
2016-10-31 18:51:37 -03:00
|
|
|
if (!sensor) {
|
2016-05-24 15:51:54 -03:00
|
|
|
delete sensor;
|
|
|
|
return nullptr;
|
2015-08-28 06:52:34 -03:00
|
|
|
}
|
2016-05-24 15:51:54 -03:00
|
|
|
|
2017-02-18 00:36:12 -04:00
|
|
|
if (sensor->_dev->get_semaphore()->take(HAL_SEMAPHORE_BLOCK_FOREVER)) {
|
2016-10-31 18:51:37 -03:00
|
|
|
uint16_t reading_cm;
|
|
|
|
if (!sensor->get_reading(reading_cm)) {
|
|
|
|
sensor->_dev->get_semaphore()->give();
|
|
|
|
delete sensor;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
sensor->_dev->get_semaphore()->give();
|
|
|
|
}
|
|
|
|
|
2016-12-06 16:02:58 -04:00
|
|
|
sensor->init();
|
|
|
|
|
2016-05-24 15:51:54 -03:00
|
|
|
return sensor;
|
2015-08-28 06:52:34 -03:00
|
|
|
}
|
|
|
|
|
2016-12-06 16:02:58 -04:00
|
|
|
void AP_RangeFinder_LightWareI2C::init()
|
|
|
|
{
|
|
|
|
// call timer() at 20Hz
|
|
|
|
_dev->register_periodic_callback(50000,
|
2017-01-13 15:26:14 -04:00
|
|
|
FUNCTOR_BIND_MEMBER(&AP_RangeFinder_LightWareI2C::timer, void));
|
2016-12-06 16:02:58 -04:00
|
|
|
}
|
|
|
|
|
2015-08-28 06:52:34 -03:00
|
|
|
// read - return last value measured by sensor
|
|
|
|
bool AP_RangeFinder_LightWareI2C::get_reading(uint16_t &reading_cm)
|
|
|
|
{
|
2016-07-12 18:51:50 -03:00
|
|
|
be16_t val;
|
2016-07-11 20:03:44 -03:00
|
|
|
|
2018-07-04 11:22:17 -03:00
|
|
|
if (params.address == 0) {
|
2016-07-11 20:03:44 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-28 06:52:34 -03:00
|
|
|
// read the high and low byte distance registers
|
2016-07-12 18:51:50 -03:00
|
|
|
bool ret = _dev->read((uint8_t *) &val, sizeof(val));
|
2016-07-11 20:03:44 -03:00
|
|
|
if (ret) {
|
|
|
|
// combine results into distance
|
2016-07-12 18:51:50 -03:00
|
|
|
reading_cm = be16toh(val);
|
2015-08-28 06:52:34 -03:00
|
|
|
}
|
|
|
|
|
2016-07-11 20:03:44 -03:00
|
|
|
return ret;
|
2015-08-28 06:52:34 -03:00
|
|
|
}
|
|
|
|
|
2016-07-12 18:51:50 -03:00
|
|
|
/*
|
2015-08-28 06:52:34 -03:00
|
|
|
update the state of the sensor
|
|
|
|
*/
|
|
|
|
void AP_RangeFinder_LightWareI2C::update(void)
|
2016-10-31 18:51:37 -03:00
|
|
|
{
|
|
|
|
// nothing to do - its all done in the timer()
|
|
|
|
}
|
|
|
|
|
2017-01-13 15:26:14 -04:00
|
|
|
void AP_RangeFinder_LightWareI2C::timer(void)
|
2015-08-28 06:52:34 -03:00
|
|
|
{
|
|
|
|
if (get_reading(state.distance_cm)) {
|
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-08-28 06:52:34 -03:00
|
|
|
// update range_valid state based on distance measured
|
|
|
|
update_status();
|
|
|
|
} else {
|
|
|
|
set_status(RangeFinder::RangeFinder_NoData);
|
2017-01-13 15:26:14 -04:00
|
|
|
}
|
2015-08-28 06:52:34 -03:00
|
|
|
}
|