2013-08-29 02:34: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/>.
|
|
|
|
*/
|
|
|
|
|
2012-11-11 11:25:29 -04:00
|
|
|
/*
|
|
|
|
* AP_RangeFinder_MaxsonarI2CXL.cpp - Arduino Library for MaxBotix I2C XL sonar
|
|
|
|
* Code by Randy Mackay. DIYDrones.com
|
|
|
|
*
|
|
|
|
* datasheet: http://www.maxbotix.com/documents/I2CXL-MaxSonar-EZ_Datasheet.pdf
|
|
|
|
*
|
|
|
|
* Sensor should be connected to the I2C port
|
|
|
|
*/
|
|
|
|
#include "AP_RangeFinder_MaxsonarI2CXL.h"
|
2016-07-12 19:00:01 -03:00
|
|
|
|
2022-03-12 06:37:29 -04:00
|
|
|
#if AP_RANGEFINDER_MAXSONARI2CXL_ENABLED
|
|
|
|
|
2016-04-14 13:42:06 -03:00
|
|
|
#include <utility>
|
2012-11-27 21:39:15 -04:00
|
|
|
|
2016-07-12 19:00:01 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include <AP_HAL/utility/sparse-endian.h>
|
|
|
|
|
2012-11-27 21:39:15 -04:00
|
|
|
extern const AP_HAL::HAL& hal;
|
2012-11-11 11:25:29 -04:00
|
|
|
|
2017-09-23 19:33:04 -03:00
|
|
|
AP_RangeFinder_MaxsonarI2CXL::AP_RangeFinder_MaxsonarI2CXL(RangeFinder::RangeFinder_State &_state,
|
2019-02-03 22:21:58 -04:00
|
|
|
AP_RangeFinder_Params &_params,
|
2017-09-23 19:33:04 -03:00
|
|
|
AP_HAL::OwnPtr<AP_HAL::I2CDevice> dev)
|
2018-07-04 11:22:17 -03:00
|
|
|
: AP_RangeFinder_Backend(_state, _params)
|
2017-09-23 19:33:04 -03:00
|
|
|
, _dev(std::move(dev))
|
2012-11-11 11:25:29 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-07-12 19:00:01 -03:00
|
|
|
/*
|
2014-06-27 01:03:47 -03:00
|
|
|
detect if a Maxbotix rangefinder is connected. We'll detect by
|
|
|
|
trying to take a reading on I2C. If we get a result the sensor is
|
|
|
|
there.
|
|
|
|
*/
|
2017-09-23 19:33:04 -03:00
|
|
|
AP_RangeFinder_Backend *AP_RangeFinder_MaxsonarI2CXL::detect(RangeFinder::RangeFinder_State &_state,
|
2018-07-04 11:22:17 -03:00
|
|
|
AP_RangeFinder_Params &_params,
|
2017-09-23 19:33:04 -03:00
|
|
|
AP_HAL::OwnPtr<AP_HAL::I2CDevice> dev)
|
2014-06-27 01:03:47 -03:00
|
|
|
{
|
2018-10-25 23:39:48 -03:00
|
|
|
if (!dev) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-04-14 13:42:06 -03:00
|
|
|
AP_RangeFinder_MaxsonarI2CXL *sensor
|
2018-07-04 11:22:17 -03:00
|
|
|
= new AP_RangeFinder_MaxsonarI2CXL(_state, _params, std::move(dev));
|
2016-11-04 00:35:41 -03:00
|
|
|
if (!sensor) {
|
2016-04-14 13:42:06 -03:00
|
|
|
return nullptr;
|
2014-06-27 01:03:47 -03:00
|
|
|
}
|
2016-07-12 19:00:01 -03:00
|
|
|
|
2016-11-04 00:35:41 -03:00
|
|
|
if (!sensor->_init()) {
|
2016-04-14 13:42:06 -03:00
|
|
|
delete sensor;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sensor;
|
2014-06-27 01:03:47 -03:00
|
|
|
}
|
2012-11-11 11:25:29 -04:00
|
|
|
|
2016-11-04 00:35:41 -03:00
|
|
|
/*
|
|
|
|
initialise sensor
|
|
|
|
*/
|
|
|
|
bool AP_RangeFinder_MaxsonarI2CXL::_init(void)
|
2012-11-11 11:25:29 -04:00
|
|
|
{
|
2020-01-18 17:42:34 -04:00
|
|
|
_dev->get_semaphore()->take_blocking();
|
2014-06-27 01:03:47 -03:00
|
|
|
|
2016-11-04 00:35:41 -03:00
|
|
|
if (!start_reading()) {
|
|
|
|
_dev->get_semaphore()->give();
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-27 01:03:47 -03:00
|
|
|
|
2016-11-04 00:35:41 -03:00
|
|
|
// give time for the sensor to process the request
|
2018-05-23 09:37:42 -03:00
|
|
|
hal.scheduler->delay(100);
|
2016-11-04 00:35:41 -03:00
|
|
|
|
|
|
|
uint16_t reading_cm;
|
|
|
|
if (!get_reading(reading_cm)) {
|
|
|
|
_dev->get_semaphore()->give();
|
|
|
|
return false;
|
|
|
|
}
|
2016-07-12 19:00:01 -03:00
|
|
|
|
2016-04-14 13:42:06 -03:00
|
|
|
_dev->get_semaphore()->give();
|
2018-05-22 09:38:12 -03:00
|
|
|
|
2018-05-23 09:37:42 -03:00
|
|
|
_dev->register_periodic_callback(100000,
|
2017-01-13 15:26:14 -04:00
|
|
|
FUNCTOR_BIND_MEMBER(&AP_RangeFinder_MaxsonarI2CXL::_timer, void));
|
2018-05-22 09:38:12 -03:00
|
|
|
|
2016-11-04 00:35:41 -03:00
|
|
|
return true;
|
|
|
|
}
|
2016-07-12 19:00:01 -03:00
|
|
|
|
2016-11-04 00:35:41 -03:00
|
|
|
// start_reading() - ask sensor to make a range reading
|
|
|
|
bool AP_RangeFinder_MaxsonarI2CXL::start_reading()
|
|
|
|
{
|
|
|
|
uint8_t cmd = AP_RANGE_FINDER_MAXSONARI2CXL_COMMAND_TAKE_RANGE_READING;
|
|
|
|
|
|
|
|
// send command to take reading
|
|
|
|
return _dev->transfer(&cmd, sizeof(cmd), nullptr, 0);
|
2012-11-11 11:25:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// read - return last value measured by sensor
|
2014-06-27 01:03:47 -03:00
|
|
|
bool AP_RangeFinder_MaxsonarI2CXL::get_reading(uint16_t &reading_cm)
|
2012-11-11 11:25:29 -04:00
|
|
|
{
|
2016-07-12 19:00:01 -03:00
|
|
|
be16_t val;
|
2016-07-11 20:05:51 -03:00
|
|
|
|
2012-11-11 11:25:29 -04:00
|
|
|
// take range reading and read back results
|
2016-07-12 19:00:01 -03:00
|
|
|
bool ret = _dev->transfer(nullptr, 0, (uint8_t *) &val, sizeof(val));
|
2014-06-27 01:03:47 -03:00
|
|
|
|
2016-07-11 20:05:51 -03:00
|
|
|
if (ret) {
|
|
|
|
// combine results into distance
|
2016-07-12 19:00:01 -03:00
|
|
|
reading_cm = be16toh(val);
|
2014-06-27 01:03:47 -03:00
|
|
|
|
2016-07-11 20:05:51 -03:00
|
|
|
// trigger a new reading
|
|
|
|
start_reading();
|
|
|
|
}
|
2014-07-08 03:28:18 -03:00
|
|
|
|
2016-07-11 20:05:51 -03:00
|
|
|
return ret;
|
2014-06-27 01:03:47 -03:00
|
|
|
}
|
|
|
|
|
2016-11-04 00:35:41 -03:00
|
|
|
/*
|
2018-05-23 09:37:42 -03:00
|
|
|
timer called at 10Hz
|
2016-11-04 00:35:41 -03:00
|
|
|
*/
|
2017-01-13 15:26:14 -04:00
|
|
|
void AP_RangeFinder_MaxsonarI2CXL::_timer(void)
|
2016-11-04 00:35:41 -03:00
|
|
|
{
|
|
|
|
uint16_t d;
|
|
|
|
if (get_reading(d)) {
|
2018-10-11 20:35:04 -03:00
|
|
|
WITH_SEMAPHORE(_sem);
|
|
|
|
distance = d;
|
|
|
|
new_distance = true;
|
|
|
|
state.last_reading_ms = AP_HAL::millis();
|
2016-11-04 00:35:41 -03:00
|
|
|
}
|
|
|
|
}
|
2017-01-13 15:26:14 -04:00
|
|
|
|
2016-07-12 19:00:01 -03:00
|
|
|
/*
|
2014-06-27 01:03:47 -03:00
|
|
|
update the state of the sensor
|
|
|
|
*/
|
|
|
|
void AP_RangeFinder_MaxsonarI2CXL::update(void)
|
|
|
|
{
|
2018-10-11 20:35:04 -03:00
|
|
|
WITH_SEMAPHORE(_sem);
|
|
|
|
if (new_distance) {
|
2021-10-18 02:45:33 -03:00
|
|
|
state.distance_m = distance * 0.01f;
|
2018-10-11 20:35:04 -03:00
|
|
|
new_distance = false;
|
|
|
|
update_status();
|
|
|
|
} else if (AP_HAL::millis() - state.last_reading_ms > 300) {
|
|
|
|
// if no updates for 0.3 seconds set no-data
|
2019-11-01 02:10:52 -03:00
|
|
|
set_status(RangeFinder::Status::NoData);
|
2015-04-13 03:08:00 -03:00
|
|
|
}
|
2012-11-27 21:39:15 -04:00
|
|
|
}
|
2022-03-12 06:37:29 -04:00
|
|
|
|
|
|
|
#endif // AP_RANGEFINDER_MAXSONARI2CXL_ENABLED
|