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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
% Simulate a step responses of ducted-fan VTOL micro-UAV.
%
% Copyright (C) 2024, Naoki Sean Pross, ETH Zürich
% This work is distributed under a permissive license, see LICENSE.txt
function [simout] = uav_sim_step(params, model, ctrl, uncert, nsamp, T, do_plots, do_noise)
% Load closed loop model and add controller
% more or less equivalent to doing usys = lft(Pnom, K)
h = load_system('uav_model_uncertain_clp');
hws = get_param('uav_model_uncertain_clp', 'modelworkspace');
if isfield(ctrl, 'K')
hws.assignin('K', ctrl.K);
else
error('You need to provide a controller ctrl.K');
end
% There is an uncertainty block
if isfield(uncert, 'Delta')
fprintf(' - Running worst case with provided Delta.\n');
hws.assignin('Delta', uncert.Delta);
else
fprintf(' - No uncertainty error Delta was provided, setting to zero.\n');
Delta = tf(0) * zeros(sum(model.uncertain.BlockStructure));
hws.assignin('Delta', Delta);
end
ulmod_clp = linmod('uav_model_uncertain_clp');
P_clp = minreal(ss(ulmod_clp.a, ulmod_clp.b, ulmod_clp.c, ulmod_clp.d), [], false);
% Check that closed loop is actually stable
nx = size(P_clp.A, 1);
fprintf(' - Closed loop dynamics have %d states\n', nx);
if nx < 60
[~, nsta, nctrb, nustab, nobsv, nudetb] = pbhtest(P_clp);
if nx ~= nsta
error('Closed loop is not stable!');
end
fprintf(' %d stable modes, %d unstable modes\n', nsta, nx - nsta);
fprintf(' %d controllable modes, %d unstabilizable\n', nctrb, nustab);
fprintf(' %d observable modes, %d undetectable\n', nobsv, nudetb);
else
eigvals = eig(P_clp.A);
for i = 1:nx
if real(eigvals(i)) >= 0
fprintf(' Closed loop is not stable!\n');
break;
end
end
end
% Generate indices for closed loop plant
o = model.uncertain.dims.output;
i = model.uncertain.dims.input;
dims = struct(...
'ErrorAlpha', o.OutputErrorAlpha, ...
'ErrorOmega', o.OutputErrorOmega, ...
'ErrorPosition', o.OutputErrorPosition, ...
'ErrorVelocity', o.OutputErrorVelocity, ...
'ErrorEulerAngles', o.OutputErrorEulerAngles, ...
'Position', o.OutputNominalPosition, ...
'Velocity', o.OutputNominalVelocity, ...
'EulerAngles', o.OutputNominalEulerAngles, ...
'AngularRates', o.OutputNominalAngularRates, ...
'PlotAlpha', o.OutputPlotAlpha, ...
'PlotOmega', o.OutputPlotOmega, ...
'PlotReference', o.OutputPlotReference, ...
'PlotPosition', o.OutputPlotPosition, ...
'InputAlpha', i.InputNominalAlpha, ...
'InputOmega', i.InputNominalOmega ...
);
idx = struct();
start = 1; fields = fieldnames(dims);
for f = fields'
dim = getfield(dims, f{1});
i = (start:(start + dim - 1))';
idx = setfield(idx, f{1}, i);
start = start + dim;
end
% Input indices
idx.InputDisturbanceWind = (1:3)';
idx.InputDisturbanceFlaps = (4:7)';
idx.InputReference = (8:10)';
% Create noise
noise = zeros(7, nsamp);
if do_noise
% Noise (normalized)
noise_alpha_amp = (.5 * (pi / 180)) / params.actuators.ServoAbsMaxAngle;
noise_wind_amp = .01;
noise = [noise_wind_amp * randn(3, nsamp);
noise_alpha_amp * randn(4, nsamp)];
end
% Step size
step_size_h = .5 / params.normalization.HPosition;
step_size_v = .5 / params.normalization.VPosition;
% Create step inputs (normalized)
ref_step = ones(1, nsamp); % 1d step function
in_step_x = [ noise; step_size_h * ref_step; zeros(2, nsamp) ];
in_step_y = [ noise; zeros(1, nsamp); step_size_h * ref_step; zeros(1, nsamp) ];
in_step_z = [ noise; zeros(2, nsamp); -step_size_v * ref_step ]; % z points down
% Simulation time
t = linspace(0, T, nsamp);
% Scale simulation outputs
S = blkdiag(...
model.uncertain.scaling.OutputErrorScaling, ...
model.uncertain.scaling.OutputNominalScaling, ...
model.uncertain.scaling.OutputPlotScaling, ...
model.uncertain.scaling.InputNominalScaling ...
);
% Simulate step responses
out_step_x_norm = lsim(P_clp, in_step_x, t, 'foh');
out_step_y_norm = lsim(P_clp, in_step_y, t, 'foh');
out_step_z_norm = lsim(P_clp, in_step_z, t, 'foh');
out_step_x = out_step_x_norm * S;
out_step_y = out_step_y_norm * S;
out_step_z = out_step_z_norm * S;
% Return simulation
simout = struct(...
'Time', t, ...
'StepX', out_step_x, ...
'StepY', out_step_y, ...
'StepZ', out_step_z, ...
'StepXNorm', out_step_x_norm, ...
'StepYNorm', out_step_y_norm, ...
'StepZNorm', out_step_z_norm, ...
'Simulink', ulmod_clp, ...
'StateSpace', P_clp, ...
'index', idx);
if do_plots
% Conversion factors
to_deg = 180 / pi; % radians to degrees
to_rpm = pi / 30; % rad / s to RPM
% Figure for flaps and Euler angles
figure;
sgtitle(sprintf(...
'\\bfseries Step Response of Flap and Euler Angles (%s)', ...
ctrl.Name), 'Interpreter', 'latex');
% Plot limits
alpha_max_deg = params.actuators.ServoAbsMaxAngle * to_deg;
euler_lim_deg = .5;
omega_max_rpm = (params.actuators.PropellerMaxAngularVelocity ...
- params.linearization.Inputs(5)) * to_rpm;
omega_min_rpm = -params.linearization.Inputs(5) * to_rpm;
% Plot step response from x to alpha
subplot(2, 3, 1);
hold on;
plot(t, out_step_x(:, idx.PlotAlpha(1)) * to_deg);
plot(t, out_step_x(:, idx.PlotAlpha(2)) * to_deg);
plot(t, out_step_x(:, idx.PlotAlpha(3)) * to_deg);
plot(t, out_step_x(:, idx.PlotAlpha(4)) * to_deg);
plot([0, T], [1, 1] * alpha_max_deg, 'r--');
plot([0, T], [-1, -1] * alpha_max_deg, 'r--');
grid on;
xlim([0, T]);
ylim([-alpha_max_deg * 1.1, alpha_max_deg * 1.1]);
title('Horizontal $x$ to Flaps', 'Interpreter', 'latex');
ylabel('Flap Angle (degrees)', 'Interpreter', 'latex');
xlabel('Time (seconds)', 'Interpreter', 'latex');
legend('$\alpha_1(t)$', '$\alpha_2(t)$', '$\alpha_3(t)$', '$\alpha_4(t)$', ...
'Interpreter', 'latex');
% Plot step response from y to alpha
subplot(2, 3, 2); hold on;
plot(t, out_step_y(:, idx.PlotAlpha(1)) * to_deg);
plot(t, out_step_y(:, idx.PlotAlpha(2)) * to_deg);
plot(t, out_step_y(:, idx.PlotAlpha(3)) * to_deg);
plot(t, out_step_y(:, idx.PlotAlpha(4)) * to_deg);
plot([0, T], [1, 1] * alpha_max_deg, 'r--');
plot([0, T], [-1, -1] * alpha_max_deg, 'r--');
grid on;
xlim([0, T]);
ylim([-alpha_max_deg * 1.1, alpha_max_deg * 1.1]);
title('Horizontal $y$ to Flaps', 'Interpreter', 'latex');
ylabel('Flap Angle (degrees)', 'Interpreter', 'latex');
xlabel('Time (seconds)', 'Interpreter', 'latex');
legend('$\alpha_1(t)$', '$\alpha_2(t)$', '$\alpha_3(t)$', '$\alpha_4(t)$', ...
'Interpreter', 'latex');
% Plot step response from z to alpha
subplot(2, 3, 3); hold on;
plot(t, out_step_z(:, idx.PlotAlpha(1)) * to_deg);
plot(t, out_step_z(:, idx.PlotAlpha(2)) * to_deg);
plot(t, out_step_z(:, idx.PlotAlpha(3)) * to_deg);
plot(t, out_step_z(:, idx.PlotAlpha(4)) * to_deg);
plot([0, T], [1, 1] * alpha_max_deg, 'r--');
plot([0, T], [-1, -1] * alpha_max_deg, 'r--');
grid on;
xlim([0, T]);
ylim([-alpha_max_deg * 1.1, alpha_max_deg * 1.1]);
title('Vertical $z$ to Flaps', 'Interpreter', 'latex');
ylabel('Flap Angle (degrees)', 'Interpreter', 'latex');
xlabel('Time (seconds)', 'Interpreter', 'latex');
legend('$\alpha_1(t)$', '$\alpha_2(t)$', '$\alpha_3(t)$', '$\alpha_4(t)$', ...
'Interpreter', 'latex');
% Plot step response from x to Theta
subplot(2, 3, 4); hold on;
plot(t, out_step_x(:, idx.EulerAngles(1)) * to_deg);
plot(t, out_step_x(:, idx.EulerAngles(2)) * to_deg);
plot(t, out_step_x(:, idx.EulerAngles(3)) * to_deg);
grid on;
xlim([0, T]);
ylim([-euler_lim_deg, euler_lim_deg]);
title('Horizontal $x$ to Euler Angles', 'Interpreter', 'latex');
ylabel('Euler Angle (degrees)', 'Interpreter', 'latex');
xlabel('Time (seconds)', 'Interpreter', 'latex');
legend('$\phi(t)$ Roll ', '$\theta(t)$ Pitch ', '$\psi(t)$ Yaw ', ...
'Interpreter', 'latex');
% Plot step response from y to Theta
subplot(2, 3, 5); hold on;
plot(t, out_step_y(:, idx.EulerAngles(1)) * to_deg);
plot(t, out_step_y(:, idx.EulerAngles(2)) * to_deg);
plot(t, out_step_y(:, idx.EulerAngles(3)) * to_deg);
grid on;
xlim([0, T]);
ylim([-euler_lim_deg, euler_lim_deg]);
title('Horizontal $y$ to Euler Angles', 'Interpreter', 'latex');
ylabel('Euler Angle (degrees)', 'Interpreter', 'latex');
xlabel('Time (seconds)', 'Interpreter', 'latex');
legend('$\phi(t)$ Roll ', '$\theta(t)$ Pitch ', '$\psi(t)$ Yaw ', ...
'Interpreter', 'latex');
% Plot step response from z to Theta
subplot(2, 3, 6); hold on;
plot(t, out_step_z(:, idx.EulerAngles(1)) * to_deg);
plot(t, out_step_z(:, idx.EulerAngles(2)) * to_deg);
plot(t, out_step_z(:, idx.EulerAngles(3)) * to_deg);
grid on;
xlim([0, T]);
ylim([-euler_lim_deg, euler_lim_deg]);
title('Vertical $z$ to Euler Angles', 'Interpreter', 'latex');
ylabel('Euler Angle (degrees)', 'Interpreter', 'latex');
xlabel('Time (seconds)', 'Interpreter', 'latex');
legend('$\phi(t)$ Roll ', '$\theta(t)$ Pitch ', '$\psi(t)$ Yaw ', ...
'Interpreter', 'latex');
% Plot step response from z to omega
figure;
sgtitle(sprintf(...
'\\bfseries Step Response to Propeller (%s)', ...
ctrl.Name), 'Interpreter', 'latex');
hold on;
step(P_clp(idx.PlotOmega, idx.InputReference(3)) * to_rpm, T);
% plot([0, T], [1, 1] * omega_min_rpm, 'r--');
% plot([0, T], [1, 1] * omega_max_rpm, 'r--');
grid on;
% ylim([omega_min_rpm - 1, omega_max_rpm + 1]);
title('Vertical $z$ to Thruster $\omega$', 'Interpreter', 'latex');
ylabel('Angular Velocity (RPM)', 'Interpreter', 'latex');
xlabel('Time (seconds)', 'Interpreter', 'latex');
legend('$\omega(t)$', 'Interpreter', 'latex');
% Figure for position and velocity
figure;
sgtitle(sprintf(...
'\\bfseries Step Response of Position and Speed (%s)', ...
ctrl.Name), 'Interpreter', 'latex');
% Plot step response from horizontal reference to horizontal position
subplot(2, 2, 1); hold on;
plot(t, out_step_x(:, idx.PlotPosition(1)));
plot(t, out_step_y(:, idx.PlotPosition(2)));
plot(t, out_step_x(:, idx.PlotReference(1)), '--');
grid on;
title('Horizontal Position', 'Interpreter', 'latex');
ylabel('Distance (meters)', 'Interpreter', 'latex');
xlabel('Time (seconds)', 'Interpreter', 'latex');
legend('$x(t)$', '$y(t)$', 'Interpreter', 'latex');
% Plot step response horizontal reference to horizontal speed
subplot(2, 2, 2); hold on;
plot(t, out_step_x(:, idx.Velocity(1)));
plot(t, out_step_y(:, idx.Velocity(2)));
grid on;
title('Horizontal Velocity', 'Interpreter', 'latex');
ylabel('Velocity (m / s)', 'Interpreter', 'latex');
xlabel('Time (seconds)', 'Interpreter', 'latex');
legend('$\dot{x}(t)$', '$\dot{y}(t)$', 'Interpreter', 'latex');
% Plot step response from vertical reference to vertical position
subplot(2, 2, 3); hold on;
plot(t, out_step_z(:, idx.PlotPosition(3)));
plot(t, out_step_z(:, idx.PlotReference(3)), '--');
grid on;
title('Vertical Position', 'Interpreter', 'latex');
ylabel('Distance (meters)', 'Interpreter', 'latex');
xlabel('Time (seconds)', 'Interpreter', 'latex');
legend('$z(t)$', 'Interpreter', 'latex');
% Plot step response vertical reference to vertical speed
subplot(2, 2, 4); hold on;
plot(t, out_step_z(:, idx.ErrorVelocity(3)));
grid on;
title('Vertical Velocity', 'Interpreter', 'latex');
ylabel('Velocity (m / s)', 'Interpreter', 'latex');
xlabel('Time (seconds)', 'Interpreter', 'latex');
legend('$\dot{z}(t)$', 'Interpreter', 'latex');
end
end
% vim:ts=2 sw=2 et:
|