2015-05-29 23:12:49 -03:00
|
|
|
#include "Copter.h"
|
|
|
|
|
2013-04-26 06:51:07 -03:00
|
|
|
// Code to integrate AC_Fence library with main ArduCopter code
|
|
|
|
|
2022-07-19 08:33:13 -03:00
|
|
|
#if AP_FENCE_ENABLED
|
2013-04-26 06:51:07 -03:00
|
|
|
|
|
|
|
// fence_check - ask fence library to check for breaches and initiate the response
|
|
|
|
// called at 1hz
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::fence_check()
|
2013-04-26 06:51:07 -03:00
|
|
|
{
|
2017-12-15 05:36:25 -04:00
|
|
|
const uint8_t orig_breaches = fence.get_breaches();
|
|
|
|
|
2024-07-21 11:54:12 -03:00
|
|
|
bool is_landing_or_landed = flightmode->is_landing() || ap.land_complete || !motors->armed();
|
2024-05-31 10:13:43 -03:00
|
|
|
|
2017-12-15 05:36:25 -04:00
|
|
|
// check for new breaches; new_breaches is bitmask of fence types breached
|
2024-07-21 11:54:12 -03:00
|
|
|
const uint8_t new_breaches = fence.check(is_landing_or_landed);
|
2017-12-15 05:36:25 -04:00
|
|
|
|
2018-05-07 23:04:55 -03:00
|
|
|
// we still don't do anything when disarmed, but we do check for fence breaches.
|
|
|
|
// fence pre-arm check actually checks if any fence has been breached
|
|
|
|
// that's not ever going to be true if we don't call check on AP_Fence while disarmed.
|
|
|
|
if (!motors->armed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-26 06:51:07 -03:00
|
|
|
// if there is a new breach take action
|
2017-12-15 05:36:25 -04:00
|
|
|
if (new_breaches) {
|
2013-04-26 06:51:07 -03:00
|
|
|
|
2021-02-12 20:27:12 -04:00
|
|
|
if (!copter.ap.land_complete) {
|
2024-01-02 15:47:43 -04:00
|
|
|
fence.print_fence_message("breached", new_breaches);
|
2021-02-12 20:27:12 -04:00
|
|
|
}
|
|
|
|
|
2013-04-26 06:51:07 -03:00
|
|
|
// if the user wants some kind of response and motors are armed
|
2019-01-24 06:01:26 -04:00
|
|
|
uint8_t fence_act = fence.get_action();
|
|
|
|
if (fence_act != AC_FENCE_ACTION_REPORT_ONLY ) {
|
2013-04-26 06:51:07 -03:00
|
|
|
|
2014-10-16 02:09:02 -03:00
|
|
|
// disarm immediately if we think we are on the ground or in a manual flight mode with zero throttle
|
2013-04-26 06:51:07 -03:00
|
|
|
// don't disarm if the high-altitude fence has been broken because it's likely the user has pulled their throttle to zero to bring it down
|
2017-12-05 19:56:38 -04:00
|
|
|
if (ap.land_complete || (flightmode->has_manual_throttle() && ap.throttle_zero && !failsafe.radio && ((fence.get_breaches() & AC_FENCE_TYPE_ALT_MAX)== 0))){
|
2020-02-21 09:09:57 -04:00
|
|
|
arming.disarm(AP_Arming::Method::FENCEBREACH);
|
2018-07-24 01:41:27 -03:00
|
|
|
|
|
|
|
} else {
|
2019-01-24 06:01:26 -04:00
|
|
|
|
|
|
|
// if more than 100m outside the fence just force a land
|
|
|
|
if (fence.get_breach_distance(new_breaches) > AC_FENCE_GIVE_UP_DISTANCE) {
|
2019-10-17 00:49:22 -03:00
|
|
|
set_mode(Mode::Number::LAND, ModeReason::FENCE_BREACHED);
|
2019-01-24 06:01:26 -04:00
|
|
|
} else {
|
|
|
|
switch (fence_act) {
|
|
|
|
case AC_FENCE_ACTION_RTL_AND_LAND:
|
|
|
|
default:
|
|
|
|
// switch to RTL, if that fails then Land
|
2019-10-17 00:49:22 -03:00
|
|
|
if (!set_mode(Mode::Number::RTL, ModeReason::FENCE_BREACHED)) {
|
|
|
|
set_mode(Mode::Number::LAND, ModeReason::FENCE_BREACHED);
|
2019-01-24 06:01:26 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AC_FENCE_ACTION_ALWAYS_LAND:
|
|
|
|
// if always land option mode is specified, land
|
2019-10-17 00:49:22 -03:00
|
|
|
set_mode(Mode::Number::LAND, ModeReason::FENCE_BREACHED);
|
2019-01-24 06:01:26 -04:00
|
|
|
break;
|
|
|
|
case AC_FENCE_ACTION_SMART_RTL:
|
|
|
|
// Try SmartRTL, if that fails, RTL, if that fails Land
|
2019-10-17 00:49:22 -03:00
|
|
|
if (!set_mode(Mode::Number::SMART_RTL, ModeReason::FENCE_BREACHED)) {
|
|
|
|
if (!set_mode(Mode::Number::RTL, ModeReason::FENCE_BREACHED)) {
|
|
|
|
set_mode(Mode::Number::LAND, ModeReason::FENCE_BREACHED);
|
2019-01-24 06:01:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AC_FENCE_ACTION_BRAKE:
|
|
|
|
// Try Brake, if that fails Land
|
2019-10-17 00:49:22 -03:00
|
|
|
if (!set_mode(Mode::Number::BRAKE, ModeReason::FENCE_BREACHED)) {
|
|
|
|
set_mode(Mode::Number::LAND, ModeReason::FENCE_BREACHED);
|
2019-01-24 06:01:26 -04:00
|
|
|
}
|
|
|
|
break;
|
2021-01-30 22:34:21 -04:00
|
|
|
case AC_FENCE_ACTION_SMART_RTL_OR_LAND:
|
|
|
|
// Try SmartRTL, if that fails, Land
|
|
|
|
if (!set_mode(Mode::Number::SMART_RTL, ModeReason::FENCE_BREACHED)) {
|
|
|
|
set_mode(Mode::Number::LAND, ModeReason::FENCE_BREACHED);
|
|
|
|
}
|
|
|
|
break;
|
2013-04-26 06:51:07 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-13 21:58:09 -03:00
|
|
|
LOGGER_WRITE_ERROR(LogErrorSubsystem::FAILSAFE_FENCE, LogErrorCode(new_breaches));
|
2013-04-26 06:51:07 -03:00
|
|
|
|
2024-01-02 15:47:43 -04:00
|
|
|
} else if (orig_breaches && fence.get_breaches() == 0) {
|
2024-07-21 08:29:04 -03:00
|
|
|
if (!copter.ap.land_complete) {
|
2024-06-25 13:11:46 -03:00
|
|
|
GCS_SEND_TEXT(MAV_SEVERITY_NOTICE, "Fence breach cleared");
|
|
|
|
}
|
2017-12-15 05:36:25 -04:00
|
|
|
// record clearing of breach
|
2023-07-13 21:58:09 -03:00
|
|
|
LOGGER_WRITE_ERROR(LogErrorSubsystem::FAILSAFE_FENCE, LogErrorCode::ERROR_RESOLVED);
|
2013-04-26 06:51:07 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|