Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 37 · Projects and Practice

Project: Inverted Pendulum with LQR

Balance an inverted pendulum on a cart: derive the model, linearize it, design an LQR controller, and test it on the full nonlinear dynamics to find its region of attraction.

Balancing a broom on your hand is the canonical unstable control problem. The cart-pole (inverted pendulum on a cart) is its laboratory version: a cart moves along a track, a pole is hinged on top, and the only input is a horizontal force on the cart. The upright position is an equilibrium, but an unstable one: any small tilt grows exponentially unless the controller reacts quickly and correctly. This project walks through the standard recipe: model, linearize, design with LQR, verify on the nonlinear plant.

It uses state-space models, controllability and LQR.

flowchart LR
    A["Nonlinear model (Lagrange)"] --> B["Linearize near upright"]
    B --> C["Check controllability"]
    C --> D["LQR: choose Q, R"]
    D --> E["Simulate on the nonlinear model"]
    E -->|"fails?"| D

The design workflow: linearize to design, then test on the real nonlinear model.

Step 1: the model

Take a cart of mass M and a point mass m at the end of a massless rod of length l. Let x be the cart position and θ the rod angle measured from the upright position. The Lagrange equations of motion are

TEXT
(M + m)·x'' + m·l·cos θ·θ'' - m·l·sin θ·θ'^2 = u
        cos θ·x'' + l·θ'' = g·sin θ

with the force u on the cart as the input. We use M = 1 kg, m = 0.1 kg, l = 0.5 m, g = 9.81 m/s².

Step 2: linearize and check

Near upright (θ small): sin θ ≈ θ, cos θ ≈ 1, and the quadratic term θ'^2 vanishes. The state (x, x', θ, θ') then obeys x' = Ax + Bu with

TEXT
A = [[0, 1, 0, 0],
     [0, 0, -m g / M, 0],
     [0, 0, 0, 1],
     [0, 0, (M + m) g / (M l), 0]]      B = [0, 1/M, 0, -1/(M l)]ᵀ

The open-loop poles are 0, 0 (the cart can drift anywhere) and ±√((M+m) g/(M l)) = ±4.65 rad/s. The positive one means the pole falls with a time constant of about 1/4.65 = 0.2 s: the controller has to be fast. The system is controllable (rank 4), so full-state feedback can stabilize it.

Step 3: design the LQR

We want a small angle error (large weight on θ), a modest cart error, and a force that stays inside what a real motor can deliver. Take Q = diag(1, 1, 10, 1) and R = 0.1.

Python Linearize, check controllability, solve the Riccati equation

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

All four closed-loop poles are real and stable, between -1.2 and -8.8. Since K multiplies the state in u = -Kx, the large θ coefficient (-46) says "push the cart hard in the direction the pole leans", the standard balancing rule: to catch a falling pole you must move the base under it.

Step 4: test on the nonlinear plant

A linear design is only valid near the equilibrium. The real test is the full model: solve for x'' and θ'' from the 2×2 linear system above at every step, integrate with RK4, and clip the force to ±20 N, as a real motor would. We start the pole tilted at several angles and see which are recovered.

Python Nonlinear simulation with force limits

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The controller recovers every start angle up to 45° (the linear model is accurate only to about 20 degrees, but LQR's robustness carries it further). At 60° the force limit and the nonlinearity win and the pendulum falls. The range of initial states that can be recovered is the region of attraction; knowing it is a design deliverable.

Look at the peak force column: small tilts need only a few newtons, while 30° and above saturate the 20 N limit. The tilt angle that causes saturation sets a practical limit that no linear design can overcome.

Step 5: improve and extend

Try these experiments in the editors above:

  1. Change Q. Raise the cart-position weight (the first entry) and see how the cart stays closer to the origin at the cost of a larger angle swing.
  2. Change R. Make the force more expensive (R = 1) and find the largest recoverable initial angle again.
  3. Add measurement noise and an observer: measure only x and θ, estimate the velocities.
  4. Tracking. Add a reference for x via u = -K(x - x_ref) and see how fast the cart can move without losing the pole.
  5. Swing-up. The LQR only works near upright; a swing-up controller (energy shaping) brings the pole from hanging to the neighborhood where LQR can take over.
  6. Friction and delays. Add cart friction and a 10 ms actuation delay; check the largest delay the design tolerates.

The cart's position is a marginally stable mode: the pole is stabilized, but the cart wanders slowly unless x has a nonzero weight in Q. That is why including the cart states in Q matters even though the unstable mode is the pole angle.

What you learned

  • Unstable plants need feedback just to exist; the speed of the unstable pole (4.65 rad/s) sets how fast the controller must be.
  • The workflow is always: nonlinear model, linearize, check controllability, design, then test on the nonlinear plant with actuator limits.
  • LQR gives a stabilizing gain from weights; the state weights mirror what you care about.
  • The region of attraction is finite: linear design plus saturation gives a bounded set of recoverable starts.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. The linearized pendulum has an open-loop pole at +4.65 rad/s. What does it mean?

    A pole at +p gives e^(p t) growth, a time constant of 1/p = 0.2 s.
  2. 2. Why test the LQR design on the nonlinear model?

    The linear design assumes small angles and unlimited force; the nonlinear test reveals the real region of attraction.
  3. 3. What does a large weight on theta in Q do?

    A larger state weight makes deviations in that state more expensive, so LQR reacts harder to them.
  4. 4. Why include the cart position in Q?

    The pole can be balanced while the cart slowly drifts; weighting x gives a controller that also centers the cart.

Summary

  • The cart-pole has two integrators and one unstable pole pair at ±4.65; it is controllable by force alone.
  • Linearize about upright, design LQR with Q = diag(1, 1, 10, 1) and R = 0.1, and verify on the nonlinear plant with force limits.
  • Recovery works for tilts up to about 45° in this setup and fails at 60°.
  • The workflow generalizes to drones, rockets and balancing robots.

Next: Control Systems Practice Problems