Difference between revisions of "LQR"

From Bike Wiki
Jump to navigation Jump to search
(+ see also)
Line 1: Line 1:
 +
==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, <math>\vec{x} = \begin{bmatrix}\phi & \dot{\phi} & \delta\end{bmatrix}^\intercal</math>. Thus, our total error function is:
 
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, <math>\vec{x} = \begin{bmatrix}\phi & \dot{\phi} & \delta\end{bmatrix}^\intercal</math>. Thus, our total error function is:
  
Line 5: Line 9:
 
where (when we use it) <math>\vec{u}</math> is <math>[\dot{\delta}]</math> and <math>R</math> is <math>[0.01]</math>. Also, <math>Q</math> is: <math>Q = \begin{bmatrix} 1&0&0\\0&0.01&0\\0&0&0.01\end{bmatrix}</math> Your mileage may vary. Try changing these weights and seeing what happens.
 
where (when we use it) <math>\vec{u}</math> is <math>[\dot{\delta}]</math> and <math>R</math> is <math>[0.01]</math>. Also, <math>Q</math> is: <math>Q = \begin{bmatrix} 1&0&0\\0&0.01&0\\0&0&0.01\end{bmatrix}</math> Your mileage may vary. Try changing these weights and seeing what happens.
  
<hr />
+
LQR gives us a K vector that satisfies <math>\vec{u} = -K\vec{x}</math> where those u inputs minimize the total error. <math>K</math> is given by the matlab function:
 
 
Ryan thinks we really have to ensure that each state must have a nonzero cost, because LQR will eventually minimize cost. If we can possibly get negative cost somehow, then LQR would just give us that state, which sucks.
 
 
 
So. What LQR does is it says we must use the control law <math>\vec{u} = -K\vec{x}</math>, where you get <math>K</math> using matlab using the code:
 
  
 
  [K, S, e] = lqr(A, B, Q, R);
 
  [K, S, e] = lqr(A, B, Q, R);
  
 
where the system is defined as <math>\dot{\vec{x}} = A\vec{x} + B\vec{u}</math>
 
where the system is defined as <math>\dot{\vec{x}} = A\vec{x} + B\vec{u}</math>
 +
U 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.!!!!!!!!!!!!!
  
On the bike, it is possible that we can use the control law <math>\vec{u} = K\vec{x}</math>
+
==Why LQR?==
 
 
<hr />
 
  
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 (we just want to avoid this!).
+
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 ==
 
== See also ==
 
* [[Matlab code]], where the LQR controllers and everything else get tested
 
* [[Matlab code]], where the LQR controllers and everything else get tested

Revision as of 19:58, 14 May 2020

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 u inputs minimize the total error. is given by the matlab function:

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

where the system is defined as U 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