The mission
The rocket stands on a gimbal: two hobby servos tilt the nozzle to push the body back upright. Every loop reads the IMU, estimates the tilt and corrects the nozzle. He wrote it in 2024.
What happened
A 2024 thrust-vector-control gimbal: an MPU6050 and two SG90 servos, PID on the tilt. Read again in 2026.
The wobble grows with every swing. When an error grows instead of dying out, the loop is feeding it.
What I believed
v1 fuses gyro and accelerometer with a complementary filter (0.96 / 0.04).
- Turn up P. It isn't pushing hard enough. More gain made it swing faster. Before tuning how hard a controller pushes, check what it's being fed.
- Filter the accelerometer harder. It's noise. Smoothing didn't help. The noise wasn't the cause; the estimate itself was wrong on every cycle.
What was true
Each call creates a new uninitialized local angleX, rather than retaining the previous estimate. V2 separates X and Y loops but uses accelerometer-only angles. Its I and D gains use milliseconds, so their numerical values cannot be copied unchanged into a seconds-based loop. Servo commands are clamped; the error integrals are not.
Try it yourself
Loading the diagram. It needs JavaScript; the text around it says the same thing in words.
The fix
Keep the filter's state outside the loop, and write dt in seconds: the integral and derivative had been carrying a factor of a thousand in milliseconds. A filter with no memory isn't a filter.
Evidence: gist v1 (2024-09-12), gist V2 (2024-09-13)
Keep doing
State has to live somewhere. And units are part of the gain.
Do differently
Persist filter state; specify time and gain units; handle a zero interval and actuator saturation.