2015-08-11 03:28:41 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
|
|
|
#include "AC_Fence.h"
|
2016-06-23 03:24:48 -03:00
|
|
|
#include <GCS_MAVLink/GCS_MAVLink.h>
|
|
|
|
#include <GCS_MAVLink/GCS.h>
|
2013-04-26 06:47:07 -03:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2015-10-25 14:03:46 -03:00
|
|
|
const AP_Param::GroupInfo AC_Fence::var_info[] = {
|
2013-04-26 06:47:07 -03:00
|
|
|
// @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
|
2016-08-30 20:54:57 -03:00
|
|
|
AP_GROUPINFO("ENABLE", 0, AC_Fence, _enabled, 0),
|
2013-04-26 06:47:07 -03:00
|
|
|
|
|
|
|
// @Param: TYPE
|
|
|
|
// @DisplayName: Fence Type
|
|
|
|
// @Description: Enabled fence types held as bitmask
|
2016-06-23 03:24:48 -03:00
|
|
|
// @Values: 0:None,1:Altitude,2:Circle,3:Altitude and Circle,4:Polygon,5:Altitude and Polygon,6:Circle and Polygon,7:All
|
|
|
|
// @Bitmask: 0:Altitude,1:Circle,2:Polygon
|
2013-04-26 06:47:07 -03:00
|
|
|
// @User: Standard
|
2016-06-23 03:24:48 -03:00
|
|
|
AP_GROUPINFO("TYPE", 1, AC_Fence, _enabled_fences, AC_FENCE_TYPE_ALT_MAX | AC_FENCE_TYPE_CIRCLE | AC_FENCE_TYPE_POLYGON),
|
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),
|
2016-06-23 03:24:48 -03:00
|
|
|
|
|
|
|
// @Param: TOTAL
|
|
|
|
// @DisplayName: Fence polygon point total
|
|
|
|
// @Description: Number of polygon points saved in eeprom (do not update manually)
|
|
|
|
// @Range: 1 20
|
|
|
|
// @User: Standard
|
|
|
|
AP_GROUPINFO("TOTAL", 6, AC_Fence, _total, 0),
|
|
|
|
|
2017-02-03 00:18:25 -04:00
|
|
|
// @Param: ALT_MIN
|
|
|
|
// @DisplayName: Fence Minimum Altitude
|
|
|
|
// @Description: Minimum altitude allowed before geofence triggers
|
|
|
|
// @Units: Meters
|
|
|
|
// @Range: -100 100
|
|
|
|
// @Increment: 1
|
|
|
|
// @User: Standard
|
|
|
|
AP_GROUPINFO_FRAME("ALT_MIN", 7, AC_Fence, _alt_min, AC_FENCE_ALT_MIN_DEFAULT, AP_PARAM_FRAME_SUB),
|
|
|
|
|
2013-04-26 06:47:07 -03:00
|
|
|
AP_GROUPEND
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Default constructor.
|
2016-07-02 05:14:17 -03:00
|
|
|
AC_Fence::AC_Fence(const AP_AHRS& ahrs, const AP_InertialNav& inav) :
|
|
|
|
_ahrs(ahrs),
|
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
|
2017-03-24 01:38:19 -03:00
|
|
|
bool AC_Fence::pre_arm_check(const char* &fail_msg) const
|
2013-04-26 06:47:07 -03:00
|
|
|
{
|
2017-03-24 01:38:19 -03:00
|
|
|
fail_msg = nullptr;
|
|
|
|
|
2013-04-26 06:47:07 -03:00
|
|
|
// 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) {
|
2017-03-24 01:38:19 -03:00
|
|
|
fail_msg = "vehicle outside fence";
|
2013-04-26 06:47:07 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we have horizontal limits enabled, check inertial nav position is ok
|
2017-03-23 23:04:25 -03:00
|
|
|
if ((_enabled_fences & (AC_FENCE_TYPE_CIRCLE | AC_FENCE_TYPE_POLYGON))>0 && !_inav.get_filter_status().flags.horiz_pos_abs && !_inav.get_filter_status().flags.pred_horiz_pos_abs) {
|
2017-03-24 01:38:19 -03:00
|
|
|
fail_msg = "fence requires position";
|
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
|
2015-11-19 23:05:31 -04:00
|
|
|
if ((AP_HAL::millis() - _manual_recovery_start_ms) < AC_FENCE_MANUAL_RECOVERY_TIME_MIN) {
|
2014-04-26 23:08:37 -03:00
|
|
|
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);
|
2016-12-14 10:59:20 -04:00
|
|
|
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);
|
2016-12-14 10:59:20 -04:00
|
|
|
ret |= AC_FENCE_TYPE_CIRCLE;
|
2013-04-26 06:47:07 -03:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-23 03:24:48 -03:00
|
|
|
// polygon fence check
|
|
|
|
if ((_enabled_fences & AC_FENCE_TYPE_POLYGON) != 0 ) {
|
2016-07-02 05:13:05 -03:00
|
|
|
// check consistency of number of points
|
|
|
|
if (_boundary_num_points != _total) {
|
|
|
|
_boundary_loaded = false;
|
|
|
|
}
|
2016-06-23 03:24:48 -03:00
|
|
|
// load fence if necessary
|
|
|
|
if (!_boundary_loaded) {
|
|
|
|
load_polygon_from_eeprom();
|
|
|
|
} else if (_boundary_valid) {
|
|
|
|
// check if vehicle is outside the polygon fence
|
|
|
|
const Vector3f& position = _inav.get_position();
|
|
|
|
if (_poly_loader.boundary_breached(Vector2f(position.x, position.y), _boundary_num_points, _boundary, true)) {
|
|
|
|
// check if this is a new breach
|
|
|
|
if ((_breached_fences & AC_FENCE_TYPE_POLYGON) == 0) {
|
|
|
|
// record that we have breached the polygon
|
|
|
|
record_breach(AC_FENCE_TYPE_POLYGON);
|
2016-12-14 10:59:20 -04:00
|
|
|
ret |= AC_FENCE_TYPE_POLYGON;
|
2016-06-23 03:24:48 -03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// clear breach if present
|
|
|
|
if ((_breached_fences & AC_FENCE_TYPE_POLYGON) != 0) {
|
|
|
|
clear_breach(AC_FENCE_TYPE_POLYGON);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-26 06:47:07 -03:00
|
|
|
// return any new breaches that have occurred
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-05-19 05:31:36 -03:00
|
|
|
// returns true if the destination is within fence (used to reject waypoints outside the fence)
|
2016-07-02 05:14:17 -03:00
|
|
|
bool AC_Fence::check_destination_within_fence(const Location_Class& loc)
|
2016-04-28 04:29:35 -03:00
|
|
|
{
|
|
|
|
// Altitude fence check
|
2016-07-02 05:14:17 -03:00
|
|
|
if ((get_enabled_fences() & AC_FENCE_TYPE_ALT_MAX)) {
|
|
|
|
int32_t alt_above_home_cm;
|
|
|
|
if (loc.get_alt_cm(Location_Class::ALT_FRAME_ABOVE_HOME, alt_above_home_cm)) {
|
|
|
|
if ((alt_above_home_cm * 0.01f) > _alt_max) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-04-28 04:29:35 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Circular fence check
|
2016-07-02 05:14:17 -03:00
|
|
|
if ((get_enabled_fences() & AC_FENCE_TYPE_CIRCLE)) {
|
|
|
|
if ((get_distance_cm(_ahrs.get_home(), loc) * 0.01f) > _circle_radius) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// polygon fence check
|
|
|
|
if ((get_enabled_fences() & AC_FENCE_TYPE_POLYGON) && _boundary_num_points > 0) {
|
|
|
|
// check ekf has a good location
|
|
|
|
Location temp_loc;
|
|
|
|
if (_inav.get_location(temp_loc)) {
|
|
|
|
const struct Location &ekf_origin = _inav.get_origin();
|
|
|
|
Vector2f position = location_diff(ekf_origin, loc) * 100.0f;
|
|
|
|
if (_poly_loader.boundary_breached(position, _boundary_num_points, _boundary, true)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-04-28 04:29:35 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-26 06:47:07 -03:00
|
|
|
/// 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) {
|
2015-11-19 23:05:31 -04:00
|
|
|
_breach_time = AP_HAL::millis();
|
2013-04-26 06:47:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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:
|
2015-11-27 13:11:58 -04:00
|
|
|
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
|
2015-11-19 23:05:31 -04:00
|
|
|
_manual_recovery_start_ms = AP_HAL::millis();
|
2014-04-26 23:08:37 -03:00
|
|
|
}
|
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
|
|
|
|
Vector2f* AC_Fence::get_polygon_points(uint16_t& num_points) const
|
|
|
|
{
|
|
|
|
num_points = _boundary_num_points;
|
|
|
|
return _boundary;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// returns true if we've breached the polygon boundary. simple passthrough to underlying _poly_loader object
|
|
|
|
bool AC_Fence::boundary_breached(const Vector2f& location, uint16_t num_points, const Vector2f* points) const
|
|
|
|
{
|
|
|
|
return _poly_loader.boundary_breached(location, num_points, points, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// handler for polygon fence messages with GCS
|
|
|
|
void AC_Fence::handle_msg(mavlink_channel_t chan, mavlink_message_t* msg)
|
|
|
|
{
|
|
|
|
// exit immediately if null message
|
2016-10-30 02:24:21 -03:00
|
|
|
if (msg == nullptr) {
|
2016-06-23 03:24:48 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (msg->msgid) {
|
|
|
|
// receive a fence point from GCS and store in EEPROM
|
|
|
|
case MAVLINK_MSG_ID_FENCE_POINT: {
|
|
|
|
mavlink_fence_point_t packet;
|
|
|
|
mavlink_msg_fence_point_decode(msg, &packet);
|
|
|
|
if (!check_latlng(packet.lat,packet.lng)) {
|
|
|
|
GCS_MAVLINK::send_statustext_chan(MAV_SEVERITY_WARNING, chan, "Invalid fence point, lat or lng too large");
|
|
|
|
} else {
|
|
|
|
Vector2l point;
|
|
|
|
point.x = packet.lat*1.0e7f;
|
|
|
|
point.y = packet.lng*1.0e7f;
|
|
|
|
if (!_poly_loader.save_point_to_eeprom(packet.idx, point)) {
|
|
|
|
GCS_MAVLINK::send_statustext_chan(MAV_SEVERITY_WARNING, chan, "Failed to save polygon point, too many points?");
|
2016-07-02 05:13:05 -03:00
|
|
|
} else {
|
|
|
|
// trigger reload of points
|
|
|
|
_boundary_loaded = false;
|
2016-06-23 03:24:48 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// send a fence point to GCS
|
|
|
|
case MAVLINK_MSG_ID_FENCE_FETCH_POINT: {
|
|
|
|
mavlink_fence_fetch_point_t packet;
|
|
|
|
mavlink_msg_fence_fetch_point_decode(msg, &packet);
|
|
|
|
// attempt to retrieve from eeprom
|
|
|
|
Vector2l point;
|
|
|
|
if (_poly_loader.load_point_from_eeprom(packet.idx, point)) {
|
|
|
|
mavlink_msg_fence_point_send_buf(msg, chan, msg->sysid, msg->compid, packet.idx, _total, point.x*1.0e-7f, point.y*1.0e-7f);
|
|
|
|
} else {
|
|
|
|
GCS_MAVLINK::send_statustext_chan(MAV_SEVERITY_WARNING, chan, "Bad fence point");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
// do nothing
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// load polygon points stored in eeprom into boundary array and perform validation
|
|
|
|
bool AC_Fence::load_polygon_from_eeprom(bool force_reload)
|
|
|
|
{
|
|
|
|
// exit immediately if already loaded
|
|
|
|
if (_boundary_loaded && !force_reload) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if we need to create array
|
|
|
|
if (!_boundary_create_attempted) {
|
|
|
|
_boundary = (Vector2f *)_poly_loader.create_point_array(sizeof(Vector2f));
|
2016-12-09 10:21:10 -04:00
|
|
|
_boundary_create_attempted = true;
|
2016-06-23 03:24:48 -03:00
|
|
|
}
|
|
|
|
|
2016-12-09 10:21:10 -04:00
|
|
|
// exit if we could not allocate RAM for the boundary
|
|
|
|
if (_boundary == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-23 03:24:48 -03:00
|
|
|
// get current location from EKF
|
|
|
|
Location temp_loc;
|
|
|
|
if (!_inav.get_location(temp_loc)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const struct Location &ekf_origin = _inav.get_origin();
|
|
|
|
|
|
|
|
// sanity check total
|
|
|
|
_total = constrain_int16(_total, 0, _poly_loader.max_points());
|
|
|
|
|
|
|
|
// load each point from eeprom
|
|
|
|
Vector2l temp_latlon;
|
|
|
|
for (uint16_t index=0; index<_total; index++) {
|
|
|
|
// load boundary point as lat/lon point
|
|
|
|
_poly_loader.load_point_from_eeprom(index, temp_latlon);
|
|
|
|
// move into location structure and convert to offset from ekf origin
|
|
|
|
temp_loc.lat = temp_latlon.x;
|
|
|
|
temp_loc.lng = temp_latlon.y;
|
|
|
|
_boundary[index] = location_diff(ekf_origin, temp_loc) * 100.0f;
|
|
|
|
}
|
|
|
|
_boundary_num_points = _total;
|
|
|
|
_boundary_loaded = true;
|
|
|
|
|
|
|
|
// update validity of polygon
|
|
|
|
_boundary_valid = _poly_loader.boundary_valid(_boundary_num_points, _boundary, true);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|