Inverted Pendulum Simulator

Cart-pole (inverted pendulum) with PID control. Watch the pendulum balance upright on a moving cart, tune the controller, or destabilize it with a kick to see how the controller recovers.

RK4 integrator, substepped at 0.5 ms, runs entirely in the browser.

Cart-Pole Animation

Presets:
Default parameters
Cart position p Reference (target p) Pole Control force F

Time-Series Response

Cart p (m) Setpoint (m) Pole angle (deg) Control F (N)

p

0.000

m

theta

0.0

deg

F

0.00

N

|theta|max

0.0

peak (deg)

Phase Plot — (θ, ω) Trajectory

Trajectory (newer = brighter) Current state Start of run (u = upright equilibrium at origin)

Cart and Pole Parameters

2.0 kg
0.2 kg
0.5 m
0.1
0.01

Controller

Controller mode

PID gains

30
0
0.5
20
Computed K (from sliders below):
F = −[Kp, Kθ, Kv, Kω] · [p, θ, v, ω]T
K = [-4.47, -87.29, -8.94, -18.44]
Closed-loop: −0.94±0.48j, −6.05±1.78j

LQR cost weights — drag to retune

Cost = pT Q p + R F². Q penalizes state error (cart position p, pole angle θ, cart velocity v, pole rate ω); R penalizes control effort. Higher Q_θ makes the controller more aggressive on pole angle; higher R makes it more conservative.

1
50
1
1
0.05
30 N
1×

Equations of Motion

The cart-pole has four state variables: cart position p, cart velocity v, pole angle theta (0 = straight up), and pole angular velocity omega. The control goal is to keep the pole upright (θ → 0); Kp/Ki/Kd act on θ. The cart position is uncontrolled and settles to whatever offset the balance requires. Equations follow Åström & Murray, Feedback Systems (Princeton, 2008), §2.2 Example 2.2 — the simplified pole-only form (eq. 2.10) is the exact rocket-stabilization controller this tool implements. The full coupled cart-pole EOMs (eq. 2.9 in Åström & Murray, or §5 in Spong, Hutchinson & Vidyasagar, Robot Modeling and Control, Wiley, 2006) are then pre-solved into the decoupled form shown below under a point-mass assumption for the pole (mass m concentrated at the center of mass a distance L from the pivot, so I = mL2). The slider labelled L is the half-length (pivot-to-CoM distance), not the full rod length. PID form from Åström & Hägglund, Advanced PID Control (ISA, 2006). The Åström Ex. 2.2 button above loads the textbook scenario.

Want the full picture? The Inverted Pendulum Modeling and Control tutorial walks through the derivation, the linearization, state-feedback design (pole placement and LQR), and why the simplified Ex. 2.2 form cannot by itself hold the cart still.

Symbol Definitions

Symbol Units Meaning
mCart position along the rail (positive = right of center)
m/sCart velocity
radPole angle from vertical (0 = upright, positive = falling to the right)
rad/secPole angular velocity
NControl force applied to the cart (clamped to )
NForce saturation limit (slider, default 30 N)
mSetpoint for cart position (0 by default)
kgCart mass (slider)
kgPole mass (slider)
mHalf-length of the pole (distance from pivot to center of mass; slider)
kg·m²Pole moment of inertia about the pivot. Point-mass assumption: mass m concentrated at the CoM a distance L from the pivot, so
m/s²Gravitational acceleration (9.81 m/s²)
N·s/mCart viscous friction coefficient (slider)
N·m·s/radPivot viscous friction coefficient (slider)
PID gains (sliders)
1/sDerivative-filter pole (slider). is the filtered pole angular velocity:
radPole-angle error (target 0; the PID acts on this). Equivalent to the pole angle θ in this controller.
rad·sIntegrated pole-angle error (anti-windup not shown in the compact form)
Cite this tool: Li, H. (2026). Inverted Pendulum Simulator. https://hongbinli.ca/tools/inverted-pendulum-simulator