From a839e4bcf106b8569fc9ca9679f586b47a112552 Mon Sep 17 00:00:00 2001 From: RomanBapst Date: Fri, 6 Nov 2026 07:21:09 +0300 Subject: [PATCH] fix(tecs): recover from a single non-finite frame AlphaFilter keeps a non-finite sample in its state for good. A single non-finite setpoint or measurement, e.g. the non-finite setpoint when exiting offboard velocity mode (#25906), therefore left the throttle non-finite until TECS was reinitialised. Since the throttle low-passes its energy rate error instead of the estimate, this also happens for a non-finite setpoint. Only take finite samples into the energy rate error filter and hold its state otherwise, seed it with a finite value on initialisation, and hold the last throttle on a non-finite throttle demand. The existing tests only checked that the pitch stays finite, which its fallback guarantees. The new test checks that pitch and throttle return to those of a controller that never saw the bad frame. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: RomanBapst --- src/lib/tecs/TECS.cpp | 30 ++++++++++++++++++--- src/lib/tecs/TECSTest.cpp | 55 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 3 deletions(-) diff --git a/src/lib/tecs/TECS.cpp b/src/lib/tecs/TECS.cpp index 7c25e1d17fe..d19dd58a5ce 100644 --- a/src/lib/tecs/TECS.cpp +++ b/src/lib/tecs/TECS.cpp @@ -49,6 +49,22 @@ using math::min; static constexpr bool isValidTimeStep(float dt) { return (PX4_ISFINITE(dt) && dt > FLT_EPSILON);} static constexpr bool fastDescendFullyEngaged(float fast_descend) { return 1.f - fast_descend < FLT_EPSILON; } +// AlphaFilter keeps a single non-finite sample in its state for good, so the controller would not recover from a +// single bad frame (e.g. a non-finite setpoint when exiting offboard velocity mode). Only take finite samples and hold +// the state otherwise. +static void updateIfFinite(AlphaFilter &filter, float dt, float time_const, float sample) +{ + if (PX4_ISFINITE(sample)) { + filter.setParameters(dt, time_const); + filter.update(sample); + } +} + +static void resetIfFinite(AlphaFilter &filter, float sample, float fallback) +{ + filter.reset(PX4_ISFINITE(sample) ? sample : fallback); +} + void TECSAirspeedFilter::initialize(const float equivalent_airspeed, const float equivalent_airspeed_trim, const bool airspeed_sensor_available) { @@ -233,10 +249,14 @@ void TECSControl::initialize(const Setpoint &setpoint, const Input &input, const specific_energy_rate.spe_rate.setpoint + specific_energy_rate.ske_rate.setpoint, param); ControlValues ste_rate{_calcThrottleControlSteRate(limit, specific_energy_rate, param)}; - _ste_rate_error_filter.reset(_getControlError(ste_rate)); + resetIfFinite(_ste_rate_error_filter, _getControlError(ste_rate), 0.f); _throttle_setpoint = _calcThrottleControlOutput(limit, ste_rate, param, flag); + if (!PX4_ISFINITE(_throttle_setpoint)) { + _throttle_setpoint = constrain(param.throttle_trim, param.throttle_min, param.throttle_max); + } + // Debug output _debug_output.total_energy_rate_estimate = ste_rate.estimate; _debug_output.total_energy_rate_sp = ste_rate.setpoint; @@ -574,8 +594,7 @@ void TECSControl::_calcThrottleControl(float dt, const STERateLimit &limit, { ControlValues ste_rate{_calcThrottleControlSteRate(limit, specific_energy_rates, param)}; - _ste_rate_error_filter.setParameters(dt, param.ste_rate_time_const); - _ste_rate_error_filter.update(_getControlError(ste_rate)); + updateIfFinite(_ste_rate_error_filter, dt, param.ste_rate_time_const, _getControlError(ste_rate)); // During fast descend, we control airspeed over the pitch control loop. Give minimal thrust as soon as we are descending float throttle_setpoint{param.throttle_min}; @@ -586,6 +605,11 @@ void TECSControl::_calcThrottleControl(float dt, const STERateLimit &limit, flag) + param.fast_descend * param.throttle_min; } + // Hold the last throttle on a non-finite demand, e.g. from a non-finite setpoint for a single frame + if (!PX4_ISFINITE(throttle_setpoint)) { + throttle_setpoint = _throttle_setpoint; + } + // Rate limit the throttle demand if (fabsf(param.throttle_slewrate) > FLT_EPSILON) { const float throttle_increment_limit = dt * (param.throttle_max - param.throttle_min) * param.throttle_slewrate; diff --git a/src/lib/tecs/TECSTest.cpp b/src/lib/tecs/TECSTest.cpp index f431bae484d..03678d04db6 100644 --- a/src/lib/tecs/TECSTest.cpp +++ b/src/lib/tecs/TECSTest.cpp @@ -767,6 +767,61 @@ TEST(TECSControlTest, NonFiniteDemandFallsBackToLevelFlightPitch) EXPECT_FLOAT_EQ(control.getPitchSetpoint(), param.level_flight_pitch); } +// A single non-finite frame must not stay in the filtered states: once the inputs are finite again, pitch and +// throttle return to those of a controller that never saw the bad frame, instead of a non-finite throttle and a pitch +// stuck on its non-finite fallback until the controller is reinitialised. +TEST(TECSControlTest, RecoversFromSingleNonFiniteFrame) +{ + using Corrupt = void (*)(TECSControl::Setpoint &, TECSControl::Input &); + const struct { + const char *name; + Corrupt corrupt; + } cases[] = { + {"tas_setpoint", [](TECSControl::Setpoint & s, TECSControl::Input &) { s.tas_setpoint = NAN; }}, + {"altitude_reference", [](TECSControl::Setpoint & s, TECSControl::Input &) { s.altitude_reference.alt = NAN; }}, + {"tas", [](TECSControl::Setpoint &, TECSControl::Input & i) { i.tas = NAN; }}, + {"tas_rate", [](TECSControl::Setpoint &, TECSControl::Input & i) { i.tas_rate = NAN; }}, + {"altitude", [](TECSControl::Setpoint &, TECSControl::Input & i) { i.altitude = NAN; }}, + {"altitude_rate", [](TECSControl::Setpoint &, TECSControl::Input & i) { i.altitude_rate = NAN; }}, + }; + + const TECSControl::Param param = makeParam(); + const TECSControl::Flag flag = makeFlag(); + const TECSControl::Input input = makeInput(); + TECSControl::Setpoint setpoint = makeSetpoint(); + setpoint.altitude_reference.alt = 150.f; // climb at full throttle, so that kinetic priority is active + + for (const auto &c : cases) { + SCOPED_TRACE(c.name); + + TECSControl reference; + TECSControl control; + reference.initialize(makeSetpoint(), input, param, flag); + control.initialize(makeSetpoint(), input, param, flag); + + for (int i = 0; i < 100; i++) { + reference.update(kDt, setpoint, input, param, flag); + control.update(kDt, setpoint, input, param, flag); + } + + // WHEN a single frame is non-finite + TECSControl::Setpoint bad_setpoint = setpoint; + TECSControl::Input bad_input = input; + c.corrupt(bad_setpoint, bad_input); + reference.update(kDt, setpoint, input, param, flag); + control.update(kDt, bad_setpoint, bad_input, param, flag); + + for (int i = 0; i < 500; i++) { + reference.update(kDt, setpoint, input, param, flag); + control.update(kDt, setpoint, input, param, flag); + } + + // THEN the controller is back to the reference + EXPECT_NEAR(control.getThrottleSetpoint(), reference.getThrottleSetpoint(), 1e-3f); + EXPECT_NEAR(control.getPitchSetpoint(), reference.getPitchSetpoint(), 1e-3f); + } +} + // Underspeed starts at 1/3 stall + 2/3 min, leaving a margin below tas_min itself // so a TAS setpoint pinned at the minimum doesn't constantly trigger mitigation. TEST(TECSUnderspeedTest, NoMitigationAtOrAboveStartingThreshold)