2016-07-21 09:44:09 -03:00
|
|
|
#include "Copter.h"
|
|
|
|
#include <AP_Notify/AP_Notify.h>
|
|
|
|
|
2020-09-19 05:37:52 -03:00
|
|
|
#if HAL_ADSB_ENABLED
|
2016-07-21 09:44:09 -03:00
|
|
|
void Copter::avoidance_adsb_update(void)
|
|
|
|
{
|
2016-07-21 00:10:33 -03:00
|
|
|
adsb.update();
|
2016-07-21 09:44:09 -03:00
|
|
|
avoidance_adsb.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
MAV_COLLISION_ACTION AP_Avoidance_Copter::handle_avoidance(const AP_Avoidance::Obstacle *obstacle, MAV_COLLISION_ACTION requested_action)
|
|
|
|
{
|
|
|
|
MAV_COLLISION_ACTION actual_action = requested_action;
|
|
|
|
bool failsafe_state_change = false;
|
|
|
|
|
|
|
|
// check for changes in failsafe
|
|
|
|
if (!copter.failsafe.adsb) {
|
|
|
|
copter.failsafe.adsb = true;
|
|
|
|
failsafe_state_change = true;
|
|
|
|
// record flight mode in case it's required for the recovery
|
2020-01-30 03:29:36 -04:00
|
|
|
prev_control_mode = copter.flightmode->mode_number();
|
2016-07-21 09:44:09 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// take no action in some flight modes
|
2020-01-30 03:29:36 -04:00
|
|
|
if (copter.flightmode->mode_number() == Mode::Number::LAND ||
|
2018-03-14 17:16:16 -03:00
|
|
|
#if MODE_THROW_ENABLED == ENABLED
|
2020-01-30 03:29:36 -04:00
|
|
|
copter.flightmode->mode_number() == Mode::Number::THROW ||
|
2018-03-14 17:16:16 -03:00
|
|
|
#endif
|
2020-01-30 03:29:36 -04:00
|
|
|
copter.flightmode->mode_number() == Mode::Number::FLIP) {
|
2016-07-21 09:44:09 -03:00
|
|
|
actual_action = MAV_COLLISION_ACTION_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if landed and we will take some kind of action, just disarm
|
|
|
|
if ((actual_action > MAV_COLLISION_ACTION_REPORT) && copter.should_disarm_on_failsafe()) {
|
2020-02-21 09:09:57 -04:00
|
|
|
copter.arming.disarm(AP_Arming::Method::ADSBCOLLISIONACTION);
|
2016-07-21 09:44:09 -03:00
|
|
|
actual_action = MAV_COLLISION_ACTION_NONE;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// take action based on requested action
|
|
|
|
switch (actual_action) {
|
|
|
|
|
|
|
|
case MAV_COLLISION_ACTION_RTL:
|
|
|
|
// attempt to switch to RTL, if this fails (i.e. flying in manual mode with bad position) do nothing
|
|
|
|
if (failsafe_state_change) {
|
2019-10-17 00:49:22 -03:00
|
|
|
if (!copter.set_mode(Mode::Number::RTL, ModeReason::AVOIDANCE)) {
|
2016-07-21 09:44:09 -03:00
|
|
|
actual_action = MAV_COLLISION_ACTION_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MAV_COLLISION_ACTION_HOVER:
|
|
|
|
// attempt to switch to Loiter, if this fails (i.e. flying in manual mode with bad position) do nothing
|
|
|
|
if (failsafe_state_change) {
|
2019-10-17 00:49:22 -03:00
|
|
|
if (!copter.set_mode(Mode::Number::LOITER, ModeReason::AVOIDANCE)) {
|
2016-07-21 09:44:09 -03:00
|
|
|
actual_action = MAV_COLLISION_ACTION_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MAV_COLLISION_ACTION_ASCEND_OR_DESCEND:
|
|
|
|
// climb or descend to avoid obstacle
|
|
|
|
if (!handle_avoidance_vertical(obstacle, failsafe_state_change)) {
|
|
|
|
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)) {
|
|
|
|
actual_action = MAV_COLLISION_ACTION_NONE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MAV_COLLISION_ACTION_MOVE_PERPENDICULAR:
|
|
|
|
if (!handle_avoidance_perpendicular(obstacle, failsafe_state_change)) {
|
|
|
|
actual_action = MAV_COLLISION_ACTION_NONE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
// unsupported actions and those that require no response
|
|
|
|
case MAV_COLLISION_ACTION_NONE:
|
2016-08-13 06:04:15 -03:00
|
|
|
return actual_action;
|
2016-07-21 09:44:09 -03:00
|
|
|
case MAV_COLLISION_ACTION_REPORT:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (failsafe_state_change) {
|
2019-03-24 22:31:46 -03:00
|
|
|
AP::logger().Write_Error(LogErrorSubsystem::FAILSAFE_ADSB,
|
|
|
|
LogErrorCode(actual_action));
|
2016-07-21 09:44:09 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// return with action taken
|
|
|
|
return actual_action;
|
|
|
|
}
|
|
|
|
|
2020-08-15 23:12:52 -03:00
|
|
|
void AP_Avoidance_Copter::handle_recovery(RecoveryAction recovery_action)
|
2016-07-21 09:44:09 -03:00
|
|
|
{
|
|
|
|
// check we are coming out of failsafe
|
|
|
|
if (copter.failsafe.adsb) {
|
|
|
|
copter.failsafe.adsb = false;
|
2019-03-24 22:31:46 -03:00
|
|
|
AP::logger().Write_Error(LogErrorSubsystem::FAILSAFE_ADSB,
|
|
|
|
LogErrorCode::ERROR_RESOLVED);
|
2016-07-21 09:44:09 -03:00
|
|
|
|
|
|
|
// restore flight mode if requested and user has not changed mode since
|
2019-10-17 00:49:22 -03:00
|
|
|
if (copter.control_mode_reason == ModeReason::AVOIDANCE) {
|
2016-08-17 04:17:05 -03:00
|
|
|
switch (recovery_action) {
|
|
|
|
|
2020-08-15 23:12:52 -03:00
|
|
|
case RecoveryAction::REMAIN_IN_AVOID_ADSB:
|
2016-08-17 04:17:05 -03:00
|
|
|
// do nothing, we'll stay in the AVOID_ADSB mode which is guided which will loiter forever
|
|
|
|
break;
|
|
|
|
|
2020-08-15 23:12:52 -03:00
|
|
|
case RecoveryAction::RESUME_PREVIOUS_FLIGHTMODE:
|
2016-08-17 04:17:05 -03:00
|
|
|
set_mode_else_try_RTL_else_LAND(prev_control_mode);
|
|
|
|
break;
|
|
|
|
|
2020-08-15 23:12:52 -03:00
|
|
|
case RecoveryAction::RTL:
|
2019-09-07 20:33:56 -03:00
|
|
|
set_mode_else_try_RTL_else_LAND(Mode::Number::RTL);
|
2016-08-17 04:17:05 -03:00
|
|
|
break;
|
|
|
|
|
2020-08-15 23:12:52 -03:00
|
|
|
case RecoveryAction::RESUME_IF_AUTO_ELSE_LOITER:
|
2019-09-07 20:33:56 -03:00
|
|
|
if (prev_control_mode == Mode::Number::AUTO) {
|
|
|
|
set_mode_else_try_RTL_else_LAND(Mode::Number::AUTO);
|
2016-07-21 09:44:09 -03:00
|
|
|
}
|
2016-08-17 04:17:05 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
} // switch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-07 20:33:56 -03:00
|
|
|
void AP_Avoidance_Copter::set_mode_else_try_RTL_else_LAND(Mode::Number mode)
|
2016-08-17 04:17:05 -03:00
|
|
|
{
|
2019-10-17 00:49:22 -03:00
|
|
|
if (!copter.set_mode(mode, ModeReason::AVOIDANCE_RECOVERY)) {
|
2016-08-17 04:17:05 -03:00
|
|
|
// on failure RTL or LAND
|
2019-10-17 00:49:22 -03:00
|
|
|
if (!copter.set_mode(Mode::Number::RTL, ModeReason::AVOIDANCE_RECOVERY)) {
|
|
|
|
copter.set_mode(Mode::Number::LAND, ModeReason::AVOIDANCE_RECOVERY);
|
2016-07-21 09:44:09 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-08 23:17:08 -04:00
|
|
|
int16_t AP_Avoidance_Copter::get_altitude_minimum() const
|
|
|
|
{
|
|
|
|
#if MODE_RTL_ENABLED == ENABLED
|
|
|
|
// do not descend if below RTL alt
|
|
|
|
return copter.g.rtl_altitude;
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-07-21 09:44:09 -03:00
|
|
|
// check flight mode is avoid_adsb
|
|
|
|
bool AP_Avoidance_Copter::check_flightmode(bool allow_mode_change)
|
|
|
|
{
|
|
|
|
// ensure copter is in avoid_adsb mode
|
2020-01-30 03:29:36 -04:00
|
|
|
if (allow_mode_change && copter.flightmode->mode_number() != Mode::Number::AVOID_ADSB) {
|
2019-10-17 00:49:22 -03:00
|
|
|
if (!copter.set_mode(Mode::Number::AVOID_ADSB, ModeReason::AVOIDANCE)) {
|
2016-07-21 09:44:09 -03:00
|
|
|
// failed to set mode so exit immediately
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check flight mode
|
2020-01-30 03:29:36 -04:00
|
|
|
return (copter.flightmode->mode_number() == Mode::Number::AVOID_ADSB);
|
2016-07-21 09:44:09 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_Avoidance_Copter::handle_avoidance_vertical(const AP_Avoidance::Obstacle *obstacle, bool allow_mode_change)
|
|
|
|
{
|
|
|
|
// ensure copter is in avoid_adsb mode
|
|
|
|
if (!check_flightmode(allow_mode_change)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// decide on whether we should climb or descend
|
|
|
|
bool should_climb = false;
|
|
|
|
Location my_loc;
|
2019-07-09 03:05:42 -03:00
|
|
|
if (AP::ahrs().get_position(my_loc)) {
|
2016-07-21 09:44:09 -03:00
|
|
|
should_climb = my_loc.alt > obstacle->_location.alt;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get best vector away from obstacle
|
|
|
|
Vector3f velocity_neu;
|
|
|
|
if (should_climb) {
|
2019-01-24 01:01:46 -04:00
|
|
|
velocity_neu.z = copter.wp_nav->get_default_speed_up();
|
2016-07-21 09:44:09 -03:00
|
|
|
} else {
|
2019-01-24 01:01:46 -04:00
|
|
|
velocity_neu.z = -copter.wp_nav->get_default_speed_down();
|
2021-01-08 23:17:08 -04:00
|
|
|
// do not descend if below minimum altitude
|
|
|
|
if (copter.current_loc.alt < get_altitude_minimum()) {
|
2016-07-21 09:44:09 -03:00
|
|
|
velocity_neu.z = 0.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// send target velocity
|
2017-12-11 01:43:27 -04:00
|
|
|
copter.mode_avoid_adsb.set_velocity(velocity_neu);
|
2016-07-21 09:44:09 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_Avoidance_Copter::handle_avoidance_horizontal(const AP_Avoidance::Obstacle *obstacle, bool allow_mode_change)
|
|
|
|
{
|
|
|
|
// ensure copter 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-normalise
|
|
|
|
velocity_neu.normalize();
|
|
|
|
// convert horizontal components to velocities
|
2019-01-24 01:01:46 -04:00
|
|
|
velocity_neu.x *= copter.wp_nav->get_default_speed_xy();
|
|
|
|
velocity_neu.y *= copter.wp_nav->get_default_speed_xy();
|
2016-07-21 09:44:09 -03:00
|
|
|
// send target velocity
|
2017-12-11 01:43:27 -04:00
|
|
|
copter.mode_avoid_adsb.set_velocity(velocity_neu);
|
2016-07-21 09:44:09 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we got this far we failed to set the new target
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AP_Avoidance_Copter::handle_avoidance_perpendicular(const AP_Avoidance::Obstacle *obstacle, bool allow_mode_change)
|
|
|
|
{
|
|
|
|
// ensure copter 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)) {
|
|
|
|
// convert horizontal components to velocities
|
2019-01-24 01:01:46 -04:00
|
|
|
velocity_neu.x *= copter.wp_nav->get_default_speed_xy();
|
|
|
|
velocity_neu.y *= copter.wp_nav->get_default_speed_xy();
|
2016-07-21 09:44:09 -03:00
|
|
|
// use up and down waypoint speeds
|
|
|
|
if (velocity_neu.z > 0.0f) {
|
2019-01-24 01:01:46 -04:00
|
|
|
velocity_neu.z *= copter.wp_nav->get_default_speed_up();
|
2016-07-21 09:44:09 -03:00
|
|
|
} else {
|
2019-01-24 01:01:46 -04:00
|
|
|
velocity_neu.z *= copter.wp_nav->get_default_speed_down();
|
2021-01-08 23:17:08 -04:00
|
|
|
// do not descend if below minimum altitude
|
|
|
|
if (copter.current_loc.alt < get_altitude_minimum()) {
|
2016-07-21 09:44:09 -03:00
|
|
|
velocity_neu.z = 0.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// send target velocity
|
2017-12-11 01:43:27 -04:00
|
|
|
copter.mode_avoid_adsb.set_velocity(velocity_neu);
|
2016-07-21 09:44:09 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we got this far we failed to set the new target
|
|
|
|
return false;
|
|
|
|
}
|
2018-02-16 10:21:55 -04:00
|
|
|
#endif
|