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
|
|
|
|
|
|
|
|
#if AC_FENCE == ENABLED
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
// check for new breaches; new_breaches is bitmask of fence types breached
|
|
|
|
const uint8_t new_breaches = fence.check();
|
|
|
|
|
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
|
|
|
|
|
|
|
// 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))){
|
2013-04-26 06:51:07 -03:00
|
|
|
init_disarm_motors();
|
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) {
|
2018-07-24 01:41:27 -03:00
|
|
|
set_mode(LAND, MODE_REASON_FENCE_BREACH);
|
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
|
|
|
|
if (!set_mode(RTL, MODE_REASON_FENCE_BREACH)) {
|
|
|
|
set_mode(LAND, MODE_REASON_FENCE_BREACH);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AC_FENCE_ACTION_ALWAYS_LAND:
|
|
|
|
// if always land option mode is specified, land
|
2016-01-25 19:40:41 -04:00
|
|
|
set_mode(LAND, MODE_REASON_FENCE_BREACH);
|
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
|
|
|
|
if (!set_mode(SMART_RTL, MODE_REASON_FENCE_BREACH)) {
|
|
|
|
if (!set_mode(RTL, MODE_REASON_FENCE_BREACH)) {
|
|
|
|
set_mode(LAND, MODE_REASON_FENCE_BREACH);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AC_FENCE_ACTION_BRAKE:
|
|
|
|
// Try Brake, if that fails Land
|
|
|
|
if (!set_mode(BRAKE, MODE_REASON_FENCE_BREACH)) {
|
|
|
|
set_mode(LAND, MODE_REASON_FENCE_BREACH);
|
|
|
|
}
|
|
|
|
break;
|
2013-04-26 06:51:07 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-24 22:31:46 -03:00
|
|
|
AP::logger().Write_Error(LogErrorSubsystem::FAILSAFE_FENCE, LogErrorCode(new_breaches));
|
2013-04-26 06:51:07 -03:00
|
|
|
|
2017-12-15 05:36:25 -04:00
|
|
|
} else if (orig_breaches) {
|
|
|
|
// record clearing of breach
|
2019-03-24 22:31:46 -03:00
|
|
|
AP::logger().Write_Error(LogErrorSubsystem::FAILSAFE_FENCE, LogErrorCode::ERROR_RESOLVED);
|
2013-04-26 06:51:07 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|