Thanh Le / bench notes
EMBEDDED · IN PROGRESS

My control library (ctl): PID to MPC, in simulation

A header-only C++ control library I wrote for small MCUs, and the demos that check every law against a plant instead of a promise

#control#simulation#cpp#testing
Last change2026-11-22: hub page, four STAR write-ups re-run in double and float

My control library (ctl): PID to MPC, in simulation

The control library in numbers One library, every law checked against a plant: back from windup in 0.77 s instead of 108, LQI holding the pole to 0.0008 rad, all in simulation. On the left, part of my anti-windup plot.

Same PID, same clamped actuator: without anti-windup it takes 108 s to come back

Every firmware project I start ends up with a PID copied from the last one, usually with the same bug. In September 2026 I stopped doing that and wrote one library instead: ctl, header-only C++17, no allocation, no exceptions, no peripheral access. A control law takes a setpoint, a measurement and dt, and returns a number. That’s all it knows, which is exactly why I can test it on the PC.

It grew more than I planned. Next to the PID there’s a relay autotuner, gain scheduling, a bounded adaptive PID, fuzzy PID, ADRC, sliding mode, a Smith predictor, discrete LQR (and LQI), a Kalman filter, recursive least squares, a small constrained MPC, motion profiles and path tracking. The plants live in a second library (act, more on that on the plant models hub), because a PID with no plant is just a difference equation.

Everything on these pages ran in simulation, nothing on a real motor yet. I rebuilt and re-ran every demo on 2026-11-22 before writing this, in double and in single precision (-DCTL_REAL_FLOAT), because the float build is the one an MCU gets and it has caught real bugs before. All of them passed in both.

The stories

#StoryResultStatus
1 PID: 108 s of windup, and gains from a relay
Seven configurations of one PID on one clamped plant, then a relay experiment that measures Tu and Ku instead of guessing
Recovery after saturation 108.55 s -> 0.77 s with back-calculation; relay test hits Tu within 1.3 %, Ku within 2 % Done
2 Five controllers on one pendulum, and the LQR that parks 0.36 rad off
Old adaptive law, PID, bounded adaptive PID, LQR and LQI against a constant torque the model doesn't know about
LQR holds the pole with 8.7 N but sits 0.36 rad off under a 3 N torque; LQI brings it to 0.0008 rad with 6.1 N Done
3 RLS: finding the inertia the datasheet doesn't know
Recursive least squares on a servo axis (physics) and on an unstable cart-pole in closed loop (state-space), and the two ways it lies
Servo J to 5.3 % from 12 s of normal motion (datasheet was 10x off); cart-pole Ad to 0.024 %, LQR gain within 0.6 % of the true design Done
4 MPC on two tanks: closer to the overflow than a PI dares
A 7 kB constrained MPC against two tuned PIs on a two-tank rig where the pump can't run backwards and the tank overflows for real
Integrated error 14.4 m·s with MPC vs 54.8 and 58.9 for the two PIs; tank one peaks at 0.458 m of 0.50, 19 ADMM iterations worst case Done

Still open

The library has never closed a loop on hardware. The first board port will re-measure every number here. And the demos’ printed commentary can go stale: one of them still claims 0.22 rad where the run says 0.36 (it’s on the pendulum page). The numbers are asserted, the sentences aren’t.