Previous post: Wind Tunnel in the Browser Next post: Heat Spread on a Grid

Pole Held Upright on a Cart

A cart-pole held up by fixed gains with true swing math, RK4 steps, and live trace plots on Canvas 2D.

Overview

A pole on a cart is a classic control task. Hold the pole up by pushing the cart side to side. The rig is top-heavy by build. The controller keeps working out the push needed to keep the high balance point.

This page runs true swing math with RK4 steps and draws on Canvas 2D. Click to tap the pole with a spin kick. Watch the controller catch it.

Swing Math

The rig has 2 free moves: cart place x and pole angle θ (gauged from hang-down, so θ=π is up). With the pole as a point mass, full swing math reads:

x¨=−m2L2gcos⁡θsin⁡θ+mL2(mLθ˙2sin⁡θ−dx˙)+mL2umL2(m+M(1−cos2⁡θ)) θ¨=(m+M)mgLsin⁡θ−mLcos⁡θ(mLθ˙2sin⁡θ−dx˙)−mLcos⁡θ⋅umL2(m+M(1−cos2⁡θ))

Here M is cart mass, m pole mass, L arm length, d cart drag, g gravity, and u push force. Code matches these forms in dynamics().

Gravity uses −10 as a sim sign choice. Down is baked into the angle gauge, so the sign looks odd but acts right.

Straight-Line Form

Near the high point (θ=π, all speeds zero), set ϕ=θ−π. For small shifts, sin⁡θ≈−ϕ, cos⁡θ≈−1, θ˙2≈0 trim the rig to:

𝐱˙=A𝐱+Bu

where 𝐱=[x,x˙,θ−π,θ˙]T and A plus B are fixed slope grids at the high point.

Controller

The goal scores place plus push:

J=∫0∞(𝐱TQ𝐱+u2R)dt

Best straight push is u=−K𝐱.

In code gains are fixed numbers, not solved live: K=[−100,−183.17,1683.18,646.61] with Q=I and R=0.0001. No Riccati solver runs in the browser. The numbers stand in for the solved gains.

Build

Item Value
M 5, cart mass
m 1, pole point mass
L 2 in physics, 3 in draw length (draw arm is longer for sight)
g −10 by sim sign
d 1, cart drag
Steps RK4, fixed 1/120 s, frame gaps capped at 0.05 s
Draw Canvas 2D, no libraries. Cart box, 2 wheels, arm line, end ball
Push Fixed gains above
Bounds World x ±6, y −3.5 to 5

Start is [−1,0,π+0.1,0] with a goal mark at [1,0,π,0] plus a goal line. The text before left out the goal mark.

Canvas 2D, not WebGL. The scene holds no 3D shapes or shaders. It draws a box, 2 rings, a line, and a ball. Canvas 2D holds 60 fps with ease and no libraries.

Live traces. Three traces (x−xref, θ−π, u) scroll in the lower pane over a 10 s span with fixed y spans [-3,3], [-0.5,0.5], [-1200,1200]. Backing store holds 1200 ticks. Tags read x−1/θ−π/u with dash plus hue codes.

Click taps. Clicks add a spin kick to the pole to fake a knock. Kick size is random with range by nearness: reach under 1.5 gives size 1.0 else 2.0, kick is (rand − 0.5) × size added to spin speed. The cart then speeds to catch the falling pole. Pivot and cart sizes shape the feel but add no physics.

Honest Limits

Fixed gains near the upright position only. The controller holds the high balance point and does not swing the pole up. The straight-line model holds for small shifts around θ=π. Draw arm length differs from physics length for sight. Click kicks probe recovery, not a rated disturbance.

References