2016-12-15 20:53:30 -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/>.
|
|
|
|
*/
|
|
|
|
/*
|
2017-07-28 09:46:52 -03:00
|
|
|
driver for TeraRanger I2C rangefinders
|
2016-12-15 20:53:30 -04:00
|
|
|
*/
|
2017-07-28 09:46:52 -03:00
|
|
|
#include "AP_RangeFinder_TeraRangerI2C.h"
|
2016-12-15 20:53:30 -04:00
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2016-12-16 18:40:58 -04:00
|
|
|
#include <AP_Math/crc.h>
|
2016-12-15 20:53:30 -04:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
// registers
|
2017-07-28 09:46:52 -03:00
|
|
|
#define TR_MEASURE 0x00
|
|
|
|
#define TR_WHOAMI 0x01
|
|
|
|
#define TR_WHOAMI_VALUE 0xA1
|
2016-12-15 20:53:30 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
The constructor also initializes the rangefinder. Note that this
|
|
|
|
constructor is not called until detect() returns true, so we
|
|
|
|
already know that we should setup the rangefinder
|
|
|
|
*/
|
2017-07-28 09:20:10 -03:00
|
|
|
AP_RangeFinder_TeraRangerI2C::AP_RangeFinder_TeraRangerI2C(RangeFinder::RangeFinder_State &_state,
|
|
|
|
AP_HAL::OwnPtr<AP_HAL::I2CDevice> i2c_dev)
|
2017-08-08 04:32:53 -03:00
|
|
|
: AP_RangeFinder_Backend(_state)
|
2017-07-28 09:20:10 -03:00
|
|
|
, dev(std::move(i2c_dev))
|
2016-12-15 20:53:30 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-07-28 09:46:52 -03:00
|
|
|
detect if a TeraRanger rangefinder is connected. We'll detect by
|
2016-12-15 20:53:30 -04:00
|
|
|
trying to take a reading on I2C. If we get a result the sensor is
|
|
|
|
there.
|
|
|
|
*/
|
2017-07-28 09:20:10 -03:00
|
|
|
AP_RangeFinder_Backend *AP_RangeFinder_TeraRangerI2C::detect(RangeFinder::RangeFinder_State &_state,
|
|
|
|
AP_HAL::OwnPtr<AP_HAL::I2CDevice> i2c_dev)
|
2016-12-15 20:53:30 -04:00
|
|
|
{
|
2017-07-28 09:20:10 -03:00
|
|
|
AP_RangeFinder_TeraRangerI2C *sensor = new AP_RangeFinder_TeraRangerI2C(_state, std::move(i2c_dev));
|
2016-12-15 20:53:30 -04:00
|
|
|
if (!sensor) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sensor->init()) {
|
|
|
|
delete sensor;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sensor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
initialise sensor
|
|
|
|
*/
|
2017-07-28 09:46:52 -03:00
|
|
|
bool AP_RangeFinder_TeraRangerI2C::init(void)
|
2016-12-15 20:53:30 -04:00
|
|
|
{
|
2017-02-18 00:36:12 -04:00
|
|
|
if (!dev->get_semaphore()->take(HAL_SEMAPHORE_BLOCK_FOREVER)) {
|
2016-12-15 20:53:30 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->set_retries(10);
|
|
|
|
|
2016-12-16 18:40:58 -04:00
|
|
|
// check WHOAMI
|
|
|
|
uint8_t whoami;
|
2017-07-28 09:46:52 -03:00
|
|
|
if (!dev->read_registers(TR_WHOAMI, &whoami, 1) ||
|
|
|
|
whoami != TR_WHOAMI_VALUE) {
|
2016-12-16 18:40:58 -04:00
|
|
|
return false;
|
|
|
|
}
|
2018-08-03 11:41:15 -03:00
|
|
|
|
2016-12-15 20:53:30 -04:00
|
|
|
if (!measure()) {
|
|
|
|
dev->get_semaphore()->give();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// give time for the sensor to process the request
|
|
|
|
hal.scheduler->delay(70);
|
|
|
|
|
2017-08-08 02:54:09 -03:00
|
|
|
uint16_t _distance_cm;
|
2018-08-03 11:41:15 -03:00
|
|
|
if (!collect_raw(_distance_cm)) {
|
2016-12-15 20:53:30 -04:00
|
|
|
dev->get_semaphore()->give();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->get_semaphore()->give();
|
|
|
|
|
|
|
|
dev->set_retries(1);
|
|
|
|
|
|
|
|
dev->register_periodic_callback(50000,
|
2017-07-28 09:46:52 -03:00
|
|
|
FUNCTOR_BIND_MEMBER(&AP_RangeFinder_TeraRangerI2C::timer, void));
|
2016-12-15 20:53:30 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// measure() - ask sensor to make a range reading
|
2017-07-28 09:46:52 -03:00
|
|
|
bool AP_RangeFinder_TeraRangerI2C::measure()
|
2016-12-15 20:53:30 -04:00
|
|
|
{
|
2017-07-28 09:46:52 -03:00
|
|
|
uint8_t cmd = TR_MEASURE;
|
2016-12-15 20:53:30 -04:00
|
|
|
return dev->transfer(&cmd, 1, nullptr, 0);
|
|
|
|
}
|
|
|
|
|
2018-08-03 11:41:45 -03:00
|
|
|
// collect_raw() - return last value measured by sensor
|
2018-08-03 11:41:15 -03:00
|
|
|
bool AP_RangeFinder_TeraRangerI2C::collect_raw(uint16_t &raw_distance)
|
2016-12-15 20:53:30 -04:00
|
|
|
{
|
|
|
|
uint8_t d[3];
|
|
|
|
|
2018-08-03 11:41:15 -03:00
|
|
|
// Take range reading
|
2016-12-15 20:53:30 -04:00
|
|
|
if (!dev->transfer(nullptr, 0, d, sizeof(d))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-03 11:41:15 -03:00
|
|
|
// Check for CRC
|
2016-12-16 18:40:58 -04:00
|
|
|
if (d[2] != crc_crc8(d, 2)) {
|
|
|
|
return false;
|
2018-08-03 11:41:15 -03:00
|
|
|
} else {
|
|
|
|
raw_distance = ((uint16_t(d[0]) << 8) | d[1]);
|
|
|
|
return true;
|
2016-12-16 18:40:58 -04:00
|
|
|
}
|
2018-08-03 11:41:15 -03:00
|
|
|
}
|
2016-12-15 20:53:30 -04:00
|
|
|
|
2018-08-03 11:41:15 -03:00
|
|
|
// Checks for error code and if correct converts to cm
|
|
|
|
bool AP_RangeFinder_TeraRangerI2C::check_measure(uint16_t raw_distance, uint16_t &output_distance_cm)
|
|
|
|
{
|
|
|
|
// Check for error codes
|
|
|
|
if (raw_distance == 0xFFFF) {
|
2018-08-03 11:41:45 -03:00
|
|
|
// Too far away
|
2018-08-03 11:41:15 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (raw_distance == 0x0000) {
|
2018-08-03 11:41:45 -03:00
|
|
|
// Too close
|
2018-08-03 11:41:15 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (raw_distance == 0x0001) {
|
|
|
|
// Unable to measure
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
output_distance_cm = raw_distance/10;
|
|
|
|
return true;
|
2016-12-15 20:53:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
timer called at 20Hz
|
|
|
|
*/
|
2017-07-28 09:46:52 -03:00
|
|
|
void AP_RangeFinder_TeraRangerI2C::timer(void)
|
2016-12-15 20:53:30 -04:00
|
|
|
{
|
2018-08-03 11:41:15 -03:00
|
|
|
// Take a reading
|
|
|
|
uint16_t _raw_distance = 0;
|
|
|
|
uint16_t _distance_cm = 0;
|
|
|
|
|
|
|
|
if (collect_raw(_raw_distance) && _sem->take(HAL_SEMAPHORE_BLOCK_FOREVER)) {
|
|
|
|
if(check_measure(_raw_distance, _distance_cm)){
|
|
|
|
accum.sum += _distance_cm;
|
|
|
|
accum.count++;
|
|
|
|
}
|
2016-12-15 20:53:30 -04:00
|
|
|
_sem->give();
|
|
|
|
}
|
|
|
|
// and immediately ask for a new reading
|
|
|
|
measure();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
update the state of the sensor
|
|
|
|
*/
|
2017-07-28 09:46:52 -03:00
|
|
|
void AP_RangeFinder_TeraRangerI2C::update(void)
|
2016-12-15 20:53:30 -04:00
|
|
|
{
|
2017-02-18 00:36:12 -04:00
|
|
|
if (_sem->take(HAL_SEMAPHORE_BLOCK_FOREVER)) {
|
2016-12-15 20:53:30 -04:00
|
|
|
if (accum.count > 0) {
|
|
|
|
state.distance_cm = accum.sum / accum.count;
|
|
|
|
accum.sum = 0;
|
|
|
|
accum.count = 0;
|
|
|
|
update_status();
|
|
|
|
} else {
|
|
|
|
set_status(RangeFinder::RangeFinder_NoData);
|
|
|
|
}
|
2018-08-03 11:41:15 -03:00
|
|
|
_sem->give();
|
2016-12-15 20:53:30 -04:00
|
|
|
}
|
|
|
|
}
|