ENع
←Inverted pendulum
Lesson S3 · Special topics

Balancing an inverted pendulum

16 min10 steps5 questionsIntermediate
About this lesson

A pole on a cart falls over in under a second if nobody acts. Model the rig, linearise it about upright, measure its four-number state and place the closed-loop poles with state feedback u = −Kx. The figure runs the full nonlinear simulation: the same 5.7° lean that falls flat in 0.76 s is caught and the cart brought home.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig S3 · Interactive
01 / 10

A pole on a cart

A 0.5 m pole stands on a free pivot on a cart. Our job: keep it upright by pushing the cart left and right. To start, it leans 0.1 rad, about 5.7°, to the right.

cart     M = 1 kg on a straight track
pole     a light rod, l = 0.5 m
bob      m = 0.1 kg at the top
pivot    free: nothing holds the pole up

lean     θ = 0.1 rad (5.7°), + = to the right

The only thing we can touch is the cart.
Loading figure…
or ← →
Read the lesson step by step
  1. A pole on a cart

    A 0.5 m pole stands on a free pivot on a cart. Our job: keep it upright by pushing the cart left and right. To start, it leans 0.1 rad, about 5.7°, to the right.

    cart     M = 1 kg on a straight track
    pole     a light rod, l = 0.5 m
    bob      m = 0.1 kg at the top
    pivot    free: nothing holds the pole up
    
    lean     θ = 0.1 rad (5.7°), + = to the right
    
    The only thing we can touch is the cart.
  2. Upright is unstable

    With no control, gravity pulls the bob further the more it leans. The lean passes 30° after 0.51 s, and the pole lies flat after 0.76 s. Upright is an unstable equilibrium: balanced, but any nudge grows.

    u = 0 N (motor off), start θ = 5.7°
    
    θ = 30°   after 0.51 s
    θ = 90°   after 0.76 s (flat)
    
    equilibrium   a pose where nothing changes
    stable        a nudge dies away (a hanging
                  pendulum)
    unstable      a nudge grows (this one)
  3. Small leans, straight lines

    Near upright, sin θ ≈ θ (0.17% off at 0.1 rad). The equations turn linear: θ̈ = 21.582·θ − 2u. With u = 0, one system pole (a growth rate, not the rod) is +4.65 /s. Positive means growth: the lean doubles every 0.15 s.

    full:     l·θ̈ = g·sin θ − ẍ·cos θ
              (M + m)ẍ + m·l·θ̈·cos θ − m·l·θ̇²·sin θ = u
    linear:   ẍ = u − 0.981·θ
              θ̈ = 21.582·θ − 2·u
    
    open-loop poles  0, 0, ±√21.582 = ±4.65 rad/s
    +4.65 > 0  →  e^(4.65·t) grows:
    doubles every ln 2 / 4.65 = 0.15 s
  4. The only handle: the cart

    A motor drives the cart along the track with a belt, pushing with force u. Push the cart right and the pivot moves under the bob, swinging the pole back to the left. The cart is the only handle we have.

    u > 0   push right:  ẍ = u − 0.981·θ   cart speeds up
                         θ̈ = 21.582·θ − 2·u  lean falls
    
    Like balancing a broom on your hand:
    move your hand towards the side it is
    falling to, and get under it.
  5. Four numbers say everything

    Two encoders count how far the motor and the pivot have turned, giving the cart position x and the lean θ. Their rates give the speeds v and ω. These four numbers are the state: given the force u, they fix everything that happens next.

    state  s = [x, v, θ, ω]
      x  cart position      0 m
      v  cart speed         0 m/s
      θ  lean               0.1 rad
      ω  lean rate          0 rad/s
    
    ṡ = A·s + B·u
    A = [[0, 1, 0,      0],
         [0, 0, −0.981, 0],
         [0, 0, 0,      1],
         [0, 0, 21.582, 0]]
    B = [0, 1, 0, −2]ᵀ
  6. Choose where the poles go

    Feed all four back: u = −K·s. Each gain moves the closed-loop poles. We choose −2, −3, −4 and −5, all in the left half, and match coefficients to get K. This is pole placement.

    det(sI − A + BK) = s⁴ + (k2 − 2k4)s³
       + (k1 − 2k3 − 21.582)s² − 19.62·k2·s − 19.62·k1
    want (s+2)(s+3)(s+4)(s+5)
       = s⁴ + 14s³ + 71s² + 154s + 120
    
    k1 = −120/19.62 = -6.116
    k2 = −154/19.62 = -7.849
    k3 = -49.349   k4 = -10.925
    
    # python-control: K = control.place(A, B, [-2, -3, -4, -5])
    # works because the system is controllable (rank 4)
  7. The rule in one line

    At the start only θ is non-zero, so u = 49.3 × 0.1 = 4.93 N: push right, towards the lean. The controller reads the state and recomputes u every millisecond.

    u = −(k1·x + k2·v + k3·θ + k4·ω)
      = −(−6.1·0 + −7.8·0 + −49.3·0.1 + −10.9·0)
      = +4.93 N   (push right)
    
    The big angle gain does most of the
    work; the small position gains bring the
    cart home afterwards.
  8. Catch the lean

    The cart darts right under the bob. The lean crosses zero after 0.28 s, swings to −2.5° at 0.6 s, then settles. The same start that fell over in 0.76 s now stands.

    # balance the pole: nonlinear cart-pole, RK4, 1 ms
    import math
    M, m, l, g, dt = 1.0, 0.1, 0.5, 9.81, 0.001
    k = [-6.1162079510703364, -7.8491335372069315, -49.349103975535172, -10.924566768603466]
    def f(s, u):
        x, v, th, w = s
        S, C = math.sin(th), math.cos(th)
        xdd = (u + m*S*(l*w*w - g*C)) / (M + m*S*S)
        return [v, xdd, w, (g*S - xdd*C) / l]
    s = [0.0, 0.0, 0.1, 0.0]
    for n in range(1000):          # 1 s
        u = -(k[0]*s[0] + k[1]*s[1] + k[2]*s[2] + k[3]*s[3])
        k1 = f(s, u)
        k2 = f([a + dt/2*b for a, b in zip(s, k1)], u)
        k3 = f([a + dt/2*b for a, b in zip(s, k2)], u)
        k4 = f([a + dt*b for a, b in zip(s, k3)], u)
        s = [a + dt/6*(p + 2*q + 2*r + z)
             for a, p, q, r, z in zip(s, k1, k2, k3, k4)]
    print(round(math.degrees(s[2]), 1))   # -0.9
  9. Bring the cart home

    Catching the pole carried the cart 14 cm to the right (most at 0.7 s). The position gains then ease it back, slowly, because −2 is the slowest pole we chose. It is back within 1 mm after 3.8 s.

    max cart travel  14 cm at 0.7 s
    |x| < 1 mm       from t = 3.8 s on
    
    slowest pole −2 → e^(−2t): the last
    bit of error halves every 0.35 s.
    Faster poles bring it home sooner but
    ask the motor for bigger forces.
  10. What the model leaves out

    The gains came from the linear model, yet they balance the full nonlinear one here. That holds only for small leans. Real rigs also have motor force limits, friction, noise and unmeasured speeds, which an observer can estimate.

    designed on   the linear model
    tested on     the full nonlinear one
    peak force    4.93 N at t = 0
    
    left out:
      motor saturation (a big lean needs
      more force than the motor has)
      track friction, belt stretch
      encoder noise; v and ω estimated
      by differencing or an observer

Quiz Unlocks after the last step.