2013-04-26 06:47:07 -03:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
#include <AP_HAL.h>
|
|
|
|
#include <AC_Fence.h>
|
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
|
|
|
const AP_Param::GroupInfo AC_Fence::var_info[] PROGMEM = {
|
|
|
|
// @Param: ENABLE
|
|
|
|
// @DisplayName: Fence enable/disable
|
|
|
|
// @Description: Allows you to enable (1) or disable (0) the fence functionality
|
|
|
|
// @Values: 0:Disabled,1:Enabled
|
|
|
|
// @User: Standard
|
|
|
|
AP_GROUPINFO("ENABLE", 0, AC_Fence, _enabled, 0),
|
|
|
|
|
|
|
|
// @Param: TYPE
|
|
|
|
// @DisplayName: Fence Type
|
|
|
|
// @Description: Enabled fence types held as bitmask
|
2013-05-01 05:05:04 -03:00
|
|
|
// @Values: 0:None,1:Altitude,2:Circle,3:Altitude and Circle
|
2013-04-26 06:47:07 -03:00
|
|
|
// @User: Standard
|
2013-05-01 05:05:04 -03:00
|
|
|
AP_GROUPINFO("TYPE", 1, AC_Fence, _enabled_fences, AC_FENCE_TYPE_ALT_MAX | AC_FENCE_TYPE_CIRCLE),
|
2013-04-26 06:47:07 -03:00
|
|
|
|
|
|
|
// @Param: ACTION
|
2013-11-26 09:18:05 -04:00
|
|
|
// @DisplayName: Fence Action
|
|
|
|
// @Description: What action should be taken when fence is breached
|
2013-05-01 05:05:04 -03:00
|
|
|
// @Values: 0:Report Only,1:RTL or Land
|
2013-04-26 06:47:07 -03:00
|
|
|
// @User: Standard
|
|
|
|
AP_GROUPINFO("ACTION", 2, AC_Fence, _action, AC_FENCE_ACTION_RTL_AND_LAND),
|
|
|
|
|
|
|
|
// @Param: ALT_MAX
|
|
|
|
// @DisplayName: Fence Maximum Altitude
|
|
|
|
// @Description: Maximum altitude allowed before geofence triggers
|
2013-05-01 05:05:04 -03:00
|
|
|
// @Units: Meters
|
|
|
|
// @Range: 10 1000
|
|
|
|
// @Increment: 1
|
2013-04-26 06:47:07 -03:00
|
|
|
// @User: Standard
|
2013-05-01 05:05:04 -03:00
|
|
|
AP_GROUPINFO("ALT_MAX", 3, AC_Fence, _alt_max, AC_FENCE_ALT_MAX_DEFAULT),
|
2013-04-26 06:47:07 -03:00
|
|
|
|
|
|
|
// @Param: RADIUS
|
|
|
|
// @DisplayName: Circular Fence Radius
|
2013-05-01 05:05:04 -03:00
|
|
|
// @Description: Circle fence radius which when breached will cause an RTL
|
|
|
|
// @Units: Meters
|
2013-07-26 22:28:33 -03:00
|
|
|
// @Range: 30 10000
|
2013-04-26 06:47:07 -03:00
|
|
|
// @User: Standard
|
2013-05-01 05:05:04 -03:00
|
|
|
AP_GROUPINFO("RADIUS", 4, AC_Fence, _circle_radius, AC_FENCE_CIRCLE_RADIUS_DEFAULT),
|
2013-08-15 04:05:38 -03:00
|
|
|
|
|
|
|
// @Param: MARGIN
|
|
|
|
// @DisplayName: Fence Margin
|
|
|
|
// @Description: Distance that autopilot's should maintain from the fence to avoid a breach
|
|
|
|
// @Units: Meters
|
|
|
|
// @Range: 1 10
|
|
|
|
// @User: Standard
|
|
|
|
AP_GROUPINFO("MARGIN", 5, AC_Fence, _margin, AC_FENCE_MARGIN_DEFAULT),
|
2013-04-26 06:47:07 -03:00
|
|
|
|
|
|
|
AP_GROUPEND
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Default constructor.
|
2015-04-13 19:07:40 -03:00
|
|
|
AC_Fence::AC_Fence(const AP_InertialNav& inav) :
|
2013-04-26 06:47:07 -03:00
|
|
|
_inav(inav),
|
2013-05-01 05:05:04 -03:00
|
|
|
_alt_max_backup(0),
|
|
|
|
_circle_radius_backup(0),
|
|
|
|
_alt_max_breach_distance(0),
|
|
|
|
_circle_breach_distance(0),
|
|
|
|
_home_distance(0),
|
|
|
|
_breached_fences(AC_FENCE_TYPE_NONE),
|
|
|
|
_breach_time(0),
|
2014-04-26 23:08:37 -03:00
|
|
|
_breach_count(0),
|
|
|
|
_manual_recovery_start_ms(0)
|
2013-04-26 06:47:07 -03:00
|
|
|
{
|
|
|
|
AP_Param::setup_object_defaults(this, var_info);
|
|
|
|
|
|
|
|
// check for silly fence values
|
2014-07-14 22:55:51 -03:00
|
|
|
if (_alt_max < 0.0f) {
|
2013-05-01 05:05:04 -03:00
|
|
|
_alt_max.set_and_save(AC_FENCE_ALT_MAX_DEFAULT);
|
2013-04-26 06:47:07 -03:00
|
|
|
}
|
2013-05-01 05:05:04 -03:00
|
|
|
if (_circle_radius < 0) {
|
|
|
|
_circle_radius.set_and_save(AC_FENCE_CIRCLE_RADIUS_DEFAULT);
|
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 AC_Fence::get_enabled_fences() const
|
2013-04-26 06:47:07 -03:00
|
|
|
{
|
2013-05-01 05:05:04 -03:00
|
|
|
if (!_enabled) {
|
|
|
|
return AC_FENCE_TYPE_NONE;
|
2013-04-26 06:47:07 -03:00
|
|
|
}else{
|
|
|
|
return _enabled_fences;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// pre_arm_check - returns true if all pre-takeoff checks have completed successfully
|
|
|
|
bool AC_Fence::pre_arm_check() const
|
|
|
|
{
|
|
|
|
// if not enabled or not fence set-up always return true
|
2013-05-01 05:05:04 -03:00
|
|
|
if (!_enabled || _enabled_fences == AC_FENCE_TYPE_NONE) {
|
2013-04-26 06:47:07 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check no limits are currently breached
|
2013-05-01 05:05:04 -03:00
|
|
|
if (_breached_fences != AC_FENCE_TYPE_NONE) {
|
2013-04-26 06:47:07 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we have horizontal limits enabled, check inertial nav position is ok
|
2015-04-13 19:07:40 -03:00
|
|
|
if ((_enabled_fences & AC_FENCE_TYPE_CIRCLE)!=0 && !_inav.get_filter_status().flags.horiz_pos_abs && !_inav.get_filter_status().flags.pred_horiz_pos_abs) {
|
2013-04-26 06:47:07 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we got this far everything must be ok
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// check_fence - returns the fence type that has been breached (if any)
|
2015-04-14 04:00:17 -03:00
|
|
|
/// curr_alt is the altitude above home in meters
|
2015-04-13 19:09:28 -03:00
|
|
|
uint8_t AC_Fence::check_fence(float curr_alt)
|
2013-04-26 06:47:07 -03:00
|
|
|
{
|
|
|
|
uint8_t ret = AC_FENCE_TYPE_NONE;
|
|
|
|
|
|
|
|
// return immediately if disabled
|
2013-05-01 05:05:04 -03:00
|
|
|
if (!_enabled || _enabled_fences == AC_FENCE_TYPE_NONE) {
|
2013-04-26 06:47:07 -03:00
|
|
|
return AC_FENCE_TYPE_NONE;
|
|
|
|
}
|
|
|
|
|
2014-04-26 23:08:37 -03:00
|
|
|
// check if pilot is attempting to recover manually
|
|
|
|
if (_manual_recovery_start_ms != 0) {
|
|
|
|
// we ignore any fence breaches during the manual recovery period which is about 10 seconds
|
|
|
|
if ((hal.scheduler->millis() - _manual_recovery_start_ms) < AC_FENCE_MANUAL_RECOVERY_TIME_MIN) {
|
|
|
|
return AC_FENCE_TYPE_NONE;
|
|
|
|
} else {
|
|
|
|
// recovery period has passed so reset manual recovery time and continue with fence breach checks
|
|
|
|
_manual_recovery_start_ms = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-01 05:05:04 -03:00
|
|
|
// altitude fence check
|
|
|
|
if ((_enabled_fences & AC_FENCE_TYPE_ALT_MAX) != 0) {
|
|
|
|
|
|
|
|
// check if we are over the altitude fence
|
|
|
|
if( curr_alt >= _alt_max ) {
|
|
|
|
|
|
|
|
// record distance above breach
|
|
|
|
_alt_max_breach_distance = curr_alt - _alt_max;
|
|
|
|
|
|
|
|
// check for a new breach or a breach of the backup fence
|
2015-05-04 23:34:42 -03:00
|
|
|
if ((_breached_fences & AC_FENCE_TYPE_ALT_MAX) == 0 || (!is_zero(_alt_max_backup) && curr_alt >= _alt_max_backup)) {
|
2013-04-26 06:47:07 -03:00
|
|
|
|
|
|
|
// record that we have breached the upper limit
|
|
|
|
record_breach(AC_FENCE_TYPE_ALT_MAX);
|
|
|
|
ret = ret | AC_FENCE_TYPE_ALT_MAX;
|
2013-05-01 05:05:04 -03:00
|
|
|
|
|
|
|
// create a backup fence 20m higher up
|
|
|
|
_alt_max_backup = curr_alt + AC_FENCE_ALT_MAX_BACKUP_DISTANCE;
|
2013-04-26 06:47:07 -03:00
|
|
|
}
|
|
|
|
}else{
|
2013-05-01 05:05:04 -03:00
|
|
|
// clear alt breach if present
|
|
|
|
if ((_breached_fences & AC_FENCE_TYPE_ALT_MAX) != 0) {
|
|
|
|
clear_breach(AC_FENCE_TYPE_ALT_MAX);
|
2014-07-14 22:55:51 -03:00
|
|
|
_alt_max_backup = 0.0f;
|
|
|
|
_alt_max_breach_distance = 0.0f;
|
2013-05-01 05:05:04 -03:00
|
|
|
}
|
2013-04-26 06:47:07 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// circle fence check
|
2013-05-01 05:05:04 -03:00
|
|
|
if ((_enabled_fences & AC_FENCE_TYPE_CIRCLE) != 0 ) {
|
|
|
|
|
|
|
|
// check if we are outside the fence
|
|
|
|
if (_home_distance >= _circle_radius) {
|
|
|
|
|
|
|
|
// record distance outside the fence
|
|
|
|
_circle_breach_distance = _home_distance - _circle_radius;
|
|
|
|
|
|
|
|
// check for a new breach or a breach of the backup fence
|
2015-05-04 23:34:42 -03:00
|
|
|
if ((_breached_fences & AC_FENCE_TYPE_CIRCLE) == 0 || (!is_zero(_circle_radius_backup) && _home_distance >= _circle_radius_backup)) {
|
2013-05-01 05:05:04 -03:00
|
|
|
|
2013-04-26 06:47:07 -03:00
|
|
|
// record that we have breached the circular distance limit
|
|
|
|
record_breach(AC_FENCE_TYPE_CIRCLE);
|
|
|
|
ret = ret | AC_FENCE_TYPE_CIRCLE;
|
|
|
|
|
2013-05-01 05:05:04 -03:00
|
|
|
// create a backup fence 20m further out
|
|
|
|
_circle_radius_backup = _home_distance + AC_FENCE_CIRCLE_RADIUS_BACKUP_DISTANCE;
|
2013-04-26 06:47:07 -03:00
|
|
|
}
|
|
|
|
}else{
|
2013-05-01 05:05:04 -03:00
|
|
|
// clear circle breach if present
|
|
|
|
if ((_breached_fences & AC_FENCE_TYPE_CIRCLE) != 0) {
|
|
|
|
clear_breach(AC_FENCE_TYPE_CIRCLE);
|
2014-07-14 22:55:51 -03:00
|
|
|
_circle_radius_backup = 0.0f;
|
|
|
|
_circle_breach_distance = 0.0f;
|
2013-05-01 05:05:04 -03:00
|
|
|
}
|
2013-04-26 06:47:07 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// return any new breaches that have occurred
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
// To-Do: add min alt and polygon check
|
|
|
|
//outside = Polygon_outside(location, &geofence_state->boundary[1], geofence_state->num_points-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// record_breach - update breach bitmask, time and count
|
|
|
|
void AC_Fence::record_breach(uint8_t fence_type)
|
|
|
|
{
|
|
|
|
// if we haven't already breached a limit, update the breach time
|
2013-05-01 05:05:04 -03:00
|
|
|
if (_breached_fences == AC_FENCE_TYPE_NONE) {
|
2013-04-26 06:47:07 -03:00
|
|
|
_breach_time = hal.scheduler->millis();
|
|
|
|
}
|
|
|
|
|
|
|
|
// update breach count
|
2013-05-01 05:05:04 -03:00
|
|
|
if (_breach_count < 65500) {
|
2013-04-26 06:47:07 -03:00
|
|
|
_breach_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update bitmask
|
2013-05-01 05:05:04 -03:00
|
|
|
_breached_fences |= fence_type;
|
2013-04-26 06:47:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// clear_breach - update breach bitmask, time and count
|
|
|
|
void AC_Fence::clear_breach(uint8_t fence_type)
|
|
|
|
{
|
|
|
|
// return immediately if this fence type was not breached
|
2013-05-01 05:05:04 -03:00
|
|
|
if ((_breached_fences & fence_type) == 0) {
|
2013-04-26 06:47:07 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update bitmask
|
2013-05-01 05:05:04 -03:00
|
|
|
_breached_fences &= ~fence_type;
|
|
|
|
}
|
2013-04-26 06:47:07 -03:00
|
|
|
|
2013-05-01 05:05:04 -03:00
|
|
|
/// get_breach_distance - returns distance in meters outside of the given fence
|
|
|
|
float AC_Fence::get_breach_distance(uint8_t fence_type) const
|
|
|
|
{
|
|
|
|
switch (fence_type) {
|
|
|
|
case AC_FENCE_TYPE_ALT_MAX:
|
|
|
|
return _alt_max_breach_distance;
|
|
|
|
break;
|
|
|
|
case AC_FENCE_TYPE_CIRCLE:
|
|
|
|
return _circle_breach_distance;
|
|
|
|
break;
|
|
|
|
case AC_FENCE_TYPE_ALT_MAX | AC_FENCE_TYPE_CIRCLE:
|
|
|
|
return max(_alt_max_breach_distance,_circle_breach_distance);
|
2013-04-26 06:47:07 -03:00
|
|
|
}
|
2013-05-01 05:05:04 -03:00
|
|
|
|
|
|
|
// we don't recognise the fence type so just return 0
|
|
|
|
return 0;
|
2013-07-24 11:05:21 -03:00
|
|
|
}
|
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
|
|
|
|
/// has no effect if no breaches have occurred
|
|
|
|
void AC_Fence::manual_recovery_start()
|
|
|
|
{
|
|
|
|
// return immediate if we haven't breached a fence
|
|
|
|
if (_breached_fences == AC_FENCE_TYPE_NONE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// record time pilot began manual recovery
|
|
|
|
_manual_recovery_start_ms = hal.scheduler->millis();
|
|
|
|
}
|