MPC求解(基于apollo代码的理解)
转载请引用本文链接
参考follow轻尘并做了修正
方向盘控制模型[Rajamani R. Vehicle Dynamics and Control[M]. Springer Science, 2006.]
d d t ( l a t e r a l _ e r r o r l a t e r a l _ e r r o r _ r a t e h e a d i n g _ e r r o r h e a d i n g _ e r r o r _ r a t e s t a t i o n _ e r r o r s p e e d _ e r r o r ) = ( 0 1 0 0 0 0 0 − c f + c r m v c f + c r m c r l r − c f l f m v 0 0 0 0 0 1 0 0 0 c r l r − c f l f I z v c r l r − c f l f I z − c r l r 2 + c f l f 2 I z v 0 0 0 0 0 0 0 1 0 0 0 0 0 0 ) ( l a t e r a l _ e r r o r l a t e r a l _ e r r o r _ r a t e h e a d i n g _ e r r o r h e a d i n g _ e r r o r _ r a t e s t a t i o n _ e r r o r s p e e d _ e r r o r ) \frac{d}{{dt}}\left( {\begin{array}{} {lateral\_error}\\ {lateral\_error\_rate}\\ {heading\_error}\\ {heading\_error\_rate}\\ {\begin{array}{} {station\_error}\\ {speed\_error} \end{array}} \end{array}} \right) = \left( {\begin{array}{} 0&1&0&0&0&0\\ 0&{ - \frac{{{c_f} + {c_r}}}{{mv}}}&{\frac{{{c_f} + {c_r}}}{m}}&{\frac{{{c_r}{l_r} - {c_f}{l_f}}}{{mv}}}&0&0\\ 0&0&0&1&0&0\\ 0&{\frac{{{c_r}{l_r} - {c_f}{l_f}}}{{{I_z}v}}}&{\frac{{{c_r}{l_r} - {c_f}{l_f}}}{{{I_z}}}}&{ - \frac{{{c_r}l_r^2 + {c_f}l_f^2}}{{{I_z}v}}}&0&0\\ 0&0&0&0&0&1\\ 0&0&0&0&0&0 \end{array}} \right)\left( {\begin{array}{} {lateral\_error}\\ {lateral\_error\_rate}\\ {heading\_error}\\ {heading\_error\_rate}\\ {\begin{array}{} {station\_error}\\ {speed\_error} \end{array}} \end{array}} \right) dtd⎝⎜⎜⎜⎜⎜⎜⎛lateral_errorlateral_error_rateheading_errorheading_error_ratestation_errorspeed_error⎠⎟⎟⎟⎟⎟⎟⎞=⎝⎜⎜⎜⎜⎜⎜⎜⎛0000001−mvcf+cr0Izvcrlr−cflf000mcf+cr0Izcrlr−cflf000mvcrlr−cflf1−Izvcrlr2+cflf200000000000010⎠⎟⎟⎟⎟⎟⎟⎟⎞⎝⎜⎜⎜⎜⎜⎜⎛lateral_errorlateral_error_rateheading_errorheading_error_ratestation_errorspeed_error⎠⎟⎟⎟⎟⎟⎟⎞
+ ( 0 0 c f m 0 0 0 c f l f I z 0 0 0 0 − 1 ) ( δ f a ) + ( 0 c r l r − c f l f m v − v 0 − c r l r 2 + c f l f 2 I z v 0 1 ) φ ˙ +\left( {\begin{array}{} 0&0\\ {\frac{{{c_f}}}{m}}&0\\ 0&0\\ {\frac{{{c_f}{l_f}}}{{{I_z}}}}&0\\ 0&0\\ 0&{ - 1} \end{array}} \right)\left( {\begin{array}{} {{\delta _f}}\\ a \end{array}} \right) + \left( {\begin{array}{} 0\\ {\frac{{{c_r}{l_r} - {c_f}{l_f}}}{{mv}} - v}\\ 0\\ { - \frac{{{c_r}l_r^2 + {c_f}l_f^2}}{{{I_z}v}}}\\ 0\\ 1 \end{array}} \right)\dot \varphi +⎝⎜⎜⎜⎜⎜⎜⎛0mcf0Izcflf0000000−1⎠⎟⎟⎟⎟⎟⎟⎞(δfa)+⎝⎜⎜⎜⎜⎜⎜⎜⎛0mvcrlr−cflf−v0−Izvcrlr2+cflf201⎠⎟⎟⎟⎟⎟⎟⎟⎞φ˙
经双线性变换得到
A = ( I − t s A ′ 2 ) − 1 ( I + t s A ′ 2 ) , B = t s B ′ , C = t s C ′ φ ˙ A = {(I - \frac{{{t_s}{A'}}}{2}){ - 1}}(I + \frac{{{t_s}{A'}}}{2}), B = {t_s}{B'},C = {t_s}{C'}\dot \varphi A=(I−2tsA′)−1(I+2tsA′),B=tsB′,C=tsC′φ˙
其中 A ′ , B ′ , C ′ {A'},{B'},{C'} A′,B′,C′分别对应上述模型的系数矩阵,因此新的离散模型可以写成
x ( k + 1 ) = A x ( k ) + B u ( k ) + C x(k + 1) = Ax(k) + Bu(k) + C x(k+1)=Ax(k)+Bu(k)+C
这里对于代码的关系:
matrix_a: A A A,matrix_b: B B B,matrix_c: C C C
对于模型预测控制,预测未来10(假设)个阶段内的状态变量,有如下关系
{ x ( 1 ) = A x ( 0 ) + B u ( 0 ) + C x ( 2 ) = A 2 x ( 0 ) + A B u ( 0 ) + B u ( 1 ) + C + A C x ( 3 ) = A 3 x ( 0 ) + A 2 B u ( 0 ) + A B u ( 1 ) + B u ( 2 ) + C + A C + A 2 C . . . \left\{ {\begin{array}{} {x(1) = Ax(0) + Bu(0) + C}\\ {x(2) = {A^2}x(0) + ABu(0) + Bu(1) + C + AC}\\ {x(3) = {A^3}x(0) + {A^2}Bu(0) + ABu(1) + Bu(2) + C + AC + {A^2}C}\\ {...} \end{array}} \right. ⎩⎪⎪⎨⎪⎪⎧x(1)=Ax(0)+Bu(0)+Cx(2)=A2x(0)+ABu(0)+Bu(1)+C+ACx(3)=A3x(0)+A2Bu(0)+ABu(1)+Bu(2)+C+AC+A2C...
对于任意 k k k阶段,有
x ( k ) = A k x ( 0 ) + ∑ i = 0 k − 1 A i B u ( k − 1 − i ) + ∑ i = 0 k − 1 A i C x(k) = {A^k}x(0) + \sum\limits_{i = 0}^{k - 1} {{A^i}Bu(k - 1 - i) + \sum\limits_{i = 0}^{k - 1} {{A^i}C} } x(k)=Akx(0)+i=0∑k−1AiBu(k−1−i)+i=0∑k−1AiC
因此,对于10个预测的阶段,可以写成矩阵形式(*)
( x 1 x 2 ⋮ x 10 ) = ( A A 2 ⋯ A 10 ) ( x 0 x 1 ⋮ x 9 ) + ( B A B B ⋮ B A 9 B A 8 B ⋯ B ) ( u 0 u 1 ⋮ u 9 ) + ( C C + A C ⋮ C + A C + . . . + A 9 C ) ( ∗ ) \left( {\begin{array}{} {{x_1}}\\ {{x_2}}\\ \vdots \\ {{x_{10}}} \end{array}} \right) = \left( {\begin{array}{} A&{{A^2}}& \cdots &{{A^{10}}} \end{array}} \right)\left( {\begin{array}{} {{x_0}}\\ {{x_1}}\\ \vdots \\ {{x_9}} \end{array}} \right) + \left( {\begin{array}{} B&{}&{}&{}\\ {AB}&B&{}&{}\\ \vdots &{}&B&{}\\ {{A^9}B}&{{A^8}B}& \cdots &B \end{array}} \right)\left( {\begin{array}{} {{u_0}}\\ {{u_1}}\\ \vdots \\ {{u_9}} \end{array}} \right) + \left( {\begin{array}{} C\\ {C + AC}\\ \vdots \\ {C + AC + ... + {A^9}C} \end{array}} \right)(*) ⎝⎜⎜⎜⎛x1x2⋮x10⎠⎟⎟⎟⎞=(AA2⋯A10)⎝⎜⎜⎜⎛x0x1⋮x9⎠⎟⎟⎟⎞+⎝⎜⎜⎜⎛BAB⋮A9BBA8BB⋯B⎠⎟⎟⎟⎞⎝⎜⎜⎜⎛u0u1⋮u9⎠⎟⎟⎟⎞+⎝⎜⎜⎜⎛CC+AC⋮C+AC+...+A9C⎠⎟⎟⎟⎞(∗)
与代码对应关系:
matrix_aa: = A ~ = ( A A 2 ⋯ A 10 ) = \tilde A = \left( {\begin{array}{} A&{{A^2}}& \cdots &{{A^{10}}} \end{array}} \right) =A~=(AA2⋯A10)
matrix_k:
= K ~ = ( B A B B ⋮ B A 9 B A 8 B ⋯ B ) = \tilde K = \left( {\begin{array}{} B&{}&{}&{}\\ {AB}&B&{}&{}\\ \vdots &{}&B&{}\\ {{A^9}B}&{{A^8}B}& \cdots &B \end{array}} \right) =K~=⎝⎜⎜⎜⎛BAB⋮A9BBA8BB⋯B⎠⎟⎟⎟⎞
matrix_cc
= C ~ = ( C C + A C ⋮ C + A C + . . . + A 9 C ) = \tilde C = \left( {\begin{array}{} C\\ {C + AC}\\ \vdots \\ {C + AC + ... + {A^9}C} \end{array}} \right) =C~=⎝⎜⎜⎜⎛CC+AC⋮C+AC+...+A9C⎠⎟⎟⎟⎞
另外的权重矩阵: Q Q Q=matrix_q, R R R=matrix_r,则有
matrix_qq
= Q ~ = ( Q Q ⋱ Q ) = \tilde Q = \left( {\begin{array}{} Q&{}&{}&{}\\ {}&Q&{}&{}\\ {}&{}& \ddots &{}\\ {}&{}&{}&Q \end{array}} \right) =Q~=⎝⎜⎜⎛QQ⋱Q⎠⎟⎟⎞
matrix_rr
= R ~ = ( R R ⋱ R ) = \tilde R = \left( {\begin{array}{} R&{}&{}&{}\\ {}&R&{}&{}\\ {}&{}& \ddots &{}\\ {}&{}&{}&R \end{array}} \right) =R~=⎝⎜⎜⎛RR⋱R⎠⎟⎟⎞
matrix_m
= M = ( A ∗ m a t r i x _ i n i t i a l _ s t a t e A 2 ∗ m a t r i x _ i n i t i a l _ s t a t e ⋮ A 10 ∗ m a t r i x _ i n i t i a l _ s t a t e ) = M = \left( {\begin{array}{} {A*matrix\_initial\_state}\\ {{A^2}*matrix\_initial\_state}\\ \vdots \\ {{A^{10}}*matrix\_initial\_state} \end{array}} \right) =M=⎝⎜⎜⎜⎛A∗matrix_initial_stateA2∗matrix_initial_state⋮A10∗matrix_initial_state⎠⎟⎟⎟⎞
下面解释由mpc控制问题转换成二次规划问题的过程
对于每一轮滚动优化,我们希望找到最优的10个
u i = ( δ f i a i ) {u_i} = \left( {\begin{array}{} {{\delta _{fi}}}\\ {{a_i}} \end{array}} \right) ui=(δfiai)
也即
U = ( u 0 u 1 ⋮ u 9 ) U = \left( {\begin{array}{} {{u_0}}\\ {{u_1}}\\ \vdots \\ {{u_9}} \end{array}} \right) U=⎝⎜⎜⎜⎛u0u1⋮u9⎠⎟⎟⎟⎞
使得性能函数最优,进一步的,我们希望采用如下二次规划的形式来求解这个问题
{ min f ( x ) = U T H U + G T U s . t . M 1 U = N 1 M 2 U ≤ N 2 b 1 ≤ U ≤ b 2 \left\{ {\begin{array}{} {\begin{array}{} {\min }&{f(x) = {U^T}HU + {G^T}U} \end{array}}\\ {\begin{array}{} {s.t.}&{} \end{array}}\\ {\begin{array}{} {}&{{M_1}U = {N_1}} \end{array}}\\ {\begin{array}{} {\begin{array}{} {}&{{M_2}U \le {N_2}} \end{array}}\\ {{b_1} \le U \le {b_2}} \end{array}} \end{array}} \right. ⎩⎪⎪⎪⎪⎨⎪⎪⎪⎪⎧minf(x)=UTHU+GTUs.t.M1U=N1M2U≤N2b1≤U≤b2
具体到路径跟踪问题,构造如下形式的目标函数
f ( x ) = ( X − X r e f ) T Q ~ ( X − X r e f ) + U T R ~ U f(x) = {(X - {X_{ref}})^T}\tilde Q(X - {X_{ref}}) + {U^T}\tilde RU f(x)=(X−Xref)TQ~(X−Xref)+UTR~U
其中 X r e f {X_{ref}} Xref为10个状态量组合的参考(期望)值, Q ~ \tilde Q Q~和 R ~ \tilde R R~分别为状态量和控制量的权重矩阵,由于本控制问题的状态变量已经是偏差量了,所以 X r e f {X_{ref}} Xref为零向量,与代码相符
根据式(*)代入相关量,对于 f ( x ) f(x) f(x)进行推导
f ( x ) = ( A ~ X + K ~ U + C ~ − X r e f ) T Q ~ ( A ~ X + K ~ U + C ~ − X r e f ) + U T R ~ U f(x) = {(\tilde AX + \tilde KU + \tilde C - {X_{ref}})^T}\tilde Q(\tilde AX + \tilde KU + \tilde C - {X_{ref}}) + {U^T}\tilde RU f(x)=(A~X+K~U+C~−Xref)TQ~(A~X+K~U+C~−Xref)+UTR~U
令 e = A ~ X + C ~ − X r e f e = \tilde AX + \tilde C - {X_{ref}} e=A~X+C~−Xref,则有
f ( x ) = ( e + K ~ U ) T Q ~ ( e + K ~ U ) + U T R ~ U = ( e T Q ~ + ( K ~ U ) T Q ~ ) ( e + K ~ U ) + U T R ~ U = e T Q ~ e + e T Q ~ K ~ U + ( K ~ U ) T Q ~ e + ( K ~ U ) T Q ~ K ~ U + U T R ~ U ≈ 2 ( K ~ U ) T Q ~ e + U T K ~ T Q ~ K ~ U + U T R ~ U = U T ( K ~ T Q ~ K ~ + R ~ ) U + 2 U T K ~ T Q ~ e = U T ( K ~ T Q ~ K ~ + R ~ ) U + 2 U T K ~ T Q ~ ( A ~ X + C ~ − X r e f ) = U T ( K ~ T Q ~ K ~ + R ~ ) U + 2 U T K ~ T Q ~ ( M + C ~ − X r e f ) f(x) = {(e + \tilde KU)^T}\tilde Q(e + \tilde KU) + {U^T}\tilde RU\\ \begin{array}{} {\hspace{1.5cm} = ({e^T}\tilde Q + {{(\tilde KU)}^T}\tilde Q)(e + \tilde KU) + {U^T}\tilde RU}\\ { \hspace{3cm}= {e^T}\tilde Qe + {e^T}\tilde Q\tilde KU + {{(\tilde KU)}^T}\tilde Qe + {{(\tilde KU)}^T}\tilde Q\tilde KU + {U^T}\tilde RU}\\ { \approx 2{{(\tilde KU)}^T}\tilde Qe + {U^T}{{\tilde K}^T}\tilde Q\tilde KU + {U^T}\tilde RU}\\ {\begin{array}{} { \hspace{0.5cm}= {U^T}({{\tilde K}^T}\tilde Q\tilde K + \tilde R)U + 2{U^T}{{\tilde K}^T}\tilde Qe}\\ {\begin{array}{} { = {U^T}({{\tilde K}^T}\tilde Q\tilde K + \tilde R)U + 2{U^T}{{\tilde K}^T}\tilde Q(\tilde AX + \tilde C - {X_{ref}})}\\ { = {U^T}({{\tilde K}^T}\tilde Q\tilde K + \tilde R)U + 2{U^T}{{\tilde K}^T}\tilde Q(M + \tilde C - {X_{ref}})} \end{array}} \end{array}} \end{array} f(x)=(e+K~U)TQ~(e+K~U)+UTR~U=(eTQ~+(K~U)TQ~)(e+K~U)+UTR~U=eTQ~e+eTQ~K~U+(K~U)TQ~e+(K~U)TQ~K~U+UTR~U≈2(K~U)TQ~e+UTK~TQ~K~U+UTR~U=UT(K~TQ~K~+R~)U+2UTK~TQ~e=UT(K~TQ~K~+R~)U+2UTK~TQ~(A~X+C~−Xref)=UT(K~TQ~K~+R~)U+2UTK~TQ~(M+C~−Xref)
根据上面式子,得到一个二次规划的目标函数
f ( x ) = 2 ( 1 2 U T H U + U T G ) f(x) = 2\left( {\frac{1}{2}{U^T}HU + {U^T}G} \right) f(x)=2(21UTHU+UTG)
其中,各符号与代码的对应关系如下
matrix_m1 = H = K ~ T Q ~ K ~ + R ~ = H = {\tilde K^T}\tilde Q\tilde K + \tilde R =H=K~TQ~K~+R~
matrix_m2 = G = K ~ T Q ~ ( A ~ X + C ~ − X r e f ) = G = {\tilde K^T}\tilde Q(\tilde AX + \tilde C - {X_{ref}}) =G=K~TQ~(A~X+C~−Xref)
推导结果与代码一致
对
U = ( u 0 u 1 ⋮ u 9 ) U = \left( {\begin{array}{} {{u_0}}\\ {{u_1}}\\ \vdots \\ {{u_9}} \end{array}} \right) U=⎝⎜⎜⎜⎛u0u1⋮u9⎠⎟⎟⎟⎞添加约束条件,即matrix_ll ≤ U ≤ \le U \le ≤U≤matrix_uu
matrix_ll = ( m a t r i x _ l o w e r m a t r i x _ l o w e r ⋮ m a t r i x _ l o w e r ) = \left( {\begin{array}{} {{\rm{matrix\_lower}}}\\ {{\rm{matrix\_lower}}}\\ \vdots \\ {{\rm{matrix\_lower}}} \end{array}} \right) =⎝⎜⎜⎜⎛matrix_lowermatrix_lower⋮matrix_lower⎠⎟⎟⎟⎞
matrix_uu = ( m a t r i x _ u p p e r m a t r i x _ u p p e r ⋮ m a t r i x _ u p p e r ) = \left( {\begin{array}{} {{\rm{matrix\_upper}}}\\ {{\rm{matrix\_upper}}}\\ \vdots \\ {{\rm{matrix\_upper}}} \end{array}} \right) =⎝⎜⎜⎜⎛matrix_uppermatrix_upper⋮matrix_upper⎠⎟⎟⎟⎞
代码中二次规划求解过程,不等式约束设置为单位阵,等式约束、等式边界均设置为零矩阵