2013-04-26 06:51:07 -03:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
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
|
|
|
{
|
|
|
|
uint8_t new_breaches; // the type of fence that has been breached
|
|
|
|
uint8_t orig_breaches = fence.get_breaches();
|
|
|
|
|
2013-05-01 05:06:58 -03:00
|
|
|
// give fence library our current distance from home in meters
|
|
|
|
fence.set_home_distance(home_distance*0.01f);
|
2013-04-26 06:51:07 -03:00
|
|
|
|
|
|
|
// check for a breach
|
2015-05-13 03:27:29 -03:00
|
|
|
new_breaches = fence.check_fence(current_loc.alt/100.0f);
|
2013-04-26 06:51:07 -03:00
|
|
|
|
2015-07-08 03:21:31 -03:00
|
|
|
// return immediately if motors are not armed
|
|
|
|
if(!motors.armed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-26 06:51:07 -03:00
|
|
|
// if there is a new breach take action
|
|
|
|
if( new_breaches != AC_FENCE_TYPE_NONE ) {
|
|
|
|
|
|
|
|
// 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
|
2015-01-21 06:57:02 -04:00
|
|
|
if (ap.land_complete || (mode_has_manual_throttle(control_mode) && 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) {
|
|
|
|
if (!set_mode(RTL)) {
|
2013-10-24 04:42:14 -03:00
|
|
|
set_mode(LAND);
|
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
|
|
|
|
set_mode(LAND);
|
2013-04-26 06:51:07 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// log an error in the dataflash
|
|
|
|
Log_Write_Error(ERROR_SUBSYSTEM_FAILSAFE_FENCE, new_breaches);
|
|
|
|
}
|
|
|
|
|
|
|
|
// record clearing of breach
|
|
|
|
if(orig_breaches != AC_FENCE_TYPE_NONE && fence.get_breaches() == AC_FENCE_TYPE_NONE) {
|
|
|
|
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;
|
|
|
|
}
|
2013-05-04 11:18:16 -03:00
|
|
|
if ((breaches & AC_FENCE_TYPE_CIRCLE) != 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
|