2011-12-15 03:09:29 -04:00
|
|
|
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
/*
|
2012-08-16 21:50:15 -03:00
|
|
|
* geo-fencing support
|
|
|
|
* Andrew Tridgell, December 2011
|
2011-12-15 03:09:29 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#if GEOFENCE_ENABLED == ENABLED
|
2014-03-25 22:42:58 -03:00
|
|
|
|
2014-03-26 20:40:00 -03:00
|
|
|
#define MIN_GEOFENCE_POINTS 5 // 3 to define a minimal polygon (triangle)
|
|
|
|
// + 1 for return point and +1 for last
|
|
|
|
// pt (same as first)
|
2014-03-25 22:42:58 -03:00
|
|
|
|
2011-12-15 03:09:29 -04:00
|
|
|
/*
|
2012-08-16 21:50:15 -03:00
|
|
|
* The state of geo-fencing. This structure is dynamically allocated
|
|
|
|
* the first time it is used. This means we only pay for the pointer
|
|
|
|
* and not the structure on systems where geo-fencing is not being
|
|
|
|
* used.
|
|
|
|
*
|
|
|
|
* We store a copy of the boundary in memory as we need to access it
|
|
|
|
* very quickly at runtime
|
2011-12-15 03:09:29 -04:00
|
|
|
*/
|
2013-11-22 18:40:19 -04:00
|
|
|
static struct GeofenceState {
|
2011-12-15 03:09:29 -04:00
|
|
|
uint8_t num_points;
|
|
|
|
bool boundary_uptodate;
|
|
|
|
bool fence_triggered;
|
2014-02-13 18:49:42 -04:00
|
|
|
bool is_pwm_enabled; //true if above FENCE_ENABLE_PWM threshold
|
|
|
|
bool previous_is_pwm_enabled; //true if above FENCE_ENALBE_PWM threshold
|
|
|
|
// last time we checked
|
|
|
|
bool is_enabled;
|
|
|
|
GeofenceEnableReason enable_reason;
|
2011-12-15 21:41:11 -04:00
|
|
|
uint16_t breach_count;
|
|
|
|
uint8_t breach_type;
|
|
|
|
uint32_t breach_time;
|
2012-12-04 18:22:21 -04:00
|
|
|
uint8_t old_switch_position;
|
2014-03-26 20:41:24 -03:00
|
|
|
int32_t guided_lat;
|
|
|
|
int32_t guided_lng;
|
2011-12-15 03:09:29 -04:00
|
|
|
/* point 0 is the return point */
|
2011-12-15 22:48:15 -04:00
|
|
|
Vector2l boundary[MAX_FENCEPOINTS];
|
2011-12-15 03:09:29 -04:00
|
|
|
} *geofence_state;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-08-16 21:50:15 -03:00
|
|
|
* fence boundaries fetch/store
|
2011-12-15 03:09:29 -04:00
|
|
|
*/
|
2011-12-15 22:48:15 -04:00
|
|
|
static Vector2l get_fence_point_with_index(unsigned i)
|
2011-12-15 03:09:29 -04:00
|
|
|
{
|
2012-12-12 17:58:28 -04:00
|
|
|
uint16_t mem;
|
2011-12-15 22:48:15 -04:00
|
|
|
Vector2l ret;
|
2011-12-15 03:09:29 -04:00
|
|
|
|
2011-12-15 21:41:11 -04:00
|
|
|
if (i > (unsigned)g.fence_total) {
|
2011-12-15 22:48:15 -04:00
|
|
|
return Vector2l(0,0);
|
2011-12-15 03:09:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// read fence point
|
|
|
|
mem = FENCE_START_BYTE + (i * FENCE_WP_SIZE);
|
2012-12-12 17:58:28 -04:00
|
|
|
ret.x = hal.storage->read_dword(mem);
|
2011-12-15 22:48:15 -04:00
|
|
|
mem += sizeof(uint32_t);
|
2012-12-12 17:58:28 -04:00
|
|
|
ret.y = hal.storage->read_dword(mem);
|
2011-12-15 03:09:29 -04:00
|
|
|
|
2011-12-15 21:41:11 -04:00
|
|
|
return ret;
|
2011-12-15 03:09:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// save a fence point
|
2011-12-15 22:48:15 -04:00
|
|
|
static void set_fence_point_with_index(Vector2l &point, unsigned i)
|
2011-12-15 03:09:29 -04:00
|
|
|
{
|
2012-12-12 17:58:28 -04:00
|
|
|
uint16_t mem;
|
2011-12-15 03:09:29 -04:00
|
|
|
|
2011-12-15 21:41:11 -04:00
|
|
|
if (i >= (unsigned)g.fence_total.get()) {
|
2011-12-15 03:09:29 -04:00
|
|
|
// not allowed
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mem = FENCE_START_BYTE + (i * FENCE_WP_SIZE);
|
|
|
|
|
2012-12-12 17:58:28 -04:00
|
|
|
hal.storage->write_dword(mem, point.x);
|
2011-12-15 22:48:15 -04:00
|
|
|
mem += sizeof(uint32_t);
|
2012-12-12 17:58:28 -04:00
|
|
|
hal.storage->write_dword(mem, point.y);
|
2011-12-15 03:09:29 -04:00
|
|
|
|
|
|
|
if (geofence_state != NULL) {
|
|
|
|
geofence_state->boundary_uptodate = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-08-16 21:50:15 -03:00
|
|
|
* allocate and fill the geofence state structure
|
2011-12-15 03:09:29 -04:00
|
|
|
*/
|
|
|
|
static void geofence_load(void)
|
|
|
|
{
|
|
|
|
uint8_t i;
|
|
|
|
|
|
|
|
if (geofence_state == NULL) {
|
2013-12-27 23:51:37 -04:00
|
|
|
if (hal.util->available_memory() < 512 + sizeof(struct GeofenceState)) {
|
2011-12-15 03:09:29 -04:00
|
|
|
// too risky to enable as we could run out of stack
|
|
|
|
goto failed;
|
|
|
|
}
|
2013-11-22 18:40:19 -04:00
|
|
|
geofence_state = (struct GeofenceState *)calloc(1, sizeof(struct GeofenceState));
|
2011-12-15 03:09:29 -04:00
|
|
|
if (geofence_state == NULL) {
|
|
|
|
// not much we can do here except disable it
|
|
|
|
goto failed;
|
|
|
|
}
|
2013-12-16 23:13:32 -04:00
|
|
|
geofence_state->old_switch_position = 254;
|
2011-12-15 03:09:29 -04:00
|
|
|
}
|
|
|
|
|
2012-08-27 22:59:28 -03:00
|
|
|
if (g.fence_total <= 0) {
|
2012-08-24 01:56:28 -03:00
|
|
|
g.fence_total.set(0);
|
2012-08-27 22:59:28 -03:00
|
|
|
return;
|
2012-08-24 01:56:28 -03:00
|
|
|
}
|
|
|
|
|
2011-12-15 03:09:29 -04:00
|
|
|
for (i=0; i<g.fence_total; i++) {
|
|
|
|
geofence_state->boundary[i] = get_fence_point_with_index(i);
|
|
|
|
}
|
|
|
|
geofence_state->num_points = i;
|
|
|
|
|
|
|
|
if (!Polygon_complete(&geofence_state->boundary[1], geofence_state->num_points-1)) {
|
|
|
|
// first point and last point must be the same
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
if (Polygon_outside(geofence_state->boundary[0], &geofence_state->boundary[1], geofence_state->num_points-1)) {
|
|
|
|
// return point needs to be inside the fence
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
geofence_state->boundary_uptodate = true;
|
|
|
|
geofence_state->fence_triggered = false;
|
|
|
|
|
|
|
|
gcs_send_text_P(SEVERITY_LOW,PSTR("geo-fence loaded"));
|
2011-12-18 20:24:55 -04:00
|
|
|
gcs_send_message(MSG_FENCE_STATUS);
|
2011-12-15 03:09:29 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
g.fence_action.set(FENCE_ACTION_NONE);
|
|
|
|
gcs_send_text_P(SEVERITY_HIGH,PSTR("geo-fence setup error"));
|
|
|
|
}
|
|
|
|
|
2014-02-13 18:49:42 -04:00
|
|
|
/*
|
2014-03-25 22:42:58 -03:00
|
|
|
* return true if a geo-fence has been uploaded and
|
|
|
|
* FENCE_ACTION is 1 (not necessarily enabled)
|
2014-02-13 18:49:42 -04:00
|
|
|
*/
|
|
|
|
static bool geofence_present(void)
|
|
|
|
{
|
|
|
|
//require at least a return point and a triangle
|
|
|
|
//to define a geofence area:
|
2014-04-07 04:20:22 -03:00
|
|
|
if (g.fence_action == FENCE_ACTION_NONE || g.fence_total < MIN_GEOFENCE_POINTS) {
|
2014-02-13 18:49:42 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-07 04:20:22 -03:00
|
|
|
/*
|
|
|
|
check FENCE_CHANNEL and update the is_pwm_enabled state
|
|
|
|
*/
|
|
|
|
static void geofence_update_pwm_enabled_state()
|
|
|
|
{
|
|
|
|
bool is_pwm_enabled;
|
|
|
|
if (g.fence_channel == 0) {
|
|
|
|
is_pwm_enabled = false;
|
|
|
|
} else {
|
|
|
|
is_pwm_enabled = (hal.rcin->read(g.fence_channel-1) > FENCE_ENABLE_PWM);
|
|
|
|
}
|
|
|
|
if (is_pwm_enabled && geofence_state == NULL) {
|
|
|
|
// we need to load the fence
|
|
|
|
geofence_load();
|
2014-02-13 18:49:42 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-07 04:20:22 -03:00
|
|
|
if (geofence_state == NULL) {
|
|
|
|
// not loaded
|
|
|
|
return;
|
2014-02-13 18:49:42 -04:00
|
|
|
}
|
|
|
|
|
2014-04-07 04:20:22 -03:00
|
|
|
geofence_state->previous_is_pwm_enabled = geofence_state->is_pwm_enabled;
|
|
|
|
geofence_state->is_pwm_enabled = is_pwm_enabled;
|
2014-02-13 18:49:42 -04:00
|
|
|
|
2014-04-07 04:20:22 -03:00
|
|
|
if (geofence_state->is_pwm_enabled != geofence_state->previous_is_pwm_enabled) {
|
|
|
|
geofence_set_enabled(geofence_state->is_pwm_enabled, PWM_TOGGLED);
|
2014-02-13 18:49:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//return true on success, false on failure
|
2014-04-07 04:20:22 -03:00
|
|
|
static bool geofence_set_enabled(bool enable, GeofenceEnableReason r)
|
|
|
|
{
|
|
|
|
if (geofence_state == NULL && enable) {
|
|
|
|
geofence_load();
|
|
|
|
}
|
2014-02-13 18:49:42 -04:00
|
|
|
if (geofence_state == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
geofence_state->is_enabled = enable;
|
|
|
|
geofence_state->enable_reason = r;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-12-15 03:09:29 -04:00
|
|
|
/*
|
2012-08-16 21:50:15 -03:00
|
|
|
* return true if geo-fencing is enabled
|
2011-12-15 03:09:29 -04:00
|
|
|
*/
|
|
|
|
static bool geofence_enabled(void)
|
|
|
|
{
|
2014-02-13 18:49:42 -04:00
|
|
|
geofence_update_pwm_enabled_state();
|
|
|
|
|
2014-04-07 04:20:22 -03:00
|
|
|
if (geofence_state == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-12-15 03:09:29 -04:00
|
|
|
if (g.fence_action == FENCE_ACTION_NONE ||
|
2014-02-13 18:49:42 -04:00
|
|
|
!geofence_present() ||
|
2014-04-07 04:20:22 -03:00
|
|
|
(g.fence_action != FENCE_ACTION_REPORT && !geofence_state->is_enabled)) {
|
2011-12-15 03:09:29 -04:00
|
|
|
// geo-fencing is disabled
|
2014-04-07 04:20:22 -03:00
|
|
|
// re-arm for when the channel trigger is switched on
|
|
|
|
geofence_state->fence_triggered = false;
|
2011-12-15 03:09:29 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-15 21:41:11 -04:00
|
|
|
/*
|
2012-08-16 21:50:15 -03:00
|
|
|
* return true if we have breached the geo-fence minimum altiude
|
2011-12-15 21:41:11 -04:00
|
|
|
*/
|
|
|
|
static bool geofence_check_minalt(void)
|
|
|
|
{
|
|
|
|
if (g.fence_maxalt <= g.fence_minalt) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (g.fence_minalt == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-01-03 07:33:40 -04:00
|
|
|
return (adjusted_altitude_cm() < (g.fence_minalt*100.0) + home.alt);
|
2011-12-15 21:41:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-08-16 21:50:15 -03:00
|
|
|
* return true if we have breached the geo-fence maximum altiude
|
2011-12-15 21:41:11 -04:00
|
|
|
*/
|
|
|
|
static bool geofence_check_maxalt(void)
|
|
|
|
{
|
|
|
|
if (g.fence_maxalt <= g.fence_minalt) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (g.fence_maxalt == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-01-03 07:33:40 -04:00
|
|
|
return (adjusted_altitude_cm() > (g.fence_maxalt*100.0) + home.alt);
|
2011-12-15 21:41:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-15 03:09:29 -04:00
|
|
|
/*
|
2012-08-16 21:50:15 -03:00
|
|
|
* check if we have breached the geo-fence
|
2011-12-15 03:09:29 -04:00
|
|
|
*/
|
2011-12-15 21:41:11 -04:00
|
|
|
static void geofence_check(bool altitude_check_only)
|
2011-12-15 03:09:29 -04:00
|
|
|
{
|
|
|
|
if (!geofence_enabled()) {
|
2011-12-17 00:15:41 -04:00
|
|
|
// switch back to the chosen control mode if still in
|
|
|
|
// GUIDED to the return point
|
|
|
|
if (geofence_state != NULL &&
|
2013-09-07 18:31:10 -03:00
|
|
|
(g.fence_action == FENCE_ACTION_GUIDED || g.fence_action == FENCE_ACTION_GUIDED_THR_PASS) &&
|
2011-12-17 00:15:41 -04:00
|
|
|
control_mode == GUIDED &&
|
2014-02-13 18:49:42 -04:00
|
|
|
geofence_present() &&
|
2011-12-17 00:15:41 -04:00
|
|
|
geofence_state->boundary_uptodate &&
|
|
|
|
geofence_state->old_switch_position == oldSwitchPosition &&
|
2014-03-26 20:41:24 -03:00
|
|
|
guided_WP_loc.lat == geofence_state->guided_lat &&
|
|
|
|
guided_WP_loc.lng == geofence_state->guided_lng) {
|
2013-12-16 23:13:32 -04:00
|
|
|
geofence_state->old_switch_position = 254;
|
2014-02-13 18:49:42 -04:00
|
|
|
set_mode(get_previous_mode());
|
2011-12-17 00:15:41 -04:00
|
|
|
}
|
2011-12-15 03:09:29 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate the geo-fence state if need be */
|
|
|
|
if (geofence_state == NULL || !geofence_state->boundary_uptodate) {
|
|
|
|
geofence_load();
|
2011-12-15 21:41:11 -04:00
|
|
|
if (!geofence_enabled()) {
|
2011-12-15 03:09:29 -04:00
|
|
|
// may have been disabled by load
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool outside = false;
|
2011-12-15 21:41:11 -04:00
|
|
|
uint8_t breach_type = FENCE_BREACH_NONE;
|
2012-08-15 01:31:10 -03:00
|
|
|
struct Location loc;
|
2011-12-15 03:09:29 -04:00
|
|
|
|
2011-12-15 21:41:11 -04:00
|
|
|
if (geofence_check_minalt()) {
|
|
|
|
outside = true;
|
|
|
|
breach_type = FENCE_BREACH_MINALT;
|
|
|
|
} else if (geofence_check_maxalt()) {
|
2011-12-15 03:09:29 -04:00
|
|
|
outside = true;
|
2011-12-15 21:41:11 -04:00
|
|
|
breach_type = FENCE_BREACH_MAXALT;
|
2014-01-02 07:06:25 -04:00
|
|
|
} else if (!altitude_check_only && ahrs.get_position(loc)) {
|
2011-12-15 22:48:15 -04:00
|
|
|
Vector2l location;
|
2012-08-15 01:31:10 -03:00
|
|
|
location.x = loc.lat;
|
|
|
|
location.y = loc.lng;
|
2011-12-15 03:09:29 -04:00
|
|
|
outside = Polygon_outside(location, &geofence_state->boundary[1], geofence_state->num_points-1);
|
2011-12-15 21:41:11 -04:00
|
|
|
if (outside) {
|
|
|
|
breach_type = FENCE_BREACH_BOUNDARY;
|
|
|
|
}
|
2011-12-15 03:09:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!outside) {
|
2011-12-15 21:41:11 -04:00
|
|
|
if (geofence_state->fence_triggered && !altitude_check_only) {
|
2011-12-15 03:09:29 -04:00
|
|
|
// we have moved back inside the fence
|
|
|
|
geofence_state->fence_triggered = false;
|
2011-12-15 21:41:11 -04:00
|
|
|
gcs_send_text_P(SEVERITY_LOW,PSTR("geo-fence OK"));
|
2012-08-16 21:50:15 -03:00
|
|
|
#if FENCE_TRIGGERED_PIN > 0
|
2014-06-01 20:28:27 -03:00
|
|
|
hal.gpio->pinMode(FENCE_TRIGGERED_PIN, HAL_GPIO_OUTPUT);
|
|
|
|
hal.gpio->write(FENCE_TRIGGERED_PIN, 0);
|
2012-08-16 21:50:15 -03:00
|
|
|
#endif
|
2011-12-18 20:24:55 -04:00
|
|
|
gcs_send_message(MSG_FENCE_STATUS);
|
2011-12-15 03:09:29 -04:00
|
|
|
}
|
|
|
|
// we're inside, all is good with the world
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we are outside the fence
|
2012-08-16 21:50:15 -03:00
|
|
|
if (geofence_state->fence_triggered &&
|
2012-08-14 22:17:38 -03:00
|
|
|
(control_mode == GUIDED || g.fence_action == FENCE_ACTION_REPORT)) {
|
2011-12-15 03:09:29 -04:00
|
|
|
// we have already triggered, don't trigger again until the
|
|
|
|
// user disables/re-enables using the fence channel switch
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we are outside, and have not previously triggered.
|
|
|
|
geofence_state->fence_triggered = true;
|
2011-12-15 21:41:11 -04:00
|
|
|
geofence_state->breach_count++;
|
|
|
|
geofence_state->breach_time = millis();
|
|
|
|
geofence_state->breach_type = breach_type;
|
2011-12-16 15:28:25 -04:00
|
|
|
|
2012-08-16 21:50:15 -03:00
|
|
|
#if FENCE_TRIGGERED_PIN > 0
|
2014-06-01 20:28:27 -03:00
|
|
|
hal.gpio->pinMode(FENCE_TRIGGERED_PIN, HAL_GPIO_OUTPUT);
|
|
|
|
hal.gpio->write(FENCE_TRIGGERED_PIN, 1);
|
2012-08-16 21:50:15 -03:00
|
|
|
#endif
|
2011-12-16 15:28:25 -04:00
|
|
|
|
2011-12-15 03:09:29 -04:00
|
|
|
gcs_send_text_P(SEVERITY_LOW,PSTR("geo-fence triggered"));
|
2011-12-18 20:24:55 -04:00
|
|
|
gcs_send_message(MSG_FENCE_STATUS);
|
2011-12-15 03:09:29 -04:00
|
|
|
|
|
|
|
// see what action the user wants
|
|
|
|
switch (g.fence_action) {
|
2012-08-14 22:17:38 -03:00
|
|
|
case FENCE_ACTION_REPORT:
|
|
|
|
break;
|
|
|
|
|
2011-12-15 03:09:29 -04:00
|
|
|
case FENCE_ACTION_GUIDED:
|
2013-09-07 18:31:10 -03:00
|
|
|
case FENCE_ACTION_GUIDED_THR_PASS:
|
2014-02-13 18:49:42 -04:00
|
|
|
if (g.fence_ret_rally != 0) { //return to a rally point
|
2014-04-18 16:19:13 -03:00
|
|
|
guided_WP_loc = rally.calc_best_rally_or_home_location(current_loc, read_alt_to_hold());
|
2014-02-13 18:49:42 -04:00
|
|
|
|
|
|
|
} else { //return to fence return point, not a rally point
|
|
|
|
if (g.fence_retalt > 0) {
|
|
|
|
//fly to the return point using fence_retalt
|
|
|
|
guided_WP_loc.alt = home.alt + 100.0*g.fence_retalt;
|
|
|
|
} else if (g.fence_minalt >= g.fence_maxalt) {
|
|
|
|
// invalid min/max, use RTL_altitude
|
|
|
|
guided_WP_loc.alt = home.alt + g.RTL_altitude_cm;
|
|
|
|
} else {
|
|
|
|
// fly to the return point, with an altitude half way between
|
|
|
|
// min and max
|
|
|
|
guided_WP_loc.alt = home.alt + 100.0*(g.fence_minalt + g.fence_maxalt)/2;
|
|
|
|
}
|
|
|
|
guided_WP_loc.options = 0;
|
|
|
|
guided_WP_loc.lat = geofence_state->boundary[0].x;
|
|
|
|
guided_WP_loc.lng = geofence_state->boundary[0].y;
|
2011-12-15 03:09:29 -04:00
|
|
|
}
|
2014-03-26 20:41:24 -03:00
|
|
|
geofence_state->guided_lat = guided_WP_loc.lat;
|
|
|
|
geofence_state->guided_lng = guided_WP_loc.lng;
|
2011-12-17 00:15:41 -04:00
|
|
|
geofence_state->old_switch_position = oldSwitchPosition;
|
|
|
|
|
2013-12-11 10:34:27 -04:00
|
|
|
set_guided_WP();
|
|
|
|
|
2014-03-28 23:53:19 -03:00
|
|
|
// make sure we don't auto trim the surfaces on this mode change
|
|
|
|
int8_t saved_auto_trim = g.auto_trim;
|
|
|
|
g.auto_trim.set(0);
|
2011-12-15 08:42:54 -04:00
|
|
|
|
2011-12-15 03:09:29 -04:00
|
|
|
set_mode(GUIDED);
|
2014-03-28 23:53:19 -03:00
|
|
|
g.auto_trim.set(saved_auto_trim);
|
|
|
|
|
2013-09-07 18:31:10 -03:00
|
|
|
if (g.fence_action == FENCE_ACTION_GUIDED_THR_PASS) {
|
|
|
|
guided_throttle_passthru = true;
|
|
|
|
}
|
2011-12-15 03:09:29 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-08-16 21:50:15 -03:00
|
|
|
* return true if geofencing allows stick mixing. When we have
|
|
|
|
* triggered failsafe and are in GUIDED mode then stick mixing is
|
|
|
|
* disabled. Otherwise the aircraft may not be able to recover from
|
|
|
|
* a breach of the geo-fence
|
2011-12-15 03:09:29 -04:00
|
|
|
*/
|
|
|
|
static bool geofence_stickmixing(void) {
|
|
|
|
if (geofence_enabled() &&
|
|
|
|
geofence_state != NULL &&
|
|
|
|
geofence_state->fence_triggered &&
|
|
|
|
control_mode == GUIDED) {
|
|
|
|
// don't mix in user input
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// normal mixing rules
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-12-15 21:41:11 -04:00
|
|
|
/*
|
2012-08-16 21:50:15 -03:00
|
|
|
*
|
2011-12-15 21:41:11 -04:00
|
|
|
*/
|
|
|
|
static void geofence_send_status(mavlink_channel_t chan)
|
|
|
|
{
|
|
|
|
if (geofence_enabled() && geofence_state != NULL) {
|
|
|
|
mavlink_msg_fence_status_send(chan,
|
|
|
|
(int8_t)geofence_state->fence_triggered,
|
|
|
|
geofence_state->breach_count,
|
|
|
|
geofence_state->breach_type,
|
|
|
|
geofence_state->breach_time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-14 03:54:56 -03:00
|
|
|
// public function for use in failsafe modules
|
2012-08-16 21:50:15 -03:00
|
|
|
bool geofence_breached(void)
|
|
|
|
{
|
|
|
|
return geofence_state ? geofence_state->fence_triggered : false;
|
2012-08-14 03:54:56 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-15 03:09:29 -04:00
|
|
|
#else // GEOFENCE_ENABLED
|
|
|
|
|
2012-08-16 21:50:15 -03:00
|
|
|
static void geofence_check(bool altitude_check_only) {
|
|
|
|
}
|
|
|
|
static bool geofence_stickmixing(void) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
static bool geofence_enabled(void) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-12-15 03:09:29 -04:00
|
|
|
|
2014-02-13 18:49:42 -04:00
|
|
|
static bool geofence_present(void) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool geofence_set_enabled(bool enable, GeofenceEnableReason r) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool geofence_breached(void) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-12-15 03:09:29 -04:00
|
|
|
#endif // GEOFENCE_ENABLED
|