2016-08-12 18:28:21 -03:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "Plane.h"
|
|
|
|
|
2020-09-19 05:38:20 -03:00
|
|
|
#if HAL_ADSB_ENABLED
|
2016-08-12 18:28:21 -03:00
|
|
|
void Plane::avoidance_adsb_update(void)
|
|
|
|
{
|
|
|
|
adsb.update();
|
|
|
|
avoidance_adsb.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MAV_COLLISION_ACTION AP_Avoidance_Plane::handle_avoidance(const AP_Avoidance::Obstacle *obstacle, MAV_COLLISION_ACTION requested_action)
|
|
|
|
{
|
|
|
|
MAV_COLLISION_ACTION actual_action = requested_action;
|
2016-08-13 06:05:48 -03:00
|
|
|
bool failsafe_state_change = false;
|
|
|
|
|
|
|
|
// check for changes in failsafe
|
|
|
|
if (!plane.failsafe.adsb) {
|
|
|
|
plane.failsafe.adsb = true;
|
|
|
|
failsafe_state_change = true;
|
|
|
|
// record flight mode in case it's required for the recovery
|
2019-01-15 13:46:13 -04:00
|
|
|
prev_control_mode_number = plane.control_mode->mode_number();
|
2016-08-13 06:05:48 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// take no action in some flight modes
|
2021-09-10 03:28:21 -03:00
|
|
|
bool flightmode_prohibits_action = false;
|
2019-01-15 13:46:13 -04:00
|
|
|
if (plane.control_mode == &plane.mode_manual ||
|
|
|
|
(plane.control_mode == &plane.mode_auto && !plane.auto_state.takeoff_complete) ||
|
2017-01-11 01:29:03 -04:00
|
|
|
(plane.flight_stage == AP_Vehicle::FixedWing::FLIGHT_LAND) || // TODO: consider allowing action during approach
|
2021-09-10 03:28:21 -03:00
|
|
|
plane.control_mode == &plane.mode_autotune) {
|
|
|
|
flightmode_prohibits_action = true;
|
|
|
|
}
|
|
|
|
#if HAL_QUADPLANE_ENABLED
|
|
|
|
if (plane.control_mode == &plane.mode_qland) {
|
|
|
|
flightmode_prohibits_action = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (flightmode_prohibits_action) {
|
2016-08-13 06:05:48 -03:00
|
|
|
actual_action = MAV_COLLISION_ACTION_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// take action based on requested action
|
|
|
|
switch (actual_action) {
|
|
|
|
|
|
|
|
case MAV_COLLISION_ACTION_RTL:
|
|
|
|
if (failsafe_state_change) {
|
2019-10-17 00:49:32 -03:00
|
|
|
plane.set_mode(plane.mode_rtl, ModeReason::AVOIDANCE);
|
2016-08-13 06:05:48 -03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MAV_COLLISION_ACTION_HOVER:
|
|
|
|
if (failsafe_state_change) {
|
2021-09-10 03:28:21 -03:00
|
|
|
#if HAL_QUADPLANE_ENABLED
|
2016-08-13 06:05:48 -03:00
|
|
|
if (plane.quadplane.is_flying()) {
|
2019-10-17 00:49:32 -03:00
|
|
|
plane.set_mode(plane.mode_qloiter, ModeReason::AVOIDANCE);
|
2021-09-10 03:28:21 -03:00
|
|
|
break;
|
2016-08-13 06:05:48 -03:00
|
|
|
}
|
2021-09-10 03:28:21 -03:00
|
|
|
#endif
|
|
|
|
plane.set_mode(plane.mode_loiter, ModeReason::AVOIDANCE);
|
2016-08-13 06:05:48 -03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MAV_COLLISION_ACTION_ASCEND_OR_DESCEND:
|
|
|
|
// climb or descend to avoid obstacle
|
|
|
|
if (handle_avoidance_vertical(obstacle, failsafe_state_change)) {
|
|
|
|
plane.set_guided_WP();
|
|
|
|
} else {
|
|
|
|
actual_action = MAV_COLLISION_ACTION_NONE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MAV_COLLISION_ACTION_MOVE_HORIZONTALLY:
|
|
|
|
// move horizontally to avoid obstacle
|
|
|
|
if (handle_avoidance_horizontal(obstacle, failsafe_state_change)) {
|
|
|
|
plane.set_guided_WP();
|
|
|
|
} else {
|
|
|
|
actual_action = MAV_COLLISION_ACTION_NONE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MAV_COLLISION_ACTION_MOVE_PERPENDICULAR:
|
|
|
|
{
|
|
|
|
// move horizontally and vertically to avoid obstacle
|
|
|
|
const bool success_vert = handle_avoidance_vertical(obstacle, failsafe_state_change);
|
|
|
|
const bool success_hor = handle_avoidance_horizontal(obstacle, failsafe_state_change);
|
|
|
|
if (success_vert || success_hor) {
|
|
|
|
plane.set_guided_WP();
|
|
|
|
} else {
|
|
|
|
actual_action = MAV_COLLISION_ACTION_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// unsupported actions and those that require no response
|
|
|
|
case MAV_COLLISION_ACTION_NONE:
|
|
|
|
return actual_action;
|
|
|
|
case MAV_COLLISION_ACTION_REPORT:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (failsafe_state_change) {
|
2017-07-09 00:49:39 -03:00
|
|
|
gcs().send_text(MAV_SEVERITY_ALERT, "Avoid: Performing action: %d", actual_action);
|
2016-08-13 06:05:48 -03:00
|
|
|
}
|
2016-08-12 18:28:21 -03:00
|
|
|
|
|
|
|
// return with action taken
|
|
|
|
return actual_action;
|
|
|
|
}
|
|
|
|
|
2020-08-15 23:12:53 -03:00
|
|
|
void AP_Avoidance_Plane::handle_recovery(RecoveryAction recovery_action)
|
2016-08-12 18:28:21 -03:00
|
|
|
{
|
2016-08-13 06:05:48 -03:00
|
|
|
// check we are coming out of failsafe
|
|
|
|
if (plane.failsafe.adsb) {
|
|
|
|
plane.failsafe.adsb = false;
|
2020-08-15 23:12:53 -03:00
|
|
|
gcs().send_text(MAV_SEVERITY_INFO, "Avoid: Resuming with action: %u", (unsigned)recovery_action);
|
2016-08-13 06:05:48 -03:00
|
|
|
|
|
|
|
// restore flight mode if requested and user has not changed mode since
|
2019-10-17 00:49:32 -03:00
|
|
|
if (plane.control_mode_reason == ModeReason::AVOIDANCE) {
|
2016-08-13 06:05:48 -03:00
|
|
|
switch (recovery_action) {
|
|
|
|
|
2020-08-15 23:12:53 -03:00
|
|
|
case RecoveryAction::REMAIN_IN_AVOID_ADSB:
|
2016-08-13 06:05:48 -03:00
|
|
|
// do nothing, we'll stay in the AVOID_ADSB mode which is guided which will loiter
|
|
|
|
break;
|
|
|
|
|
2020-08-15 23:12:53 -03:00
|
|
|
case RecoveryAction::RESUME_PREVIOUS_FLIGHTMODE:
|
2019-10-17 00:49:32 -03:00
|
|
|
plane.set_mode_by_number(prev_control_mode_number, ModeReason::AVOIDANCE_RECOVERY);
|
2016-08-13 06:05:48 -03:00
|
|
|
break;
|
|
|
|
|
2020-08-15 23:12:53 -03:00
|
|
|
case RecoveryAction::RTL:
|
2019-10-17 00:49:32 -03:00
|
|
|
plane.set_mode(plane.mode_rtl, ModeReason::AVOIDANCE_RECOVERY);
|
2016-08-13 06:05:48 -03:00
|
|
|
break;
|
|
|
|
|
2020-08-15 23:12:53 -03:00
|
|
|
case RecoveryAction::RESUME_IF_AUTO_ELSE_LOITER:
|
2019-01-15 13:46:13 -04:00
|
|
|
if (prev_control_mode_number == Mode::Number::AUTO) {
|
2019-10-17 00:49:32 -03:00
|
|
|
plane.set_mode(plane.mode_auto, ModeReason::AVOIDANCE_RECOVERY);
|
2020-08-15 22:24:44 -03:00
|
|
|
} else {
|
|
|
|
// let ModeAvoidADSB continue in its guided
|
|
|
|
// behaviour, but reset the loiter location,
|
|
|
|
// rather than where the avoidance location was
|
|
|
|
plane.guided_WP_loc = plane.current_loc;
|
|
|
|
plane.set_guided_WP();
|
2016-08-13 06:05:48 -03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2020-08-15 22:24:44 -03:00
|
|
|
// user has specified an invalid recovery action;
|
|
|
|
// loiter where we are
|
|
|
|
plane.guided_WP_loc = plane.current_loc;
|
|
|
|
plane.set_guided_WP();
|
2016-08-13 06:05:48 -03:00
|
|
|
break;
|
|
|
|
} // switch
|
|
|
|
}
|
|
|
|
}
|
2016-08-12 18:28:21 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// check flight mode is avoid_adsb
|
|
|
|
bool AP_Avoidance_Plane::check_flightmode(bool allow_mode_change)
|
|
|
|
{
|
2016-08-13 06:05:48 -03:00
|
|
|
// ensure plane is in avoid_adsb mode
|
2019-01-15 13:46:13 -04:00
|
|
|
if (allow_mode_change && plane.control_mode != &plane.mode_avoidADSB) {
|
2019-10-17 00:49:32 -03:00
|
|
|
plane.set_mode(plane.mode_avoidADSB, ModeReason::AVOIDANCE);
|
2016-08-13 06:05:48 -03:00
|
|
|
}
|
|
|
|
|
2016-08-12 18:28:21 -03:00
|
|
|
// check flight mode
|
2019-01-15 13:46:13 -04:00
|
|
|
return (plane.control_mode == &plane.mode_avoidADSB);
|
2016-08-12 18:28:21 -03:00
|
|
|
}
|
|
|
|
|
2016-08-13 06:05:48 -03:00
|
|
|
bool AP_Avoidance_Plane::handle_avoidance_vertical(const AP_Avoidance::Obstacle *obstacle, bool allow_mode_change)
|
2016-08-12 18:28:21 -03:00
|
|
|
{
|
2016-08-13 06:05:48 -03:00
|
|
|
// ensure copter is in avoid_adsb mode
|
|
|
|
if (!check_flightmode(allow_mode_change)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get best vector away from obstacle
|
|
|
|
if (plane.current_loc.alt > obstacle->_location.alt) {
|
|
|
|
// should climb
|
|
|
|
plane.guided_WP_loc.alt = plane.current_loc.alt + 1000; // set alt demand to be 10m above us, climb rate will be TECS_CLMB_MAX
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} else if (plane.current_loc.alt > plane.g.RTL_altitude_cm) {
|
|
|
|
// should descend while above RTL alt
|
|
|
|
// TODO: consider using a lower altitude than RTL_altitude_cm since it's default (100m) is quite high
|
|
|
|
plane.guided_WP_loc.alt = plane.current_loc.alt - 1000; // set alt demand to be 10m below us, sink rate will be TECS_SINK_MAX
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2016-08-12 18:28:21 -03:00
|
|
|
}
|
|
|
|
|
2016-08-13 06:05:48 -03:00
|
|
|
bool AP_Avoidance_Plane::handle_avoidance_horizontal(const AP_Avoidance::Obstacle *obstacle, bool allow_mode_change)
|
2016-08-12 18:28:21 -03:00
|
|
|
{
|
2016-08-13 06:05:48 -03:00
|
|
|
// ensure plane is in avoid_adsb mode
|
|
|
|
if (!check_flightmode(allow_mode_change)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get best vector away from obstacle
|
|
|
|
Vector3f velocity_neu;
|
|
|
|
if (get_vector_perpendicular(obstacle, velocity_neu)) {
|
|
|
|
// remove vertical component
|
|
|
|
velocity_neu.z = 0.0f;
|
|
|
|
|
|
|
|
// check for divide by zero
|
|
|
|
if (is_zero(velocity_neu.x) && is_zero(velocity_neu.y)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// re-normalize
|
|
|
|
velocity_neu.normalize();
|
|
|
|
|
|
|
|
// push vector further away.
|
|
|
|
velocity_neu *= 10000;
|
|
|
|
|
|
|
|
// set target
|
2019-02-24 20:11:45 -04:00
|
|
|
plane.guided_WP_loc.offset(velocity_neu.x, velocity_neu.y);
|
2016-08-13 06:05:48 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-12 18:28:21 -03:00
|
|
|
// if we got this far we failed to set the new target
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-13 06:05:48 -03:00
|
|
|
|
2020-09-22 14:00:39 -03:00
|
|
|
#endif // HAL_ADSB_ENABLED
|
2020-09-19 05:38:20 -03:00
|
|
|
|