欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

基于运动学模型的无人机模型预测控制(MPC)-3

程序员文章站 2022-07-12 13:16:55
...

基于差分模型的无人机模型预测控制(MPC)-无约束情况

1. 模型建立

无人机运动学模型:
{x˙=vxvx˙=uxy=vyvy˙=uy \left\{ \begin{aligned} \dot x & = v_x \qquad \dot{v_x}=u_x\\ y & = v_y \qquad \dot{v_y}=u_y \\ \end{aligned} \right.
其中 nxnu:nm:n_x:状态变量量个数,n_u:控制变量个数,n_m:输出变量个数,我们得到如下状态空间:
[x˙v˙xy˙v˙y]=[0100000000010000][xvxyvy]+[00100001][uxuy] \begin{bmatrix} \dot{x}\\ \dot v_x \\ \dot y\\ \dot v_y \end{bmatrix}= \begin{bmatrix} 0 & 1 & 0 & 0\\ 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 1\\ 0 & 0 & 0 & 0\\ \end{bmatrix} \begin{bmatrix} x\\ v_x \\ y\\ v_y \end{bmatrix}+ \begin{bmatrix} 0 & 0\\ 1 & 0 \\ 0 & 0\\ 0 & 1\\ \end{bmatrix} \begin{bmatrix} u_x \\ u_y\\ \end{bmatrix}
[xy]=[10000010][xvxyvy] \begin{bmatrix} x\\ y \end{bmatrix} = \begin{bmatrix} 1&0&0&0\\ 0&0&1&0\\ \end{bmatrix} \begin{bmatrix} x\\ v_x\\ y\\ v_y \end{bmatrix}
其中
A=[0100000000010000]B=[00100001]C=[10000010]x(k)=[xvxyvy]u(k)=[uxuy] A = \begin{bmatrix} 0 & 1 & 0 & 0\\ 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 1\\ 0 & 0 & 0 & 0\\ \end{bmatrix} \quad B = \begin{bmatrix} 0 & 0\\ 1 & 0 \\ 0 & 0\\ 0 & 1\\ \end{bmatrix} \quad C=\begin{bmatrix} 1&0&0&0\\ 0&0&1&0\\ \end{bmatrix}\quad x(k)=\begin{bmatrix} x\\ v_x \\ y\\ v_y \end{bmatrix} \quad u(k)=\begin{bmatrix} u_x \\ u_y\\ \end{bmatrix} \quad

令:
xm(k)=[x(k)vx(k)y(k)vy(k)]Tum(k)=[ux(k)uy(k)]T \begin{aligned} &x_m(k)= \begin{bmatrix} x(k) \quad v_x(k)\quad y(k)\quad v_y(k) \end{bmatrix}^{T}\\ &u_m(k)=[u_x(k)\quad u_y(k)]^{T} \end{aligned}
将上述模型离散化,我们得到:
Ak=AΔt+IBk=BΔt \begin{aligned} &A_k=A*\Delta t+I\\ &B_k=B*\Delta t \end{aligned}

即我们得到系统方程:
xm(k+1)=Akxm(k)+Bkum(k)ym(k+1)=Cxm(k+1)=CAkxm(k)+CBkum(k)xm(k+1)nx×1Aknx×nxBknx×nu \begin{aligned} &x_m(k+1)=A_kx_m(k)+B_ku_m(k)\\ &y_m(k+1) = Cx_m(k+1)=CA_kx_m(k)+CB_ku_m(k)\\ &x_m(k+1)\in{n_{x}\times1}\quad A_k\in{n_{x}\times n_{x}}\quad B_k\in{n_{x}\times n_{u}} \end{aligned}

构建差分系统方程:
Δxm(k+1)=xm(k+1)xm(k)=AkΔxm(k)+BkΔum(k) \Delta x_m(k+1)=x_m(k+1)-x_m(k)=A_k\Delta x_m(k)+B_k\Delta u_m(k)
即得到:
[Δxm(k+1)ym(k+1)]=[(Ak)nx×nx0nx×nm(CAk)nm×nxInm×nm][Δxm(k)nx×1ym(k)nm×1]+[(Bk)nx×nu(CBk)nm×nu]Δum(k)nu×1 \begin{aligned} \begin{bmatrix} \Delta x_m(k+1)\\ y_m(k+1) \end{bmatrix}= \begin{bmatrix} &(A_k)_{{n_x\times n_x}}&0_{n_x\times n_m}\\ &(CA_k)_{{n_m\times n_x}}&I_{n_m\times n_m} \end{bmatrix} \begin{bmatrix} &\Delta x_m(k)_{n_x\times 1}\\ &y_m(k)_{n_m\times 1} \end{bmatrix}+ \begin{bmatrix} (B_k)_{{n_x\times n_u}}\\ (CB_k)_{{n_m\times n_u}} \end{bmatrix} \Delta u_m(k)_{n_u\times 1} \end{aligned}

ym(k+1)ym(k)=C(xm(k+1)xm(k))=CΔxm(k+1)=CAkΔxm(k)+CBkΔum(k) y_m(k+1)-y_m(k)=C(x_m(k+1)-x_m(k))=C\Delta x_m(k+1)=CA_k\Delta x_m(k)+CB_k\Delta u_m(k)

我们得到如下差分系统方程:
Δx(k+1)=AuΔx(k)+BuΔu(k)Δy(k)=CuΔx(k) \begin{aligned} \Delta x(k+1)&=A_u\Delta x(k)+B_u\Delta u(k)\\ \Delta y(k)&=C_u\Delta x(k) \end{aligned}

其中:
Δx(k+1)=[Δxm(k+1);ym(k+1)](nx+nm)×1Δu(k)nu×1=Δum(k)Δy(k)nm×1Au=[(Ak)nx×nx0nx×nm(CAk)nm×nxInm×nm]Bu=[(Bk)nx×nu(CBk)nm×nu]Cu=[0nm×nxInm×nm] \begin{aligned} &\Delta x(k+1)=[\Delta x_m(k+1);y_m(k+1)]_{(n_x+n_m)\times 1}\\ &\Delta u(k)_{n_u\times 1}=\Delta u_m(k)\\ &\Delta y(k)_{n_m\times 1}\\ &A_u= \begin{bmatrix} &(A_k)_{{n_x\times n_x}}&0_{n_x\times n_m}\\ &(CA_k)_{{n_m\times n_x}}&I_{n_m\times n_m} \end{bmatrix}\quad B_u= \begin{bmatrix} (B_k)_{{n_x\times n_u}}\\ (CB_k)_{{n_m\times n_u}} \end{bmatrix} \quad C_u= \begin{bmatrix} 0_{n_m\times n_x}\quad I_{n_m\times n_m} \end{bmatrix} \end{aligned}

递推公式推导:
{Δx(ki+1ki)=AuΔx(ki)+BuΔu(ki)Δx(ki+2ki)=Au2Δx(ki)+AuBuΔu(ki)+BuΔu(ki+1)Δx(ki+3ki)=Au3Δx(ki)+Au2BuΔu(ki)+AuBuΔu(ki+1)+BuΔu(ki+2)Δx(ki+Npki)=AuNpΔx(ki)+AuNp1BuΔu(ki)+AuNp2BuΔu(ki+1)++AuNpNcBuΔu(ki+Nc1) \left\{ \begin{aligned} \Delta x(k_i+1|k_i)&=A_u\Delta x(k_i)+B_u\Delta u(k_i)\\ \Delta x(k_i+2|k_i)&=A_u^{2}\Delta x(k_i)+A_uB_u \Delta u(k_i)+B_u\Delta u(k_i+1)\\ \Delta x(k_i+3|k_i)&=A_u^{3}\Delta x(k_i)+A_u^{2}B_u\Delta u(k_i)+A_uB_u\Delta u(k_i+1)+B_u\Delta u(k_i+2)\\ \quad\vdots\\ \Delta x(k_i+N_p|k_i)&=A_u^{N_p}\Delta x(k_i)+A_u^{N_p-1}B_u\Delta u(k_i)+A_u^{N_p-2}B_u\Delta u(k_i+1)+\cdots +A_u^{N_p-N_c}B_u\Delta u(k_i+N_c-1)\\ \end{aligned} \right.

{Δy(ki+1ki)=CuAuΔx(ki)+CuBuΔu(ki)Δy(ki+2ki)=CuAu2Δx(ki)+CuAuBuΔu(ki)+CuBuΔu(ki+1)Δy(ki+3ki)=CuAu3Δx(ki)+CuAu2BuΔu(ki)+CuAuBuΔu(ki+1)+CuBuΔu(ki+2)Δy(ki+Npki)=CuAuNpΔx(ki)+CuAuNp1BuΔu(ki)+CuAuNp2BuΔu(ki+1)++CuAuNpNcBuΔu(ki+Nc1) \left\{ \begin{aligned} \Delta y(k_i+1|k_i)&=C_uA_u\Delta x(k_i)+C_uB_u\Delta u(k_i)\\ \Delta y(k_i+2|k_i)&=C_uA_u^{2}\Delta x(k_i)+C_uA_uB_u\Delta u(k_i)+C_uB_u\Delta u(k_i+1)\\ \Delta y(k_i+3|k_i)&=C_uA_u^{3}\Delta x(k_i)+C_uA_u^{2}B_u\Delta u(k_i)+C_uA_uB_u\Delta u(k_i+1)+C_uB_u\Delta u(k_i+2)\\ \quad\vdots\\ \Delta y(k_i+N_p|k_i)&=C_uA_u^{N_p}\Delta x(k_i)+C_uA_u^{N_p-1}B_u\Delta u(k_i)+C_uA_u^{N_p-2}B_u\Delta u(k_i+1)+\cdots \\ &+C_uA_u^{N_p-N_c}B_u\Delta u(k_i+N_c-1)\\ \end{aligned} \right.

即得到如下递推方程:
Y=FΔx(ki)+ΦU Y = F\Delta x(k_i)+\Phi U

性能指标:
J=(RsY)T(RsY)+UTRU=(RsFx(ki)ΦU)T(RsFx(ki)ΦU)+UTRU=(RsFx(ki))T(RsFx(ki))2UTΦ(RsFx(ki))+UT(ΦTΦ+R)U \begin{aligned} J&=(R_s-Y)^{T}(R_s-Y)+U^{T}RU\\ &=(R_s-Fx(k_i)-\Phi U)^{T}(R_s-Fx(k_i)-\Phi U)+U^{T}RU\\ &=(R_s-Fx(k_i))^{T}(R_s-Fx(k_i))-2U^{T}\Phi (R_s-Fx(k_i))+U^{T}(\Phi^{T}\Phi+R)U \end{aligned}

JU\frac{\partial J}{\partial U}得:
JU=2ΦT(RsFx(ki))+2(ΦTΦ+R)U=0U=(ΦTΦ+R)1ΦT(RsFx(ki)) \begin{aligned} \frac{\partial J}{\partial U}&=-2\Phi^{T}(R_s-Fx(k_i))+2(\Phi^{T}\Phi+R)U=0\\ U&=(\Phi^{T}\Phi+R)^{-1}\Phi^{T}(R_s-Fx(k_i)) \end{aligned}

即差分方程迭代:
Δx(ki+1)=AkΔx(:,ki)+BkU(1:nm)U=U(1:nm)+oldUX(:,i+1)=AkX(:,ki)+BkU \begin{aligned} \Delta x(k_i+1)&=A_k\Delta x(:,k_i)+B_kU(1:n_m)\\ U &= U(1:n_m)+oldU\\ X(:,i+1)&=A_kX(:,k_i)+B_kU \end{aligned}

2. matlab 仿真代码

%================无人机模型预测控制-基于差分模型的模型预测================%
clear all;clc;close all;
%% 无人机参数设定--采用运动学模型进行轨迹跟踪
x0 = 10; y0 = 5; x1 = 11; y1 = 6;
vx0 = 0; vy0 = 0; vx1 = 1; vy1 = 1;
x(1) = x0; y(1) = y0;vx(1) = vx0;vy(1) = vy0;
%% 领航者参数设定
inter = 0.05;  % 采样周期
time = 60;  % 总时长
R = 2;
omega = 2;
t = 0:inter:time;
%% 八字形
for i = 1:1:length(t)
   if (mod(floor(omega*t(i)/(2*pi)),2) == 0)
    Xr(i) = R*cos(omega*t(i))-R;
    Yr(i) = R*sin(omega*t(i));
    Vxr(i) = -R*sin(omega*t(i))*omega;
    Vyr(i) = R*cos(omega*t(i))*omega;
    Uxr(i) = -R*cos(omega*t(i))*omega^2;
    Uyr(i) = -R*sin(omega*t(i))*omega^2;
   else
    Xr(i) = -R*cos(omega*t(i))+R;
    Yr(i) = R*sin(omega*t(i));   
    Vxr(i) = R*sin(omega*t(i))*omega;
    Vyr(i) = R*cos(omega*t(i))*omega;
    Uxr(i) = R*cos(omega*t(i))*omega^2;
    Uyr(i) = -R*sin(omega*t(i))*omega^2;
   end

end
%% 直线
% Xr = (2*t)';
% Yr = 3*ones(length(t),1);
% Vxr = 2*ones(length(t),1);
% Vyr = 2*zeros(length(t),1);
% Uxr = zeros(length(t),1);
% Uyr = zeros(length(t),1);
%% 圆形
% Xr = -R*cos(t);
% Yr = R*sin(t);
% Vxr = R*sin(t);
% Vyr = R*cos(t);
% Uxr = R*cos(t);
% Uyr = -R*sin(t);
%%
% EX(:,1) = [x0 - Xr(1);vx0 - Vxr(1);y0 - Yr(1);vy0 - Vyr(1)];

X(:,1) = [x0;vx0;y0;vy0];
deltaX(:,1) = [0;0;0;0;x0;y0];
%% 领航者轨迹
% figure
% grid minor
% l1 = [];
% axis([-7 7 -7 7]);
% axis equal
% for i = 2:1:length(t)
%   hold on
%   plot([Xr(i) Xr(i-1)],[Yr(i) Yr(i-1)],'b');
%   hold on
%   delete(l1);
%   l1 =  plot(Xr(i),Yr(i),'r.','MarkerSize',20);
%   pause(0.1);
%   
% end
%% 模型预测控制参数设定
Np = 20;     % 预测步长
Nc = 10;      % 控制步长
A = [0 1 0 0;0 0 0 0;0 0 0 1;0 0 0 0];  B = [0 0;1 0;0 0;0 1]; 
C = [1 0 0 0;0 0 1 0];
nx = size(A);
nx = nx(1);
nu = size(B);
nu = nu(2);
nm = 2;
 R = 0.002*eye(Nc*nu);
Ak = A*inter + eye(nx);
Bk = B*inter;
Au = [Ak zeros(nx,nm);C*Ak eye(nm)];
Bu = [Bk;C*Bk];
Cu = [zeros(nm,nx) eye(nm)];

F = cell(Np,1);
PHI = cell(Np,Nc);
for i = 1:1:Np         % 计算预测方程矩阵
  F{i,1} = Cu*Au^i;
end
F = cell2mat(F);

for i = 1:1:Np
   for j = 1:1:Nc
       if (j<=i)
           PHI{i,j} = Cu*Au^(i-j)*Bu;
       else
           PHI{i,j} = zeros(nm,nu);
       end
   end
end
PHI = cell2mat(PHI);
k1 =2;k2 =2;
XX = [];
%% 迭代计算
k = 1;
oldU = [0;0];
for i = 1:1:length(t)-1
   for j = i:1:(Np+i-1)
     if j >= length(Xr)
         j = length(Xr);
     end
     XX = [XX;[Xr(j);Yr(j)]]; 
   end
  U = inv(PHI'*PHI + R)*PHI'*(XX- F*deltaX(:,i));
  XX = [];
  u = U(1:2,1) + oldU;
  oldU = u;
  X(:,i+1) = Ak*X(:,i) + Bk*u;
  deltaX(:,i+1) = Au*deltaX(:,i) + Bu*U(1:2,1);
  
%   err =[X(:,i+1) - [Xr(i+1);Vxr(i+1);Yr(i+1);Vyr(i+1)]] ;
end
x = (X(1,:))';
vx = (X(2,:))';
y = (X(3,:))';
vy = (X(4,:))';
% VV = vecnorm([Vxr;Vyr]);
% VX = vecnorm([vx;vy]);
% plot(t,VV,'r')
% hold on
% plot(t,VX(1:length(t)),'b')
figure
thetr = atan2(Yr,Xr);
thet = atan2(y,x);
plot(t,thetr(1:length(t)),'r');
hold on
plot(t,thet(1:length(t)),'k');
legend('Leader','follower1')

l1 = [];
l2 = [];
pic_num = 1;
figure
 grid minor
%  axis([-5 5 -5 5])
axis equal
Tag1 = animatedline('Color','r');
for i = 1:1:length(Xr)-1
    
    hold on
    delete(l1);
   delete(l2);

    plot([x(i) x(i+1)],[y(i) y(i+1)],'b');
   hold on
   plot([Xr(i) Xr(i+1)],[Yr(i) Yr(i+1)],'r');
   hold on
   l1 = plot(x(i+1),y(i+1),'b.','MarkerSize',20);
   hold on
   l2 = plot(Xr(i+1),Yr(i+1),'r.','MarkerSize',20);
   pause(0.1);
%    addpoints(Tag1,t(i),x(i));
%    drawnow;
%     F=getframe(gcf);
%     I=frame2im(F);
%     [I,map]=rgb2ind(I,256);
%     if pic_num == 1
%         imwrite(I,map,'test.gif','gif', 'Loopcount',inf,'DelayTime',0.2);
%     else
%         imwrite(I,map,'test.gif','gif','WriteMode','append','DelayTime',0.2);
%     end
%     pic_num = pic_num + 1;
    F = getframe(gcf);
    I = frame2im(F);
    [I,map] = rgb2ind(I,256);
    if pic_num == 1
        imwrite(I,map,'test.gif','gif','Loopcount',inf,'DelayTime',0.2);
    else
        imwrite(I,map,'test.gif','gif','WriteMode','append','DelayTime',0.2);
    end
    pic_num = pic_num + 1;
     
end
相关标签: MPC