#include "Sub.h"12// Code to integrate AC_Fence library with main ArduSub code34#if AP_FENCE_ENABLED56// fence_check - ask fence library to check for breaches and initiate the response7// async fence checking io callback at 1Khz8void Sub::fence_checks_async()9{10const uint32_t now = AP_HAL::millis();11// ignore any fence activity when not armed12if (!motors.armed()) {13return;14}1516if (!AP_HAL::timeout_expired(fence_breaches.last_check_ms, now, 333U)) { // 3Hz update rate17return;18}1920fence_breaches.last_check_ms = now;21const uint8_t orig_breaches = fence.get_breaches();22// check for new breaches; new_breaches is bitmask of fence types breached23const uint8_t new_breaches = fence.check();2425// if the user wants some kind of response and motors are armed26if (new_breaches) {27if (fence.get_action() != AC_Fence::Action::REPORT_ONLY) {28//29// // disarm immediately if we think we are on the ground or in a manual flight mode with zero throttle30// // 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 down31// if (ap.land_complete || (mode_has_manual_throttle(control_mode) && ap.throttle_zero && !failsafe.manual_control && ((fence.get_breaches() & AC_FENCE_TYPE_ALT_MAX)== 0))){32// init_disarm_motors();33// }else{34// // if we are within 100m of the fence, RTL35// if (fence.get_breach_distance(new_breaches) <= AC_FENCE_GIVE_UP_DISTANCE) {36// if (!set_mode(RTL, MODE_REASON_FENCE_BREACH)) {37// set_mode(LAND, MODE_REASON_FENCE_BREACH);38// }39// }else{40// // if more than 100m outside the fence just force a land41// set_mode(LAND, MODE_REASON_FENCE_BREACH);42// }43// }44}4546LOGGER_WRITE_ERROR(LogErrorSubsystem::FAILSAFE_FENCE, LogErrorCode(fence_breaches.new_breaches));4748} else if (orig_breaches) {49// record clearing of breach50LOGGER_WRITE_ERROR(LogErrorSubsystem::FAILSAFE_FENCE, LogErrorCode::ERROR_RESOLVED);51}52}5354#endif555657