2016-02-17 21:25:06 -04:00
# pragma once
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>
# include <AP_Param/AP_Param.h>
# include <AP_Math/AP_Math.h>
2019-02-14 02:40:09 -04:00
# include <GCS_MAVLink/GCS.h>
2016-06-25 03:43:01 -03:00
# include <AC_Fence/AC_PolyFence_loader.h>
2016-07-02 05:14:17 -03:00
# include <AP_Common/Location.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
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
2013-04-26 06:47:07 -03:00
// default boundaries
2013-05-03 02:57:37 -03:00
# define AC_FENCE_ALT_MAX_DEFAULT 100.0f // default max altitude is 100m
2017-02-03 00:18:25 -04:00
# define AC_FENCE_ALT_MIN_DEFAULT -10.0f // default maximum depth in meters
2014-07-13 23:21:39 -03:00
# define AC_FENCE_CIRCLE_RADIUS_DEFAULT 300.0f // default circular fence radius is 300m
2013-05-01 05:05:04 -03:00
# define AC_FENCE_ALT_MAX_BACKUP_DISTANCE 20.0f // after fence is broken we recreate the fence 20m further up
# define AC_FENCE_CIRCLE_RADIUS_BACKUP_DISTANCE 20.0f // after fence is broken we recreate the fence 20m further out
2013-08-15 04:05:38 -03:00
# define AC_FENCE_MARGIN_DEFAULT 2.0f // default distance in meters that autopilot's should maintain from the fence to avoid a breach
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
2014-04-26 23:08:37 -03:00
# define AC_FENCE_MANUAL_RECOVERY_TIME_MIN 10000 // pilot has 10seconds to recover during which time the autopilot will not attempt to re-take control
2013-04-26 06:47:07 -03:00
class AC_Fence
{
public :
2019-01-30 20:37:22 -04:00
AC_Fence ( ) ;
2017-08-29 20:06:10 -03:00
/* Do not allow copies */
AC_Fence ( const AC_Fence & other ) = delete ;
AC_Fence & operator = ( const AC_Fence & ) = delete ;
2013-04-26 06:47:07 -03:00
/// enable - allows fence to be enabled/disabled. Note: this does not update the eeprom saved value
2017-07-20 13:42:28 -03:00
void enable ( bool value ) ;
2013-04-26 06:47:07 -03:00
/// enabled - returns true if fence is enabled
2013-05-01 05:05:04 -03:00
bool enabled ( ) const { return _enabled ; }
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
/// pre_arm_check - returns true if all pre-takeoff checks have completed successfully
2017-03-24 01:38:19 -03:00
bool pre_arm_check ( const char * & fail_msg ) 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)
uint8_t check ( ) ;
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)
2019-01-01 22:54:09 -04:00
bool check_destination_within_fence ( const 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 ; }
/// get_breach_distance - returns distance in meters outside of the given fence
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 ; }
/// get_safe_alt_min - returns the minimum safe altitude (i.e. alt_min - margin)
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 ( ) ; }
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 ( ) ;
2016-06-23 03:24:48 -03:00
///
/// polygon related methods
///
2019-05-12 22:59:00 -03:00
/// returns true if polygon fence is valid (i.e. has at least 3 sides)
bool is_polygon_valid ( ) const { return _boundary_valid ; }
2016-06-23 03:24:48 -03:00
/// returns pointer to array of polygon points and num_points is filled in with the total number
2019-05-12 22:59:00 -03:00
/// points are offsets from EKF origin in NE frame
2019-05-23 22:33:28 -03:00
Vector2f * get_boundary_points ( uint16_t & num_points ) const ;
2016-06-23 03:24:48 -03:00
/// returns true if we've breached the polygon boundary. simple passthrough to underlying _poly_loader object
bool boundary_breached ( const Vector2f & location , uint16_t num_points , const Vector2f * points ) const ;
/// handler for polygon fence messages with GCS
2019-04-30 07:22:47 -03:00
void handle_msg ( GCS_MAVLINK & link , const mavlink_message_t & msg ) ;
2016-06-23 03:24:48 -03:00
2019-05-29 23:40:48 -03:00
/// return system time of last update to the boundary (allows external detection of boundary changes)
uint32_t get_boundary_update_ms ( ) const { return _boundary_update_ms ; }
2013-04-26 06:47:07 -03:00
static const struct AP_Param : : GroupInfo var_info [ ] ;
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-01-30 21:54:13 -04:00
// get singleton instance
static AC_Fence * get_singleton ( ) { return _singleton ; }
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
2017-12-07 20:53:18 -04:00
/// check_fence_alt_max - true if 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
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:
2017-12-13 22:54:03 -04:00
bool pre_arm_check_polygon ( const char * & fail_msg ) const ;
2017-12-26 22:53:39 -04:00
bool pre_arm_check_circle ( const char * & fail_msg ) const ;
bool pre_arm_check_alt ( const char * & fail_msg ) const ;
2017-12-13 22:54:03 -04:00
2016-06-23 03:24:48 -03:00
/// load polygon points stored in eeprom into boundary array and perform validation. returns true if load successfully completed
2019-05-30 01:25:22 -03:00
bool load_polygon_from_eeprom ( ) ;
2016-06-23 03:24:48 -03:00
2019-03-06 00:45:00 -04:00
// returns true if we have breached the fence:
bool polygon_fence_is_breached ( ) ;
2013-04-26 06:47:07 -03:00
// parameters
AP_Int8 _enabled ; // top level enable/disable control
AP_Int8 _enabled_fences ; // bit mask holding which fences are enabled
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
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
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
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)
2017-12-15 05:35:22 -04:00
float _curr_alt ;
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
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
// polygon fence variables
2016-06-25 03:43:01 -03:00
AC_PolyFence_loader _poly_loader ; // helper for loading/saving polygon points
2016-10-30 02:24:21 -03:00
Vector2f * _boundary = nullptr ; // array of boundary points. Note: point 0 is the return point
2016-06-23 03:24:48 -03:00
uint8_t _boundary_num_points = 0 ; // number of points in the boundary array (should equal _total parameter after load has completed)
bool _boundary_create_attempted = false ; // true if we have attempted to create the boundary array
bool _boundary_valid = false ; // true if boundary forms a closed polygon
2019-05-29 23:40:48 -03:00
uint32_t _boundary_update_ms ; // system time of last update to the boundary
2013-04-26 06:47:07 -03:00
} ;
2019-01-30 21:54:13 -04:00
namespace AP {
AC_Fence * fence ( ) ;
} ;