Balancing & Optimal Control, bonus step
Bonus: Balancing with noisy sensors
Every reading is noisy now. Filter it with the model you've used all along, and balance on the estimate: LQR plus a Kalman filter.
Builds on Combining two sensors and Sensors, rates & delay, from the free Foundations.
Write and run this step in the simulator with ProNoisy readings
So far cartpole.state() has told the truth. Now every reading has noise on it, with standard deviations of 2 mm on , 5 cm/s on , 0.01 rad (0.6°) on and 0.5 rad/s on . The rate readings are the worst. Working the rate out from two noisy angles instead doesn't help: has noise rad/s.
LQR multiplies every reading by its gain, noise and all. With Step 4's firm gains, half a radian per second of noise on turns into about 3 N of force chattering back and forth.
Filter with the model
You know how the cart-pole moves: . A Kalman filter keeps an estimate with a covariance (how unsure it is), and every period it
- predicts with the model and the force you applied:
- corrects with the new reading , which measures every state:
- , the process noise: how far the model may be off in one period.
- : the readings' noise.
- , the Kalman gain (not the LQR gain ): roughly, for each state, the share of the surprise to believe.
The model predicts the rates far better than the rate readings measure them, so the filter leans on the model there and on the readings for positions. Feeding to LQR is called LQG control. The update is the one from Foundations: Combining two sensors.
Your task
Implement kf_step(A, B, Qn, Rn, xhat, P, u, z) and return the new xhat, P.
The program balances for 3 s on raw readings, resets, and balances 3 s more on your estimates. The white pole is your estimate. To pass, after the first second your estimate must be within 0.15 rad/s RMS (root mean square) of the truth, and the force must chatter less than 1 N RMS, never over the motor's 10 N, with the pole within 5° of upright.