2018-08-29 11:18:49 -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_PWM.h"
|
|
|
|
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include <GCS_MAVLink/GCS.h>
|
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
/*
|
|
|
|
The constructor also initialises the rangefinder.
|
|
|
|
*/
|
2019-01-08 23:08:08 -04:00
|
|
|
AP_RangeFinder_PWM::AP_RangeFinder_PWM(RangeFinder::RangeFinder_State &_state,
|
2018-07-04 11:22:17 -03:00
|
|
|
AP_RangeFinder_Params &_params,
|
2019-01-08 23:08:08 -04:00
|
|
|
float &_estimated_terrain_height) :
|
2018-07-04 11:22:17 -03:00
|
|
|
AP_RangeFinder_Backend(_state, _params),
|
2019-01-08 23:08:08 -04:00
|
|
|
estimated_terrain_height(_estimated_terrain_height)
|
2018-08-29 11:18:49 -03:00
|
|
|
{
|
2022-06-16 05:11:26 -03:00
|
|
|
// this gives one mm per us
|
|
|
|
params.scaling.set_default(1.0);
|
2018-08-29 11:18:49 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
There's no sensible way of detecting a PWM rangefinder as the pins are configurable
|
|
|
|
*/
|
|
|
|
bool AP_RangeFinder_PWM::detect()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read - return last value measured by sensor
|
2021-10-18 02:45:33 -03:00
|
|
|
bool AP_RangeFinder_PWM::get_reading(float &reading_m)
|
2018-08-29 11:18:49 -03:00
|
|
|
{
|
2020-09-01 02:25:44 -03:00
|
|
|
const uint32_t value_us = pwm_source.get_pwm_avg_us();
|
|
|
|
if (value_us == 0) {
|
2018-08-29 11:18:49 -03:00
|
|
|
return false;
|
|
|
|
}
|
2020-09-01 02:25:44 -03:00
|
|
|
|
2022-06-16 05:11:26 -03:00
|
|
|
// LidarLite uses one mm per us
|
|
|
|
reading_m = value_us * 0.001 * params.scaling;
|
2018-08-29 11:18:49 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-01 02:25:44 -03:00
|
|
|
bool AP_RangeFinder_PWM::check_pin()
|
2018-08-29 11:18:49 -03:00
|
|
|
{
|
2020-09-01 02:25:44 -03:00
|
|
|
if (!pwm_source.set_pin(params.pin, "RangeFinder_PWM")) {
|
|
|
|
return false;
|
2018-08-29 11:18:49 -03:00
|
|
|
}
|
2020-09-01 02:25:44 -03:00
|
|
|
return true;
|
2018-08-29 11:18:49 -03:00
|
|
|
}
|
|
|
|
|
2019-01-08 23:08:08 -04:00
|
|
|
void AP_RangeFinder_PWM::check_stop_pin()
|
|
|
|
{
|
2018-07-04 11:22:17 -03:00
|
|
|
if (params.stop_pin == last_stop_pin) {
|
2019-01-08 23:08:08 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-04 11:22:17 -03:00
|
|
|
hal.gpio->pinMode(params.stop_pin, HAL_GPIO_OUTPUT);
|
2019-01-08 23:08:08 -04:00
|
|
|
|
2018-07-04 11:22:17 -03:00
|
|
|
last_stop_pin = params.stop_pin;
|
2019-01-08 23:08:08 -04:00
|
|
|
}
|
|
|
|
|
2020-09-01 02:25:44 -03:00
|
|
|
bool AP_RangeFinder_PWM::check_pins()
|
2019-01-08 23:08:08 -04:00
|
|
|
{
|
|
|
|
check_stop_pin();
|
2020-09-01 02:25:44 -03:00
|
|
|
return check_pin();
|
2019-01-08 23:08:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-29 11:18:49 -03:00
|
|
|
/*
|
|
|
|
update the state of the sensor
|
|
|
|
*/
|
|
|
|
void AP_RangeFinder_PWM::update(void)
|
|
|
|
{
|
|
|
|
// check if pin has changed and configure interrupt handlers if required:
|
2020-09-01 02:25:44 -03:00
|
|
|
if (!check_pins()) {
|
2018-08-29 11:18:49 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-04 11:22:17 -03:00
|
|
|
if (params.stop_pin != -1) {
|
2019-01-08 23:08:08 -04:00
|
|
|
const bool oor = out_of_range();
|
|
|
|
if (oor) {
|
|
|
|
if (!was_out_of_range) {
|
|
|
|
// we are above the power saving range. Disable the sensor
|
2018-07-04 11:22:17 -03:00
|
|
|
hal.gpio->write(params.stop_pin, false);
|
2019-11-01 02:10:52 -03:00
|
|
|
set_status(RangeFinder::Status::NoData);
|
2021-10-18 02:45:33 -03:00
|
|
|
state.distance_m = 0.0f;
|
2019-01-08 23:08:08 -04:00
|
|
|
state.voltage_mv = 0;
|
|
|
|
was_out_of_range = oor;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// re-enable the sensor:
|
|
|
|
if (!oor && was_out_of_range) {
|
2018-07-04 11:22:17 -03:00
|
|
|
hal.gpio->write(params.stop_pin, true);
|
2019-01-08 23:08:08 -04:00
|
|
|
was_out_of_range = oor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 02:45:33 -03:00
|
|
|
if (!get_reading(state.distance_m)) {
|
2018-08-29 11:18:49 -03:00
|
|
|
// failure; consider changing our state
|
|
|
|
if (AP_HAL::millis() - state.last_reading_ms > 200) {
|
2019-11-01 02:10:52 -03:00
|
|
|
set_status(RangeFinder::Status::NoData);
|
2018-08-29 11:18:49 -03:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2020-02-02 21:48:47 -04:00
|
|
|
// add offset
|
2021-10-18 02:45:33 -03:00
|
|
|
state.distance_m += params.offset * 0.01f;
|
2018-08-29 11:18:49 -03:00
|
|
|
|
|
|
|
// update range_valid state based on distance measured
|
|
|
|
state.last_reading_ms = AP_HAL::millis();
|
|
|
|
update_status();
|
|
|
|
}
|
2019-01-08 23:08:08 -04:00
|
|
|
|
|
|
|
|
|
|
|
// return true if we are beyond the power saving range
|
|
|
|
bool AP_RangeFinder_PWM::out_of_range(void) const {
|
2018-07-04 11:22:17 -03:00
|
|
|
return params.powersave_range > 0 && estimated_terrain_height > params.powersave_range;
|
2019-01-08 23:08:08 -04:00
|
|
|
}
|