Files
Roman BapstandBalduin ff37963842 test(tecs): add closed-loop unit tests for TECSControl (#29875)
* test(tecs): add closed-loop unit tests for TECSControl

Couple TECSControl to a minimal aircraft model built from the energy
model TECS assumes internally: a piecewise linear throttle to total
energy rate map, climb rate from pitch and airspeed, and the remainder
of the energy rate into airspeed. With perfect state knowledge and no
model mismatch any tracking error is the controller's own, so the suite
exposes consistency errors between the pitch and throttle loops that
the unit tests on single updates cannot see.

Only the cases the current controller passes are included: steady
cruise, airspeed steps, a slow altitude step down, plant pitch offset,
gliding, fast descend and airspeed-less cruise. The altitude steps at
the default vertical acceleration, with a speed weight below 1 and
without airspeed fail today and come with the fixes they test.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: RomanBapst <bapstroman@gmail.com>

* test(tecs): explain why the airspeed step down is not first order

The step down is not first order because the SEB rate to pitch
conversion ignores the total energy rate: the pitch feedforward climbs
with the kinetic energy the throttle is already removing, and the pitch
integrator takes it out again. The loops interacting is not the cause;
with a consistent conversion the response is first order.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: RomanBapst <bapstroman@gmail.com>

* test(tecs): use the current year in the closed-loop test copyright

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: RomanBapst <bapstroman@gmail.com>

* test(tecs): drop the altitude reference model from the closed-loop test

The closed-loop harness is meant to test TECSControl on its own, but the
altitude setpoint went through TECSAltitudeReferenceModel first, so the
altitude step results also depended on how the reference model shapes
the setpoint. Feed TECSControl the raw altitude setpoint instead.

A raw step drops the throttle to idle at once, so the altitude step only
keeps airspeed exact if the pitch can follow just as fast. In TECSControl
the vertical acceleration limit only rate limits the pitch, so the step
test now disables it, and sizes the step so that the demanded sink rate
stays within FW_T_SINK_MIN, the sink rate flown at idle without speeding
up.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: RomanBapst <bapstroman@gmail.com>

* test(tecs): show the airspeed-less altitude step with half the SEB feedforward

Without airspeed the speed weight is 0, so the SEB rate is twice the
potential energy rate, while the pitch feedforward converts it to a climb
angle as if they were equal and asks for twice the climb rate. With the
feedforward gain at 0.5 the vehicle flies the demanded climb rate exactly
and the altitude error decays first order with FW_T_ALT_TC, which the
test checks for a step down and a step up. At the default gain of 1 the
same checks miss by up to 1.8 m.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: RomanBapst <bapstroman@gmail.com>

* test(tecs): disable the STE rate filter in the airspeed-less altitude step

The low-pass filter on the total energy rate estimate lags the step, so
the throttle damping briefly overfunds the climb on the step up and the
open loop airspeed rose by 3 cm/s, which the test allowed for with a
looser bound. Disable the filter instead and hold the airspeed to the
numerical tolerance like the other exact cases.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: RomanBapst <bapstroman@gmail.com>

* test(tecs): disable vert_accel_limit generally

* test(tecs): count closed-loop simulation steps as an integer

Accumulating the simulation time in float drifts (the second altitude step
of a test landed at 40.0007 s) and the float loop condition could run one
step more than the requested duration. Count steps instead and derive the
time from the step count.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Balduin <balduin@auterion.com>

* test(tecs): unify error tolerance

Replace various hardcoded tracking tolerances:
 - Add a second general constant for the tolerance of tracking errors
   which do NOT scale down with smaller DT
 - Adjust settling times so all tests reasonably reach this threshold

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Balduin <balduin@auterion.com>

* test(tecs): add a make target to plot the closed-loop test cases

The closed-loop test writes one CSV per test case when TECS_TEST_LOG_DIR
is set, but reading the step responses from numbers is tedious. Add the
plotting script back and a tecs_closed_loop_plots target that runs all
test cases with logging and plots them into
build/px4_sitl_test/tecs_closed_loop/png, also when test cases fail.
It is a separate target so that make tests and CI do not depend on
matplotlib.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: RomanBapst <bapstroman@gmail.com>

* test(tecs): rename the closed-loop test to TECSControlClosedLoopTest

The test drives TECSControl, not the outer TECS class, so name it after
the class under test like TECSTest.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Balduin <balduin@auterion.com>

* test(tecs): clean up logging & variable names

Stop mixing up previous and current steps:
 - _pitch and _throttle hold the last TECS command, which the plant
   applies until the next update (drop the _prev naming)
 - reorder logging, so each row pairs a state with the command
   computed from it:
     - find control input
     - sim stats, csv output
     - advance simulation

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Balduin <balduin@auterion.com>

* docs(test): document the TECS closed-loop test logging and plots

The closed-loop TECS test can log every test case to CSV and plot them
with the tecs_closed_loop_plots target, but neither is discoverable from
the docs. Describe both next to the other make tests options.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: RomanBapst <bapstroman@gmail.com>

* test(tecs): standardise step sizes

Use one altitude and one airspeed step size for all step tests, so the
tests are easier to compare:
 - ALT_STEP chosen to not saturate throttle while sinking or climbing
     - Altitude step up test added (disabled, see comment)
 - AIRSPEED_STEP = 1 m/s
     - Only effective change is gliding (previously 2 m/s steps)
 - FastDescend drops the altitude setpoint, which does not affect it, and
   checks the throttle is exactly at its minimum

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Balduin <balduin@auterion.com>

* test(tecs): comment polishing

* test(tecs): tighten bounds for the weird airspeed response

* test(tecs): disable pitch damping

Removes the ringing artifacts. I think pitch damping makes no sense in
this sim, see comment for details.

* test(tecs): Require low errors after integrator

And add another future test (altitude step up currently still gives a
bit too much airspeed error)

---------

Signed-off-by: RomanBapst <bapstroman@gmail.com>
Signed-off-by: Balduin <balduin@auterion.com>
Co-authored-by: Balduin <balduin@auterion.com>
2026-11-23 11:23:31 +03:00
..
…
…