-
Notifications
You must be signed in to change notification settings - Fork 0
/
UUVsimulation.asv
219 lines (198 loc) · 11.5 KB
/
UUVsimulation.asv
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
clear;
N_states = 4;
N_inputs = 2;
tspan = (0:0.1:50)'; % ode time span
syms t
%%%%%%% nominal data setting (usually equilibrium) to get the linearized system
%%%%%%% and the nominal solution
% Note that X doesn't influence the states
% We have 3 system-dependent states, and 5 variables to set to get the
% desired motion. Therefore we need to set 2 parameters to get the rest of
% the steady motion states and inputs
nominal_data_setting = [0.2; 5]; % Set Z, u.
nominal_x0 = [0; nominal_data_setting(1); nominal_data_setting(2); 0]; % nominal initial state: X, Z, u, w
[A,B,nominal_x,nominal_input] = getLinearSys(nominal_x0,nominal_data_setting);
%%%%%%% initial states
x0 = nominal_x0;
x0(2) = x0(2)+1/3*0.05; % initial Z. Note that the vehicle height is 1/3
x0(3) = x0(3)*1.05; % initial Z. Note that the vehicle height is 1/3
%%%%%%% Control gains
%%% PID got from transfer fucntions
Ku = getPID(1,nominal_x,nominal_input);
Kw = getPID(2,nominal_x,nominal_input);
KGains = [0 0 Ku(1) 0; 0 Kw(1) 0 Kw(3)]
% solve ode of nonlinear system
UUVode = @(tt,x) systemUUV(tt,x,nominal_x,nominal_input,KGains);
[thist,xhist] = ode45(UUVode,tspan,x0);
% solve ode of linearized system
Detx0 = x0-nominal_x0;
UUVode_Linear = @(tt,Detx) systemUUVLinear(tt,Detx,A,B,KGains);
[thist_LinearDet,xhist_LinearDet] = ode45(UUVode_Linear,tspan,Detx0);
% force record. 1st row: Fu / 2nd row: Fw
forceNonlinear = repmat(nominal_input,1,length(tspan)) + KGains*(repmat(nominal_x,1,length(tspan))-xhist');
forceLinear = repmat(nominal_input,1,length(tspan)) + KGains*(0-xhist_LinearDet');
%%%%% pole placement
dampingRatio = sin(40/180*pi);
sigma = 0.5;
naturalFrequency = sigma/dampingRatio;
Frequency = sqrt(1-dampingRatio^2)*naturalFrequency;
timeConst = 5;
p = [-1/timeConst; -sigma+1i*Frequency; -sigma-1i*Frequency];
K = place(A(2:end,2:end),B(2:end,:),p);
KGains = [0 K(1,:); 0 K(2,:)]
% solve ode of nonlinear system
UUVode = @(tt,x) systemUUV(tt,x,nominal_x,nominal_input,KGains);
[thist2,xhist2] = ode45(UUVode,tspan,x0);
% solve ode of linearized system
Detx0 = x0-nominal_x0;
UUVode_Linear = @(tt,Detx) systemUUVLinear(tt,Detx,A,B,KGains);
[thist_LinearDet2,xhist_LinearDet2] = ode45(UUVode_Linear,tspan,Detx0);
% force record. 1st row: Fu / 2nd row: Fw
forceNonlinear2 = repmat(nominal_input,1,length(tspan)) + KGains*(repmat(nominal_x,1,length(tspan))-xhist2');
forceLinear2 = repmat(nominal_input,1,length(tspan)) + KGains*(0-xhist_LinearDet2');
%%%%% LQR
% equal "badness"
ZstdErr = 0.02;
ustdErr = 0.2;
wstdErr = 0.005;
Q = eye(N_states-1)*100;
Q(1,1) = Q(1,1)/ZstdErr^2;
Q(2,2) = Q(2,2)/ustdErr^2;
Q(3,3) = Q(3,3)/wstdErr^2;
R = eye(N_inputs);
K = lqr(A(2:end,2:end),B(2:end,:),Q,R);
KGains = [0 K(1,:); 0 K(2,:)]
% solve ode of nonlinear system
UUVode = @(tt,x) systemUUV(tt,x,nominal_x,nominal_input,KGains);
[thist3,xhist3] = ode45(UUVode,tspan,x0);
% solve ode of linearized system
Detx0 = x0-nominal_x0;
UUVode_Linear = @(tt,Detx) systemUUVLinear(tt,Detx,A,B,KGains);
[thist_LinearDet3,xhist_LinearDet3] = ode45(UUVode_Linear,tspan,Detx0);
% force record. 1st row: Fu / 2nd row: Fw
forceNonlinear3 = repmat(nominal_input,1,length(tspan)) + KGains*(repmat(nominal_x,1,length(tspan))-xhist3');
forceLinear3 = repmat(nominal_input,1,length(tspan)) + KGains*(0-xhist_LinearDet3');
%%%%%% ode45 for nonlinear system with openloop control
% DetForce_hist = (-feedbackGain*xhist_LinearDet')';
% ControlForce_hist = zeros(length(thist_LinearDet),2);
% ControlForce_hist(:,1) = nominal_input(1,1)+DetForce_hist(:,1);
% ControlForce_hist(:,2) = nominal_input(2,1)+DetForce_hist(:,2);
% UUVode45openLoop = @(tt,x) systemUUVopenLoop(tt,x,ControlForce_hist,thist_LinearDet);
% [thist_open,xhist_open] = ode45(UUVode45openLoop,tspan,x0);
%%%%%% show the nominal trajectory
nominal_x_shown = zeros(length(tspan),4);
nominal_x_shown(:,1) = double(subs(nominal_x(1,1),t,tspan));
nominal_x_shown(:,2) = double(nominal_x(2,1));
nominal_x_shown(:,3) = double(nominal_x(3,1));
nominal_x_shown(:,4) = double(nominal_x(4,1));
% figure, % response (SISO-PID design)
% subplot(2,1,1),
% plot(thist_LinearDet, nominal_x_shown(:,2), 'r'); hold on;
% plot(thist, xhist(:,2), '-o'); hold on;
% plot(thist_LinearDet, xhist_LinearDet(:,2)+nominal_x(2,1), 'g', 'LineWidth', 2); ylabel('Z(m)'), xlabel('time(s)'),
% title({'Model response (SISO-PID design)',['Devx0: ',num2str(x0'-nominal_x0')],['(units) / Desired Z: ',num2str(double(nominal_x(2,1))),'(m) / Desired u: ',num2str(double(nominal_x(3,1))),'(m/s)']});
% legend('nominal trajectory', 'nonlinear', 'linear');
% subplot(2,1,2),
% plot(thist_LinearDet, nominal_x_shown(:,3), 'r'); hold on;
% plot(thist, xhist(:,3), '-o'); hold on;
% plot(thist_LinearDet, xhist_LinearDet(:,3)+nominal_x(3,1), 'g', 'LineWidth', 2); ylabel('u(m/s)'), xlabel('time(s)');
% % subplot(3,1,3),
% % plot(thist_LinearDet, nominal_x_shown(:,4), 'r'); hold on;
% % plot(thist, xhist(:,4)); hold on;
% % plot(thist_LinearDet, xhist_LinearDet(:,4)+nominal_x(4,1), 'g', 'LineWidth', 2); ylabel('w(m/s)'), xlabel('time(s)');
% figure, % response (pole placement)
% subplot(2,1,1),
% plot(thist_LinearDet2, nominal_x_shown(:,2), 'r'); hold on;
% plot(thist2, xhist2(:,2), '-o'); hold on;
% plot(thist_LinearDet2, xhist_LinearDet2(:,2)+nominal_x(2,1), 'g', 'LineWidth', 2); ylabel('Z(m)'), xlabel('time(s)'),
% title({'Model response (pole placement)',['Devx0: ',num2str(x0'-nominal_x0')],['(units) / Desired Z: ',num2str(double(nominal_x(2,1))),'(m) / Desired u: ',num2str(double(nominal_x(3,1))),'(m/s)']});
% legend('nominal trajectory', 'nonlinear', 'linear');
% subplot(2,1,2),
% plot(thist_LinearDet2, nominal_x_shown(:,3), 'r'); hold on;
% plot(thist2, xhist2(:,3), '-o'); hold on;
% plot(thist_LinearDet2, xhist_LinearDet2(:,3)+nominal_x(3,1), 'g', 'LineWidth', 2); ylabel('u(m/s)'), xlabel('time(s)');
% % subplot(3,1,3),
% % plot(thist_LinearDet2, nominal_x_shown(:,4), 'r'); hold on;
% % plot(thist, xhist2(:,4)); hold on;
% % plot(thist_LinearDet2, xhist_LinearDet2(:,4)+nominal_x(4,1), 'g', 'LineWidth', 2); ylabel('w(m/s)'), xlabel('time(s)');
% figure, % response (LQR)
% subplot(2,1,1),
% plot(thist_LinearDet3, nominal_x_shown(:,2), 'r'); hold on;
% plot(thist3, xhist3(:,2), '-o'); hold on;
% plot(thist_LinearDet3, xhist_LinearDet3(:,2)+nominal_x(2,1), 'g', 'LineWidth', 2); ylabel('Z(m)'), xlabel('time(s)'),
% title({'Model response (LQR)',['Devx0: ',num2str(x0'-nominal_x0')],['(units) / Desired Z: ',num2str(double(nominal_x(2,1))),'(m) / Desired u: ',num2str(double(nominal_x(3,1))),'(m/s)']});
% legend('nominal trajectory', 'nonlinear', 'linear');
% subplot(2,1,2),
% plot(thist_LinearDet3, nominal_x_shown(:,3), 'r'); hold on;
% plot(thist3, xhist3(:,3), '-o'); hold on;
% plot(thist_LinearDet3, xhist_LinearDet3(:,3)+nominal_x(3,1), 'g', 'LineWidth', 2); ylabel('u(m/s)'), xlabel('time(s)');
% % subplot(3,1,3),
% % plot(thist_LinearDet3, nominal_x_shown(:,4), 'r'); hold on;
% % plot(thist, xhist3(:,4)); hold on;
% % plot(thist_LinearDet3, xhist_LinearDet3(:,4)+nominal_x(4,1), 'g', 'LineWidth', 2); ylabel('w(m/s)'), xlabel('time(s)');
figure, % compared response
subplot(2,1,1),
plot(tspan, nominal_x_shown(:,2), 'r'); hold on;
plot(thist_LinearDet, xhist_LinearDet(:,2)+nominal_x(2,1), 'b', 'LineWidth', 1); ylabel('Z(m)'), xlabel('time(s)'); hold on;
plot(thist_LinearDet2, xhist_LinearDet2(:,2)+nominal_x(2,1), 'g', 'LineWidth', 1); ylabel('Z(m)'), xlabel('time(s)'); hold on;
plot(thist_LinearDet3, xhist_LinearDet3(:,2)+nominal_x(2,1), 'c', 'LineWidth', 1); ylabel('Z(m)'), xlabel('time(s)');
title({'Model response',['Devx0: ',num2str(x0'-nominal_x0')],['(units) / Desired Z: ',num2str(double(nominal_x(2,1))),'(m) / Desired u: ',num2str(double(nominal_x(3,1))),'(m/s)']});
legend('nominal trajectory', 'SISO-PID design', 'pole placement', 'LQR');
subplot(2,1,2),
plot(tspan, nominal_x_shown(:,3), 'r'); hold on;
plot(thist_LinearDet, xhist_LinearDet(:,3)+nominal_x(3,1), 'b', 'LineWidth', 1); ylabel('u(m)'), xlabel('time(s)'); hold on;
plot(thist_LinearDet2, xhist_LinearDet2(:,3)+nominal_x(3,1), 'g', 'LineWidth', 1); ylabel('u(m/s)'), xlabel('time(s)'); hold on;
plot(thist_LinearDet3, xhist_LinearDet3(:,3)+nominal_x(3,1), 'c', 'LineWidth', 1); ylabel('u(m/s)'), xlabel('time(s)');
% figure, % force (SISO-PID design)
% subplot(2,1,1),
% plot(thist, forceNonlinear(1,:), '-o'); hold on;
% plot(thist_LinearDet, forceLinear(1,:), 'g', 'LineWidth', 2); ylabel('Fu'), xlabel('time(s)');
% title({['Input force (SISO-PID design)'], ['Devx0: ',num2str(x0'-nominal_x0')], ['Desired Z: ',num2str(double(nominal_x(2,1))),'(m) / Desired u: ',num2str(double(nominal_x(3,1))),'(m/s)']});
% legend('nonlinear', 'linear');
% subplot(2,1,2),
% plot(thist, forceNonlinear(2,:), '-o'); hold on;
% plot(thist_LinearDet, forceLinear(2,:), 'g', 'LineWidth', 2); ylabel('Fw'), xlabel('time(s)');
% figure, % force (pole placement)
% subplot(2,1,1),
% plot(thist2, forceNonlinear2(1,:), '-o'); hold on;
% plot(thist_LinearDet2, forceLinear2(1,:), 'g', 'LineWidth', 2); ylabel('Fu'), xlabel('time(s)');
% title({['Input force (pole placement)'], ['Devx0: ',num2str(x0'-nominal_x0')], ['Desired Z: ',num2str(double(nominal_x(2,1))),'(m) / Desired u: ',num2str(double(nominal_x(3,1))),'(m/s)']});
% legend('nonlinear', 'linear');
% subplot(2,1,2),
% plot(thist2, forceNonlinear2(2,:), '-o'); hold on;
% plot(thist_LinearDet2, forceLinear2(2,:), 'g', 'LineWidth', 2); ylabel('Fw'), xlabel('time(s)');
% figure, % force (LQR)
% subplot(2,1,1),
% plot(thist3, forceNonlinear3(1,:), '-o'); hold on;
% plot(thist_LinearDet3, forceLinear3(1,:), 'g', 'LineWidth', 2); ylabel('Fu'), xlabel('time(s)');
% title({['Input force (LQR)'], ['Devx0: ',num2str(x0'-nominal_x0')], ['Desired Z: ',num2str(double(nominal_x(2,1))),'(m) / Desired u: ',num2str(double(nominal_x(3,1))),'(m/s)']});
% legend('nonlinear', 'linear');
% subplot(2,1,2),
% plot(thist3, forceNonlinear3(2,:), '-o'); hold on;
% plot(thist_LinearDet3, forceLinear3(2,:), 'g', 'LineWidth', 2); ylabel('Fw'), xlabel('time(s)');
%%%%%%%%% unused %%%%%%%%%
% figure,
% subplot(3,1,1),
% plot(thist_LinearDet, xhist_LinearDet(:,2)); ylabel('Predicted Deviation Z(m)'), xlabel('time(s)');
% title({'Predicted deviation using the linear feedback method',['u0: ',num2str(x0(3,1)),'(m/s) / Z0: ',num2str(x0(2,1)),'(m) / Desired Z: ',num2str(double(nominal_x(2,1))),'(m) / Desired u: ',num2str(double(nominal_x(3,1))),'(m/s)']});
% subplot(3,1,2),
% plot(thist_LinearDet, xhist_LinearDet(:,3)); ylabel('Predicted Deviation u(m/s)');
% subplot(3,1,3),
% plot(thist_LinearDet, xhist_LinearDet(:,4)); ylabel('Predicted Deviation w(m/s)');
% figure,
% subplot(3,1,1),
% plot(thist_open, xhist_open(:,2), thist_open, nominal_x_shown(:,2), 'r'); ylabel('Z(m)'), xlabel('time(s)');
% title({'Nonlinear model using linear feedback method',['u0: ',num2str(x0(3,1)),'(m/s) / Z0: ',num2str(x0(2,1)),'(m) / Desired Z: ',num2str(double(nominal_x(2,1))),'(m) / Desired u: ',num2str(double(nominal_x(3,1))),'(m/s)']});
% subplot(3,1,2),
% plot(thist_open, xhist_open(:,3), thist_open, nominal_x_shown(:,3), 'r'); ylabel('u(m/s)');
% subplot(3,1,3),
% plot(thist_open, xhist_open(:,4), thist_open, nominal_x_shown(:,4), 'r'); ylabel('w(m/s)');
%
% figure,
% subplot(3,1,1),
% plot(thist_open, xhist_open(:,2)-nominal_x_shown(:,2)); ylabel('Real deviation Z(m)'), xlabel('time(s)');
% title({'Real deviation using the linear feedback method',['u0: ',num2str(x0(3,1)),'(m/s) / Z0: ',num2str(x0(2,1)),'(m) / Desired Z: ',num2str(double(nominal_x(2,1))),'(m) / Desired u: ',num2str(double(nominal_x(3,1))),'(m/s)']});
% subplot(3,1,2),
% plot(thist_open, xhist_open(:,3)-nominal_x_shown(:,3)); ylabel('Real deviation u(m/s)');
% subplot(3,1,3),
% plot(thist_open, xhist_open(:,4)-nominal_x_shown(:,4)); ylabel('Real deviation w(m/s)');