LQR
Contents
What is LQR?
The Linear-quadratic regulator algorithm is used to minimize a cost function for a system. The cost function is specified by the engineer an calculates deviations from desired values of key measurements, in our balance controller, we are calculating the deviations from desired steer angle (0), desired lean rate(0), and desired lean angle(0). The cost function has two main parameters, Q and R. Q penalizes errors in state, so you have to tune your Q, to which measurement is more important for your system. R penalizes errors in input usage. So in most scenarios, having a higher R value would means that you are trying to use less input magnitude(less energy) to get to your ideal state. LQR can be used when the system dynamics are described by a set of linear differential equations. The cost is described as a quadratic function, because the LQR contains an integral that needs to be always positive to work effectively. Because the cost function is quadratic, the LQR penalizes deviations much stronger than if the cost function was left as is. The next section goes more in depth to how we use the LQR to control the bike.
Our LQR
When we use LQR, we penalize any deviations from an upright riding position. Q penalizes errors in the state. The state matrix has lean, lean rate, and delta as x1, x2, and x3 respectively. So, . Thus, our total error function is:
where (when we use it) is and is . Also, is: Your mileage may vary. Try changing these weights and seeing what happens.
LQR gives us a K vector that satisfies where those is given by the matlab function:
[K, S, e] = lqr(A, B, Q, R);
where the system is defined as <math>\vec{u}<math> is the control variable, in our case it is steer angle, as seen by the equations above, the next steer angle value is calculated by using current state and the K vector. And the expected next state is calculated using the current state and the next steer angle. The values in the state vector are values that we can measure using encoders and an IMU. See IMU page.!!!!!!!!!!!!!
Why LQR?
What's good about LQR? So you can tune the cost matrices (Q and R) instead of tuning the gains directly. The cost matrices have easier intuition. Tuning the K matrices directly doesn't have any immediately recognizable relation. However tuning the cost matrices directly tells us what we are penalizing in our control algorithm.
See also
- Matlab code, where the LQR controllers and everything else get tested