Balancing the unbalanceable
Modelling an inverted pendulum on a cart, and designing PID and lead-lag control to keep it upright.
This pendulum is live and under PID control right now. Shove it. Drag the bob, or focus the canvas and press ← / →, and watch it recover.
Scroll to see why this works, and why simpler controllers can't do it.
The system
Balance a broomstick on your palm and you'll find you can't hold your hand still. You keep moving it under the stick. A cart does the same job here: it slides left and right so the rod stays upright.
Input, output, objective
- Input u: horizontal force on the cart (N)
- Output θ: rod angle from vertical (rad)
- Objective: within 0.3 s, no , recover from pushes
Same problem, everywhere
- Segway
- Rocket landing
- Walking robots
- You, standing still
Balance it yourself
Hold ← or → (or the buttons, or touch and hold either half of the canvas) to push the cart. The rod starts with a small tilt. How long can you keep it within 60°?
The model
Before we can control it, we need maths that predicts it. Newton's laws give two coupled equations. Small angles turn them into one tidy transfer function.
Change the hardware
The equations on the left update as you drag.
Assumptions
- Frictionless cart and pivot
- Point-mass bob, massless rod
- Small angle: sin θ ≈ θ, cos θ ≈ 1
- Ideal actuator: any force, instantly
Linear versus nonlinear
Release the rod with no controller and run both models side by side. The linear model is excellent near upright and starts to lie once the angle grows past about 15°.
What the model tells us
The denominator s² − a² has two roots, one on each side of the imaginary axis. The one on the right is a mode that grows by itself: eat, doubling every 0.10 s.
Open-loop poles
Release it
Phase of G(jω)
No damping at any frequency, so the controller must add phase lead.
Controller selection
Four classic controllers, one question each: where do the closed-loop poles go? Drag the gains, watch the poles, then try each one on the real pendulum.
Compensator selection
A compensator K(s+z)/(s+p) is a zero and a pole on the real axis. Their order decides everything. Drag them and watch the branches of the swing across the imaginary axis.
Step response
Ask each closed loop to move θ from 0 to 1 and watch how it gets there. Solid lines are the two designs we chose; dashed lines are the ones we rejected.
Why does everything overshoot?
Performance comparison
Every number below is computed in your browser from the same design formulas. Click a column heading to sort.
The push test
Lean on the cart with a steady 1 N and keep leaning. A good controller fights back until the rod is straight again. The angle obeys θ/d = G/(1+CG).
Conclusion
Lead before anything
The plant has no damping. Without phase lead (a D term or a lead compensator) no gain can stabilise it.
Then add I or lag
Once it's stable, an integrator (PID) removes the lean entirely. A lag (lead-lag) shrinks it tenfold.
Overshoot is physics
An unstable pole forces every stable linear design to overshoot. You can only choose how much.
Next step: full-state feedback
Our loops watch only θ, so the cart drifts. Feeding back all four states (x, ẋ, θ, θ̇) with pole placement or keeps the rod up and the cart on the track.