2017-08-16 07:02:56 -03:00
|
|
|
#include "Rover.h"
|
|
|
|
|
|
|
|
// fence_check - ask fence library to check for breaches and initiate the response
|
|
|
|
void Rover::fence_check()
|
|
|
|
{
|
2022-07-19 08:33:13 -03:00
|
|
|
#if AP_FENCE_ENABLED
|
2017-08-16 07:02:56 -03:00
|
|
|
uint8_t new_breaches; // the type of fence that has been breached
|
2022-03-04 12:41:00 -04:00
|
|
|
const uint8_t orig_breaches = fence.get_breaches();
|
2017-08-16 07:02:56 -03:00
|
|
|
|
|
|
|
// check for a breach
|
2022-03-04 12:41:00 -04:00
|
|
|
new_breaches = fence.check();
|
2017-08-16 07:02:56 -03:00
|
|
|
|
|
|
|
// return immediately if motors are not armed
|
|
|
|
if (!arming.is_armed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if there is a new breach take action
|
|
|
|
if (new_breaches) {
|
|
|
|
// if the user wants some kind of response and motors are armed
|
2023-09-21 11:22:37 -03:00
|
|
|
if ((FailsafeAction)fence.get_action() != FailsafeAction::None) {
|
2020-02-05 02:18:11 -04:00
|
|
|
// if within 100m of the fence, it will take the action specified by the FENCE_ACTION parameter
|
2022-03-04 12:41:00 -04:00
|
|
|
if (fence.get_breach_distance(new_breaches) <= AC_FENCE_GIVE_UP_DISTANCE) {
|
2023-09-21 11:22:37 -03:00
|
|
|
switch ((FailsafeAction)fence.get_action()) {
|
|
|
|
case FailsafeAction::None:
|
2020-02-05 11:29:36 -04:00
|
|
|
break;
|
2023-09-21 11:22:37 -03:00
|
|
|
case FailsafeAction::SmartRTL:
|
|
|
|
if (set_mode(mode_smartrtl, ModeReason::BATTERY_FAILSAFE)) {
|
|
|
|
break;
|
2020-02-05 11:29:36 -04:00
|
|
|
}
|
2023-09-21 11:22:37 -03:00
|
|
|
FALLTHROUGH;
|
|
|
|
case FailsafeAction::RTL:
|
|
|
|
if (set_mode(mode_rtl, ModeReason::BATTERY_FAILSAFE)) {
|
|
|
|
break;
|
2020-02-05 11:29:36 -04:00
|
|
|
}
|
2023-09-21 11:22:37 -03:00
|
|
|
FALLTHROUGH;
|
|
|
|
case FailsafeAction::Hold:
|
|
|
|
set_mode(mode_hold, ModeReason::BATTERY_FAILSAFE);
|
2020-02-05 11:29:36 -04:00
|
|
|
break;
|
2023-09-21 11:22:37 -03:00
|
|
|
case FailsafeAction::SmartRTL_Hold:
|
2020-02-05 11:29:36 -04:00
|
|
|
if (!set_mode(mode_smartrtl, ModeReason::FENCE_BREACHED)) {
|
|
|
|
set_mode(mode_hold, ModeReason::FENCE_BREACHED);
|
|
|
|
}
|
|
|
|
break;
|
2023-09-21 11:23:24 -03:00
|
|
|
case FailsafeAction::Terminate:
|
|
|
|
arming.disarm(AP_Arming::Method::FENCEBREACH);
|
|
|
|
break;
|
2020-02-05 11:29:36 -04:00
|
|
|
}
|
2017-08-16 07:02:56 -03:00
|
|
|
} else {
|
|
|
|
// if more than 100m outside the fence just force to HOLD
|
2019-10-17 00:48:47 -03:00
|
|
|
set_mode(mode_hold, ModeReason::FENCE_BREACHED);
|
2017-08-16 07:02:56 -03:00
|
|
|
}
|
|
|
|
}
|
2024-01-10 00:21:43 -04:00
|
|
|
LOGGER_WRITE_ERROR(LogErrorSubsystem::FAILSAFE_FENCE, LogErrorCode(new_breaches));
|
2017-08-16 07:02:56 -03:00
|
|
|
|
|
|
|
} else if (orig_breaches) {
|
|
|
|
// record clearing of breach
|
2024-01-10 00:21:43 -04:00
|
|
|
LOGGER_WRITE_ERROR(LogErrorSubsystem::FAILSAFE_FENCE,
|
2019-03-24 22:06:24 -03:00
|
|
|
LogErrorCode::ERROR_RESOLVED);
|
2017-08-16 07:02:56 -03:00
|
|
|
}
|
2022-07-19 08:33:13 -03:00
|
|
|
#endif // AP_FENCE_ENABLED
|
2017-08-16 07:02:56 -03:00
|
|
|
}
|