LQR Inverted Pendulum

2026 · Hardware & Control Systems Engineer

LQR Inverted Pendulum

Real-time cart-pole balance and disturbance rejection on an RP2040

The inverted pendulum is a classic controls problem. An inherently unstable, nonlinear, underactuated system that needs to be stabilized. I built this physical benchtop model from scratch to test all control theory i've learned from my time competing in robotics.

Controls

  • LQR: Truthfully, I have not learnt enough math and physics to derive the equations of motion for the inverted pendulum. I threw the problem at a LLM and it wrote a program to take physical parameters and spit out kx,kv,kθ,kωk_x, k_v, k_{\theta}, k_{\omega}. Great starting point, but the LQR controller was not robust to real-world disturbances. I had to tune the gains manually to get a stable system.
  • The Challenge: I had chosen to use an arm to make the challenge as hard a possible. A 9 gram arm that tapered to a point, with a small moment of inertia, and a long length.

Embedded Implementation (RP2040 C/C++ SDK)

  • Hardware PIO Encoders: Configured RP2040 Programmable I/O (PIO) state machines to continuously decode quadrature pulses from both the cart and pendulum optical encoders. This eliminated CPU interrupt overhead and guaranteed zero missed ticks during high-velocity swings.
  • Velocity Estimation & Filtering: Raw finite-difference velocity calculation (Δθ/Δt)(\Delta \theta / \Delta t) amplified discrete quantization noise. I used a library to that filtered the encoder readings and estimated velocity using a discrete Kalman filter.

Physical Results

  • Recovers reliably from external physical taps and disturbance impulses.
  • Maintains continuous stable balance along the linear rail indefinitely with zero steady-state drift.
  • Full CAD models, dynamic simulation scripts, and C++ firmware are published on GitHub.

Stack

Control & Mathematics

Lagrangian DynamicsState-Space LinearizationLQR Optimal ControlRiccati EquationDiscrete Filtering

Embedded & Firmware

Raspberry Pi Pico (RP2040)RP2040 C/C++ SDKPIO Hardware EncodersTMC2209 Stepper DriverOptical Quadrature Encoders