From b81a5b3efad6ba6a15dcc269e7537c16a6572c22 Mon Sep 17 00:00:00 2001 From: Daniel Agar Date: Tue, 8 Mar 2022 11:13:13 -0500 Subject: [PATCH] ekf2: request mag yaw reset after calibration or sensor change --- src/modules/ekf2/EKF/ekf.h | 3 ++- src/modules/ekf2/EKF/ekf_helper.cpp | 8 +++++++- src/modules/ekf2/EKF2.cpp | 4 ++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/modules/ekf2/EKF/ekf.h b/src/modules/ekf2/EKF/ekf.h index a0d39576b2d7..b74517f6bbdd 100644 --- a/src/modules/ekf2/EKF/ekf.h +++ b/src/modules/ekf2/EKF/ekf.h @@ -193,7 +193,8 @@ class Ekf final : public EstimatorInterface void resetAccelBias(); // Reset all magnetometer bias states and covariances to initial alignment values. - void resetMagBias(); + // Requests full mag yaw reset (if using mag) + void resetMagBiasAndYaw(); Vector3f getVelocityVariance() const { return P.slice<3, 3>(4, 4).diag(); }; diff --git a/src/modules/ekf2/EKF/ekf_helper.cpp b/src/modules/ekf2/EKF/ekf_helper.cpp index a731bc8047a9..5c54dc36c875 100644 --- a/src/modules/ekf2/EKF/ekf_helper.cpp +++ b/src/modules/ekf2/EKF/ekf_helper.cpp @@ -907,7 +907,7 @@ void Ekf::resetAccelBias() _prev_dvel_bias_var = P.slice<3, 3>(13, 13).diag(); } -void Ekf::resetMagBias() +void Ekf::resetMagBiasAndYaw() { // Zero the magnetometer bias states _state.mag_B.zero(); @@ -918,6 +918,12 @@ void Ekf::resetMagBias() // reset any saved covariance data for re-use when auto-switching between heading and 3-axis fusion _saved_mag_bf_variance.zero(); + + if (_control_status.flags.mag_hdg || _control_status.flags.mag_3D) { + _mag_yaw_reset_req = true; + } + + _control_status.flags.mag_fault = false; } // get EKF innovation consistency check status information comprising of: diff --git a/src/modules/ekf2/EKF2.cpp b/src/modules/ekf2/EKF2.cpp index e8f72196d149..9b3b40e3d2f3 100644 --- a/src/modules/ekf2/EKF2.cpp +++ b/src/modules/ekf2/EKF2.cpp @@ -1818,14 +1818,14 @@ void EKF2::UpdateMagSample(ekf2_timestamps_s &ekf2_timestamps) reset = true; - } else if (magnetometer.calibration_count > _mag_calibration_count) { + } else if (magnetometer.calibration_count != _mag_calibration_count) { // existing calibration has changed, reset saved mag bias PX4_DEBUG("%d - mag %" PRIu32 " calibration updated, resetting bias", _instance, _device_id_mag); reset = true; } if (reset) { - _ekf.resetMagBias(); + _ekf.resetMagBiasAndYaw(); _device_id_mag = magnetometer.device_id; _mag_calibration_count = magnetometer.calibration_count;