2016-02-17 21:25:06 -04:00
# pragma once
2013-04-26 06:47:07 -03:00
2022-07-19 08:32:28 -03:00
# include "AC_Fence_config.h"
# if AP_FENCE_ENABLED
2013-04-26 06:47:07 -03:00
# include <inttypes.h>
2015-08-11 03:28:41 -03:00
# include <AP_Common/AP_Common.h>
2023-12-04 12:58:40 -04:00
# include <AP_Common/ExpandingString.h>
2015-08-11 03:28:41 -03:00
# include <AP_Param/AP_Param.h>
# include <AP_Math/AP_Math.h>
2016-06-25 03:43:01 -03:00
# include <AC_Fence/AC_PolyFence_loader.h>
2013-04-26 06:47:07 -03:00
// bit masks for enabled fence types. Used for TYPE parameter
2013-05-01 05:05:04 -03:00
# define AC_FENCE_TYPE_ALT_MAX 1 // high alt fence which usually initiates an RTL
# define AC_FENCE_TYPE_CIRCLE 2 // circular horizontal fence (usually initiates an RTL)
2016-06-23 03:24:48 -03:00
# define AC_FENCE_TYPE_POLYGON 4 // polygon horizontal fence
2020-09-10 03:28:09 -03:00
# define AC_FENCE_TYPE_ALT_MIN 8 // low alt fence which usually initiates an RTL
2023-12-04 12:58:40 -04:00
# define AC_FENCE_ARMING_FENCES (AC_FENCE_TYPE_ALT_MAX | AC_FENCE_TYPE_CIRCLE | AC_FENCE_TYPE_POLYGON)
# define AC_FENCE_ALL_FENCES (AC_FENCE_ARMING_FENCES | AC_FENCE_TYPE_ALT_MIN)
2013-04-26 06:47:07 -03:00
// valid actions should a fence be breached
2013-05-01 05:05:04 -03:00
# define AC_FENCE_ACTION_REPORT_ONLY 0 // report to GCS that boundary has been breached but take no further action
# define AC_FENCE_ACTION_RTL_AND_LAND 1 // return to launch and, if that fails, land
2018-07-24 01:19:12 -03:00
# define AC_FENCE_ACTION_ALWAYS_LAND 2 // always land
2019-01-24 06:00:42 -04:00
# define AC_FENCE_ACTION_SMART_RTL 3 // smartRTL, if that fails, RTL, it that still fails, land
# define AC_FENCE_ACTION_BRAKE 4 // brake, if that fails, land
2021-01-30 22:34:40 -04:00
# define AC_FENCE_ACTION_SMART_RTL_OR_LAND 5 // SmartRTL, if that fails, Land
2020-09-10 03:28:09 -03:00
# define AC_FENCE_ACTION_GUIDED 6 // guided mode, with target waypoint as fence return point
# define AC_FENCE_ACTION_GUIDED_THROTTLE_PASS 7 // guided mode, but pilot retains manual throttle control
2019-01-24 06:00:42 -04:00
2013-05-01 05:05:04 -03:00
// give up distance
# 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 code
2013-04-26 06:47:07 -03:00
class AC_Fence
{
public :
2023-07-22 19:03:08 -03:00
friend class AC_PolyFence_loader ;
2020-09-10 03:28:09 -03:00
2023-12-04 12:58:40 -04:00
enum class AutoEnable : uint8_t
2020-09-10 03:28:09 -03:00
{
ALWAYS_DISABLED = 0 ,
2023-12-04 12:58:40 -04:00
ENABLE_ON_AUTO_TAKEOFF = 1 , // enable on auto takeoff
ENABLE_DISABLE_FLOOR_ONLY = 2 , // enable on takeoff but disable floor on landing
ONLY_WHEN_ARMED = 3 // enable on arming
} ;
enum class MavlinkFenceActions : uint16_t
{
DISABLE_FENCE = 0 ,
ENABLE_FENCE = 1 ,
DISABLE_ALT_MIN_FENCE = 2
2020-09-10 03:28:09 -03:00
} ;
2019-01-30 20:37:22 -04:00
AC_Fence ( ) ;
2017-08-29 20:06:10 -03:00
/* Do not allow copies */
2022-09-30 06:50:43 -03:00
CLASS_NO_COPY ( AC_Fence ) ;
2013-04-26 06:47:07 -03:00
2019-08-28 04:22:16 -03:00
void init ( ) {
_poly_loader . init ( ) ;
}
// get singleton instance
static AC_Fence * get_singleton ( ) { return _singleton ; }
2020-09-10 03:28:09 -03:00
/// enable - allows fence to be enabled/disabled.
2023-12-04 12:58:40 -04:00
/// returns a bitmask of fences that were changed
uint8_t enable ( bool value , uint8_t fence_types , bool update_auto_mask = true ) ;
/// enable_configured - allows configured fences to be enabled/disabled.
/// returns a bitmask of fences that were changed
uint8_t enable_configured ( bool value ) { return enable ( value , _configured_fences , true ) ; }
2013-04-26 06:47:07 -03:00
2021-01-06 08:15:59 -04:00
/// auto_enabled - automaticaly enable/disable fence depending of flight status
2024-05-31 10:12:38 -03:00
AutoEnable auto_enabled ( ) const { return static_cast < AutoEnable > ( _auto_enabled . get ( ) ) ; }
2020-09-10 03:28:09 -03:00
/// enable_floor - allows fence floor to be enabled/disabled. Note this does not update the eeprom saved value
void enable_floor ( ) ;
/// disable_floor - allows fence floor to be enabled/disabled. Note this does not update the eeprom saved value
void disable_floor ( ) ;
2021-01-13 19:35:48 -04:00
/// auto_enable_fence_on_takeoff - auto enables the fence. Called after takeoff conditions met
void auto_enable_fence_after_takeoff ( ) ;
2023-12-04 12:58:40 -04:00
/// auto_enable_fences_on_arming - auto enables all applicable fences on arming
void auto_enable_fence_on_arming ( ) ;
2024-03-11 09:40:31 -03:00
/// auto_disable_fences_on_disarming - auto disables all applicable fences on disarming
void auto_disable_fence_on_disarming ( ) ;
2024-05-31 10:12:38 -03:00
uint8_t get_auto_disable_fences ( void ) const ;
2023-12-04 12:58:40 -04:00
/// auto_enable_fence_floor - auto enables fence floor once desired altitude has been reached.
bool auto_enable_fence_floor ( ) ;
2013-04-26 06:47:07 -03:00
2023-12-04 12:58:40 -04:00
/// enabled - returns whether fencing is enabled or not
bool enabled ( ) const { return _enabled_fences ; }
/// present - returns true if any of the provided types is present
uint8_t present ( ) const ;
2020-09-10 03:28:09 -03:00
2013-04-26 06:47:07 -03:00
/// get_enabled_fences - returns bitmask of enabled fences
2013-05-01 05:05:04 -03:00
uint8_t get_enabled_fences ( ) const ;
2013-04-26 06:47:07 -03:00
2019-08-28 04:22:16 -03:00
// should be called @10Hz to handle loading from eeprom
2023-12-04 12:58:40 -04:00
void update ( ) ;
2019-08-28 04:22:16 -03:00
2013-04-26 06:47:07 -03:00
/// pre_arm_check - returns true if all pre-takeoff checks have completed successfully
2024-06-22 08:02:21 -03:00
bool pre_arm_check ( char * failure_msg , const uint8_t failure_msg_len ) const ;
2013-04-26 06:47:07 -03:00
///
/// methods to check we are within the boundaries and recover
///
2017-12-15 05:35:22 -04:00
/// check - returns the fence type that has been breached (if any)
2024-05-31 10:12:38 -03:00
/// disabled_fences can be used to disable fences for certain conditions (e.g. landing)
uint8_t check ( bool disable_auto_fence = false ) ;
2013-04-26 06:47:07 -03:00
2016-05-19 05:31:36 -03:00
// returns true if the destination is within fence (used to reject waypoints outside the fence)
2022-02-25 01:06:26 -04:00
bool check_destination_within_fence ( const class Location & loc ) ;
2016-04-28 04:29:35 -03:00
2013-04-26 06:47:07 -03:00
/// get_breaches - returns bit mask of the fence types that have been breached
uint8_t get_breaches ( ) const { return _breached_fences ; }
/// get_breach_time - returns time the fence was breached
uint32_t get_breach_time ( ) const { return _breach_time ; }
/// get_breach_count - returns number of times we have breached the fence
2013-05-01 05:05:04 -03:00
uint16_t get_breach_count ( ) const { return _breach_count ; }
2019-08-08 00:16:54 -03:00
/// get_breach_distance - returns maximum distance in meters outside
/// of the given fences. fence_type is a bitmask here.
2013-05-01 05:05:04 -03:00
float get_breach_distance ( uint8_t fence_type ) const ;
2013-04-26 06:47:07 -03:00
/// get_action - getter for user requested action on limit breach
uint8_t get_action ( ) const { return _action . get ( ) ; }
2013-08-15 04:05:38 -03:00
/// get_safe_alt - returns maximum safe altitude (i.e. alt_max - margin)
2017-02-03 00:18:25 -04:00
float get_safe_alt_max ( ) const { return _alt_max - _margin ; }
2017-08-24 06:48:17 -03:00
/// get_safe_alt_min - returns the minimum safe altitude (i.e. alt_min + margin)
2017-02-03 00:18:25 -04:00
float get_safe_alt_min ( ) const { return _alt_min + _margin ; }
2014-04-26 23:08:37 -03:00
2016-06-19 22:14:14 -03:00
/// get_radius - returns the fence radius in meters
float get_radius ( ) const { return _circle_radius . get ( ) ; }
/// get_margin - returns the fence margin in meters
float get_margin ( ) const { return _margin . get ( ) ; }
2020-09-10 03:28:09 -03:00
/// get_return_rally - returns whether returning to fence return point or rally point
uint8_t get_return_rally ( ) const { return _ret_rally ; }
/// get_return_rally - returns whether returning to fence return point or rally point
float get_return_altitude ( ) const { return _ret_altitude ; }
2023-12-04 12:58:40 -04:00
/// get a user-friendly list of fences
static void get_fence_names ( uint8_t fences , ExpandingString & msg ) ;
// print a message about the fences to the GCS
void print_fence_message ( const char * msg , uint8_t fences ) const ;
2014-04-26 23:08:37 -03:00
/// manual_recovery_start - caller indicates that pilot is re-taking manual control so fence should be disabled for 10 seconds
/// should be called whenever the pilot changes the flight mode
/// has no effect if no breaches have occurred
void manual_recovery_start ( ) ;
2018-12-04 00:32:42 -04:00
// methods for mavlink SYS_STATUS message (send_sys_status)
2017-12-27 00:31:32 -04:00
bool sys_status_present ( ) const ;
bool sys_status_enabled ( ) const ;
bool sys_status_failed ( ) const ;
2017-12-12 01:49:14 -04:00
2019-08-28 04:22:16 -03:00
AC_PolyFence_loader & polyfence ( ) ;
const AC_PolyFence_loader & polyfence ( ) const ;
2019-01-30 21:54:13 -04:00
2022-08-12 16:32:28 -03:00
enum class OPTIONS {
2023-07-22 19:03:08 -03:00
DISABLE_MODE_CHANGE = 1U < < 0 ,
INCLUSION_UNION = 1U < < 1 ,
2022-08-12 16:32:28 -03:00
} ;
2023-07-22 19:03:08 -03:00
static bool option_enabled ( OPTIONS opt , const AP_Int16 & options ) {
return ( options . get ( ) & int16_t ( opt ) ) ! = 0 ;
}
2022-08-12 16:32:28 -03:00
bool option_enabled ( OPTIONS opt ) const {
2023-07-22 19:03:08 -03:00
return option_enabled ( opt , _options ) ;
2022-08-12 16:32:28 -03:00
}
2019-08-28 04:22:16 -03:00
static const struct AP_Param : : GroupInfo var_info [ ] ;
2019-05-29 10:02:04 -03:00
2024-02-20 20:00:49 -04:00
# if AP_SDCARD_STORAGE_ENABLED
bool failed_sdcard_storage ( void ) const {
return _poly_loader . failed_sdcard_storage ( ) ;
}
# endif
2013-04-26 06:47:07 -03:00
private :
2019-01-30 21:54:13 -04:00
static AC_Fence * _singleton ;
2017-12-15 05:35:22 -04:00
2020-09-10 03:28:09 -03:00
/// check_fence_alt_max - true if max alt fence has been newly breached
2017-12-15 05:35:22 -04:00
bool check_fence_alt_max ( ) ;
2017-12-07 20:53:18 -04:00
2020-09-10 03:28:09 -03:00
/// check_fence_alt_min - true if min alt fence has been newly breached
bool check_fence_alt_min ( ) ;
2017-12-07 20:10:16 -04:00
/// check_fence_polygon - true if polygon fence has been newly breached
bool check_fence_polygon ( ) ;
2017-12-07 20:18:29 -04:00
/// check_fence_circle - true if circle fence has been newly breached
bool check_fence_circle ( ) ;
2013-04-26 06:47:07 -03:00
/// record_breach - update breach bitmask, time and count
void record_breach ( uint8_t fence_type ) ;
/// clear_breach - update breach bitmask, time and count
void clear_breach ( uint8_t fence_type ) ;
2017-12-26 22:53:39 -04:00
// additional checks for the different fence types:
2024-06-22 08:02:21 -03:00
bool pre_arm_check_polygon ( char * failure_msg , const uint8_t failure_msg_len ) const ;
bool pre_arm_check_circle ( char * failure_msg , const uint8_t failure_msg_len ) const ;
bool pre_arm_check_alt ( char * failure_msg , const uint8_t failure_msg_len ) const ;
2023-12-04 12:58:40 -04:00
// fence floor is enabled
bool floor_enabled ( ) const { return _enabled_fences & AC_FENCE_TYPE_ALT_MIN ; }
2017-12-13 22:54:03 -04:00
2013-04-26 06:47:07 -03:00
// parameters
2023-12-04 12:58:40 -04:00
uint8_t _enabled_fences ; // fences that are currently enabled/disabled
bool _last_enabled ; // value of enabled last time we checked
AP_Int8 _enabled ; // overall feature control
2020-09-10 03:28:09 -03:00
AP_Int8 _auto_enabled ; // top level flag for auto enabling fence
2023-12-04 12:58:40 -04:00
uint8_t _last_auto_enabled ; // value of auto_enabled last time we checked
AP_Int8 _configured_fences ; // bit mask holding which fences are enabled
2013-04-26 06:47:07 -03:00
AP_Int8 _action ; // recovery action specified by user
2013-05-01 05:05:04 -03:00
AP_Float _alt_max ; // altitude upper limit in meters
2017-02-03 00:18:25 -04:00
AP_Float _alt_min ; // altitude lower limit in meters
2013-05-01 05:05:04 -03:00
AP_Float _circle_radius ; // circle fence radius in meters
2013-08-15 04:05:38 -03:00
AP_Float _margin ; // distance in meters that autopilot's should maintain from the fence to avoid a breach
2016-06-23 03:24:48 -03:00
AP_Int8 _total ; // number of polygon points saved in eeprom
2020-09-10 03:28:09 -03:00
AP_Int8 _ret_rally ; // return to fence return point or rally point/home
2021-02-15 20:41:56 -04:00
AP_Int16 _ret_altitude ; // return to this altitude
2022-08-12 16:32:28 -03:00
AP_Int16 _options ; // options bitmask, see OPTIONS enum
2013-05-01 05:05:04 -03:00
// backup fences
float _alt_max_backup ; // backup altitude upper limit in meters used to refire the breach if the vehicle continues to move further away
2020-09-10 03:28:09 -03:00
float _alt_min_backup ; // backup altitude lower limit in meters used to refire the breach if the vehicle continues to move further away
2013-05-01 05:05:04 -03:00
float _circle_radius_backup ; // backup circle fence radius in meters used to refire the breach if the vehicle continues to move further away
// breach distances
float _alt_max_breach_distance ; // distance above the altitude max
2020-09-10 03:28:09 -03:00
float _alt_min_breach_distance ; // distance below the altitude min
2013-08-15 04:05:38 -03:00
float _circle_breach_distance ; // distance beyond the circular fence
2013-04-26 06:47:07 -03:00
// other internal variables
2013-05-01 05:05:04 -03:00
float _home_distance ; // distance from home in meters (provided by main code)
2013-04-26 06:47:07 -03:00
// breach information
uint8_t _breached_fences ; // bitmask holding the fence type that was breached (i.e. AC_FENCE_TYPE_ALT_MIN, AC_FENCE_TYPE_CIRCLE)
uint32_t _breach_time ; // time of last breach in milliseconds
uint16_t _breach_count ; // number of times we have breached the fence
2021-02-12 23:01:25 -04:00
uint32_t _last_breach_notify_sent_ms ; // last time we sent a message about newly-breaching the fences
2014-04-26 23:08:37 -03:00
uint32_t _manual_recovery_start_ms ; // system time in milliseconds that pilot re-took manual control
2016-06-23 03:24:48 -03:00
2024-12-09 17:41:05 -04:00
enum class MinAltState
{
DEFAULT = 0 ,
MANUALLY_ENABLED ,
MANUALLY_DISABLED
} _min_alt_state ;
2021-02-12 23:01:25 -04:00
2023-07-22 19:03:08 -03:00
AC_PolyFence_loader _poly_loader { _total , _options } ; // polygon fence
2013-04-26 06:47:07 -03:00
} ;
2019-01-30 21:54:13 -04:00
namespace AP {
AC_Fence * fence ( ) ;
} ;
2022-03-04 12:42:09 -04:00
2022-07-19 08:32:28 -03:00
# endif // AP_FENCE_ENABLED