HomeControl Systems TheoryInverted Pendulum: The Classic Control Problem

🎯 Inverted Pendulum: The Classic Control Problem

Balance a pendulum upright on a rolling cart using real nonlinear cart-pole physics and a live-tunable PID controller. Push it, mistune it, watch it fall.

Control Systems Theory2DAdvanced60 FPS
inverted-pendulum-control ↗ Open standalone

About this simulation

This simulation integrates the real, coupled nonlinear equations of motion of a cart-pole system: a rigid pendulum pivoting freely on a cart that slides along a horizontal rail. Gravity and the pivot constraint couple the cart's acceleration ẍ and the pole's angular acceleration θ̈, so the upright position (θ = 0) is a genuinely unstable equilibrium — left alone, the pole falls, exactly as it would in reality. A PID loop reads the angle error each control tick and computes a corrective cart force F = Kp·e + Ki·∫e·dt + Kd·θ̇, saturated at a realistic actuator limit, while a slower outer loop nudges the target tilt to also drag the cart toward a chosen setpoint — the harder, dual-objective version of the problem. Tune the gains, push the pole, and the fall — or the recovery — is computed live from the physics, never scripted.

🔬 What it shows

A 3D cart-and-pole rig: the cart slides on a rail with hard end-stops, the pole pivots freely on top of it, and a green/red arrow shows the instantaneous control force. The side panel reports the live angle, angular rate, cart position, control force and a Balanced / Stabilizing / Recovering / FALLEN status computed straight from the running physics.

🎮 How to use

Drag Kp, Ki and Kd to change the angle-loop gains, and the cart setpoint slider to ask the cart to hold a different spot on the rail while still balancing. Press Push → / Push ← to inject a disturbance impulse, Weak gains to see an under-powered controller lose slowly, and Reset to start over. Watch what happens when Kp is too low to overcome gravity, or Kd is so high the cart chatters and destabilizes.

💡 Did you know?

The cart-pole problem (also called the inverted pendulum or "pole-balancing" task) has been a benchmark in control engineering since the 1960s and became one of the first standard testbeds in reinforcement learning after Barto, Sutton and Anderson's 1983 paper — the same balancing act, in disguise, that keeps a Segway, a hoverboard or a rocket booster landing upright.

Frequently asked questions

Why is the upright position "unstable" — isn't a pendulum supposed to hang there?

A free pendulum has a stable equilibrium hanging straight down (θ = 180°): nudge it and gravity pulls it back. The cart-pole's upright point (θ = 0°) is the opposite kind of fixed point — the real nonlinear equations balance there in theory (net torque is zero), but any tiny nudge grows instead of shrinking, because gravity's restoring torque near θ = 0 acts on the pole in the direction that increases the tilt, not the direction that corrects it. That is the textbook definition of an unstable equilibrium, and it is why this simulation needs an active controller running every frame — turn it off (set all gains to 0) and the pole falls immediately, with no push needed.

What are the actual equations of motion being integrated?

This simulation numerically integrates the standard coupled cart-pole ODEs (after Sutton, Barto & Anderson, 1983, with added friction): θ̈ = [g·sinθ − cosθ·((F + m·l·θ̇²·sinθ − b·ẋ)/(M+m)) − c·θ̇/(m·l)] / [l·(4/3 − m·cos²θ/(M+m))], and ẍ = [F + m·l·(θ̇²·sinθ − θ̈·cosθ) − b·ẋ] / (M+m), where M is cart mass, m is pole mass, l is the half-length of the pole, F is the applied cart force, and b, c are small viscous friction terms. These are solved with a fixed small time step every frame — nothing about the fall or the recovery is pre-animated.

Why does the PID loop need a large Kp to work at all?

Linearizing the equations above near θ = 0 shows the closed loop is only stable once Kp exceeds g·(M + cart mass), roughly 19–20 N/rad for this rig's masses — below that threshold gravity's destabilizing torque outweighs the proportional correction and the pole falls, slowly at first and then rapidly, no matter how it started. That threshold is a real, derivable property of this exact system, not a tuned-in constant, which is why the Weak gains preset (Kp well under it) reliably demonstrates a slow, genuine loss of balance.

What happens with too much Kp or Kd?

Excessive proportional gain overreacts to small angle errors, overshooting the correction and exciting oscillation; excessive derivative gain amplifies the angular-rate term so aggressively that the actuator repeatedly saturates at ±22 N and the control force starts chattering between its limits. Both failure modes are visible directly in this simulation: watch the force arrow flicker and the cart shudder back and forth as the oscillation grows until the pole tips past recovery — a real instance of the classic PID-tuning tradeoff between responsiveness and stability.

How does the cart-position setpoint work if only the angle is PID-controlled?

Balancing and repositioning are coupled in a real cart-pole: to move the cart, you first have to lean the pole slightly in the direction of travel and let gravity accelerate it, exactly like a person balancing a broomstick who leans it to walk it across a room. This simulation implements that as cascade control — a fixed outer loop turns the cart-position error into a small commanded tilt (clamped to about 8°), and the angle PID loop you tune then tracks that tilt instead of a fixed zero. That is why pushing the setpoint slider far while the gains are weak can itself cause a fall — it is the same, harder dual-objective control problem faced by real Segways and rocket landers.

What does the disturbance push actually do?

Push → and Push ← apply an instantaneous kick to the pole's angular velocity θ̇, the same effect a real shove near the top of the pole would have. Whether the controller rejects it depends entirely on the current gains and the size of the kick relative to the actuator's 22 N force limit — a well-tuned loop absorbs it and returns to vertical, while a weak or badly-tuned one cannot generate enough corrective force in time and the pole falls, exactly as the real, unmodified physics dictates.

Why is the cart-pole such a famous benchmark problem?

It is one of the simplest systems that is simultaneously nonlinear, underactuated (one motor, two degrees of freedom) and genuinely unstable, which makes it a standard stress test for both classical control (PID, state-space/LQR, pole placement) and modern reinforcement learning, where "CartPole" is one of the first environments almost every RL algorithm is benchmarked against. Its rail end-stops and angle-failure bound in this simulation echo the original 1983 Barto–Sutton–Anderson formulation (±2.4 m, ±12°), though the visual fail threshold here is set a little wider so you can actually watch the fall happen instead of the trial ending instantly.

⚙ Under the hood

Balance a pendulum upright on a rolling cart using real nonlinear cart-pole physics and a live-tunable PID controller — push it, mistune it, watch it fall.

control-theorypidcart-polenonlinear-dynamicsfeedbackunstable-equilibrium

2D · HTML5 Canvas 2D · 60 FPS target · runs fully client-side, no install

What did you find?

Add reproduction steps (optional)