From 3d50ebba3b618787b7855d927cf88504ea11de3a Mon Sep 17 00:00:00 2001 From: Jacob Dahl <37091262+dakejahl@users.noreply.github.com> Date: Wed, 4 Nov 2026 09:22:55 -0900 Subject: [PATCH] fix(ekf2): don't reset yaw to an inaccurate GNSS heading (#28846) (#29534) Nothing checked the reported heading accuracy before GNSS yaw fusion started, and the reset always claimed the default heading noise, so a receiver still resolving its baseline could align yaw to a heading it reported with a 172 deg accuracy. (cherry picked from commit c7db242da471a79ab0fb4f355dc7571c35a0670e) Hand-ported: release/1.18 has no separate GNSS heading buffer (#27102), so the accuracy gate reads gnss_sample.yaw_acc, and the test sets it through the GPS simulator, which gains setYawAccuracy(). Assisted-by: Claude:claude-opus-5-5 --- .../EKF/aid_sources/gnss/gnss_yaw_control.cpp | 13 +++++++-- .../ekf2/test/sensor_simulator/gps.cpp | 5 ++++ src/modules/ekf2/test/sensor_simulator/gps.h | 1 + src/modules/ekf2/test/test_EKF_gnss_yaw.cpp | 28 +++++++++++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/modules/ekf2/EKF/aid_sources/gnss/gnss_yaw_control.cpp b/src/modules/ekf2/EKF/aid_sources/gnss/gnss_yaw_control.cpp index 709fb762117..cf34a736fc5 100644 --- a/src/modules/ekf2/EKF/aid_sources/gnss/gnss_yaw_control.cpp +++ b/src/modules/ekf2/EKF/aid_sources/gnss/gnss_yaw_control.cpp @@ -65,10 +65,17 @@ void Ekf::controlGnssYawFusion(const gnssSample &gnss_sample) const bool is_gnss_yaw_data_intermittent = !isNewestSampleRecent(_time_last_gnss_yaw_buffer_push, 2 * GNSS_YAW_MAX_INTERVAL); + // A receiver still resolving its baseline can report a heading with an accuracy of tens of degrees. Starting may + // reset yaw to it, so hold it to the bar the yaw estimator must meet before a reset. Not every receiver reports an + // accuracy. + const bool is_gnss_yaw_accurate = !PX4_ISFINITE(gnss_sample.yaw_acc) + || (gnss_sample.yaw_acc < _params.EKFGSF_yaw_err_max); + const bool starting_conditions_passing = continuing_conditions_passing && _gnss_checks.passed() && !is_gnss_yaw_data_intermittent - && !_gps_intermittent; + && !_gps_intermittent + && is_gnss_yaw_accurate; if (_control_status.flags.gnss_yaw) { if (continuing_conditions_passing) { @@ -225,7 +232,9 @@ bool Ekf::resetYawToGnss(const float gnss_yaw, const float gnss_yaw_offset) // GNSS yaw measurement is already compensated for antenna offset in the driver const float measured_yaw = gnss_yaw; - const float yaw_variance = sq(fmaxf(_params.gnss_heading_noise, 1.e-2f)); + // Take the variance updateGnssYaw() derived from this sample's reported accuracy, so the reset is no more confident + // than the measurement + const float yaw_variance = fmaxf(_aid_src_gnss_yaw.observation_variance, sq(1.e-2f)); resetQuatStateYaw(measured_yaw, yaw_variance); return true; diff --git a/src/modules/ekf2/test/sensor_simulator/gps.cpp b/src/modules/ekf2/test/sensor_simulator/gps.cpp index d75da46ba20..e65286671ae 100644 --- a/src/modules/ekf2/test/sensor_simulator/gps.cpp +++ b/src/modules/ekf2/test/sensor_simulator/gps.cpp @@ -65,6 +65,11 @@ void Gps::setYawOffset(const float yaw_offset) _gps_data.yaw_offset = yaw_offset; } +void Gps::setYawAccuracy(const float yaw_acc) +{ + _gps_data.yaw_acc = yaw_acc; +} + void Gps::setFixType(const int fix_type) { _gps_data.fix_type = fix_type; diff --git a/src/modules/ekf2/test/sensor_simulator/gps.h b/src/modules/ekf2/test/sensor_simulator/gps.h index 1c144f391bb..cf45c6a4531 100644 --- a/src/modules/ekf2/test/sensor_simulator/gps.h +++ b/src/modules/ekf2/test/sensor_simulator/gps.h @@ -61,6 +61,7 @@ public: void setVelocity(const Vector3f &vel); void setYaw(const float yaw); void setYawOffset(const float yaw); + void setYawAccuracy(const float yaw_acc); void setFixType(const int fix_type); void setNumberOfSatellites(const int num_satellites); void setPdop(const float pdop); diff --git a/src/modules/ekf2/test/test_EKF_gnss_yaw.cpp b/src/modules/ekf2/test/test_EKF_gnss_yaw.cpp index 665fa8e8c95..7831da7e581 100644 --- a/src/modules/ekf2/test/test_EKF_gnss_yaw.cpp +++ b/src/modules/ekf2/test/test_EKF_gnss_yaw.cpp @@ -341,3 +341,31 @@ TEST_F(EkfGpsHeadingTest, stopOnGround) // THEN: the yaw variance increases EXPECT_GT(_ekf->getYawVar(), yaw_variance_before); } + +TEST_F(EkfGpsHeadingTest, inaccurateHeadingDoesNotReset) +{ + // GIVEN: a receiver still resolving its baseline, reporting a heading 30 deg off with a 1 rad accuracy + const float gps_heading = matrix::wrap_pi(_ekf_wrapper.getYawAngle() + math::radians(30.f)); + _sensor_simulator._gps.setYaw(gps_heading); + _sensor_simulator._gps.setYawAccuracy(1.f); + const int initial_quat_reset_counter = _ekf_wrapper.getQuaternionResetCounter(); + _sensor_simulator.runSeconds(4); + + // THEN: GNSS yaw fusion doesn't start, so yaw isn't reset to it + EXPECT_FALSE(_ekf_wrapper.isIntendingGpsHeadingFusion()); + EXPECT_EQ(_ekf_wrapper.getQuaternionResetCounter(), initial_quat_reset_counter); + + // WHEN: the reported accuracy drops below 15 deg + const float yaw_acc = math::radians(10.f); + _sensor_simulator._gps.setYawAccuracy(yaw_acc); + + for (int i = 0; (i < 100) && (_ekf_wrapper.getQuaternionResetCounter() == initial_quat_reset_counter); i++) { + _sensor_simulator.runMicroseconds(10000); + } + + // THEN: fusion starts with a reset to the heading, as uncertain as the reported accuracy + EXPECT_TRUE(_ekf_wrapper.isIntendingGpsHeadingFusion()); + EXPECT_EQ(_ekf_wrapper.getQuaternionResetCounter(), initial_quat_reset_counter + 1); + checkConvergence(gps_heading, 0.5f); + EXPECT_NEAR(_ekf->getYawVar(), yaw_acc * yaw_acc, 0.1f * yaw_acc * yaw_acc); +}