Inverted Pendulum on a Cart
A pendulum hanging from a pivot is boring: push it and it swings back. Stand the same pendulum upright on a cart that can only move sideways, and it becomes the sharpest teaching problem in control engineering. The system cannot hold still on its own. Left alone it falls, and it falls faster the further it has already fallen. The only way to keep it up is to move the thing it is standing on — to catch it by driving the cart underneath it.
That is what makes it worth a full case study. Almost every idea in classical control shows up here with nowhere to hide: an unstable plant, a controller that has to act before the error grows, the difference between reacting to an error and anticipating it, and the trade-off between a fast response and a violent one.
The physical setup
A cart of mass rolls on a horizontal track, with viscous friction opposing its motion. A rod of mass is pinned to the cart and free to rotate in the vertical plane. The force is applied horizontally to the cart — it is the only thing we control. The angle is measured from the upward vertical, so is balanced and positive means the rod has tipped one way.
| Symbol | Meaning | Value |
|---|---|---|
| cart mass | ||
| pendulum mass | ||
| cart friction coefficient | ||
| pivot to centre of mass | ||
| rod inertia about its centroid | ||
| gravitational acceleration |
Equations of motion
Take the cart and the rod separately and let the pin joint pass forces between them. Call the horizontal force in the joint and the vertical force .
Newton's second law along the track, for the cart alone:
The rod's centre of mass sits at horizontally (positive leans the rod back, towards ), so differentiating twice gives the horizontal force the rod needs:
Substituting removes and leaves the first equation of motion:
For the second, take moments about the rod's centre of mass. Gravity acts there so it contributes nothing; only the two joint forces do:
Two coupled nonlinear equations. Note the sign of the gravity term: moved to the right-hand side it is when is measured from upward, so it pushes the way already points — exactly what makes this system fall over rather than swing back.
Linearising about the upright position
Balancing means staying near . For small angles, , , and is second-order small and drops out entirely:
Taking Laplace transforms with zero initial conditions and eliminating between the two gives the transfer function from the applied force to the pendulum angle:
where
With the values above, and the transfer function becomes
Why it falls over
Those two minus signs in the denominator are the whole story. Factor it and the poles are
One of them is in the right half-plane. Any disturbance, however small, excites a response that grows like — the angle doubles roughly every seconds. This is not a modelling artefact. It is the rod falling over, written in algebra.
Run the diagram with the controller disconnected and you will see exactly this: a nudge, then a curve that leaves the top of the plot and never comes back.
The whole plant as one state-space model
The transfer function above describes the angle, and only the angle. Put the cart back in and the linearised system has four states — cart position and velocity, rod angle and angular rate — and one input:
Its poles are the three above plus one more at : the cart, which has no preferred position at all. Written out, the angle's transfer function from this model is
and one cancels. That cancellation is why the third-order transfer function was correct, and it is also the whole reason an angle-only controller cannot bring the cart back: seen from , the cart pole is invisible.
The cart position has zeros of its own, at . The one in the right half-plane matters later.
Building it in BlockWerk
The plant is a single StateSpace block. Its matrices are not decimals: every entry is written as an expression over the diagram's variables — mgl(M+m)/q, (I+ml^2)/q — and evaluated when the model runs. Move the slider and and follow; the engine rebuilds the block's matrices in place without restarting. is the identity, so the block has four outputs, y_0 to y_3, one per state.
Everything else hangs off those outputs. The angle goes through a Saturation at — the floor, which the linear model knows nothing about — to the scope and to the angle-only controller. All four states go to the state-feedback gains.
Disturbing the system
Here is a trap worth stepping around deliberately.
The obvious test is a step on the setpoint. For the angle it does nothing. The setpoint is zero — upright — and the model starts upright, so the error is zero, the controller outputs nothing, and the plot is a flat line forever. Nothing has gone wrong; there is simply nothing to correct.
A balancing problem is not a tracking problem. What you want to ask is _"if something knocks it, does the controller catch it?"_ So the input is a disturbance, applied to the force: a short shove, then nothing. This model uses a rectangular pulse of lasting at — an impulse of — built from two Step blocks that cancel. Without a controller the rod reaches the floor half a second later.
A controller that only sees the angle
The first loop is a PID on . Five gain sets, each measured over a run with a sample time of :
| peak | in degrees | back within rad | |||
|---|---|---|---|---|---|
| 100 | 1 | 20 | s | ||
| 100 | 0 | 20 | s | ||
| 32 | 0 | 5.5 | s | ||
| 50 | 1 | 10 | s | ||
| 200 | 1 | 40 | s |
Proportional action alone is a spring. With and nothing else the closed-loop poles sit at : the rod swings through upright at with a damping ratio of , and keeps swinging. A third pole at is still unstable — slowly.
Derivative action removes the energy. It responds to , to how fast the rod is falling, and so starts pushing while the rod is still nearly upright. With the swing dies out within a second.
The integral term moves the last slow pole. Without it the constant term of the closed-loop characteristic polynomial, , is negative, and a pole stays at . (the CTMS choice) makes it : stable on paper, with a time constant of about 80 minutes.
And through all of it the cart drifts. After the push it rolls off at a steady pace — half a metre in five seconds, two metres in twenty — because no choice of , and can move the pole at .
State feedback: the cart comes back
Measure all four states and feed each one back:
The model uses the CTMS LQR design, minimising with :
which puts the closed-loop poles at and . After the push the rod peaks at and the cart returns to the middle. A setpoint step of at takes about a second, a peak lean of and — and starts with the cart rolling the wrong way. That is the zero at : to lean the rod towards the target the cart must first move out from under it, and state feedback moves every pole but no zero.
Transport delay
A real loop has latency — filtering, sampling, a bus, the drive. Break the loop at the cart's input and the loop gain crosses at with of phase margin. A delay leaves the magnitude alone and subtracts from the phase, so the delay margin is
Measured: raises the peak lean from to and leaves of margin; rings at the edge; loses the rod.
Things to try
- The speed slider scales all four LQR poles by one factor, and the gains are designed again for whatever plant the other sliders describe. At a setpoint move costs ; at it costs and a lean.
- Combine speed with of delay. The faster controller crosses over at a higher frequency and has only about of delay margin: the rod falls.
- Set the wind to . The rod stays upright, but the cart settles about off its setpoint: at rest the controller must push back with , and the position gain only does that with an error, . Integral action on would remove it.
- Drag up. A longer rod falls more slowly — the unstable pole moves toward the origin — which is why circus performers balance long poles, not short ones.
What this model leaves out
The model is linear, which means it is only honest near upright: holds to within up to about . The linear model will dutifully catch a rod from angles a real one could not come back from. Swinging the rod up from hanging down needs a nonlinear controller first — Åström–Furuta energy control drives to zero, reinforcement learning is another route — with a linear controller like this one taking over near the top.
The cart runs on an endless track here, and the motor delivers any force asked of it.
Sources
The parameter set used here (, , , ) and the choice of this system as a worked example come from the Control Tutorials for MATLAB & Simulink (CTMS), by Prof. Dawn Tilbury (University of Michigan) and Prof. Bill Messner (Carnegie Mellon University), later extended by Prof. Rick Hill (Detroit Mercy), JD Taylor (CMU) and Prof. Shuvra Das (Detroit Mercy), with funding from the NSF (grant DUE 9554819) and MathWorks. CTMS is licensed CC BY-SA 4.0.
The derivation, the text, the block diagram and every measured number on this page are our own work, produced on BlockWerk's own engine. We use .
The equations of motion for a cart-mounted inverted pendulum are standard and appear in substantially this form in most control textbooks, among them Ogata's Modern Control Engineering, Nise's Control Systems Engineering and Franklin, Powell & Emami-Naeini's Feedback Control of Dynamic Systems.
See Also
- StateSpace: the whole plant, four states, matrices written as formulas.
- Gain: the four state-feedback gains.
- PIDController: the angle-only controller.
- Delay: the transport delay between controller and cart.
- Saturation: the floor at ±90°.
- XYPlot: the phase plane, θ against θ̇.
- uPlotDisplay: angle, cart position and control force.