2020-05-17 03:41:50 -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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AP_RangeFinder_HC_SR04.cpp - rangefinder for HC_SR04 source
|
|
|
|
*
|
|
|
|
* https://cdn.sparkfun.com/datasheets/Sensors/Proximity/HCSR04.pdf
|
|
|
|
*
|
|
|
|
* There are two pins involved - one we attach an interrupt handler
|
|
|
|
* to and use for measuring the supplied interval which is
|
|
|
|
* proportional to distance.
|
|
|
|
*
|
|
|
|
* The second pin we use for triggering the ultransonic pulse
|
|
|
|
*/
|
|
|
|
|
2022-03-12 06:37:29 -04:00
|
|
|
#include "AP_RangeFinder_HC_SR04.h"
|
|
|
|
|
|
|
|
#if AP_RANGEFINDER_HC_SR04_ENABLED
|
|
|
|
|
2020-05-17 03:41:50 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include "AP_RangeFinder.h"
|
|
|
|
#include "AP_RangeFinder_Params.h"
|
|
|
|
|
|
|
|
#include <GCS_MAVLink/GCS.h>
|
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
AP_RangeFinder_HC_SR04::AP_RangeFinder_HC_SR04(RangeFinder::RangeFinder_State &_state, AP_RangeFinder_Params &_params) :
|
|
|
|
AP_RangeFinder_Backend(_state, _params)
|
|
|
|
{
|
|
|
|
set_status(RangeFinder::Status::NoData);
|
|
|
|
}
|
|
|
|
|
2020-09-01 02:25:44 -03:00
|
|
|
bool AP_RangeFinder_HC_SR04::check_pins()
|
2020-05-17 03:41:50 -03:00
|
|
|
{
|
|
|
|
check_trigger_pin();
|
2020-09-01 02:25:44 -03:00
|
|
|
return check_echo_pin() && trigger_pin > 0;
|
2020-05-17 03:41:50 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AP_RangeFinder_HC_SR04::check_trigger_pin()
|
|
|
|
{
|
|
|
|
if (params.stop_pin == trigger_pin) {
|
|
|
|
// no change
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
trigger_pin = params.stop_pin;
|
|
|
|
}
|
|
|
|
|
2020-09-01 02:25:44 -03:00
|
|
|
bool AP_RangeFinder_HC_SR04::check_echo_pin()
|
2020-05-17 03:41:50 -03:00
|
|
|
{
|
2020-09-01 02:25:44 -03:00
|
|
|
return pwm_source.set_pin(params.pin, "HC_SR04");
|
2020-05-17 03:41:50 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
detect if an HC_SR04 rangefinder is connected. The only thing we
|
|
|
|
can do is check if the pin number is valid. If it is, then assume
|
|
|
|
that the device is connected
|
|
|
|
*/
|
|
|
|
bool AP_RangeFinder_HC_SR04::detect(AP_RangeFinder_Params &_params)
|
|
|
|
{
|
|
|
|
if (_params.pin == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (_params.stop_pin == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
update distance_cm
|
|
|
|
*/
|
|
|
|
void AP_RangeFinder_HC_SR04::update(void)
|
|
|
|
{
|
|
|
|
// check if pin has changed and configure interrupt handlers if required:
|
2020-09-01 02:25:44 -03:00
|
|
|
if (!check_pins()) {
|
2020-05-17 03:41:50 -03:00
|
|
|
// disabled (either by configuration or failure to attach interrupt)
|
2021-10-18 02:45:33 -03:00
|
|
|
state.distance_m = 0.0f;
|
2020-05-17 03:41:50 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-01 02:25:44 -03:00
|
|
|
const uint32_t value_us = pwm_source.get_pwm_us();
|
2020-05-17 03:41:50 -03:00
|
|
|
|
|
|
|
const uint32_t now = AP_HAL::millis();
|
|
|
|
if (value_us == 0) {
|
|
|
|
// no reading; check for timeout:
|
|
|
|
if (now - last_reading_ms > 1000) {
|
|
|
|
// no reading for a second - something is broken
|
2021-10-18 02:45:33 -03:00
|
|
|
state.distance_m = 0.0f;
|
2020-05-17 03:41:50 -03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// gcs().send_text(MAV_SEVERITY_WARNING, "Pong!");
|
|
|
|
// a new reading - convert time to distance
|
2021-10-18 02:45:33 -03:00
|
|
|
state.distance_m = (value_us * (1.0/58.0f)) * 0.01f; // 58 is from datasheet, mult for performance
|
2020-05-29 19:52:38 -03:00
|
|
|
|
|
|
|
// glitch remover: measurement is greater than .5m from last.
|
|
|
|
// the SR-04 seeems to suffer from single-measurement glitches
|
|
|
|
// which can be removed by a simple filter.
|
2021-10-18 02:45:33 -03:00
|
|
|
if (fabsf(state.distance_m - last_distance_m) > 0.5f) {
|
2020-05-29 19:52:38 -03:00
|
|
|
// if greater for 5 readings then pass it as new height,
|
|
|
|
// otherwise use last reading
|
|
|
|
if (glitch_count++ > 4) {
|
2021-10-18 02:45:33 -03:00
|
|
|
last_distance_m = state.distance_m;
|
2020-05-29 19:52:38 -03:00
|
|
|
} else {
|
2021-10-18 02:45:33 -03:00
|
|
|
state.distance_m = last_distance_m;
|
2020-05-29 19:52:38 -03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// is not greater 0.5m, pass on and reset glitch counter
|
2021-10-18 02:45:33 -03:00
|
|
|
last_distance_m = state.distance_m;
|
2020-05-29 19:52:38 -03:00
|
|
|
glitch_count = 0;
|
|
|
|
}
|
|
|
|
|
2020-05-17 03:41:50 -03:00
|
|
|
last_reading_ms = now;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update range_valid state based on distance measured
|
|
|
|
update_status();
|
|
|
|
|
|
|
|
// consider sending new ping
|
|
|
|
if (now - last_ping_ms > 67) { // read ~@15Hz - recommended 60ms delay from datasheet
|
|
|
|
last_ping_ms = now;
|
|
|
|
// gcs().send_text(MAV_SEVERITY_INFO, "Ping!");
|
|
|
|
// raise stop pin for n-microseconds
|
|
|
|
hal.gpio->pinMode(trigger_pin, HAL_GPIO_OUTPUT);
|
|
|
|
hal.gpio->write(trigger_pin, 1);
|
|
|
|
hal.scheduler->delay_microseconds(10);
|
|
|
|
hal.gpio->write(trigger_pin, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-12 06:37:29 -04:00
|
|
|
#endif // AP_RANGEFINDER_HC_SR04_ENABLED
|