From 32bd1adea255cb007eb003513d4c536137292fc5 Mon Sep 17 00:00:00 2001 From: Wang Jiayue Date: Wed, 20 May 2026 15:40:55 +0800 Subject: [PATCH] fix: reset StickYaw setpoint when unaided yaw recovers --- src/lib/stick_yaw/StickYaw.cpp | 10 +++++++ src/lib/stick_yaw/StickYaw.hpp | 1 + src/lib/stick_yaw/StickYawTest.cpp | 43 ++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/src/lib/stick_yaw/StickYaw.cpp b/src/lib/stick_yaw/StickYaw.cpp index c45cdee823b..2c36adadfe4 100644 --- a/src/lib/stick_yaw/StickYaw.cpp +++ b/src/lib/stick_yaw/StickYaw.cpp @@ -73,6 +73,7 @@ void StickYaw::generateYawSetpoint(float &yawspeed_setpoint, float &yaw_setpoint bool StickYaw::updateYawCorrection(const float yaw, const float unaided_yaw, const float deltatime) { if (!PX4_ISFINITE(unaided_yaw)) { + _unaided_yaw_was_invalid = true; // If unaided yaw is not available we leave yaw_correction_ unchanged // Meaning yaw_setpoint - yaw_correction_prev + _yaw_correction = yaw_setpoint return false; @@ -82,6 +83,15 @@ bool StickYaw::updateYawCorrection(const float yaw, const float unaided_yaw, con // distance from an unaided yaw source. const float yaw_error = wrap_pi(yaw - unaided_yaw); + if (_unaided_yaw_was_invalid) { + _unaided_yaw_was_invalid = false; + _yaw_error_lpf.reset(yaw_error); + _yaw_error_ref = yaw_error; + _yaw_correction = 0.f; + _yaw_estimate_converging = false; + return true; + } + // Run it through a high-pass filter to detect transients const float yaw_error_hpf = wrap_pi(yaw_error - _yaw_error_lpf.getState()); _yaw_error_lpf.update(yaw_error, deltatime); diff --git a/src/lib/stick_yaw/StickYaw.hpp b/src/lib/stick_yaw/StickYaw.hpp index a1fdaae9f0b..c620f378e65 100644 --- a/src/lib/stick_yaw/StickYaw.hpp +++ b/src/lib/stick_yaw/StickYaw.hpp @@ -60,6 +60,7 @@ private: float _yaw_error_ref{0.f}; float _yaw_correction{0.f}; bool _yaw_estimate_converging{false}; + bool _unaided_yaw_was_invalid{false}; AlphaFilter _yaw_error_lpf{_kYawErrorTimeConstant}; ///< used to create a high-pass filter static constexpr float _kYawErrorTimeConstant{1.f}; ///< time constant of the high-pass filter used to detect yaw convergence static constexpr float _kYawErrorChangeThreshold{radians(1.f)}; ///< we consider the yaw estimate as "converging" when above this threshold diff --git a/src/lib/stick_yaw/StickYawTest.cpp b/src/lib/stick_yaw/StickYawTest.cpp index 7c28b5c8e4f..0af5bca3d48 100644 --- a/src/lib/stick_yaw/StickYawTest.cpp +++ b/src/lib/stick_yaw/StickYawTest.cpp @@ -75,3 +75,46 @@ TEST(StickYawTest, UnaidedYawNanTransitionNoYawJump) << "Yaw setpoint jumped from " << yaw_sp_before << " to " << yaw_sp << " when unaided_yaw became NAN"; } + +TEST(StickYawTest, UnaidedYawNanToFiniteTransitionResetsYawLock) +{ + // When unaided_yaw recovers from NAN, stale yaw correction state from before + // the outage must not be applied to the locked yaw setpoint. + + param_control_autosave(false); + + StickYaw stick_yaw{nullptr}; + float yawspeed_sp = 0.f; + float yaw_sp = NAN; + const float dt = 0.01f; + + // Phase 1: Establish yaw lock at yaw=0 with unaided_yaw=0. + stick_yaw.reset(0.f, 0.f); + + for (int i = 0; i < 10; i++) { + stick_yaw.generateYawSetpoint(yawspeed_sp, yaw_sp, 0.f, 0.f, dt, 0.f); + } + + EXPECT_NEAR(yaw_sp, 0.f, 0.01f); + + // Phase 2: Simulate a yaw estimate correction while unaided_yaw is valid. + for (int i = 0; i < 20; i++) { + stick_yaw.generateYawSetpoint(yawspeed_sp, yaw_sp, 0.f, 0.3f, dt, 0.f); + } + + EXPECT_NEAR(yaw_sp, 0.3f, 0.01f); + + // Phase 3: unaided_yaw becomes unavailable. The yaw setpoint is held by the + // existing correction state. + stick_yaw.generateYawSetpoint(yawspeed_sp, yaw_sp, 0.f, 0.3f, dt, NAN); + + EXPECT_NEAR(yaw_sp, 0.3f, 0.01f); + + // Phase 4: unaided_yaw recovers with a new yaw error baseline. The yaw lock + // should reset to the current yaw instead of applying stale correction state. + stick_yaw.generateYawSetpoint(yawspeed_sp, yaw_sp, 0.f, 0.3f, dt, 0.3f); + + EXPECT_NEAR(yaw_sp, 0.3f, 0.01f) + << "Yaw setpoint jumped to " << yaw_sp + << " when unaided_yaw recovered from NAN"; +}