2016-08-15 03:17:15 -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/>.
|
|
|
|
*/
|
|
|
|
|
2021-03-25 04:32:09 -03:00
|
|
|
#include "AP_Proximity_Backend.h"
|
|
|
|
|
|
|
|
#if HAL_PROXIMITY_ENABLED
|
2016-08-15 03:17:15 -03:00
|
|
|
#include <AP_Common/AP_Common.h>
|
2019-05-29 03:20:48 -03:00
|
|
|
#include <AP_Common/Location.h>
|
|
|
|
#include <AP_AHRS/AP_AHRS.h>
|
2019-06-29 02:57:09 -03:00
|
|
|
#include <AC_Avoidance/AP_OADatabase.h>
|
2016-08-15 03:17:15 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2021-02-19 14:55:28 -04:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
2016-08-15 03:17:15 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
base class constructor.
|
|
|
|
This incorporates initialisation as well.
|
|
|
|
*/
|
|
|
|
AP_Proximity_Backend::AP_Proximity_Backend(AP_Proximity &_frontend, AP_Proximity::Proximity_State &_state) :
|
|
|
|
frontend(_frontend),
|
|
|
|
state(_state)
|
|
|
|
{
|
2016-12-16 23:42:13 -04:00
|
|
|
}
|
|
|
|
|
2021-02-17 04:13:03 -04:00
|
|
|
static_assert(PROXIMITY_MAX_DIRECTION <= 8,
|
|
|
|
"get_horizontal_distances assumes 8-bits is enough for validity bitmask");
|
|
|
|
|
2020-12-14 03:46:10 -04:00
|
|
|
// get distances in PROXIMITY_MAX_DIRECTION directions horizontally. used for sending distances to ground station
|
2017-04-12 06:00:41 -03:00
|
|
|
bool AP_Proximity_Backend::get_horizontal_distances(AP_Proximity::Proximity_Distance_Array &prx_dist_array) const
|
2016-11-25 01:01:21 -04:00
|
|
|
{
|
2021-02-27 12:49:27 -04:00
|
|
|
AP_Proximity::Proximity_Distance_Array prx_filt_dist_array; // unused
|
|
|
|
return boundary.get_layer_distances(PROXIMITY_MIDDLE_LAYER, distance_max(), prx_dist_array, prx_filt_dist_array);
|
|
|
|
}
|
|
|
|
// get distances in PROXIMITY_MAX_DIRECTION directions at a layer. used for logging
|
|
|
|
bool AP_Proximity_Backend::get_active_layer_distances(uint8_t layer, AP_Proximity::Proximity_Distance_Array &prx_dist_array, AP_Proximity::Proximity_Distance_Array &prx_filt_dist_array) const
|
|
|
|
{
|
|
|
|
return boundary.get_layer_distances(layer, distance_max(), prx_dist_array, prx_filt_dist_array);
|
2016-11-25 01:01:21 -04:00
|
|
|
}
|
|
|
|
|
2016-08-15 03:17:15 -03:00
|
|
|
// set status and update valid count
|
2019-09-27 05:58:52 -03:00
|
|
|
void AP_Proximity_Backend::set_status(AP_Proximity::Status status)
|
2016-08-15 03:17:15 -03:00
|
|
|
{
|
|
|
|
state.status = status;
|
|
|
|
}
|
2016-11-15 03:11:14 -04:00
|
|
|
|
2021-03-15 15:01:53 -03:00
|
|
|
// timeout faces that have not received data recently and update filter frequencies
|
|
|
|
void AP_Proximity_Backend::boundary_3D_checks()
|
|
|
|
{
|
|
|
|
// set the cutoff freq for low pass filter
|
|
|
|
boundary.set_filter_freq(frontend.get_filter_freq());
|
|
|
|
|
|
|
|
// check if any face has valid distance when it should not
|
|
|
|
const uint32_t now_ms = AP_HAL::millis();
|
|
|
|
// run this check every PROXIMITY_BOUNDARY_3D_TIMEOUT_MS
|
|
|
|
if ((now_ms - _last_timeout_check_ms) > PROXIMITY_BOUNDARY_3D_TIMEOUT_MS) {
|
|
|
|
_last_timeout_check_ms = now_ms;
|
|
|
|
boundary.check_face_timeout();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-30 02:13:45 -03:00
|
|
|
// correct an angle (in degrees) based on the orientation and yaw correction parameters
|
|
|
|
float AP_Proximity_Backend::correct_angle_for_orientation(float angle_degrees) const
|
|
|
|
{
|
|
|
|
const float angle_sign = (frontend.get_orientation(state.instance) == 1) ? -1.0f : 1.0f;
|
|
|
|
return wrap_360(angle_degrees * angle_sign + frontend.get_yaw_correction(state.instance));
|
|
|
|
}
|
|
|
|
|
2021-02-19 14:55:28 -04:00
|
|
|
// check if a reading should be ignored because it falls into an ignore area or if obstacle is near land
|
|
|
|
bool AP_Proximity_Backend::ignore_reading(uint16_t angle_deg, float distance_m) const
|
2016-11-15 03:27:34 -04:00
|
|
|
{
|
2019-12-03 22:46:03 -04:00
|
|
|
// check angle vs each ignore area
|
2016-11-15 03:27:34 -04:00
|
|
|
for (uint8_t i=0; i < PROXIMITY_MAX_IGNORE; i++) {
|
|
|
|
if (frontend._ignore_width_deg[i] != 0) {
|
2020-06-02 19:07:06 -03:00
|
|
|
if (abs(angle_deg - frontend._ignore_angle_deg[i]) <= (frontend._ignore_width_deg[i]/2)) {
|
2019-12-03 22:46:03 -04:00
|
|
|
return true;
|
2016-11-15 03:27:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-19 14:55:28 -04:00
|
|
|
|
|
|
|
// check if obstacle is near land
|
|
|
|
return check_obstacle_near_ground(angle_deg, distance_m);
|
|
|
|
}
|
|
|
|
|
|
|
|
// store rangefinder values
|
|
|
|
void AP_Proximity_Backend::set_rangefinder_alt(bool use, bool healthy, float alt_cm)
|
|
|
|
{
|
|
|
|
_last_downward_update_ms = AP_HAL::millis();
|
|
|
|
_rangefinder_use = use;
|
|
|
|
_rangefinder_healthy = healthy;
|
|
|
|
_rangefinder_alt = alt_cm * 0.01f;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get alt from rangefinder in meters
|
|
|
|
bool AP_Proximity_Backend::get_rangefinder_alt(float &alt_m) const
|
|
|
|
{
|
|
|
|
if (!_rangefinder_use || !_rangefinder_healthy) {
|
|
|
|
// range finder is not healthy
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint32_t dt = AP_HAL::millis() - _last_downward_update_ms;
|
|
|
|
if (dt > PROXIMITY_ALT_DETECT_TIMEOUT_MS) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// readings are healthy
|
|
|
|
alt_m = _rangefinder_alt;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if Obstacle defined by body-frame yaw and pitch is near ground
|
|
|
|
bool AP_Proximity_Backend::check_obstacle_near_ground(float yaw, float pitch, float distance) const
|
|
|
|
{
|
|
|
|
if (!frontend._ign_gnd_enable) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!hal.util->get_soft_armed()) {
|
|
|
|
// don't run this feature while vehicle is disarmed, otherwise proximity data will not show up on GCS
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((pitch > 90.0f) || (pitch < -90.0f)) {
|
|
|
|
// sanity check on pitch
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Assume object is yaw and pitch bearing and distance meters away from the vehicle
|
|
|
|
Vector3f object_3D;
|
|
|
|
object_3D.offset_bearing(wrap_180(yaw), (pitch * -1.0f), distance);
|
|
|
|
const Matrix3f body_to_ned = AP::ahrs().get_rotation_body_to_ned();
|
|
|
|
const Vector3f rotated_object_3D = body_to_ned * object_3D;
|
|
|
|
return check_obstacle_near_ground(rotated_object_3D);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if Obstacle defined by Vector3f is near ground. The vector is assumed to be body frame FRD
|
|
|
|
bool AP_Proximity_Backend::check_obstacle_near_ground(const Vector3f &obstacle) const
|
|
|
|
{
|
|
|
|
if (!frontend._ign_gnd_enable) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!hal.util->get_soft_armed()) {
|
|
|
|
// don't run this feature while vehicle is disarmed, otherwise proximity data will not show up on GCS
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
float alt = FLT_MAX;
|
|
|
|
if (!get_rangefinder_alt(alt)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (obstacle.z > -0.5f) {
|
|
|
|
// obstacle is at the most 0.5 meters above vehicle
|
|
|
|
if ((alt - PROXIMITY_GND_DETECT_THRESHOLD) < obstacle.z) {
|
|
|
|
// obstacle is near or below ground
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2019-12-03 22:46:03 -04:00
|
|
|
return false;
|
2016-11-15 03:27:34 -04:00
|
|
|
}
|
2019-05-29 03:20:48 -03:00
|
|
|
|
2019-06-29 02:57:09 -03:00
|
|
|
// returns true if database is ready to be pushed to and all cached data is ready
|
2020-06-08 04:01:01 -03:00
|
|
|
bool AP_Proximity_Backend::database_prepare_for_push(Vector3f ¤t_pos, Matrix3f &body_to_ned)
|
2019-05-29 03:20:48 -03:00
|
|
|
{
|
2019-06-29 02:57:09 -03:00
|
|
|
AP_OADatabase *oaDb = AP::oadatabase();
|
|
|
|
if (oaDb == nullptr || !oaDb->healthy()) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-29 03:20:48 -03:00
|
|
|
|
2020-06-08 04:01:01 -03:00
|
|
|
if (!AP::ahrs().get_relative_position_NED_origin(current_pos)) {
|
2019-06-29 02:57:09 -03:00
|
|
|
return false;
|
2019-05-29 03:20:48 -03:00
|
|
|
}
|
|
|
|
|
2020-06-08 04:01:01 -03:00
|
|
|
body_to_ned = AP::ahrs().get_rotation_body_to_ned();
|
|
|
|
|
2019-06-29 02:57:09 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update Object Avoidance database with Earth-frame point
|
2019-11-26 21:32:56 -04:00
|
|
|
void AP_Proximity_Backend::database_push(float angle, float distance)
|
2019-06-29 02:57:09 -03:00
|
|
|
{
|
2020-06-08 04:01:01 -03:00
|
|
|
Vector3f current_pos;
|
|
|
|
Matrix3f body_to_ned;
|
|
|
|
|
|
|
|
if (database_prepare_for_push(current_pos, body_to_ned)) {
|
|
|
|
database_push(angle, distance, AP_HAL::millis(), current_pos, body_to_ned);
|
2019-05-29 03:20:48 -03:00
|
|
|
}
|
|
|
|
}
|
2019-06-29 02:57:09 -03:00
|
|
|
|
|
|
|
// update Object Avoidance database with Earth-frame point
|
2020-12-06 08:21:40 -04:00
|
|
|
// pitch can be optionally provided if needed
|
2021-01-10 14:14:24 -04:00
|
|
|
void AP_Proximity_Backend::database_push(float angle, float pitch, float distance, uint32_t timestamp_ms, const Vector3f ¤t_pos, const Matrix3f &body_to_ned)
|
2019-06-29 02:57:09 -03:00
|
|
|
{
|
|
|
|
AP_OADatabase *oaDb = AP::oadatabase();
|
|
|
|
if (oaDb == nullptr || !oaDb->healthy()) {
|
|
|
|
return;
|
|
|
|
}
|
2021-02-19 14:55:28 -04:00
|
|
|
if ((pitch > 90.0f) || (pitch < -90.0f)) {
|
|
|
|
// sanity check on pitch
|
|
|
|
return;
|
|
|
|
}
|
2020-12-06 08:21:40 -04:00
|
|
|
//Assume object is angle and pitch bearing and distance meters away from the vehicle
|
|
|
|
Vector3f object_3D;
|
2021-02-19 14:55:28 -04:00
|
|
|
object_3D.offset_bearing(wrap_180(angle), (pitch * -1.0f), distance);
|
2020-06-08 04:01:01 -03:00
|
|
|
const Vector3f rotated_object_3D = body_to_ned * object_3D;
|
2021-02-11 03:36:58 -04:00
|
|
|
|
2020-06-08 04:01:01 -03:00
|
|
|
//Calculate the position vector from origin
|
|
|
|
Vector3f temp_pos = current_pos + rotated_object_3D;
|
|
|
|
//Convert the vector to a NEU frame from NED
|
|
|
|
temp_pos.z = temp_pos.z * -1.0f;
|
2021-02-11 03:36:58 -04:00
|
|
|
|
2019-12-04 03:17:23 -04:00
|
|
|
oaDb->queue_push(temp_pos, timestamp_ms, distance);
|
2020-12-06 08:21:40 -04:00
|
|
|
}
|
2021-03-25 04:32:09 -03:00
|
|
|
|
|
|
|
#endif // HAL_PROXIMITY_ENABLED
|