2015-05-29 23:12:49 -03:00
|
|
|
#include "Copter.h"
|
|
|
|
|
2014-07-21 07:06:20 -03:00
|
|
|
/**
|
|
|
|
*
|
2016-07-25 15:45:29 -03:00
|
|
|
* Detects failures of the ekf or inertial nav system triggers an alert
|
|
|
|
* to the pilot and helps take countermeasures
|
2014-07-21 07:06:20 -03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef EKF_CHECK_ITERATIONS_MAX
|
|
|
|
# define EKF_CHECK_ITERATIONS_MAX 10 // 1 second (ie. 10 iterations at 10hz) of bad variances signals a failure
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef EKF_CHECK_WARNING_TIME
|
|
|
|
# define EKF_CHECK_WARNING_TIME (30*1000) // warning text messages are sent to ground no more than every 30 seconds
|
|
|
|
#endif
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2019-03-25 20:57:55 -03:00
|
|
|
// EKF_check structure
|
2014-07-21 07:06:20 -03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
static struct {
|
2015-03-27 03:23:17 -03:00
|
|
|
uint8_t fail_count; // number of iterations ekf or dcm have been out of tolerances
|
|
|
|
uint8_t bad_variance : 1; // true if ekf should be considered untrusted (fail_count has exceeded EKF_CHECK_ITERATIONS_MAX)
|
2014-07-21 07:06:20 -03:00
|
|
|
uint32_t last_warn_time; // system time of last warning in milliseconds. Used to throttle text warnings sent to GCS
|
|
|
|
} ekf_check_state;
|
|
|
|
|
2015-03-27 03:23:17 -03:00
|
|
|
// ekf_check - detects if ekf variance are out of tolerance and triggers failsafe
|
2014-07-21 07:06:20 -03:00
|
|
|
// should be called at 10hz
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::ekf_check()
|
2014-07-21 07:06:20 -03:00
|
|
|
{
|
2015-07-16 02:52:17 -03:00
|
|
|
// exit immediately if ekf has no origin yet - this assumes the origin can never become unset
|
|
|
|
Location temp_loc;
|
2015-10-17 03:00:32 -03:00
|
|
|
if (!ahrs.get_origin(temp_loc)) {
|
2015-07-16 02:52:17 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-19 21:33:37 -03:00
|
|
|
// return immediately if motors are not armed, or ekf check is disabled
|
|
|
|
if (!motors->armed() || (g.fs_ekf_thresh <= 0.0f)) {
|
2015-03-27 03:23:17 -03:00
|
|
|
ekf_check_state.fail_count = 0;
|
|
|
|
ekf_check_state.bad_variance = false;
|
|
|
|
AP_Notify::flags.ekf_bad = ekf_check_state.bad_variance;
|
2014-07-21 07:06:20 -03:00
|
|
|
failsafe_ekf_off_event(); // clear failsafe
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-31 10:43:04 -03:00
|
|
|
// compare compass and velocity variance vs threshold
|
2018-04-13 00:16:25 -03:00
|
|
|
if (ekf_over_threshold()) {
|
2014-07-21 07:06:20 -03:00
|
|
|
// if compass is not yet flagged as bad
|
2015-03-27 03:23:17 -03:00
|
|
|
if (!ekf_check_state.bad_variance) {
|
2014-07-21 07:06:20 -03:00
|
|
|
// increase counter
|
2015-03-27 03:23:17 -03:00
|
|
|
ekf_check_state.fail_count++;
|
2014-07-21 07:06:20 -03:00
|
|
|
// if counter above max then trigger failsafe
|
2015-03-27 03:23:17 -03:00
|
|
|
if (ekf_check_state.fail_count >= EKF_CHECK_ITERATIONS_MAX) {
|
2014-07-21 07:06:20 -03:00
|
|
|
// limit count from climbing too high
|
2015-03-27 03:23:17 -03:00
|
|
|
ekf_check_state.fail_count = EKF_CHECK_ITERATIONS_MAX;
|
|
|
|
ekf_check_state.bad_variance = true;
|
2019-03-24 22:31:46 -03:00
|
|
|
AP::logger().Write_Error(LogErrorSubsystem::EKFCHECK, LogErrorCode::EKFCHECK_BAD_VARIANCE);
|
2014-07-21 07:06:20 -03:00
|
|
|
// send message to gcs
|
2015-11-19 23:04:45 -04:00
|
|
|
if ((AP_HAL::millis() - ekf_check_state.last_warn_time) > EKF_CHECK_WARNING_TIME) {
|
2017-07-08 21:56:49 -03:00
|
|
|
gcs().send_text(MAV_SEVERITY_CRITICAL,"EKF variance");
|
2015-11-19 23:04:45 -04:00
|
|
|
ekf_check_state.last_warn_time = AP_HAL::millis();
|
2014-07-21 07:06:20 -03:00
|
|
|
}
|
|
|
|
failsafe_ekf_event();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2014-10-27 00:37:56 -03:00
|
|
|
// reduce counter
|
2015-03-27 03:23:17 -03:00
|
|
|
if (ekf_check_state.fail_count > 0) {
|
|
|
|
ekf_check_state.fail_count--;
|
2014-10-27 00:37:56 -03:00
|
|
|
|
|
|
|
// if compass is flagged as bad and the counter reaches zero then clear flag
|
2015-03-27 03:23:17 -03:00
|
|
|
if (ekf_check_state.bad_variance && ekf_check_state.fail_count == 0) {
|
|
|
|
ekf_check_state.bad_variance = false;
|
2019-03-24 22:31:46 -03:00
|
|
|
AP::logger().Write_Error(LogErrorSubsystem::EKFCHECK, LogErrorCode::EKFCHECK_VARIANCE_CLEARED);
|
2014-07-21 07:06:20 -03:00
|
|
|
// clear failsafe
|
|
|
|
failsafe_ekf_off_event();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set AP_Notify flags
|
2015-03-27 03:23:17 -03:00
|
|
|
AP_Notify::flags.ekf_bad = ekf_check_state.bad_variance;
|
2014-07-21 07:06:20 -03:00
|
|
|
|
|
|
|
// To-Do: add ekf variances to extended status
|
2014-10-16 04:18:37 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ekf_over_threshold - returns true if the ekf's variance are over the tolerance
|
2015-05-29 23:12:49 -03:00
|
|
|
bool Copter::ekf_over_threshold()
|
2014-10-16 04:18:37 -03:00
|
|
|
{
|
|
|
|
// return false immediately if disabled
|
2015-06-09 22:16:52 -03:00
|
|
|
if (g.fs_ekf_thresh <= 0.0f) {
|
2014-10-16 04:18:37 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// use EKF to get variance
|
2017-07-07 05:17:40 -03:00
|
|
|
float position_variance, vel_variance, height_variance, tas_variance;
|
|
|
|
Vector3f mag_variance;
|
2014-10-16 04:18:37 -03:00
|
|
|
Vector2f offset;
|
2017-07-07 05:17:40 -03:00
|
|
|
ahrs.get_variances(vel_variance, position_variance, height_variance, mag_variance, tas_variance, offset);
|
2014-10-16 04:18:37 -03:00
|
|
|
|
2019-03-15 03:35:18 -03:00
|
|
|
// return true if two of compass, velocity and position variances are over the threshold OR velocity variance is twice the threshold
|
2017-07-07 05:17:40 -03:00
|
|
|
uint8_t over_thresh_count = 0;
|
|
|
|
if (mag_variance.length() >= g.fs_ekf_thresh) {
|
|
|
|
over_thresh_count++;
|
|
|
|
}
|
2019-03-13 05:21:14 -03:00
|
|
|
if (vel_variance >= (2.0f * g.fs_ekf_thresh)) {
|
2019-03-15 03:35:18 -03:00
|
|
|
over_thresh_count += 2;
|
2019-03-13 05:21:14 -03:00
|
|
|
} else if (vel_variance >= g.fs_ekf_thresh) {
|
2017-07-07 05:17:40 -03:00
|
|
|
over_thresh_count++;
|
|
|
|
}
|
|
|
|
if (position_variance >= g.fs_ekf_thresh) {
|
|
|
|
over_thresh_count++;
|
|
|
|
}
|
|
|
|
|
2018-04-13 00:16:25 -03:00
|
|
|
if (over_thresh_count >= 2) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// either optflow relative or absolute position estimate OK
|
|
|
|
if (optflow_position_ok() || ekf_position_ok()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2014-07-21 07:06:20 -03:00
|
|
|
}
|
|
|
|
|
2014-10-16 04:18:37 -03:00
|
|
|
|
2014-07-21 07:06:20 -03:00
|
|
|
// failsafe_ekf_event - perform ekf failsafe
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::failsafe_ekf_event()
|
2014-07-21 07:06:20 -03:00
|
|
|
{
|
2014-10-16 04:18:37 -03:00
|
|
|
// return immediately if ekf failsafe already triggered
|
|
|
|
if (failsafe.ekf) {
|
2014-07-21 07:06:20 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// EKF failsafe event has occurred
|
|
|
|
failsafe.ekf = true;
|
2019-03-24 22:31:46 -03:00
|
|
|
AP::logger().Write_Error(LogErrorSubsystem::FAILSAFE_EKFINAV, LogErrorCode::FAILSAFE_OCCURRED);
|
2014-07-21 07:06:20 -03:00
|
|
|
|
2018-04-20 22:07:43 -03:00
|
|
|
// sometimes LAND *does* require GPS so ensure we are in non-GPS land
|
|
|
|
if (control_mode == LAND && landing_with_GPS()) {
|
|
|
|
mode_land.do_not_use_GPS();
|
2018-04-13 00:16:25 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-20 22:07:43 -03:00
|
|
|
// does this mode require position?
|
|
|
|
if (!copter.flightmode->requires_GPS() && (g.fs_ekf_action != FS_EKF_ACTION_LAND_EVEN_STABILIZE)) {
|
2018-04-13 00:16:25 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-09 23:39:06 -03:00
|
|
|
// take action based on fs_ekf_action parameter
|
|
|
|
switch (g.fs_ekf_action) {
|
|
|
|
case FS_EKF_ACTION_ALTHOLD:
|
|
|
|
// AltHold
|
2016-01-25 19:40:41 -04:00
|
|
|
if (failsafe.radio || !set_mode(ALT_HOLD, MODE_REASON_EKF_FAILSAFE)) {
|
|
|
|
set_mode_land_with_pause(MODE_REASON_EKF_FAILSAFE);
|
2015-06-09 23:39:06 -03:00
|
|
|
}
|
|
|
|
break;
|
2018-04-13 00:16:25 -03:00
|
|
|
case FS_EKF_ACTION_LAND:
|
|
|
|
case FS_EKF_ACTION_LAND_EVEN_STABILIZE:
|
2015-06-09 23:39:06 -03:00
|
|
|
default:
|
2016-01-25 19:40:41 -04:00
|
|
|
set_mode_land_with_pause(MODE_REASON_EKF_FAILSAFE);
|
2015-06-09 23:39:06 -03:00
|
|
|
break;
|
2014-07-21 07:06:20 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// failsafe_ekf_off_event - actions to take when EKF failsafe is cleared
|
2015-05-29 23:12:49 -03:00
|
|
|
void Copter::failsafe_ekf_off_event(void)
|
2014-07-21 07:06:20 -03:00
|
|
|
{
|
|
|
|
// return immediately if not in ekf failsafe
|
|
|
|
if (!failsafe.ekf) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
failsafe.ekf = false;
|
2019-03-24 22:31:46 -03:00
|
|
|
AP::logger().Write_Error(LogErrorSubsystem::FAILSAFE_EKFINAV, LogErrorCode::FAILSAFE_RESOLVED);
|
2014-07-21 07:06:20 -03:00
|
|
|
}
|
2016-10-10 03:35:08 -03:00
|
|
|
|
|
|
|
// check for ekf yaw reset and adjust target heading, also log position reset
|
|
|
|
void Copter::check_ekf_reset()
|
|
|
|
{
|
|
|
|
// check for yaw reset
|
2018-01-06 02:46:50 -04:00
|
|
|
float yaw_angle_change_rad;
|
2016-10-10 03:35:08 -03:00
|
|
|
uint32_t new_ekfYawReset_ms = ahrs.getLastYawResetAngle(yaw_angle_change_rad);
|
|
|
|
if (new_ekfYawReset_ms != ekfYawReset_ms) {
|
2018-01-06 02:46:50 -04:00
|
|
|
attitude_control->inertial_frame_reset();
|
2016-10-10 03:35:08 -03:00
|
|
|
ekfYawReset_ms = new_ekfYawReset_ms;
|
|
|
|
Log_Write_Event(DATA_EKF_YAW_RESET);
|
|
|
|
}
|
|
|
|
|
2016-10-10 03:44:11 -03:00
|
|
|
#if AP_AHRS_NAVEKF_AVAILABLE
|
|
|
|
// check for change in primary EKF (log only, AC_WPNav handles position target adjustment)
|
2016-10-13 03:20:16 -03:00
|
|
|
if ((EKF2.getPrimaryCoreIndex() != ekf_primary_core) && (EKF2.getPrimaryCoreIndex() != -1)) {
|
2018-01-06 02:46:50 -04:00
|
|
|
attitude_control->inertial_frame_reset();
|
2016-10-10 03:44:11 -03:00
|
|
|
ekf_primary_core = EKF2.getPrimaryCoreIndex();
|
2019-03-24 22:31:46 -03:00
|
|
|
AP::logger().Write_Error(LogErrorSubsystem::EKF_PRIMARY, LogErrorCode(ekf_primary_core));
|
2018-02-01 08:58:34 -04:00
|
|
|
gcs().send_text(MAV_SEVERITY_WARNING, "EKF primary changed:%d", (unsigned)ekf_primary_core);
|
2016-10-10 03:44:11 -03:00
|
|
|
}
|
|
|
|
#endif
|
2016-10-10 03:35:08 -03:00
|
|
|
}
|