2015-05-29 23:12:49 -03:00
|
|
|
#include "Copter.h"
|
|
|
|
|
2013-04-26 06:51:07 -03:00
|
|
|
// Code to integrate AC_Fence library with main ArduCopter code
|
|
|
|
|
|
|
|
#if AC_FENCE == ENABLED
|
|
|
|
|
|
|
|
// fence_check - ask fence library to check for breaches and initiate the response
|
|
|
|
// called at 1hz
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::fence_check()
|
2013-04-26 06:51:07 -03:00
|
|
|
{
|
2017-12-15 05:36:25 -04:00
|
|
|
// ignore any fence activity when not armed
|
2017-01-09 03:31:26 -04:00
|
|
|
if(!motors->armed()) {
|
2015-07-08 03:21:31 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-15 05:36:25 -04:00
|
|
|
const uint8_t orig_breaches = fence.get_breaches();
|
|
|
|
|
|
|
|
// check for new breaches; new_breaches is bitmask of fence types breached
|
|
|
|
const uint8_t new_breaches = fence.check();
|
|
|
|
|
2013-04-26 06:51:07 -03:00
|
|
|
// if there is a new breach take action
|
2017-12-15 05:36:25 -04:00
|
|
|
if (new_breaches) {
|
2013-04-26 06:51:07 -03:00
|
|
|
|
|
|
|
// if the user wants some kind of response and motors are armed
|
|
|
|
if(fence.get_action() != AC_FENCE_ACTION_REPORT_ONLY ) {
|
|
|
|
|
2014-10-16 02:09:02 -03:00
|
|
|
// disarm immediately if we think we are on the ground or in a manual flight mode with zero throttle
|
2013-04-26 06:51:07 -03:00
|
|
|
// don't disarm if the high-altitude fence has been broken because it's likely the user has pulled their throttle to zero to bring it down
|
2017-12-05 19:56:38 -04:00
|
|
|
if (ap.land_complete || (flightmode->has_manual_throttle() && ap.throttle_zero && !failsafe.radio && ((fence.get_breaches() & AC_FENCE_TYPE_ALT_MAX)== 0))){
|
2013-04-26 06:51:07 -03:00
|
|
|
init_disarm_motors();
|
|
|
|
}else{
|
2013-11-11 09:24:18 -04:00
|
|
|
// if we are within 100m of the fence, RTL
|
|
|
|
if (fence.get_breach_distance(new_breaches) <= AC_FENCE_GIVE_UP_DISTANCE) {
|
2016-01-25 19:40:41 -04:00
|
|
|
if (!set_mode(RTL, MODE_REASON_FENCE_BREACH)) {
|
|
|
|
set_mode(LAND, MODE_REASON_FENCE_BREACH);
|
2013-04-26 06:51:07 -03:00
|
|
|
}
|
|
|
|
}else{
|
2013-11-11 09:24:18 -04:00
|
|
|
// if more than 100m outside the fence just force a land
|
2016-01-25 19:40:41 -04:00
|
|
|
set_mode(LAND, MODE_REASON_FENCE_BREACH);
|
2013-04-26 06:51:07 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// log an error in the dataflash
|
|
|
|
Log_Write_Error(ERROR_SUBSYSTEM_FAILSAFE_FENCE, new_breaches);
|
|
|
|
|
2017-12-15 05:36:25 -04:00
|
|
|
} else if (orig_breaches) {
|
|
|
|
// record clearing of breach
|
2013-04-26 06:51:07 -03:00
|
|
|
Log_Write_Error(ERROR_SUBSYSTEM_FAILSAFE_FENCE, ERROR_CODE_ERROR_RESOLVED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fence_send_mavlink_status - send fence status to ground station
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::fence_send_mavlink_status(mavlink_channel_t chan)
|
2013-05-01 05:06:58 -03:00
|
|
|
{
|
2013-04-26 06:51:07 -03:00
|
|
|
if (fence.enabled()) {
|
2013-05-01 05:06:58 -03:00
|
|
|
// traslate fence library breach types to mavlink breach types
|
|
|
|
uint8_t mavlink_breach_type = FENCE_BREACH_NONE;
|
|
|
|
uint8_t breaches = fence.get_breaches();
|
2013-05-04 11:18:16 -03:00
|
|
|
if ((breaches & AC_FENCE_TYPE_ALT_MAX) != 0) {
|
2013-05-01 05:06:58 -03:00
|
|
|
mavlink_breach_type = FENCE_BREACH_MAXALT;
|
|
|
|
}
|
2017-07-20 11:39:18 -03:00
|
|
|
if ((breaches & (AC_FENCE_TYPE_CIRCLE | AC_FENCE_TYPE_POLYGON)) != 0) {
|
2013-05-01 05:06:58 -03:00
|
|
|
mavlink_breach_type = FENCE_BREACH_BOUNDARY;
|
|
|
|
}
|
|
|
|
|
|
|
|
// send status
|
|
|
|
mavlink_msg_fence_status_send(chan,
|
|
|
|
(int8_t)(fence.get_breaches()!=0),
|
|
|
|
fence.get_breach_count(),
|
|
|
|
mavlink_breach_type,
|
|
|
|
fence.get_breach_time());
|
2013-04-26 06:51:07 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|