Our approach to the Cartpole is not black box. A Cartpole is a pretty simple system all things considered.
The first thing to do is derive the equations of motion. Originally I was using the Lagrangian for the system and deriving the equations of motion that way, which includes the back reaction of the pole back on the acceleration of the cart for example.
But the motor complicates things. I have a tough time in general modeling motors. What physical thing does a command to our motor driver correspond to? Velocity? Power? Torque? I have guesses. The easiest thing to do is just build and measure. It turns out for us that the commands are basically velocity control.
So in our case the back reaction is basically irrelevant. We have direct control over the cart velocity. In that case, one can use some easy enough hand waving to get the equations of motion.
Let’s set the angle at pole down with positive angle going counter clockwise. We can assume that gravity acts at the center of mass of the pole, which is at the midpoint. This gives a torque . One way to see the negative sign is that a slight positive angle should give a negative torque returning it back to the down position. The moment of inertia of the pole is . You can look this up as a pole around one of it’s ends or derive it from . The comes from the integration of the . Putting these together we get .
Now we need to actually put in the cart stuff. The cart puts the pole in an accelerating frame, where basically you have a new component of gravity that points horizontally. This adds a torque . As far as all of the signs go, honestly we just fiddled with them until it worked.
Now that we have all that in hand, we can talk about the Linear Quadratic Regulator (LQR) control.
The model that LQR uses is that the equations of motion are linear and the cost function that you want to minimize are quadratic in the controls and state . This is plausibly tractable because Quadratic and linear stuff is usually ok. I’m serious.
These letter choices for the various bits are pretty standard.
If you just look at the wikipedia page, you can already just plug and chug to the solution.
Jesus that equation looks like shit.
which is QUITE CONVENIENTLY solved by the following scipy function https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.linalg.solve_continuous_are.html
Now given this shit, we need to approximate our nonlinear equations of motion with linear equations in order to use the LQR framework.
Near the top where we are linearizing
We can move some of those constants to the other side to get by itself.
Another thing you have to do is massage these second order equations into first order form. You do this by introducing a new state variable
In matrix form this is
In addition to this, it is nice to add the cart dynamics, even though they are pretty trivial. This is because we can then add some weighting terms to discourage the cart from trying to go off the track or go faster than the motors support. There are ways to make it so that the cartpole never tries to go out of bounds, but they are a bit more complicated. I’ve got some blog posts about them.
So we can read off our needed matrices A and B from here.
Now in regards to the weighting matrices Q and R, this is a bit tougher to say what we want. We sort of want all the state variables to be small but the relative important isn’t a priori clear to me. So we picked diagonal matrices and tried out some different values. One thing to note though is that the states variables could possibly have very different scales, since their units are different. The characteristic time of the system is . The characteristic length is the size of our track and the rough angle scale is -ish.
Now that we have our matrices we can plug it all into scipy and use it!
One thing to be careful about is that the pole can have swung around multiple times leading to the angle being some multiple of . Our hack around this is to just take the of the angle.