LQR

From Bike Wiki
Revision as of 00:40, 18 May 2020 by Kunalroy (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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, desired lean rate, and desired lean angle. When doing straight line balancing these three values are 0, when doing navigation control, these values are calculated by the navigation controller. 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 the control equation where those inputs minimize the total error. This is the most important equation, the state variables are variables we can measure using the IMU, we then scale them with the K values and add them together to get the control input.

is given by the matlab function:

[K, S, e] = lqr(A, B, Q, R);

where the system is defined as

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