Level 8 (after Lessons 3.2, 8.1 and 8.2; Level 21.4 for the theory) · Time 3 to 4 hours · Difficulty 3 of 5
Balance the course cart-pole (models/cartpole.xml) upright from a tilted start by linear-quadratic regulation, using MuJoCo itself to compute the linear model. You will see that MuJoCo is a source of models for control design, not only a place to test controllers.
mj_forward), Lesson 3.2 (where this balance law first appears).scipy.linalg.solve_discrete_are.starter.py asks for three functions:
linearize(model, qpos): the discrete-time $A$ and $B$ of one mj_step around an equilibrium, from mujoco.mjd_transitionFD (finite differences of the transition map).lqr_gain(a, b, q, r): the infinite-horizon discrete LQR gain $K$ with $u = -Kx$.controller(k, data): the cart force for the current state $x = [q_\text{cart}, q_\text{pole}, \dot q_\text{cart}, \dot q_\text{pole}]$.The tests use $Q = \mathrm{diag}(1, 10, 0.1, 0.1)$ and $R = 0.01$.
The linear model predicts one simulated step from a small perturbation to within $2 \times 10^{-5}$. The closed-loop matrix $A - BK$ has all eigenvalues inside the unit circle. From a 0.15 rad tilt the pole never exceeds 0.2 rad, the cart stays within 1.4 m of the centre (the rail ends at 1.5 m), the force never exceeds the actuator’s 20 N, and after 10 s the pole is upright to $10^{-3}$ rad. The same gains still balance a pole 50% heavier than the one they were designed for.
MJC_IMPL=starter pytest projects/p02_cartpole
pytest projects/p02_cartpole
cartpole.xml the pole angle is 0 when upright; linearizing at the hanging position ($\pi$) gives a stable linear model and a controller that does nothing useful upright.scipy returns $P$; the gain is $K = (R + B^\top P B)^{-1} B^\top P A$ and the control is $u = -Kx$.mjd_transitionFD with eps around $10^{-6}$ is accurate here; check with the one-step prediction test.delay and nsample on the motor, MuJoCo 3.5+) and find the largest delay the LQR controller tolerates. Compare with a prediction from the linear model.solution.py, about 25 lines.