-
Notifications
You must be signed in to change notification settings - Fork 2
/
sim.m
157 lines (123 loc) · 3.8 KB
/
sim.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
ctrl = {'lqru', 'pid', 'mpc', 'mpc2', 'quadprog', 'fastmpc'};
%controller_type = 'lqru';
%controller_type = 'pid';
%controller_type = 'mpc';
for ctrl_idx = [2,5, 6]
controller_type = ctrl{ctrl_idx};
clear vehicle;
qzVehicle;
cvx_solver sedumi;
cvx_quiet(true);
%target Fx Fz
%Ftarget = [15; -1.2*vehicle.weight];
Ftarget = [3; -1*vehicle.weight];
uLoop = true;
uTarget = 2;
Tmax = 3;
t = 0:vehicle.dt:Tmax;
n = length(t);
U = zeros(4,n);
FM = zeros(3,n);
Y = zeros(size(vehicle.sysSim.c,1),n);
Mycmd = zeros(1,n);
thetaCmd = Mycmd;
%estimator
Mydist = Mycmd;
ThetaQEst = zeros(2,n);
Fhist = zeros(2,n);
%initial attitude
vehicle.xsim(1) = 5*pi/180;
Y(:,1) = vehicle.sysSim.C * vehicle.xsim;
%intialize disturbance estimator
vehicle.estimator_dist.xd = [vehicle.xsim(1); 0 ; 0; 0];
ThetaQEst(:,1) = vehicle.estimator_dist.xd(1:2);
%vehicle.estimator_dist.Myd = 1;
vehicle = dynamics(vehicle,zeros(4,1));
tstart = tic;
for i = 1:n-1
if(uLoop)
vehicle = control_u_pid(vehicle,uTarget);
Ftarget(1) = vehicle.control_u_pid.FxTarget;
end
Fhist(:,i) = Ftarget;
switch controller_type
case 'fastmpc'
vehicle = control_fastmpc(vehicle,Ftarget);
Mycmd(:,i) = 0;
thetaCmd(:,i) = 0;
case 'quadprog'
vehicle = control_quadprog(vehicle,Ftarget);
Mycmd(:,i) = 0;
thetaCmd(:,i) = 0;
case 'mpc2'
vehicle = control_cvx_noineq(vehicle,Ftarget);
Mycmd(:,i) = vehicle.lcm(3,:) * vehicle.U;
thetaCmd(:,i) = 0;
case 'mpc'
vehicle = control_cvx(vehicle,Ftarget);
Mycmd(:,i) = 0;
thetaCmd(:,i) = 0;
case 'pid'
vehicle = control_pid(vehicle,Ftarget);
Mycmd(:,i) = vehicle.control_pid.Mycmd;
thetaCmd(:,i) = vehicle.control_pid.cmd;
case 'lqru'
vehicle = control_lqru(vehicle,Ftarget);
Mycmd(:,i) = vehicle.control_lqru.Mycmd;
thetaCmd(:,i) = vehicle.control_lqru.theta_cmd;
end
U(:,i) = vehicle.U;
vehicle = dynamics(vehicle,U(:,i));
vehicle = estimator(vehicle);
Y(:,i+1) = vehicle.ysim;
FM(:,i+1) = vehicle.FM;
ThetaQEst(:,i+1) = vehicle.estimator_dist.xd(1:2);
Mydist(:,i) = vehicle.estimator_dist.Myd;
if(mod(i,50) == 0)
fprintf(1,'%2.1f%%\n',100*i/n);
end
end
deltaT = toc(tstart);
if(isfield(vehicle.control_cvx,'numSol'))
numSol = vehicle.control_cvx.numSol;
deltaT_persolve = vehicle.control_cvx.deltaT/vehicle.control_cvx.numSol;
else
numSol = 0;
deltaT_persolve = 0;
end
fprintf(1,'runttime was %f (total solves = %i , per solve = %3.0fms) \n', deltaT, numSol, deltaT_persolve*1000);
%%
U = U/(vehicle.weight/size(U,1));
thetaCmd_d = thetaCmd * 180/pi;
theta_d = Y(vehicle.thetaIndex,:)*180/pi;
q_d = Y(vehicle.qIndex,:)*180/pi;
u = Y(vehicle.uIndex,:);
thetaEst_d = ThetaQEst(1,:)*180/pi;
Fx = FM(1,:);
Fz = FM(2,:);
My = FM(3,:);
figuren(controller_type); clf;
subplot(2,2,1);
plot(t,theta_d,'b',t,thetaCmd_d,'b--',t,thetaEst_d,'k--');
hold on;
plot(t,q_d,'g--');
plot(t,u,'r');
ylim([-20 20]); grid on;
xlabel('time'); ylabel('theta');
legend('Theta','ThetaCmd','ThetaEst','q','u','Location','EastOutside');
subplot(2,2,2);
plot(t,U(1,:), t, U(2,:) , t, U(3,:), t, U(4,:));
ylim([0 1.3]); grid on;
xlabel('time'); ylabel('Normalized control');
%legend({'t1','t2','t3','t4'});
subplot(2,2,3);
plot(t,Fx./vehicle.weight , t,Fhist(1,:)./vehicle.weight, t, Fz./Ftarget(2));
ylim([-1 1.5]); grid on;
xlabel('time'); ylabel('Normalized Forces');
legend({'fx', 'fxcmd','fz'});
subplot(2,2,4);
plot(t,My,'b', t, Mycmd,'b--', t, -Mydist, 'k--');
ylim([-3 3]); grid on;
xlabel('time'); ylabel('My');
legend('My','Mycmd','-Mydist');
end