2017-02-09 07:30:04 -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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "AP_Proximity_RangeFinder.h"
|
2021-03-25 04:32:09 -03:00
|
|
|
|
|
|
|
#if HAL_PROXIMITY_ENABLED
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2017-02-09 07:30:04 -04:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdio.h>
|
2019-09-27 06:04:30 -03:00
|
|
|
#include <AP_RangeFinder/AP_RangeFinder.h>
|
2019-11-10 22:48:22 -04:00
|
|
|
#include <AP_RangeFinder/AP_RangeFinder_Backend.h>
|
2017-02-09 07:30:04 -04:00
|
|
|
|
|
|
|
// update the state of the sensor
|
|
|
|
void AP_Proximity_RangeFinder::update(void)
|
|
|
|
{
|
|
|
|
// exit immediately if no rangefinder object
|
2019-09-27 06:04:30 -03:00
|
|
|
const RangeFinder *rngfnd = AP::rangefinder();
|
2017-02-09 07:30:04 -04:00
|
|
|
if (rngfnd == nullptr) {
|
2019-09-27 05:58:52 -03:00
|
|
|
set_status(AP_Proximity::Status::NoData);
|
2017-02-09 07:30:04 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-11 16:29:11 -03:00
|
|
|
uint32_t now = AP_HAL::millis();
|
|
|
|
|
2017-02-09 07:30:04 -04:00
|
|
|
// look through all rangefinders
|
2018-05-13 04:31:37 -03:00
|
|
|
for (uint8_t i=0; i < rngfnd->num_sensors(); i++) {
|
2017-08-08 01:52:19 -03:00
|
|
|
AP_RangeFinder_Backend *sensor = rngfnd->get_backend(i);
|
|
|
|
if (sensor == nullptr) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (sensor->has_data()) {
|
2017-02-09 07:30:04 -04:00
|
|
|
// check for horizontal range finders
|
2017-08-08 01:52:19 -03:00
|
|
|
if (sensor->orientation() <= ROTATION_YAW_315) {
|
2020-12-06 08:23:55 -04:00
|
|
|
const uint8_t sector = (uint8_t)sensor->orientation();
|
2020-12-14 03:59:28 -04:00
|
|
|
const float angle = sector * 45;
|
2022-07-07 00:35:43 -03:00
|
|
|
const AP_Proximity_Boundary_3D::Face face = frontend.boundary.get_face(angle);
|
2020-12-06 08:23:55 -04:00
|
|
|
// distance in meters
|
2021-10-18 02:45:33 -03:00
|
|
|
const float distance = sensor->distance();
|
2019-06-29 02:57:09 -03:00
|
|
|
_distance_min = sensor->min_distance_cm() * 0.01f;
|
|
|
|
_distance_max = sensor->max_distance_cm() * 0.01f;
|
2021-12-29 07:16:34 -04:00
|
|
|
if ((distance <= _distance_max) && (distance >= _distance_min) && !ignore_reading(angle, distance, false)) {
|
2022-07-07 04:55:05 -03:00
|
|
|
frontend.boundary.set_face_attributes(face, angle, distance, state.instance);
|
2020-12-06 08:23:55 -04:00
|
|
|
// update OA database
|
2021-10-18 02:45:33 -03:00
|
|
|
database_push(angle, distance);
|
2020-12-14 03:59:28 -04:00
|
|
|
} else {
|
2022-07-07 04:55:05 -03:00
|
|
|
frontend.boundary.reset_face(face, state.instance);
|
2020-12-06 08:23:55 -04:00
|
|
|
}
|
2018-05-11 16:29:11 -03:00
|
|
|
_last_update_ms = now;
|
2017-02-09 07:30:04 -04:00
|
|
|
}
|
|
|
|
// check upward facing range finder
|
2017-08-08 01:52:19 -03:00
|
|
|
if (sensor->orientation() == ROTATION_PITCH_90) {
|
2018-05-11 16:29:11 -03:00
|
|
|
int16_t distance_upward = sensor->distance_cm();
|
|
|
|
int16_t up_distance_min = sensor->min_distance_cm();
|
|
|
|
int16_t up_distance_max = sensor->max_distance_cm();
|
|
|
|
if ((distance_upward >= up_distance_min) && (distance_upward <= up_distance_max)) {
|
2019-09-17 08:11:21 -03:00
|
|
|
_distance_upward = distance_upward * 0.01f;
|
2018-05-11 16:29:11 -03:00
|
|
|
} else {
|
|
|
|
_distance_upward = -1.0; // mark an valid reading
|
|
|
|
}
|
|
|
|
_last_upward_update_ms = now;
|
2017-02-09 07:30:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for timeout and set health status
|
2021-06-15 21:38:56 -03:00
|
|
|
if ((_last_update_ms == 0 || (now - _last_update_ms > PROXIMITY_RANGEFIDER_TIMEOUT_MS)) &&
|
|
|
|
(_last_upward_update_ms == 0 || (now - _last_upward_update_ms > PROXIMITY_RANGEFIDER_TIMEOUT_MS))) {
|
2019-09-27 05:58:52 -03:00
|
|
|
set_status(AP_Proximity::Status::NoData);
|
2017-02-09 07:30:04 -04:00
|
|
|
} else {
|
2019-09-27 05:58:52 -03:00
|
|
|
set_status(AP_Proximity::Status::Good);
|
2017-02-09 07:30:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get distance upwards in meters. returns true on success
|
|
|
|
bool AP_Proximity_RangeFinder::get_upward_distance(float &distance) const
|
|
|
|
{
|
2018-05-13 04:31:37 -03:00
|
|
|
if ((AP_HAL::millis() - _last_upward_update_ms <= PROXIMITY_RANGEFIDER_TIMEOUT_MS) &&
|
2018-05-11 16:29:11 -03:00
|
|
|
is_positive(_distance_upward)) {
|
2017-02-09 07:30:04 -04:00
|
|
|
distance = _distance_upward;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-25 04:32:09 -03:00
|
|
|
|
|
|
|
#endif // HAL_PROXIMITY_ENABLED
|