From 930f730612af51ffaa1824c724c4463d5516008b Mon Sep 17 00:00:00 2001 From: Paul Riseborough Date: Wed, 19 Aug 2015 12:32:09 +1000 Subject: [PATCH] AP_NavEKF: Reset mag and heading states to try and pass pre-flight checks --- libraries/AP_NavEKF/AP_NavEKF.cpp | 16 ++++++++++++++++ libraries/AP_NavEKF/AP_NavEKF.h | 2 ++ 2 files changed, 18 insertions(+) diff --git a/libraries/AP_NavEKF/AP_NavEKF.cpp b/libraries/AP_NavEKF/AP_NavEKF.cpp index cc9dea7369..dbc1bdae8e 100644 --- a/libraries/AP_NavEKF/AP_NavEKF.cpp +++ b/libraries/AP_NavEKF/AP_NavEKF.cpp @@ -4376,6 +4376,9 @@ void NavEKF::readMagData() // read compass data and scale to improve numerical conditioning magData = _ahrs->get_compass()->get_field_milligauss() * 0.001f; + // check for consistent data between magnetometers + consistentMagData = _ahrs->get_compass()->consistent(); + // get states stored at time closest to measurement time after allowance for measurement delay RecallStates(statesAtMagMeasTime, (imuSampleTime_ms - msecMagDelay)); @@ -4686,6 +4689,7 @@ void NavEKF::InitialiseVariables() ekfStartTime_ms = imuSampleTime_ms; lastGpsVelFail_ms = 0; lastGpsAidBadTime_ms = 0; + magYawResetTimer_ms = imuSampleTime_ms; // initialise other variables gpsNoiseScaler = 1.0f; @@ -5218,6 +5222,18 @@ bool NavEKF::calcGpsGoodToAlign(void) "GPS horiz error %.1f", (double)hAcc); } + // If we have good magnetometer consistency and bad innovations for longer than 5 seconds then we reset heading and field states + // This enables us to handle large changes to the external magnetic field environment that occur before arming + if ((magTestRatio.x <= 1.0f && magTestRatio.y <= 1.0f) || !consistentMagData) { + magYawResetTimer_ms = imuSampleTime_ms; + } + if (imuSampleTime_ms - magYawResetTimer_ms > 5000) { + // reset heading and field states + Vector3f eulerAngles; + getEulerAngles(eulerAngles); + state.quat = calcQuatAndFieldStates(eulerAngles.x, eulerAngles.y); + } + // fail if magnetometer innovations are outside limits indicating bad yaw // with bad yaw we are unable to use GPS bool yawFail; diff --git a/libraries/AP_NavEKF/AP_NavEKF.h b/libraries/AP_NavEKF/AP_NavEKF.h index 80728c9345..9866aea66f 100644 --- a/libraries/AP_NavEKF/AP_NavEKF.h +++ b/libraries/AP_NavEKF/AP_NavEKF.h @@ -685,6 +685,8 @@ private: bool useGpsVertVel; // true if GPS vertical velocity should be used float yawResetAngle; // Change in yaw angle due to last in-flight yaw reset in radians. A positive value means the yaw angle has increased. bool yawResetAngleWaiting; // true when the yaw reset angle has been updated and has not been retrieved via the getLastYawResetAngle() function + uint32_t magYawResetTimer_ms; // timer in msec used to track how long good magnetometer data is failing innovation consistency checks + bool consistentMagData; // true when the magnetometers are passing consistency checks // Used by smoothing of state corrections Vector10 gpsIncrStateDelta; // vector of corrections to attitude, velocity and position to be applied over the period between the current and next GPS measurement