From b408535e76fee409e9fb39f040c33d9faaedc308 Mon Sep 17 00:00:00 2001 From: bresch Date: Mon, 16 Nov 2026 16:51:23 +0100 Subject: [PATCH] fix(ekf2): remove the manual heading expiry The manual heading flag expired after 30 s of heading fusion to allow heading resets to the mag again. In flight those resets are gone, so the flag now stays set and keeps protecting the heading on the ground. Assisted-by: Claude:claude-fable-5-2 Signed-off-by: bresch --- src/modules/ekf2/EKF/control.cpp | 27 ------------- src/modules/ekf2/EKF/ekf.cpp | 1 - src/modules/ekf2/EKF/ekf.h | 3 -- src/modules/ekf2/test/test_EKF_mag.cpp | 56 +++++--------------------- 4 files changed, 9 insertions(+), 78 deletions(-) diff --git a/src/modules/ekf2/EKF/control.cpp b/src/modules/ekf2/EKF/control.cpp index b1c35f81894..cb238138758 100644 --- a/src/modules/ekf2/EKF/control.cpp +++ b/src/modules/ekf2/EKF/control.cpp @@ -177,31 +177,4 @@ void Ekf::controlFusionModes(const imuSample &imu_delayed) // North/East aiding only constrains the heading when the vehicle accelerates horizontally, never at rest const bool ne_aiding_heading = isNorthEastAidingActive() && !_control_status.flags.vehicle_at_rest; _control_status.flags.heading_observable = ne_aiding_heading || yaw_aiding; - - updateYawManualValidity(); -} - -void Ekf::updateYawManualValidity() -{ - const bool heading_observation_fusing = !isTimedOut(_time_last_heading_fuse, _params.no_aid_timeout_max); - - if (!_control_status.flags.yaw_manual || !heading_observation_fusing) { - _time_heading_fusion_start = 0; - return; - } - - if (_time_heading_fusion_start == 0) { - _time_heading_fusion_start = _time_delayed_us; - return; - } - - // A manually set heading is protected from automatic heading resets, but only until the - // heading aiding sources have had the opportunity to drive the heading themselves. After - // that the estimate is aiding derived and the manual value is stale, so keeping the - // protection would only block recovery resets. - static constexpr uint64_t kHeadingFusionTimeToClearYawManual = 30'000'000; - - if ((_time_delayed_us - _time_heading_fusion_start) > kHeadingFusionTimeToClearYawManual) { - _control_status.flags.yaw_manual = false; - } } diff --git a/src/modules/ekf2/EKF/ekf.cpp b/src/modules/ekf2/EKF/ekf.cpp index fecfcb6d8c3..ee51c629b5f 100644 --- a/src/modules/ekf2/EKF/ekf.cpp +++ b/src/modules/ekf2/EKF/ekf.cpp @@ -112,7 +112,6 @@ void Ekf::reset() _time_last_ver_vel_fuse = 0; _time_last_heading_fuse = 0; _time_last_terrain_fuse = 0; - _time_heading_fusion_start = 0; _last_known_gpos.setZero(); diff --git a/src/modules/ekf2/EKF/ekf.h b/src/modules/ekf2/EKF/ekf.h index aae313c2945..d80e705cbd0 100644 --- a/src/modules/ekf2/EKF/ekf.h +++ b/src/modules/ekf2/EKF/ekf.h @@ -551,7 +551,6 @@ private: uint64_t _time_last_hor_vel_fuse{0}; ///< time the last fusion of horizontal velocity measurements was performed (uSec) uint64_t _time_last_ver_vel_fuse{0}; ///< time the last fusion of verticalvelocity measurements was performed (uSec) uint64_t _time_last_heading_fuse{0}; - uint64_t _time_heading_fusion_start{0}; ///< start of the current uninterrupted period of heading observation fusion, while yaw was set manually (uSec) uint64_t _time_last_terrain_fuse{0}; LatLonAlt _last_known_gpos{}; @@ -909,8 +908,6 @@ private: // Control the filter fusion modes void controlFusionModes(const imuSample &imu_delayed); - void updateYawManualValidity(); - #if defined(CONFIG_EKF2_EXTERNAL_VISION) // control fusion of external vision observations void controlExternalVisionFusion(const imuSample &imu_sample); diff --git a/src/modules/ekf2/test/test_EKF_mag.cpp b/src/modules/ekf2/test/test_EKF_mag.cpp index 439d99f6cf9..2b351b397e7 100644 --- a/src/modules/ekf2/test/test_EKF_mag.cpp +++ b/src/modules/ekf2/test/test_EKF_mag.cpp @@ -383,40 +383,6 @@ TEST_F(EkfMagTest, velocityRotationOnYawReset) EXPECT_GT(yaw_change, 0.3f) << "Yaw change: " << degrees(yaw_change) << " deg"; } -TEST_F(EkfMagTest, manualYawExpiresWhenHeadingIsFused) -{ - // GIVEN: mag fusion is aligned in flight, so the mag remains the heading source - // even after a manual heading reset - const float mag_heading = M_PI_F / 4.f; - _sensor_simulator._mag.setData(Vector3f(0.2f * cosf(mag_heading), -0.2f * sinf(mag_heading), 0.4f)); - _sensor_simulator.runSeconds(_init_duration_s); - - _ekf->set_in_air_status(true); - _ekf->set_vehicle_at_rest(false); - _sensor_simulator._rng.setData(5.f, 100); - _sensor_simulator.startRangeFinder(); - _sensor_simulator.runSeconds(5.f); - - ASSERT_TRUE(_ekf_wrapper.isIntendingMagHeadingFusion() || _ekf_wrapper.isIntendingMag3DFusion()); - ASSERT_FALSE(_ekf->control_status_flags().yaw_manual); - - // WHEN: the heading is set manually - _ekf->resetHeadingToExternalObservation(mag_heading + radians(30.f), radians(2.f)); - _sensor_simulator.runSeconds(1.f); - - // THEN: the manual heading is protected from being overridden by the mag - EXPECT_TRUE(_ekf->control_status_flags().yaw_manual); - - // AND WHEN: the mag keeps being fused as heading source for a long time - _sensor_simulator.runSeconds(20.f); - EXPECT_TRUE(_ekf->control_status_flags().yaw_manual); - - _sensor_simulator.runSeconds(15.f); - - // THEN: the heading is mag derived again and the manual reset no longer protected - EXPECT_FALSE(_ekf->control_status_flags().yaw_manual); -} - TEST_F(EkfMagTest, manualYawSurvivesWithGnssAidingOnly) { // GIVEN: the vehicle sits on the ground with GNSS velocity and position fused @@ -466,9 +432,7 @@ TEST_F(EkfMagTest, manualYawOnGroundBlocksMagHeadingFusionIndefinitely) EXPECT_FALSE(_ekf_wrapper.isIntendingMagHeadingFusion()); EXPECT_FALSE(_ekf_wrapper.isIntendingMag3DFusion()); - // AND: this does not resolve itself while the vehicle stays on the ground. No heading - // observation is fused, so the manual heading never expires, which in turn keeps mag - // heading fusion disabled. + // AND: this does not resolve itself while the vehicle stays on the ground _sensor_simulator.runSeconds(120.f); EXPECT_TRUE(_ekf->control_status_flags().yaw_manual); @@ -486,7 +450,7 @@ TEST_F(EkfMagTest, manualYawOnGroundBlocksMagHeadingFusionIndefinitely) EXPECT_FALSE(_ekf->control_status_flags().mag_field_disturbed); } -TEST_F(EkfMagTest, manualYawSurvivesTakeoffUntilHeadingFused) +TEST_F(EkfMagTest, manualYawSurvivesTakeoffAndMagFusion) { // GIVEN: the heading was set manually on the ground, before any in-flight mag alignment const float mag_heading = M_PI_F / 4.f; @@ -508,21 +472,19 @@ TEST_F(EkfMagTest, manualYawSurvivesTakeoffUntilHeadingFused) _sensor_simulator.startRangeFinder(); _sensor_simulator.runSeconds(5.f); - // THEN: the mag becomes the heading source again, but the manual heading is neither - // overridden by the alignment nor immediately declared stale by it + // THEN: the mag becomes the heading source again, without overriding the manual heading EXPECT_TRUE(_ekf->control_status_flags().mag_aligned_in_flight); EXPECT_TRUE(_ekf_wrapper.isIntendingMag3DFusion()); EXPECT_TRUE(_ekf->control_status_flags().yaw_manual); EXPECT_NEAR(_ekf_wrapper.getYawAngle(), manual_heading, radians(1.f)); - // AND WHEN: the mag has been driving the heading for long enough - _sensor_simulator.runSeconds(20.f); + // AND WHEN: the mag keeps being fused as heading source for a long time + _sensor_simulator.runSeconds(60.f); + + // THEN: the manual heading is still kept + EXPECT_TRUE(_ekf_wrapper.isIntendingMag3DFusion()); EXPECT_TRUE(_ekf->control_status_flags().yaw_manual); - - _sensor_simulator.runSeconds(15.f); - - // THEN: the manual heading expires - EXPECT_FALSE(_ekf->control_status_flags().yaw_manual); + EXPECT_NEAR(_ekf_wrapper.getYawAngle(), manual_heading, radians(1.f)); } TEST_F(EkfMagTest, magHeadingChangeRateLimited)