From 6cdd21a369ae77c673cd6ad4995dddb8ccc8f411 Mon Sep 17 00:00:00 2001 From: bresch Date: Tue, 24 Nov 2026 16:47:13 +0100 Subject: [PATCH] fix(ekf2): delay the zero gyro update by one window A window is fused once the next one has also completed at rest, so the start of a rotation that the at-rest status reports late is not learned as gyro bias. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: bresch --- .../ekf2/EKF/aid_sources/ZeroGyroUpdate.cpp | 26 +++++++++++++------ .../ekf2/EKF/aid_sources/ZeroGyroUpdate.hpp | 3 +++ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/modules/ekf2/EKF/aid_sources/ZeroGyroUpdate.cpp b/src/modules/ekf2/EKF/aid_sources/ZeroGyroUpdate.cpp index c4e3d659cbd..b0ff5fc553b 100644 --- a/src/modules/ekf2/EKF/aid_sources/ZeroGyroUpdate.cpp +++ b/src/modules/ekf2/EKF/aid_sources/ZeroGyroUpdate.cpp @@ -44,6 +44,7 @@ void ZeroGyroUpdate::reset() { _zgup_delta_ang.setZero(); _zgup_delta_ang_dt = 0.f; + _zgup_rate_pending_valid = false; } bool ZeroGyroUpdate::update(Ekf &ekf, const estimator::imuSample &imu_delayed) @@ -58,9 +59,24 @@ bool ZeroGyroUpdate::update(Ekf &ekf, const estimator::imuSample &imu_delayed) const bool zero_gyro_update_data_ready = _zgup_delta_ang_dt >= zgup_dt; if (zero_gyro_update_data_ready) { + // The at-rest status lags the start of a rotation: a rate is fused once the + // vehicle has stayed at rest for one more window + const Vector3f rate = _zgup_rate_pending; + const bool rate_valid = _zgup_rate_pending_valid; + + _zgup_rate_pending = _zgup_delta_ang / _zgup_delta_ang_dt; + _zgup_rate_pending_valid = true; + + // Reset the integrators + _zgup_delta_ang.setZero(); + _zgup_delta_ang_dt = 0.f; + + if (!rate_valid) { + return false; + } const Vector3f earth_rate_body = ekf.state().quat_nominal.rotateVectorInverse(ekf.getEarthRateNED()); - const Vector3f gyro_bias = _zgup_delta_ang / _zgup_delta_ang_dt - earth_rate_body; + const Vector3f gyro_bias = rate - earth_rate_body; const float obs_var = sq(math::constrain(ekf.getGyroNoise(), 0.f, 1.f)); @@ -74,17 +90,11 @@ bool ZeroGyroUpdate::update(Ekf &ekf, const estimator::imuSample &imu_delayed) ekf.applyStateCorrection(state_correction); - // Reset the integrators - _zgup_delta_ang.setZero(); - _zgup_delta_ang_dt = 0.f; - return true; } } else if (ekf.control_status_prev_flags().vehicle_at_rest) { - // Reset the integrators - _zgup_delta_ang.setZero(); - _zgup_delta_ang_dt = 0.f; + reset(); } return false; diff --git a/src/modules/ekf2/EKF/aid_sources/ZeroGyroUpdate.hpp b/src/modules/ekf2/EKF/aid_sources/ZeroGyroUpdate.hpp index 1ad488e539a..4abf918775a 100644 --- a/src/modules/ekf2/EKF/aid_sources/ZeroGyroUpdate.hpp +++ b/src/modules/ekf2/EKF/aid_sources/ZeroGyroUpdate.hpp @@ -49,6 +49,9 @@ private: matrix::Vector3f _zgup_delta_ang{}; float _zgup_delta_ang_dt{0.f}; + + matrix::Vector3f _zgup_rate_pending{}; + bool _zgup_rate_pending_valid{false}; }; #endif // !EKF_ZERO_GYRO_UPDATE_HPP