#pragma once12#include "AC_Fence_config.h"34#if AP_FENCE_ENABLED56#include <inttypes.h>7#include <AP_Common/AP_Common.h>8#include <AP_Common/ExpandingString.h>9#include <AP_Param/AP_Param.h>10#include <AP_Math/AP_Math.h>11#include <AC_Fence/AC_PolyFence_loader.h>1213// bit masks for enabled fence types. Used for TYPE parameter14#define AC_FENCE_TYPE_ALT_MAX 1 // high alt fence which usually initiates an RTL15#define AC_FENCE_TYPE_CIRCLE 2 // circular horizontal fence (usually initiates an RTL)16#define AC_FENCE_TYPE_POLYGON 4 // polygon horizontal fence17#define AC_FENCE_TYPE_ALT_MIN 8 // low alt fence which usually initiates an RTL18#define AC_FENCE_ARMING_FENCES (AC_FENCE_TYPE_ALT_MAX | AC_FENCE_TYPE_CIRCLE | AC_FENCE_TYPE_POLYGON)19#define AC_FENCE_ALL_FENCES (AC_FENCE_ARMING_FENCES | AC_FENCE_TYPE_ALT_MIN)2021// give up distance22#define AC_FENCE_GIVE_UP_DISTANCE 100.0f // distance outside the fence at which we should give up and just land. Note: this is not used by library directly but is intended to be used by the main code2324class AC_Fence25{26public:27friend class AC_PolyFence_loader;2829// valid actions should a fence be breached30enum class Action {31REPORT_ONLY = 0, // report to GCS that boundary has been breached but take no further action32RTL_AND_LAND = 1, // return to launch and, if that fails, land33ALWAYS_LAND = 2, // always land34SMART_RTL = 3, // smartRTL, if that fails, RTL, and if that still fails, land35BRAKE = 4, // brake, if that fails, land36SMART_RTL_OR_LAND = 5, // SmartRTL, if that fails, Land37GUIDED = 6, // guided mode, with target waypoint as fence return point38GUIDED_THROTTLE_PASS = 7, // guided mode, but pilot retains manual throttle control39AUTOLAND_OR_RTL = 8, // fixed wing autoland,if enabled, or RTL40};4142enum class AutoEnable : uint8_t43{44ALWAYS_DISABLED = 0,45ENABLE_ON_AUTO_TAKEOFF = 1, // enable on auto takeoff46ENABLE_DISABLE_FLOOR_ONLY = 2, // enable on takeoff but disable floor on landing47ONLY_WHEN_ARMED = 3 // enable on arming48};4950enum class MavlinkFenceActions : uint16_t51{52DISABLE_FENCE = 0,53ENABLE_FENCE = 1,54DISABLE_ALT_MIN_FENCE = 255};5657AC_Fence();5859/* Do not allow copies */60CLASS_NO_COPY(AC_Fence);6162void init() {63_poly_loader.init();64}6566// get singleton instance67static AC_Fence *get_singleton() { return _singleton; }6869/// enable - allows fence to be enabled/disabled.70/// returns a bitmask of fences that were changed71uint8_t enable(bool value, uint8_t fence_types, bool update_auto_mask = true);7273/// enable_configured - allows configured fences to be enabled/disabled.74/// returns a bitmask of fences that were changed75uint8_t enable_configured(bool value) { return enable(value, _configured_fences, true); }7677/// auto_enabled - automaticaly enable/disable fence depending of flight status78AutoEnable auto_enabled() const { return static_cast<AutoEnable>(_auto_enabled.get()); }7980/// enable_floor - allows fence floor to be enabled/disabled. Note this does not update the eeprom saved value81void enable_floor();8283/// disable_floor - allows fence floor to be enabled/disabled. Note this does not update the eeprom saved value84void disable_floor();8586/// auto_enable_fence_on_takeoff - auto enables the fence. Called after takeoff conditions met87void auto_enable_fence_after_takeoff();8889/// auto_enable_fences_on_arming - auto enables all applicable fences on arming90void auto_enable_fence_on_arming();9192/// auto_disable_fences_on_disarming - auto disables all applicable fences on disarming93void auto_disable_fence_on_disarming();9495uint8_t get_auto_disable_fences(void) const;9697/// auto_enable_fence_floor - auto enables fence floor once desired altitude has been reached.98bool auto_enable_fence_floor();99100/// enabled - returns whether fencing is enabled or not101bool enabled() const { return _enabled_fences; }102103/// present - returns true if any of the provided types is present104uint8_t present() const;105106/// get_enabled_fences - returns bitmask of enabled fences107uint8_t get_enabled_fences() const;108109// should be called @10Hz to handle loading from eeprom110void update();111112/// pre_arm_check - returns true if all pre-takeoff checks have completed successfully113bool pre_arm_check(char *failure_msg, const uint8_t failure_msg_len) const;114115///116/// methods to check we are within the boundaries and recover117///118119/// check - returns the fence type that has been breached (if any)120/// disabled_fences can be used to disable fences for certain conditions (e.g. landing)121uint8_t check(bool disable_auto_fence = false);122123// returns true if the destination is within fence (used to reject waypoints outside the fence)124bool check_destination_within_fence(const class Location& loc);125126/// get_breaches - returns bit mask of the fence types that have been breached127uint8_t get_breaches() const { return _breached_fences; }128129/// get_breach_time - returns time the fence was breached130uint32_t get_breach_time() const { return _breach_time; }131132/// get_breach_count - returns number of times we have breached the fence133uint16_t get_breach_count() const { return _breach_count; }134135/// get_breaches - returns bitmask of the fence types that have had their margins breached136uint8_t get_margin_breaches() const { return _breached_fence_margins; }137138/// get_margin_breach_time - returns time the fence margin was breached139uint32_t get_margin_breach_time() const { return _margin_breach_time; }140141/// get_breach_distance - returns maximum distance in meters outside142/// of the given fences. fence_type is a bitmask here.143float get_breach_distance(uint8_t fence_type) const;144145/// get_breach_direction_NED - returns direction in meters outside/inside146/// of the given fences. fence_check_pos is the absolute position when the check was made.147// fence_type is a bitmask.148bool get_breach_direction_NED(uint8_t fence_type, Vector3f& direction, Location& fence_check_pos) const;149150/// get_action - getter for user requested action on limit breach151Action get_action() const { return _action; }152153/// get_safe_alt - returns maximum safe altitude (i.e. alt_max - margin)154float get_safe_alt_max_m() const { return _alt_max_m - _margin_m; }155156/// get_safe_alt_min_m - returns the minimum safe altitude (i.e. alt_min + margin)157float get_safe_alt_min_m() const { return _alt_min_m + _margin_m; }158159/// get_radius_m - returns the fence radius in meters160float get_radius_m() const { return _circle_radius_m.get(); }161162/// get_margin_ne_m - returns the horizontal fence margin in meters163float get_margin_ne_m() const;164165/// get_return_rally - returns whether returning to fence return point or rally point166uint8_t get_return_rally() const { return _ret_rally; }167168/// get_return_rally - returns whether returning to fence return point or rally point169float get_return_altitude() const { return _ret_altitude; }170171/// get a user-friendly list of fences172static void get_fence_names(uint8_t fences, ExpandingString& msg);173174// print a message about the fences to the GCS175void print_fence_message(const char* msg, uint8_t fences) const;176177/// manual_recovery_start - caller indicates that pilot is re-taking manual control so fence should be disabled for 10 seconds178/// should be called whenever the pilot changes the flight mode179/// has no effect if no breaches have occurred180void manual_recovery_start();181182// methods for mavlink SYS_STATUS message (send_sys_status)183bool sys_status_present() const;184bool sys_status_enabled() const;185bool sys_status_failed() const;186187AC_PolyFence_loader &polyfence();188const AC_PolyFence_loader &polyfence() const;189190enum class OPTIONS {191DISABLE_MODE_CHANGE = 1U << 0,192INCLUSION_UNION = 1U << 1,193NOTIFY_MARGIN_BREACH = 1U << 2,194};195static bool option_enabled(OPTIONS opt, const AP_Int16 &options) {196return (options.get() & int16_t(opt)) != 0;197}198bool option_enabled(OPTIONS opt) const {199return option_enabled(opt, _options);200}201202static const struct AP_Param::GroupInfo var_info[];203204#if AP_SDCARD_STORAGE_ENABLED205bool failed_sdcard_storage(void) const {206return _poly_loader.failed_sdcard_storage();207}208#endif209210private:211static AC_Fence *_singleton;212213/// check_fence_alt_max - true if max alt fence has been newly breached214bool check_fence_alt_max();215216/// check_fence_alt_min - true if min alt fence has been newly breached217bool check_fence_alt_min();218219/// check_fence_polygon - true if polygon fence has been newly breached220bool check_fence_polygon();221222/// check_fence_circle - true if circle fence has been newly breached223bool check_fence_circle();224225/// record_breach - update breach bitmask, time and count226void record_breach(uint8_t fence_type);227228/// clear_breach - update breach bitmask229void clear_breach(uint8_t fence_type);230231/// record_margin_breach - update margin breach bitmask232void record_margin_breach(uint8_t fence_type);233234/// clear_margin_breach - update margin breach bitmask235void clear_margin_breach(uint8_t fence_type);236237/// retrieve the current NED position relative to home238bool get_current_position_NED(Vector3f& currpos) const;239240// additional checks for the different fence types:241bool pre_arm_check_polygon(char *failure_msg, const uint8_t failure_msg_len) const;242bool pre_arm_check_circle(char *failure_msg, const uint8_t failure_msg_len) const;243bool pre_arm_check_alt(char *failure_msg, const uint8_t failure_msg_len) const;244// fence floor is enabled245bool floor_enabled() const { return _enabled_fences & AC_FENCE_TYPE_ALT_MIN; }246247// parameters248uint8_t _enabled_fences; // fences that are currently enabled/disabled249bool _last_enabled; // value of enabled last time we checked250AP_Int8 _enabled; // overall feature control251AP_Int8 _auto_enabled; // top level flag for auto enabling fence252uint8_t _last_auto_enabled; // value of auto_enabled last time we checked253AP_Int8 _configured_fences; // bit mask holding which fences are enabled254AP_Enum<Action> _action; // recovery action specified by user255AP_Float _alt_max_m; // altitude upper limit in meters256AP_Float _alt_min_m; // altitude lower limit in meters257AP_Float _circle_radius_m; // circle fence radius in meters258AP_Float _margin_m; // distance in meters that autopilot's should maintain from the fence to avoid a breach259AP_Float _margin_ne_m; // distance in meters that autopilot's should maintain from the horizontal fence to avoid a breach260AP_Int8 _total; // number of polygon points saved in eeprom261AP_Int8 _ret_rally; // return to fence return point or rally point/home262AP_Int16 _ret_altitude; // return to this altitude263AP_Int16 _options; // options bitmask, see OPTIONS enum264AP_Float _notify_freq; // margin notification frequency265266// backup fences267float _alt_max_backup_m; // backup altitude upper limit in meters used to refire the breach if the vehicle continues to move further away268float _alt_min_backup_m; // backup altitude lower limit in meters used to refire the breach if the vehicle continues to move further away269float _circle_radius_backup_m; // backup circle fence radius in meters used to refire the breach if the vehicle continues to move further away270271// breach distances - negative means distance to fence272float _alt_max_breach_distance_m; // distance above the altitude max273float _alt_min_breach_distance_m; // distance below the altitude min274float _circle_breach_distance_m; // distance beyond the circular fence275float _polygon_breach_distance_m; // distance beyond the polygon fence276Vector2f _polygon_nearest_point; // direction towards the polygon breach277Vector2f _circle_breach_direction; // direction towards the circle breach278Location _last_fence_check_loc; // position used in the last fence check279bool _last_fence_check_loc_valid;// whether the position determined in the last fence check was valid280281// other internal variables282float _home_distance_m; // distance from home in meters (provided by main code)283float _fence_distance_m; // distance to the nearest fence284285// breach information286uint8_t _breached_fences; // bitmask holding the fence types that were breached (i.e. AC_FENCE_TYPE_ALT_MIN, AC_FENCE_TYPE_CIRCLE)287uint8_t _breached_fence_margins; // bitmask holding the fence types that have margin breaches (i.e. AC_FENCE_TYPE_ALT_MIN, AC_FENCE_TYPE_CIRCLE)288uint32_t _breach_time; // time of last breach in milliseconds289uint32_t _margin_breach_time; // time of last margin breach in milliseconds290uint16_t _breach_count; // number of times we have breached the fence291uint32_t _last_breach_notify_sent_ms; // last time we sent a message about newly-breaching the fences292uint32_t _last_margin_breach_notify_sent_ms; // last time we sent a message about newly-breaching the fences293294uint32_t _manual_recovery_start_ms; // system time in milliseconds that pilot re-took manual control295296enum class MinAltState297{298DEFAULT = 0,299MANUALLY_ENABLED,300MANUALLY_DISABLED301} _min_alt_state;302303304AC_PolyFence_loader _poly_loader{_total, _options}; // polygon fence305};306307namespace AP {308AC_Fence *fence();309};310311#endif // AP_FENCE_ENABLED312313314