-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMAIN_UAV.m
114 lines (88 loc) · 2.95 KB
/
MAIN_UAV.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
%% initialization
clear all;close all;clc
addpath(genpath("Utils"));
addpath fcns fcns_MPC
%% --- parameters ---
% ---- gait ----
% 0-control; 1-traj;
gait = 1;
p = get_params_UAV(gait);
p.playSpeed = 1;
p.flag_movie = 1; % 1 - make movie
use_qpSWIFT = 0; % 0 - quadprog, 1 - qpSWIFT (external)
dt_sim = p.simTimeStep;
SimTimeDuration =p.SimTimeDuration; % [sec]
MAX_ITER = floor(SimTimeDuration/p.simTimeStep);
% desired trajectory
p.acc_d = 0; %acceleration
p.vel_d = [0;0];% x,y
p.yaw_d = 2;%yaw
%% Model Predictive Control
% --- initial condition ---
% Xt = [pc dpc vR wb]': [18,1] represents the robot's position, velocity, attitude, and angular velocity information.
% Ut=[t1 t2 t3 t4]' : [4,1] represents thrusts of four propeller
[Xt,Ut] = fcn_gen_XdUd_UAV(0,[],p);
% --- logging ---
tstart = 0;
tend = dt_sim;
[tout,Xout,Uout,Xdout,Udout,Uext,FSMout] = deal([]);
% --- simulation ----
h_waitbar = waitbar(0,'Calculating...');
tic
% 离线计算
for ii = 1:MAX_ITER
% --- time vector ---
t_ = dt_sim * (ii-1) + p.Tmpc * (0:p.predHorizon-1);%MPC horizon
% --- Traj ---
if gait == 1
% Ud: the simple force allocation of gravity based on the number of propeller.
[p,Xd,Ud] = fcn_UAV_ref_traj(t_, Xt, p);
else
[Xd,Ud] = fcn_gen_XdUd_UAV(t_,Xt,p);
end
% --- MPC ----
% form QP
[H,g,Aineq,bineq,Aeq,beq] = fcn_get_QP_form_eta_UAV(Xt,Ut,Xd,Ud,p);
if ~use_qpSWIFT
% solve QP using quadprog
% options1 = optimset('Display', 'iter');
% [zval] = quadprog(H,g,Aineq,bineq,Aeq,beq,[],[],[],options1);
[zval] = quadprog(H,g,Aineq,bineq,Aeq,beq,[],[],[]);
else
% interface with the QP solver qpSWIFT
[zval,basic_info] = qpSWIFT(sparse(H),g,sparse(Aeq),beq,sparse(Aineq),bineq);
end
Ut = Ut + zval(1:4);
% --- external disturbance ---
[u_ext,p_ext] = fcn_get_disturbance(tstart,p);
p_ext=0*p_ext;
u_ext = 0*u_ext;
p.p_ext = p_ext; % position of external force
% --- simulate ---
[t,X] = ode45( @(t,X)dynamics_SRB_UAV(t,X,Ut,Xd,0*u_ext,p), [tstart,tend] ,Xt);
% --- update ---
% 更新
Xt = X(end,:)';
tstart = tend;
tend = tstart + dt_sim;
% --- log ---
% 拼接和记录
lent = length(t(2:end));
tout = [tout;t(2:end)];
Xout = [Xout;X(2:end,:)];
Uout = [Uout;repmat(Ut',[lent,1])];
Xdout = [Xdout;repmat(Xd(:,1)',[lent,1])];
Udout = [Udout;repmat(Ud(:,1)',[lent,1])];
Uext = [Uext;repmat(u_ext',[lent,1])];
% FSMout = [FSMout;repmat(FSM',[lent,1])];
waitbar(ii/MAX_ITER,h_waitbar,'Calculating...');
end
close(h_waitbar)%关闭等待条
fprintf('Calculation Complete!\n')
toc
%% Animation
disp("Size of tout:");
disp(size(tout));
disp("Size of Uout:");
disp(size(Uout));
[t,EA,EAd] = fig_animate_UAV(tout,Xout,Uout,Xdout,Udout,Uext,p);