qp_spline_path_optimizer.md 8.9 KB
Newer Older
1 2
# QP-Spline-Path Optimizer

3
_**Tip**: to read the equations in the document, you are recommended to use Chrome with [a plugin](https://chrome.google.com/webstore/detail/tex-all-the-things/cbimabofgmfdkicghcadidpemeenbffn) or copy the latex equation to [an online editor](http://www.hostmath.com/)_
4

5
Quadratic programming + Spline interpolation
6

7
## 1.  Objective function
8

9
### 1.1  Get path length
10

N
Natasha Dsouza 已提交
11
Path is defined in station-lateral coordination system. The **s** range from vehicle's current position to  default planning path length.
12

13
### 1.2   Get spline segments
14

15
Split the path into **n** segments. each segment trajectory is defined by a polynomial.
16

17
### 1.3  Define function for each spline segment
18

19
Each segment ***i*** has accumulated distance $d_i$ along reference line. The trajectory for the segment is defined as a polynomial of degree five by default.
20

21
<p>
22
$$
J
Jiangtao Hu 已提交
23
l = f_i(s)
24
  = a_{i0} + a_{i1} \cdot s + a_{i2} \cdot s^2 + a_{i3} \cdot s^3 + a_{i4} \cdot s^4 + a_{i5} \cdot s^5        (0 \leq s \leq d_{i})
25
$$
26
</p>
27

28
### 1.4  Define objective function of optimization for each segment
29

30
<p>
31 32 33
$$
cost = \sum_{i=1}^{n} \Big( w_1 \cdot \int\limits_{0}^{d_i} (f_i')^2(s) ds + w_2 \cdot \int\limits_{0}^{d_i} (f_i'')^2(s) ds + w_3 \cdot \int\limits_{0}^{d_i} (f_i^{\prime\prime\prime})^2(s) ds \Big)
$$
34
</p>
35

36
### 1.5  Convert the cost function to QP formulation
37 38

QP formulation:
39
<p>
40
$$
41
\begin{aligned}
42
minimize  & \frac{1}{2}  \cdot x^T \cdot H \cdot x  + f^T \cdot x \\
43 44
s.t. \qquad & LB \leq x \leq UB \\
      & A_{eq}x = b_{eq} \\
45
      & Ax \geq b
46
\end{aligned}
47
$$
48
</p>
N
Natasha Dsouza 已提交
49
Below is the example for converting the cost function into the QP formulation. 
50
<p>
51
$$
J
jiangyifei 已提交
52
f_i(s) =
53 54 55
\begin{vmatrix} 1 & s & s^2 & s^3 & s^4 & s^5 \end{vmatrix}
\cdot
\begin{vmatrix} a_{i0} \\ a_{i1} \\ a_{i2} \\ a_{i3} \\ a_{i4} \\ a_{i5} \end{vmatrix}   
56
$$
57
</p>
58

59
And
60
<p>
61
$$
J
jiangyifei 已提交
62
f_i'(s) =
63 64 65
\begin{vmatrix} 0 & 1 & 2s & 3s^2 & 4s^3 & 5s^4 \end{vmatrix}
\cdot
\begin{vmatrix} a_{i0} \\ a_{i1} \\ a_{i2} \\ a_{i3} \\ a_{i4} \\ a_{i5} \end{vmatrix}   
66
$$
67
</p>
68 69


70
And 
71
<p>
72
$$
J
jiangyifei 已提交
73
f_i'(s)^2 =
74
\begin{vmatrix} a_{i0} & a_{i1} & a_{i2} & a_{i3} & a_{i4} & a_{i5}  \end{vmatrix} 
J
jiangyifei 已提交
75
\cdot 
76
\begin{vmatrix} 0 \\ 1 \\ 2s \\ 3s^2 \\ 4s^3 \\ 5s^4 \end{vmatrix} 
J
jiangyifei 已提交
77
\cdot 
78
\begin{vmatrix} 0 & 1 & 2s & 3s^2 & 4s^3 & 5s^4 \end{vmatrix} 
J
jiangyifei 已提交
79
\cdot 
80
\begin{vmatrix} a_{i0} \\ a_{i1} \\ a_{i2} \\ a_{i3} \\ a_{i4} \\ a_{i5}  \end{vmatrix}
81
$$
82
</p>
83
then we have,
84
<p>
85
$$
J
jiangyifei 已提交
86 87
\int\limits_{0}^{d_i} f_i'(s)^2 ds =
\int\limits_{0}^{d_i}
88
\begin{vmatrix} a_{i0} & a_{i1} & a_{i2} & a_{i3} & a_{i4} & a_{i5} \end{vmatrix} 
J
jiangyifei 已提交
89
\cdot  
90
\begin{vmatrix} 0 \\ 1 \\ 2s \\ 3s^2 \\ 4s^3 \\ 5s^4 \end{vmatrix} 
J
jiangyifei 已提交
91
\cdot 
92
\begin{vmatrix} 0 & 1 & 2s & 3s^2 & 4s^3 & 5s^4 \end{vmatrix} 
J
jiangyifei 已提交
93
\cdot 
94
\begin{vmatrix} a_{i0} \\ a_{i1} \\ a_{i2} \\ a_{i3} \\ a_{i4} \\ a_{i5}  \end{vmatrix} ds
95
$$
96
</p>
97 98


99
extract the const outside the integration, we have,
100
<p>
101
$$
J
jiangyifei 已提交
102
\int\limits_{0}^{d_i} f'(s)^2 ds =
103
\begin{vmatrix} a_{i0} & a_{i1} & a_{i2} & a_{i3} & a_{i4} & a_{i5} \end{vmatrix} 
J
jiangyifei 已提交
104 105
\cdot 
\int\limits_{0}^{d_i}  
106
\begin{vmatrix} 0 \\ 1 \\ 2s \\ 3s^2 \\ 4s^3 \\ 5s^4 \end{vmatrix} 
J
jiangyifei 已提交
107
\cdot 
108
\begin{vmatrix} 0 & 1 & 2s & 3s^2 & 4s^3 & 5s^4 \end{vmatrix} ds 
J
jiangyifei 已提交
109
\cdot 
110
\begin{vmatrix} a_{i0} \\ a_{i1} \\ a_{i2} \\ a_{i3} \\ a_{i4} \\ a_{i5}  \end{vmatrix}
111 112
$$
$$
113
\begin{vmatrix} a_{i0} & a_{i1} & a_{i2} & a_{i3} & a_{i4} & a_{i5} \end{vmatrix} 
114 115 116
\cdot \int\limits_{0}^{d_i}
\begin{vmatrix} 
0  & 0 &0&0&0&0\\ 
117 118 119 120 121
0 & 1 & 2s & 3s^2 & 4s^3 & 5s^4\\
0 & 2s & 4s^2 & 6s^3 & 8s^4 & 10s^5\\
0 & 3s^2 &  6s^3 & 9s^4 & 12s^5&15s^6 \\
0 & 4s^3 & 8s^4 &12s^5 &16s^6&20s^7 \\
0 & 5s^4 & 10s^5 & 15s^6 & 20s^7 & 25s^8 
J
jiangyifei 已提交
122 123
\end{vmatrix} ds 
\cdot 
124
\begin{vmatrix} a_{i0} \\ a_{i1} \\ a_{i2} \\ a_{i3} \\ a_{i4} \\ a_{i5} \end{vmatrix}
125
$$
126 127
</p>

128 129
Finally, we have

130
<p>
131 132
$$
\int\limits_{0}^{d_i} 
133
f'_i(s)^2 ds =\begin{vmatrix} a_{i0} & a_{i1} & a_{i2} & a_{i3} & a_{i4} & a_{i5} \end{vmatrix} 
134 135
\cdot \begin{vmatrix} 
0 & 0 & 0 & 0 &0&0\\ 
136 137 138 139 140
0 & d_i & d_i^2 & d_i^3 & d_i^4&d_i^5\\
0& d_i^2 & \frac{4}{3}d_i^3& \frac{6}{4}d_i^4 & \frac{8}{5}d_i^5&\frac{10}{6}d_i^6\\
0& d_i^3 & \frac{6}{4}d_i^4 & \frac{9}{5}d_i^5 & \frac{12}{6}d_i^6&\frac{15}{7}d_i^7\\
0& d_i^4 & \frac{8}{5}d_i^5 & \frac{12}{6}d_i^6 & \frac{16}{7}d_i^7&\frac{20}{8}d_i^8\\
0& d_i^5 & \frac{10}{6}d_i^6 & \frac{15}{7}d_i^7 & \frac{20}{8}d_i^8&\frac{25}{9}d_i^9
141 142
\end{vmatrix} 
\cdot 
143
\begin{vmatrix} a_{i0} \\ a_{i1} \\ a_{i2} \\ a_{i3} \\ a_{i4} \\ a_{i5} \end{vmatrix}
144
$$
145
</p>
146

I
ixanezis 已提交
147
Please notice that we got a 6 x 6 matrix to represent the derivative cost of 5th order spline.
148 149 150 151 152 153 154



Similar deduction can also be used to calculate the cost of second and third order derivatives.



155
## 2  Constraints  
156

157
### 2.1  The init point constraints
158

159 160 161
Assume that the first point is ($s_0$, $l_0$), ($s_0$, $l'_0$) and ($s_0$, $l''_0$), where $l_0$ , $l'_0$ and $l''_0$ is the lateral offset and its first and second derivatives on the init point of planned path, and are calculated from $f_i(s)$, $f'_i(s)$, and $f_i(s)''$.  

Convert those constraints into QP equality constraints, using: 
162
<p>
163 164 165
$$
A_{eq}x = b_{eq}
$$
166
</p>
167
Below are the steps of conversion.
168
<p>
169
$$
J
jiangyifei 已提交
170 171 172 173
f_i(s_0) = 
\begin{vmatrix} 1 & s_0 & s_0^2 & s_0^3 & s_0^4&s_0^5 \end{vmatrix} 
\cdot 
\begin{vmatrix}  a_{i0} \\ a_{i1} \\ a_{i2} \\ a_{i3} \\ a_{i4} \\ a_{i5}\end{vmatrix} = l_0
174
$$
175
</p>
176
And
177
<p>
178
$$
J
jiangyifei 已提交
179
f'_i(s_0) = 
180
\begin{vmatrix} 0& 1 & 2s_0 & 3s_0^2 & 4s_0^3 &5 s_0^4 \end{vmatrix} 
J
jiangyifei 已提交
181
\cdot 
182
\begin{vmatrix}  a_{i0} \\ a_{i1} \\ a_{i2} \\ a_{i3} \\ a_{i4} \\ a_{i5} \end{vmatrix} = l'_0
183
$$
184
</p>
185
And 
186
<p>
187
$$
J
jiangyifei 已提交
188
f''_i(s_0) = 
189
\begin{vmatrix} 0&0& 2 & 3\times2s_0 & 4\times3s_0^2 & 5\times4s_0^3  \end{vmatrix} 
J
jiangyifei 已提交
190
\cdot 
191
\begin{vmatrix}  a_{i0} \\ a_{i1} \\ a_{i2} \\ a_{i3} \\ a_{i4} \\ a_{i5} \end{vmatrix} = l''_0
192
$$
193
</p>
194
where i is the index of the segment that contains the $s_0$.
195

196
### 2.2  The end point constraints
197

198
Similar to the init point, the end point $(s_e, l_e)$ is known and should produce the same constraint as described in the init point calculations. 
199

200

201
Combine the init point and end point, and show the equality constraint as: 
202

203
<p>
204 205 206
$$
\begin{vmatrix} 
 1 & s_0 & s_0^2 & s_0^3 & s_0^4&s_0^5 \\
207 208
 0&1 & 2s_0 & 3s_0^2 & 4s_0^3 & 5s_0^4 \\
 0& 0&2 & 3\times2s_0 & 4\times3s_0^2 & 5\times4s_0^3  \\
209
 1 & s_e & s_e^2 & s_e^3 & s_e^4&s_e^5 \\
210 211
 0&1 & 2s_e & 3s_e^2 & 4s_e^3 & 5s_e^4 \\
 0& 0&2 & 3\times2s_e & 4\times3s_e^2 & 5\times4s_e^3  
J
jiangyifei 已提交
212 213 214
 \end{vmatrix} 
 \cdot 
 \begin{vmatrix}  a_{i0} \\ a_{i1} \\ a_{i2} \\ a_{i3} \\ a_{i4} \\ a_{i5} \end{vmatrix} 
215 216 217
 = 
 \begin{vmatrix}
 l_0\\
218 219
 l'_0\\
 l''_0\\
220
 l_e\\
221 222
 l'_e\\
 l''_e\\
223 224
 \end{vmatrix}
$$
225
</p>
226

227
### 2.3  Joint smoothness  constraints
228

229
This constraint is designed to smooth the spline joint.  Assume two segments $seg_k$ and $seg_{k+1}$ are connected, and the accumulated **s** of segment $seg_k$ is $s_k$. Calculate the constraint equation as: 
230
<p>
231 232 233
$$
f_k(s_k) = f_{k+1} (s_0)
$$
234
</p>
235
Below are the steps of the calculation.
236
<p>
237 238 239 240 241 242
$$
\begin{vmatrix} 
 1 & s_k & s_k^2 & s_k^3 & s_k^4&s_k^5 \\
 \end{vmatrix} 
 \cdot 
 \begin{vmatrix} 
J
jiangyifei 已提交
243
 a_{k0} \\ a_{k1} \\ a_{k2} \\ a_{k3} \\ a_{k4} \\ a_{k5} 
244 245 246 247 248 249 250
 \end{vmatrix} 
 = 
\begin{vmatrix} 
 1 & s_{0} & s_{0}^2 & s_{0}^3 & s_{0}^4&s_{0}^5 \\
 \end{vmatrix} 
 \cdot 
 \begin{vmatrix} 
J
jiangyifei 已提交
251
 a_{k+1,0} \\ a_{k+1,1} \\ a_{k+1,2} \\ a_{k+1,3} \\ a_{k+1,4} \\ a_{k+1,5} 
252 253
 \end{vmatrix}
$$
254
</p>
255
Then
256
<p>
257 258 259 260 261 262
$$
\begin{vmatrix} 
 1 & s_k & s_k^2 & s_k^3 & s_k^4&s_k^5 &  -1 & -s_{0} & -s_{0}^2 & -s_{0}^3 & -s_{0}^4&-s_{0}^5\\
 \end{vmatrix} 
 \cdot 
 \begin{vmatrix} 
J
jiangyifei 已提交
263
 a_{k0} \\ a_{k1} \\ a_{k2} \\ a_{k3} \\ a_{k4} \\ a_{k5} \\ a_{k+1,0} \\ a_{k+1,1} \\ a_{k+1,2} \\ a_{k+1,3} \\ a_{k+1,4} \\ a_{k+1,5}  
264 265 266
 \end{vmatrix} 
 = 0
$$
267
</p>
268
Use $s_0$ = 0 in the equation.
269

270
Similarly calculate the equality constraints for: 
271
<p>
272 273 274 275 276 277 278
$$
f'_k(s_k) = f'_{k+1} (s_0)
\\
f''_k(s_k) = f''_{k+1} (s_0)
\\
f'''_k(s_k) = f'''_{k+1} (s_0)
$$
279
</p>
280

281
### 2.4  Sampled points for boundary constraint
282

283
Evenly sample **m** points along the path, and check the obstacle boundary at those points.  Convert the constraint into QP inequality constraints, using:
284
<p>
285
$$
286
Ax \geq b
287
$$
288 289 290
</p>
First find the lower boundary $l_{lb,j}$ at those points $(s_j, l_j)$ and  $j\in[0, m]$ based on the road width and surrounding obstacles. Calculate the inequality constraints as:
<p>
291 292 293 294 295 296
$$
\begin{vmatrix} 
 1 & s_0 & s_0^2 & s_0^3 & s_0^4&s_0^5 \\
  1 & s_1 & s_1^2 & s_1^3 & s_1^4&s_1^5 \\
 ...&...&...&...&...&... \\
 1 & s_m & s_m^2 & s_m^3 & s_m^4&s_m^5 \\
J
jiangyifei 已提交
297
 \end{vmatrix} \cdot \begin{vmatrix}a_{i0} \\ a_{i1} \\ a_{i2} \\ a_{i3} \\ a_{i4} \\ a_{i5}  \end{vmatrix} 
298
 \geq 
299 300 301 302 303 304 305
 \begin{vmatrix}
 l_{lb,0}\\
 l_{lb,1}\\
 ...\\
 l_{lb,m}\\
 \end{vmatrix}
$$
306
</p>
307 308


309
Similarly, for the upper boundary $l_{ub,j}$, calculate the inequality constraints as: 
310
<p>
311 312
$$
\begin{vmatrix} 
313 314 315 316
 -1 & -s_0 & -s_0^2 & -s_0^3 & -s_0^4&-s_0^5 \\
  -1 & -s_1 & -s_1^2 & -s_1^3 & -s_1^4&-s_1^5 \\
 ...&...-&...&...&...&... \\
 -1 & -s_m & -s_m^2 & -s_m^3 & -s_m^4&-s_m^5 \\
J
jiangyifei 已提交
317 318 319
 \end{vmatrix} 
 \cdot 
 \begin{vmatrix} a_{i0} \\ a_{i1} \\ a_{i2} \\ a_{i3} \\ a_{i4} \\ a_{i5}  \end{vmatrix} 
320
 \geq
321 322 323 324 325 326 327 328
 -1 \cdot
 \begin{vmatrix}
 l_{ub,0}\\
 l_{ub,1}\\
 ...\\
 l_{ub,m}\\
 \end{vmatrix}
$$
329
</p>
330