Web Simulation 

 

 

 

 

Inverted Pendulum Control Tutorial 

This tutorial visualizes a cart-pole control problem. The pendulum starts hanging downward at rest. The goal is to swing the pendulum up and then keep it upright. The final cart goal position is shown from the beginning. After the pendulum is caught near upright, the controller uses an active reference xref that ramps from the initial cart position toward the final cart goal.

Mathematical Foundation

The state vector packs cart position/velocity and pendulum angle/angular-velocity:

state = [x, x', θ, θ']

x is cart position, x' is cart velocity. θ is the pendulum angle measured from upright, so θ = 0 means upright and θ = π means hanging downward. θ' is angular velocity.

Cart-Pole Dynamics

The simulator uses a standard nonlinear cart-pole model. With cart force F:

temp = (F + m L (θ')2 sinθ) / (M + m)
θ'' = (g sinθ − cosθ · temp) / (L (4/3 − m cos2θ / (M + m)))
x'' = temp − m L θ'' cosθ / (M + m)

Euler integration advances the state each step Δt:

x' ← x' + x'' Δt,   x ← x + x' Δt
θ' ← θ' + θ'' Δt,   θ ← θ + θ' Δt

Hybrid Controller Mechanism

Because the initial state is hanging downward, a simple upright stabilizer cannot work by itself. The simulator uses two modes:

  1. Swing-up mode: pump energy into the pendulum until it is close to upright.
  2. Balance mode: switch to state feedback to hold the pendulum upright and move the cart toward its goal.

Swing-up drives the pendulum energy toward the upright energy:

E = 0.5 m (L θ')2 + m g L cosθ,   Edesired = m g L
Fswing = KE (E − Edesired) θ' cosθ − Kxs x − Kvs x'
Why the startup nudge: if the pendulum is exactly hanging down with zero angular velocity, that is a symmetric equilibrium — no controller can infer which direction to swing first, so the simulator adds a small startup nudge to break the symmetry.

Balance mode switches to state feedback once the pole is near upright:

F = Kθ θerr + Kω θ' + Kx (x − xref) + Kv x'

Balance-mode interpretation:

  • Kθ θerr pushes the cart to catch the falling pole.
  • Kω θ' damps angular motion.
  • Kx (x - xref) contributes cart-position feedback in the stabilizing state-feedback convention used by this cart-pole model.
  • Kv x' contributes cart-velocity feedback in the same convention.

Worked balance example. With θerr = 0.04 rad, θ' = −0.10 rad/s, x = 0.20 m, xref = 1.00 m, x' = 0.30 m/s:

F = 95(0.04) + 18(−0.10) + 18(0.20 − 1.00) + 12(0.30)

F = 3.80 − 1.80 − 14.40 + 3.60 = −8.80 N

What The Plots Show

The animation shows the cart and pendulum. The plots show whether control is working:

  • Angle plot: θerr starts near π because the pendulum is downward, then should approach zero after swing-up.
  • Cart plot: x should approach the active reference xref, and xref should approach the selected final goal.
  • Force plot: shows how hard the actuator is working.

Simulation

The interactive simulator is below. Use the controls to explore the concepts described above.

balanced
1.00 m
95
18
18
12
0.0 N
45 N
starts hanging down

Animated Cart And Pendulum

Angle error from upright

Cart position x(t)

Control force F(t)

Live Calculation

 

Usage Instructions

  1. Press Run: The system starts with x = 0, x' = 0, θ = π, and θ' = 0. The pendulum is hanging down. Swing-up mode first moves the cart to raise the pendulum. Balance mode takes over near upright.
  2. Watch angle: The angle error starts near pi. After swing-up, a good controller brings it close to zero.
  3. Change gains: Increase K theta for stronger pole correction, K omega for angular damping, K x for stronger cart-position tracking, and K v for cart damping.
  4. Use disturbance: Add a constant external force and observe how the controller compensates.
  5. Use force limit: Lower limits make the actuator weaker. If the force saturates too much, the pole may fall.
  6. Manual perturbation: While the simulation is stopped or running, drag the pendulum bob or rod in the animation canvas. The simulator immediately changes the pendulum angle and estimates angular velocity from the drag motion.
  7. Step controls: Step Fwd advances one time step. Step Bwd restores the previous saved state. Run toggles continuous simulation.

Important Simplifications

This is an educational control simulation. The model is deliberately idealized:

  • Simplified cart-pole model. A point-mass bob on a massless rigid rod with no rod inertia, no air drag, and only viscous/no friction at the cart — real hardware has belt compliance, bearing friction, and a distributed-mass pole.
  • Explicit Euler integration. First-order stepping at a fixed Δt; large steps or stiff gains can inject numerical energy, so the dynamics are approximate near fast transients.
  • Heuristic swing-up. The energy-pumping law with a startup nudge is a hand-tuned heuristic, not a provably optimal or time-minimal swing-up.
  • Hand-tuned linear balance gains. The Kθ/Kω/Kx/Kv state feedback is manually tuned, not derived from a full LQR/pole-placement design with a cost function.
  • Ideal actuator and sensing. No motor lag, force saturation dynamics (beyond a hard limit), encoder quantization, measurement noise, or state estimation — the controller sees the exact state.
  • No physical constraints. No rail end-stop collisions, no actuator thermal limits, and an effectively unbounded track.