-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSISO_Example.m
More file actions
217 lines (188 loc) · 6.69 KB
/
Copy pathSISO_Example.m
File metadata and controls
217 lines (188 loc) · 6.69 KB
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
% Simulation of (SISO) relative degree three adaptive output-feedback
% controller based on the paper "Adaptive Control for a Class of
% Multi-Input-Multi-Output Plants with Arbitrary Relative Degree",
% agumented with integral error for command tracking. The goal is to be a
% simplified introductory example to become familiar with the adaptive
% control methodology before using the MIMO SimVFA package.
%
% Ben Thomsen
% January 2017
clear; clc;
adapt = 1; % adaptive control or baseline LQR
uncrt = 1; % uncertainty added or system completely known?
steps = 1; % step commands or sinusoidal commands
% commands for the plant output - either steps or a sine wave
if (steps)
cmd_mag = 1; % command magnitude
step_time = 15; % 15 second steps
sine_freq = 0;
else
% single sine wave input
cmd_mag = 1; % command magnitude
sine_freq = 1; % rad/s
step_time = 1;
end
tfin = 300; % simulation time (s)
dT = 0.005; % sim step size (1/Hz)
% command filter (so derivative is available)
z_cmd = 1; % zeta: damping ratio
om_cmd = 3.5; % omega: natural freq (rad/s)
A_cmd = [0, 1;
-om_cmd^2, -2*om_cmd*z_cmd];
B_cmd = [0; om_cmd^2];
C_cmd = eye(2);
% nominal first-order plant parameters
a_p = 3; % pole location (rad/s)
b_p = 1.5; % high-freq gain
% nominal second-order actuator parameters
om_n = 2.6; % omega: natural freq (rad/s)
zeta = 0.7; % zeta: damping ratio
% uncertainty - see paper for descriptions
if (uncrt)
th_star_p = 0.1; % \theta_{p}^{*}
th_star_zw = 0.1; % \theta_{\zeta \omega}^{*}
th_star_w = 0.1; % \theta_{\omega}^{*}
lam_star_p = 0.6; % \lambda_{p}^{*}
lam_star_a = 0.8; % \lambda_{a}^{*}
else
th_star_p = 0;
th_star_zw = 0;
th_star_w = 0;
lam_star_p = 1;
lam_star_a = 1;
end
lam_star = lam_star_p * lam_star_a; % \lambda^{*}
lam_max = abs(lam_star);
psi_star_1 = [-th_star_p; 0; 0; 0]; % \psi_{1}^{*}
psi_star_3 = [0; -th_star_w; -th_star_zw; 0]; % \psi_{3}^{*}
psi_max = max(norm(psi_star_1), norm(psi_star_3));
% augmented system with actuator model and integral of output error
% first state is plant output, second and third are actuator states,
% fourth is integral of output error
A = [-a_p, b_p, 0, 0;
0, 0, 1, 0;
0, -om_n^2, -2*zeta*om_n, 0
1, 0, 0, 0];
b_1 = [b_p; 0; 0; 0]; % relative degree 1 input path (for TF y(s)/u_p(s))
b_3 = [0; 0; om_n^2; 0]; % relative degree 3 input path (for TF y(s)/u(s))
c = [1; 0; 0; 0]; % output
A_star = A + b_1*psi_star_1' + b_3*psi_star_3'; % true (unknown) dynamics in sim (A^{*})
% filter coefficients and coordinate changes - see paper for details
a20 = 1; % a_{2}^{0}
a21 = 2; % a_{2}^{1}
a22 = 1; % a_{2}^{2}
ba_3 = a22 * b_3; % b_{3}^{a}
ba_2 = a22 * A * b_3 + a21 * b_3; % b_{2}^{a}
ba_1 = a22 * A^2 * b_3 + a21 * A * b_3 + a20 * b_3; % b_{1}^{a}
% closed-loop reference model (CRM)
if (steps)
Q = diag([1; 0.01; 0.01; 0.3]); % LQR weighting matrix
eps = 2; % scalar gain used in calculation of feedback gain l
else
Q = diag([1; 0.01; 0.01; 15]); % LQR weighting matrix
eps = 4; % scalar gain used in calculation of feedback gain l
end
r = 0.1; % LQR control effort weight
k = lqr(A, b_3, Q, r)'; % LQR feedback gain
A_m = A - b_3 * k'; % reference model state matrix
l = eps * ba_1 * b_p * om_n^2; % output-error feedback
b_r = [0; 0; 0; -1]; % "B_z" in MIMO section of paper (for integral error state)
% tuners for adaptive law
mu = 0.2; % scalar gain in g(x, mu) = 1 + (mu * x'x)
A_h = [0, 1;
-a20/a22, -a21/a22];
b_h = [0; a20/a22];
c_h = [1; 0];
c_h1 = [0; 1];
c_h2 = [-a20/a22; -a21/a22];
% learning rates on individual adaptive parameters
if (adapt)
gamma = 4; % to scale all gains simultaneously
g.a = 1 * gamma; % \psi_{3}^{1}
g.b = 1 * gamma; % \psi_{3}^{2}
g.c = 50 * gamma; % \lambda
g.d = 0.5 * gamma; % \undeline{\psi}_{1}
g.e = 0.1 * gamma; % \undeline{\psi}_{2}
g.f = 0.5 * gamma; % \undeline{\psi}_{3}
g.g = 50 * gamma; % \lambda^{-1} (see MIMO section of paper)
else
g.a = 0; % \psi_{3}^{1}
g.b = 0; % \psi_{3}^{2}
g.c = 0; % \lambda
g.d = 0; % \undeline{\psi}_{1}
g.e = 0; % \undeline{\psi}_{2}
g.f = 0; % \undeline{\psi}_{3}
g.g = 0; % \lambda^{-1} (see MIMO section of paper)
end
% run simulation, return data in SimulationOutput object "so"
so = sim('SISO_Example_Sim.slx');
% plot output
if (adapt)
nplots = 4;
else
nplots = 3;
end
figure('Position',[100, 100, 1200, 250*nplots]);
subplot(nplots, 1, 1)
plot(so.tout, squeeze(so.r), 'LineWidth', 1); hold on;
plot(so.tout, squeeze(so.y), 'LineWidth', 1);
plot(so.tout, squeeze(so.y_m), 'LineWidth', 1);
xlim([0 so.tout(end)]);
grid on;
title('Command, CRM Trajectory, Output', 'interpreter', 'latex')
set(gca,'fontsize',18)
subplot(nplots, 1, 2)
plot(so.tout, squeeze(so.u), 'LineWidth', 1);
xlim([0 so.tout(end)]);
grid on;
title('Control Signal $u$', 'interpreter', 'latex')
set(gca,'fontsize',18)
subplot(nplots, 1, 3)
plot(so.tout, squeeze(so.y) - squeeze(so.y_m), 'LineWidth', 1);
xlim([0 so.tout(end)]);
grid on;
title('Tracking Error $y - y_m$', 'interpreter', 'latex')
set(gca,'fontsize',18)
if (adapt)
subplot(nplots, 1, 4)
steps = length(so.tout);
norm_lam = zeros(steps, 1);
norm_psi1 = zeros(steps, 1);
norm_psi2 = zeros(steps, 1);
norm_psi31 = zeros(steps, 1);
norm_psi32 = zeros(steps, 1);
norm_psi3 = zeros(steps, 1);
norm_ilam = zeros(steps, 1);
lam = squeeze(so.lam);
psi1T = squeeze(so.psi1T);
psi2T = squeeze(so.psi2T);
psi3T = squeeze(so.psi3T);
psi31 = squeeze(so.psi31);
psi32 = squeeze(so.psi32);
ilam = squeeze(so.ilam);
for i=1:steps
norm_lam(i) = norm(lam(i));
norm_psi1(i) = norm(psi1T(:,i));
norm_psi2(i) = norm(psi2T(:,i));
norm_psi31(i) = norm(psi31(i));
norm_psi32(i) = norm(psi32(i));
norm_psi3(i) = norm(psi3T(:,i));
norm_ilam(i) = norm(ilam(i));
end
norm_lam = norm_lam/norm_lam(end);
norm_psi1 = norm_psi1/norm_psi1(end);
norm_psi2 = norm_psi2/norm_psi2(end);
norm_psi31 = norm_psi31/norm_psi31(end);
norm_psi32 = norm_psi32/norm_psi32(end);
norm_psi3 = norm_psi3/norm_psi3(end);
norm_ilam = norm_ilam/norm_ilam(end);
norms = [norm_lam, norm_psi1, norm_psi2, ...
norm_psi31, norm_psi32, norm_psi3, norm_ilam];
plot(so.tout, norms, 'LineWidth', 1);
xlim([0 so.tout(end)]);
grid on;
title('Normalized Learned Parameters', 'interpreter', 'latex')
h = legend('$\|\lambda\|$', '$\|\underline{\psi}_1\|$', '$\|\underline{\psi}_2\|$', '$\|\psi_3^1\|$', '$\|\psi_3^2\|$', '$\|\underline{\psi}_3\|$', '$\|\lambda\|^{-1}$');
set(h,'Interpreter','Latex','Location','SouthEast')
set(gca,'fontsize',18)
end