demoAIRobotics
LQR computes the cart-pole's gains from the Riccati equation
The PD-controlled cart-pole has four gains that were tuned by hand. Linearize the same dynamics, choose how much each state error and each unit of push should cost, and the Riccati equation gives the gains instead. From 25 degrees they land the pole thirteen times closer to upright than the hand-tuned ones, for a third less cost and about the same peak push.
Open in the workbenchDownload .vivOutputs below are from the site build’s run; Run all runs it again in your browser.
Look inside this program
- Atlas
The program’s map: every function and which calls which.
There’s no recorded run of this program yet.
Linearize the cart-pole about the upright and you have . Pick for how much you dislike each state and for how much you dislike pushing, and the gains that minimize are , where solves the algebraic Riccati equation. The only choices made by hand are and , and they state the tradeoff directly: angle error against cart position, and state error against force.
and are not written out here. They are computed from the library's own
cartpole_rate by central differences, so the linear model matches the
nonlinear one this post simulates, and the first test checks that they agree
near upright.
comes from integrating the differential Riccati equation backwards until it stops changing, which takes only matrix multiplication and addition. To check it, put back into the algebraic equation: the largest entry of the residual is .
What comes out, on this cart-pole with these weights: , and a closed loop whose slowest mode decays like . Simulating the nonlinear cart-pole for six seconds from 25 degrees, those gains end 0.00096 rad from upright against the hand-tuned gains' 0.0124, thirteen times closer, with an integrated cost of 6.13 against 8.98 and a peak force of 27.5 N against 26.2. The hand-tuned gains also balance the pole; the Riccati gains are the ones that minimize the cost set by and .
Printed
Tests 5 of 5 pass
- the linear model A z + B u matches the nonlinear rate near upright (passes)
- P solves the algebraic Riccati equation: the residual is under 1e-8 (passes)
- every closed-loop eigenvalue has a negative real part (passes)
- from 25 degrees the pole ends upright, within a thousandth of a radian (passes)
- the computed gains cost less than the hand-tuned ones on the same start (passes)