Austin Schuh | 2e28d87 | 2022-02-19 18:25:57 -0800 | [diff] [blame] | 1 | \documentclass[a4paper,12pt]{article} |
| 2 | \usepackage{amsmath} |
| 3 | \usepackage{graphicx} |
| 4 | \begin{document} |
| 5 | |
| 6 | TODO(austin): Now that the python matches the original problem and solves, confirm the paper matches what got implemented. |
| 7 | |
| 8 | osqp! |
| 9 | |
| 10 | \section{Catapult MPC} |
| 11 | We want to phrase our problem as trying to solve for the set of control inputs |
| 12 | which get us closest to the destination, but minimizes acceleration. |
| 13 | Specifically, we want to minimize acceleration close to the end. |
| 14 | We also have a voltage limit. |
| 15 | |
| 16 | Our model is |
| 17 | |
| 18 | \begin{equation} |
| 19 | \label{cost} |
| 20 | \begin{bmatrix} x_1 \\ v_1 \end{bmatrix} = |
| 21 | \begin{bmatrix} a_{00} & a_{01} \\ 0 & a_{11} \end{bmatrix} |
| 22 | \begin{bmatrix} x_0 \\ v_0 \end{bmatrix} + |
| 23 | \begin{bmatrix} b_{0} \\ b_{1} \end{bmatrix} \begin{bmatrix} u_0 \end{bmatrix} |
| 24 | \end{equation} |
| 25 | |
| 26 | Our acceleration can be measured as: |
| 27 | |
| 28 | \begin{equation} |
| 29 | \label{accel} |
| 30 | \frac{ \left( \boldsymbol{X_1(1)} - \boldsymbol{X_1(0)} \right)}{\Delta t} |
| 31 | \end{equation} |
| 32 | |
| 33 | This simplifies to: |
| 34 | |
| 35 | \begin{equation} |
| 36 | \frac{a_{11} v_0 + b_1 u_0 - v_0}{\Delta t} |
| 37 | \end{equation} |
| 38 | |
| 39 | and finally |
| 40 | |
| 41 | \begin{equation} |
| 42 | \frac{(a_{11} - 1) v_0 + b_1 u_0}{\Delta t} |
| 43 | \end{equation} |
| 44 | |
| 45 | |
| 46 | We can also compute our state matrix as a function of inital state and the control inputs. |
| 47 | |
| 48 | \begin{equation} |
| 49 | \label{all_x} |
| 50 | \begin{bmatrix} X_1 \\ X_2 \\ X_3 \\ \vdots \end{bmatrix} = |
| 51 | \begin{bmatrix} A \\ |
| 52 | A^2 \\ |
| 53 | A^3 \\ |
| 54 | \vdots \end{bmatrix} |
| 55 | X_0 + |
| 56 | \begin{bmatrix} B & 0 & 0 & 0 \\ |
| 57 | A B & B & 0 & 0 \\ |
| 58 | A^2 B & A B & B & 0 \\ |
| 59 | \vdots & \ddots & & \hdots \end{bmatrix} |
| 60 | \begin{bmatrix} U_0 \\ U_1 \\ U_2 \\ \vdots \end{bmatrix} |
| 61 | \end{equation} |
| 62 | |
| 63 | \section{MPC problem formulation} |
| 64 | |
| 65 | We want to penalize both final state and intermediate acceleration. |
| 66 | |
| 67 | \begin{equation} |
| 68 | C = \sum_{n=0}^{39} \frac{\left(v(n + 1) - v(n)\right)^2}{\Delta t} \pi_n + (X_{40} - X_{final})^T Q_{final} (X_{40} - X_{final}) |
| 69 | \end{equation} |
| 70 | |
| 71 | where $\pi_n$ is a constant only dependent on $n$, and designed such that it depends on the distance to the end of the sequence, not the distance from the start. |
| 72 | |
| 73 | In order to use OSQP, which has a code generator, we need to get this into the form of |
| 74 | |
| 75 | \begin{tabular}{ l l } |
| 76 | minimize & $ \frac{1}{2} X^T P X + q^T X $ \\ |
| 77 | subject to & $ l <= A X <= u $ \\ |
| 78 | \end{tabular} |
| 79 | |
| 80 | This is the simplest form of a constrained quadratic program that we can solve efficiently. |
| 81 | Luckily for us, the problem statement above fits that definition. |
| 82 | |
| 83 | \section{Manipulating the formulation} |
| 84 | |
| 85 | We need to separate constant factors from things dependent on U (or X in OSQP parlance) so we can create these matrices easier. |
| 86 | |
| 87 | \subsection{Terminal cost} |
| 88 | |
| 89 | Next step is to compute $X_{40}$ using equation \ref{all_x}. |
| 90 | We can do this by only computing the final row of the matrix. |
| 91 | |
| 92 | \begin{equation} |
| 93 | \label{x40} |
| 94 | X_{40} = \begin{bmatrix} A^{39} B & A^{38} B & \hdots & B \end{bmatrix} |
| 95 | \begin{bmatrix} U_0 \\ |
| 96 | \vdots \\ |
| 97 | U_{39} |
| 98 | \end{bmatrix} + A^{40} X_0 = B_f \boldsymbol{U} + A^{40} X_0 |
| 99 | \end{equation} |
| 100 | |
| 101 | We can substitute equation \ref{x40} into equation \ref{cost}. |
| 102 | |
| 103 | \begin{equation} |
| 104 | \label{final_cost} |
| 105 | \begin{aligned}[t] |
| 106 | C_f = & \boldsymbol{U}^T B_f^T Q_{final} B_f \boldsymbol{U} \\ |
| 107 | & + 2 (A^{40} X_0 - X_{final})^T Q_{final} B_f \boldsymbol{U} \\ |
| 108 | & + (A^{40} X_0 - X_{final})^T Q_{final} (A^{40} X_0 - X_{final}) |
| 109 | \end{aligned} |
| 110 | \end{equation} |
| 111 | |
| 112 | \subsection{Acceleration costs} |
| 113 | |
| 114 | We can compute a velocity matrix for all the times by stripping out the positions from equation \ref{all_x} by using every other row. |
| 115 | We can use this to then compute the accelerations for each time slice and then penalize them. |
| 116 | |
| 117 | \begin{equation} |
| 118 | \begin{bmatrix} v_1 \\ v_2 \\ \vdots \\ v_{40} \end{bmatrix} = |
| 119 | M \boldsymbol{U} + \begin{bmatrix} a_{11} \\ a^2_{11} \\ \vdots \\ a^{40}_{11} \end{bmatrix} v_0 = |
| 120 | M \boldsymbol{U} + m v_0 |
| 121 | \end{equation} |
| 122 | |
| 123 | We can then use equation \ref{accel} in matrix form to convert a velocity matrix to an acceleration matrix. |
| 124 | |
| 125 | \begin{equation} |
| 126 | \begin{bmatrix} \alpha_1 \\ \alpha_2 \\ \alpha_3 \\ \vdots \\ \alpha_{40} \end{bmatrix} = |
| 127 | \frac{1}{\Delta t} \left( |
| 128 | \begin{bmatrix} 1 & 0 & 0 & \hdots & 0 \\ |
| 129 | -1 & 1 & 0 & \hdots & 0 \\ |
| 130 | 0 & -1 & 1 & \hdots & 0 \\ |
| 131 | \vdots & & & \ddots & \vdots \\ |
| 132 | 0 & 0 & \hdots & -1 & 1 \\ |
| 133 | \end{bmatrix} |
| 134 | \begin{bmatrix} v_1 \\ v_2 \\ \vdots \\ v_{40} \end{bmatrix} - \begin{bmatrix} v_0 \\ 0 \\ \vdots \\ 0 \end{bmatrix} |
| 135 | \right) |
| 136 | \end{equation} |
| 137 | |
| 138 | We can pull some of these terms out to make it easier to work with. |
| 139 | |
| 140 | \begin{equation} |
| 141 | \boldsymbol{\alpha} = W V + w v_0 |
| 142 | \end{equation} |
| 143 | |
| 144 | Our acceleration cost function is then: |
| 145 | |
| 146 | \begin{equation} |
| 147 | C_a = \boldsymbol{\alpha}^T |
| 148 | \begin{bmatrix} \pi_1 & & 0 \\ |
| 149 | & \pi_2 & \\ |
| 150 | 0 & & \ddots \end{bmatrix} \boldsymbol{\alpha} = |
| 151 | \boldsymbol{\alpha}^T \Pi \boldsymbol{\alpha} |
| 152 | \end{equation} |
| 153 | |
| 154 | We can substitute everything in to get something as a function of $U$. |
| 155 | |
| 156 | \begin{equation} |
| 157 | C_a = \left(W \left(M \boldsymbol{U} + m v_0 \right) + w v_0 \right)^T \Pi \left(W \left(M \boldsymbol{U} + m v_0 \right) + w v_0 \right) |
| 158 | \end{equation} |
| 159 | |
| 160 | And then simplify this down into the expected form. |
| 161 | |
| 162 | \begin{equation} |
| 163 | C_a = \left(W M \boldsymbol{U} + (W m + w) v_0 \right)^T \Pi \left(W M \boldsymbol{U} + (W m + w) v_0 \right) |
| 164 | \end{equation} |
| 165 | |
| 166 | \begin{equation} |
| 167 | \label{accel_cost} |
| 168 | \begin{aligned}[t] |
| 169 | C_a = & \boldsymbol{U}^T M^T W^T \Pi W M \boldsymbol{U} \\ |
| 170 | & + 2 v_0 (W m + w)^T \Pi W M \boldsymbol{U} \\ |
| 171 | & + v_0 (W m + w)^T \Pi \left(W m + w \right) v_0 |
| 172 | \end{aligned} |
| 173 | \end{equation} |
| 174 | |
| 175 | \subsection{Overall cost} |
| 176 | |
| 177 | We can combine equations \ref{final_cost} and \ref{accel_cost} to get our overall cost in the correct form. |
| 178 | |
| 179 | \begin{equation} |
| 180 | \begin{aligned}[t] |
| 181 | C &= \boldsymbol{U}^T \left( M^T W^T \Pi W M + B_f^T Q_{final} B_f \right) \boldsymbol{U} \\ |
| 182 | &+ \left(2 v_0 (W m + w)^T \Pi W M |
| 183 | - 2 X_{final}^T Q_{final} B_f |
| 184 | \right) \boldsymbol{U} \\ |
| 185 | & + X_{final}^T Q_{final} X_{final} |
| 186 | + v_0 (W m + w)^T \Pi \left(W m + w \right) v_0 |
| 187 | \end{aligned} |
| 188 | \end{equation} |
| 189 | |
| 190 | |
| 191 | \section{Response} |
| 192 | |
| 193 | For a reasonable request (11 m/s after 90 degrees), we get the following response |
| 194 | |
| 195 | \begin{figure} |
| 196 | \centering |
| 197 | \includegraphics[width=\linewidth]{response.png} |
| 198 | \end{figure} |
| 199 | |
| 200 | This is well within 1\% error, and avoid saturation and keeps the acceleration down at the end. |
| 201 | |
| 202 | \end{document} |