Finish writing. Working Matlab code

This commit is contained in:
2025-04-13 17:14:33 +02:00
parent 2b9340ec64
commit f81e7f013c
18 changed files with 1072 additions and 1162 deletions

View File

@@ -69,108 +69,21 @@ H1 = 1 - H2;
% The function generateCF can also be used to synthesize the complementary filters.
% [H1, H2] = generateCF(W1, W2);
%% Bode plot of the obtained complementary filters
%% Bode plot of the Weighting filters and Obtained complementary filters
figure;
tiledlayout(3, 1, 'TileSpacing', 'Compact', 'Padding', 'None');
% Magnitude
ax1 = nexttile([2, 1]);
hold on;
set(gca,'ColorOrderIndex',1)
plot(freqs, 1./abs(squeeze(freqresp(W1, freqs, 'Hz'))), '--', 'DisplayName', '$|W_1|^{-1}$');
set(gca,'ColorOrderIndex',2)
plot(freqs, 1./abs(squeeze(freqresp(W2, freqs, 'Hz'))), '--', 'DisplayName', '$|W_2|^{-1}$');
set(gca,'ColorOrderIndex',1)
plot(freqs, abs(squeeze(freqresp(H1, freqs, 'Hz'))), '-', 'DisplayName', '$H_1$');
set(gca,'ColorOrderIndex',2)
plot(freqs, abs(squeeze(freqresp(H2, freqs, 'Hz'))), '-', 'DisplayName', '$H_2$');
plot(freqs, 1./abs(squeeze(freqresp(W1, freqs, 'Hz'))), '--', 'color', colors(1,:),'DisplayName', '$|W_1|^{-1}$');
plot(freqs, 1./abs(squeeze(freqresp(W2, freqs, 'Hz'))), '--', 'color', colors(2,:),'DisplayName', '$|W_2|^{-1}$');
plot(freqs, abs(squeeze(freqresp(H1, freqs, 'Hz'))), '-', 'color', [colors(1,:), 0.5], 'linewidth', 2.5,'DisplayName', '$H_1$');
plot(freqs, abs(squeeze(freqresp(H2, freqs, 'Hz'))), '-', 'color', [colors(2,:), 0.5], 'linewidth', 2.5,'DisplayName', '$H_2$');
hold off;
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'log');
set(gca, 'XTickLabel',[]); ylabel('Magnitude');
xlabel('Frequency [Hz]'); ylabel('Magnitude');
ylim([8e-4, 20]);
yticks([1e-3, 1e-2, 1e-1, 1, 1e1]);
yticklabels({'', '$10^{-2}$', '', '$10^0$', ''})
xlim([freqs(1), freqs(end)]);
leg = legend('location', 'south', 'FontSize', 8, 'NumColumns', 2);
leg.ItemTokenSize(1) = 18;
% Phase
ax2 = nexttile;
hold on;
set(gca,'ColorOrderIndex',1)
plot(freqs, 180/pi*phase(squeeze(freqresp(H1, freqs, 'Hz'))), '-');
set(gca,'ColorOrderIndex',2)
plot(freqs, 180/pi*phase(squeeze(freqresp(H2, freqs, 'Hz'))), '-');
hold off;
set(gca, 'XScale', 'log');
xlabel('Frequency [Hz]'); ylabel('Phase [deg]');
yticks([-180:90:180]);
ylim([-180, 200])
yticklabels({'-180', '', '0', '', '180'})
linkaxes([ax1,ax2],'x');
xlim([freqs(1), freqs(end)]);
%% Design of "Closed-loop" complementary filters
% Design of the Weighting Functions
W1 = generateWF('n', 3, 'w0', 2*pi*10, 'G0', 1000, 'Ginf', 1/10, 'Gc', 0.45);
W2 = generateWF('n', 2, 'w0', 2*pi*10, 'G0', 1/10, 'Ginf', 1000, 'Gc', 0.45);
% Generalized plant for "closed-loop" complementary filter synthesis
P = [ W1 0 1;
-W1 W2 -1];
% Standard H-Infinity Synthesis
[L, ~, gamma, ~] = hinfsyn(P, 1, 1,'TOLGAM', 0.001, 'METHOD', 'ric', 'DISPLAY', 'on');
% Complementary filters
H1 = inv(1 + L);
H2 = 1 - H1;
%% Bode plot of the obtained complementary filters after H-infinity mixed-sensitivity synthesis
figure;
tiledlayout(3, 1, 'TileSpacing', 'Compact', 'Padding', 'None');
% Magnitude
ax1 = nexttile([2, 1]);
hold on;
set(gca,'ColorOrderIndex',1)
plot(freqs, 1./abs(squeeze(freqresp(W1, freqs, 'Hz'))), '--', 'DisplayName', '$|W_1|^{-1}$');
set(gca,'ColorOrderIndex',2)
plot(freqs, 1./abs(squeeze(freqresp(W2, freqs, 'Hz'))), '--', 'DisplayName', '$|W_2|^{-1}$');
set(gca,'ColorOrderIndex',1)
plot(freqs, abs(squeeze(freqresp(H1, freqs, 'Hz'))), '-', 'DisplayName', '$H_1$');
set(gca,'ColorOrderIndex',2)
plot(freqs, abs(squeeze(freqresp(H2, freqs, 'Hz'))), '-', 'DisplayName', '$H_2$');
plot(freqs, abs(squeeze(freqresp(L, freqs, 'Hz'))), 'k--', 'DisplayName', '$|L|$');
hold off;
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'log');
set(gca, 'XTickLabel',[]); ylabel('Magnitude');
ylim([1e-3, 1e3]);
yticks([1e-3, 1e-2, 1e-1, 1, 1e1, 1e2, 1e3]);
yticklabels({'', '$10^{-2}$', '', '$10^0$', '', '$10^2$', ''});
leg = legend('location', 'northeast', 'FontSize', 8, 'NumColumns', 3);
leg.ItemTokenSize(1) = 18;
% Phase
ax2 = nexttile;
hold on;
set(gca,'ColorOrderIndex',1)
plot(freqs, 180/pi*phase(squeeze(freqresp(H1, freqs, 'Hz'))), '-');
set(gca,'ColorOrderIndex',2)
plot(freqs, 180/pi*phase(squeeze(freqresp(H2, freqs, 'Hz'))), '-');
hold off;
set(gca, 'XScale', 'log');
xlabel('Frequency [Hz]'); ylabel('Phase [deg]');
yticks([-180:90:180]);
ylim([-180, 200])
yticklabels({'-180', '', '0', '', '180'})
linkaxes([ax1,ax2],'x');
xlim([freqs(1), freqs(end)]);
%% Synthesis of a set of three complementary filters
% Design of the Weighting Functions
W1 = generateWF('n', 2, 'w0', 2*pi*1, 'G0', 1/10, 'Ginf', 1000, 'Gc', 0.5);
@@ -195,45 +108,16 @@ H1 = 1 - H2 - H3;
%% Bode plot of the inverse weighting functions and of the three complementary filters obtained using the H-infinity synthesis
figure;
tiledlayout(3, 1, 'TileSpacing', 'Compact', 'Padding', 'None');
% Magnitude
ax1 = nexttile([2, 1]);
hold on;
set(gca,'ColorOrderIndex',1)
plot(freqs, 1./abs(squeeze(freqresp(W1, freqs, 'Hz'))), '--', 'DisplayName', '$|W_1|^{-1}$');
set(gca,'ColorOrderIndex',2)
plot(freqs, 1./abs(squeeze(freqresp(W2, freqs, 'Hz'))), '--', 'DisplayName', '$|W_2|^{-1}$');
set(gca,'ColorOrderIndex',3)
plot(freqs, 1./abs(squeeze(freqresp(W3, freqs, 'Hz'))), '--', 'DisplayName', '$|W_3|^{-1}$');
set(gca,'ColorOrderIndex',1)
plot(freqs, abs(squeeze(freqresp(H1, freqs, 'Hz'))), '-', 'DisplayName', '$H_1$');
set(gca,'ColorOrderIndex',2)
plot(freqs, abs(squeeze(freqresp(H2, freqs, 'Hz'))), '-', 'DisplayName', '$H_2$');
set(gca,'ColorOrderIndex',3)
plot(freqs, abs(squeeze(freqresp(H3, freqs, 'Hz'))), '-', 'DisplayName', '$H_3$');
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'log');
plot(freqs, 1./abs(squeeze(freqresp(W1, freqs, 'Hz'))), '--', 'color', colors(1,:),'DisplayName', '$|W_1|^{-1}$');
plot(freqs, 1./abs(squeeze(freqresp(W2, freqs, 'Hz'))), '--', 'color', colors(2,:),'DisplayName', '$|W_2|^{-1}$');
plot(freqs, 1./abs(squeeze(freqresp(W3, freqs, 'Hz'))), '--', 'color', colors(3,:),'DisplayName', '$|W_3|^{-1}$');
plot(freqs, abs(squeeze(freqresp(H1, freqs, 'Hz'))), '-', 'color', [colors(1,:), 0.5], 'linewidth', 2.5,'DisplayName', '$H_1$');
plot(freqs, abs(squeeze(freqresp(H2, freqs, 'Hz'))), '-', 'color', [colors(2,:), 0.5], 'linewidth', 2.5,'DisplayName', '$H_2$');
plot(freqs, abs(squeeze(freqresp(H3, freqs, 'Hz'))), '-', 'color', [colors(3,:), 0.5], 'linewidth', 2.5,'DisplayName', '$H_3$');
hold off;
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'log');
ylabel('Magnitude');
set(gca, 'XTickLabel',[]);
ylim([1e-4, 20]);
leg = legend('location', 'northeast', 'FontSize', 8);
xlabel('Frequency [Hz]'); ylabel('Magnitude');
xlim([freqs(1), freqs(end)]); ylim([1e-4, 20]);
leg = legend('location', 'southeast', 'FontSize', 8);
leg.ItemTokenSize(1) = 18;
% Phase
ax2 = nexttile;
hold on;
set(gca,'ColorOrderIndex',1)
plot(freqs, 180/pi*phase(squeeze(freqresp(H1, freqs, 'Hz'))));
set(gca,'ColorOrderIndex',2)
plot(freqs, 180/pi*phase(squeeze(freqresp(H2, freqs, 'Hz'))));
set(gca,'ColorOrderIndex',3)
plot(freqs, 180/pi*phase(squeeze(freqresp(H3, freqs, 'Hz'))));
hold off;
xlabel('Frequency [Hz]'); ylabel('Phase [deg]');
set(gca, 'XScale', 'log');
yticks([-180:90:180]); ylim([-220, 220]);
linkaxes([ax1,ax2],'x');
xlim([freqs(1), freqs(end)]);

View File

@@ -0,0 +1,248 @@
%% Clear Workspace and Close figures
clear; close all; clc;
%% Intialize Laplace variable
s = zpk('s');
%% Path for functions, data and scripts
addpath('./src/'); % Path for functions
%% Colors for the figures
colors = colororder;
%% Initialize Frequency Vector
freqs = logspace(0, 3, 1000);
%% Compute Equation of motion
l = 1; h=2;
la = 0.5; % Horizontal position of actuators [m]
ha = 0.2; % Vertical of actuators [m]
m = 40; % Payload mass [kg]
I = 5; % Payload rotational inertia [kg m^2]
c = 2e2; % Actuator Damping [N/(m/s)]
k = 1e6; % Actuator Stiffness [N/m]
% Unit vectors of the actuators
s1 = [1;0];
s2 = [0;1];
s3 = [0;1];
% Stiffnesss and Damping matrices of the struts
Kr = diag([k,k,k]);
Cr = diag([c,c,c]);
% Location of the joints with respect to the center of mass
Mb1 = [-l/2;-ha];
Mb2 = [-la; -h/2];
Mb3 = [ la; -h/2];
% Jacobian matrix (Center of Mass)
J_CoM = [s1', Mb1(1)*s1(2)-Mb1(2)*s1(1);
s2', Mb2(1)*s2(2)-Mb2(2)*s2(1);
s3', Mb3(1)*s3(2)-Mb3(2)*s3(1)];
% Mass Matrix in frame {M}
M = diag([m,m,I]);
% Stiffness Matrix in frame {M}
K = J_CoM'*Kr*J_CoM;
% Damping Matrix in frame {M}
C = J_CoM'*Cr*J_CoM;
% Plant in the frame of the struts
G_L = J_CoM*inv(M*s^2 + C*s + K)*J_CoM';
figure;
tiledlayout(3, 3, 'TileSpacing', 'Compact', 'Padding', 'None');
for out_i = 1:3
for in_i = 1:3
nexttile;
plot(freqs, abs(squeeze(freqresp(G_L(out_i,in_i), freqs, 'Hz'))), 'k-', ...
'DisplayName', sprintf('$\\mathcal{L}_%i/\\tau_%i$', out_i, in_i));
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'log');
xlim([freqs(1), freqs(end)]); ylim([2e-8, 4e-5]);
xticks([1e0, 1e1, 1e2])
yticks([1e-7, 1e-6, 1e-5])
leg = legend('location', 'northeast', 'FontSize', 8);
leg.ItemTokenSize(1) = 18;
if in_i == 1
ylabel('Mag. [m/N]')
else
set(gca, 'YTickLabel',[]);
end
if out_i == 3
xlabel('Frequency [Hz]')
else
set(gca, 'XTickLabel',[]);
end
end
end
%% Jacobian Decoupling - Center of Mass
G_CoM = pinv(J_CoM)*G_L*pinv(J_CoM');
G_CoM.InputName = {'Fx', 'Fy', 'Mz'};
G_CoM.OutputName = {'Dx', 'Dy', 'Rz'};
figure;
hold on;
plot(freqs, abs(squeeze(freqresp(G_CoM(1, 3), freqs, 'Hz'))), 'color', [0,0,0,0.2], ...
'DisplayName', '$D_{x,\{M\}}/M_{z,\{M\}}$');
plot(freqs, abs(squeeze(freqresp(G_CoM(3, 1), freqs, 'Hz'))), 'color', [0,0,0,0.2], ...
'DisplayName', '$R_{z,\{M\}}/F_{x,\{M\}}$');
plot(freqs, abs(squeeze(freqresp(G_CoM(1, 1), freqs, 'Hz'))), 'color', colors(1,:), 'DisplayName', '$D_{x,\{M\}}/F_{x,\{M\}}$');
plot(freqs, abs(squeeze(freqresp(G_CoM(2, 2), freqs, 'Hz'))), 'color', colors(2,:), 'DisplayName', '$D_{y,\{M\}}/F_{y,\{M\}}$');
plot(freqs, abs(squeeze(freqresp(G_CoM(3, 3), freqs, 'Hz'))), 'color', colors(3,:), 'DisplayName', '$R_{z,\{M\}}/M_{z,\{M\}}$');
hold off;
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'log');
xlabel('Frequency [Hz]'); ylabel('Magnitude');
ylim([1e-10, 1e-3]);
leg = legend('location', 'southwest', 'FontSize', 8);
leg.ItemTokenSize(1) = 18;
%% Jacobian Decoupling - Center of Mass
% Location of the joints with respect to the center of stiffness
Mb1 = [-l/2; 0];
Mb2 = [-la; -h/2+ha];
Mb3 = [ la; -h/2+ha];
% Jacobian matrix (Center of Stiffness)
J_CoK = [s1', Mb1(1)*s1(2)-Mb1(2)*s1(1);
s2', Mb2(1)*s2(2)-Mb2(2)*s2(1);
s3', Mb3(1)*s3(2)-Mb3(2)*s3(1)];
G_CoK = pinv(J_CoK)*G_L*pinv(J_CoK');
G_CoK.InputName = {'Fx', 'Fy', 'Mz'};
G_CoK.OutputName = {'Dx', 'Dy', 'Rz'};
figure;
hold on;
plot(freqs, abs(squeeze(freqresp(G_CoK(1, 1), freqs, 'Hz'))), 'color', colors(1,:), 'DisplayName', '$D_{x,\{K\}}/F_{x,\{K\}}$');
plot(freqs, abs(squeeze(freqresp(G_CoK(2, 2), freqs, 'Hz'))), 'color', colors(2,:), 'DisplayName', '$D_{y,\{K\}}/F_{y,\{K\}}$');
plot(freqs, abs(squeeze(freqresp(G_CoK(3, 3), freqs, 'Hz'))), 'color', colors(3,:), 'DisplayName', '$R_{z,\{K\}}/M_{z,\{K\}}$');
plot(freqs, abs(squeeze(freqresp(G_CoK(1, 3), freqs, 'Hz'))), 'color', [0,0,0,0.2], ...
'DisplayName', '$D_{x,\{K\}}/M_{z,\{K\}}$');
plot(freqs, abs(squeeze(freqresp(G_CoK(3, 1), freqs, 'Hz'))), 'color', [0,0,0,0.2], ...
'DisplayName', '$R_{z,\{K\}}/F_{x,\{K\}}$');
hold off;
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'log');
xlabel('Frequency [Hz]'); ylabel('Kagnitude');
ylim([1e-10, 1e-3]);
leg = legend('location', 'southeast', 'FontSize', 8, 'NumColumns', 2);
leg.ItemTokenSize(1) = 18;
%% Modal decoupling
% Compute the eigen vectors
[phi, wi] = eig(M\K);
% Sort the eigen vectors by increasing associated frequency
[~, i] = sort(diag(wi));
phi = phi(:, i);
% Plant in the modal space
Gm = inv(phi)*inv(J_CoM)*G_L*inv(J_CoM')*inv(phi');
%% Modal decoupled plant
figure;
hold on;
plot(freqs, abs(squeeze(freqresp(Gm(1,1), freqs, 'Hz'))), 'color', colors(1,:), 'DisplayName', '$\mathcal{X}_{m,1}/\tau_{m,1}$');
plot(freqs, abs(squeeze(freqresp(Gm(2,2), freqs, 'Hz'))), 'color', colors(2,:), 'DisplayName', '$\mathcal{X}_{m,2}/\tau_{m,2}$');
plot(freqs, abs(squeeze(freqresp(Gm(3,3), freqs, 'Hz'))), 'color', colors(3,:), 'DisplayName', '$\mathcal{X}_{m,3}/\tau_{m,3}$');
hold off;
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'log');
xlabel('Frequency [Hz]'); ylabel('Magnitude');
ylim([1e-8, 1e-4]);
leg = legend('location', 'northeast', 'FontSize', 8);
leg.ItemTokenSize(1) = 18;
%% SVD Decoupling
wc = 2*pi*100; % Decoupling frequency [rad/s]
% System's response at the decoupling frequency
H1 = evalfr(G_L, j*wc);
% Real approximation of G(j.wc)
D = pinv(real(H1'*H1));
H1 = pinv(D*real(H1'*diag(exp(j*angle(diag(H1*D*H1.'))/2))));
[U,S,V] = svd(H1);
Gsvd = inv(U)*G_L*inv(V');
figure;
hold on;
for i_in = 1:3
for i_out = [i_in+1:3]
plot(freqs, abs(squeeze(freqresp(Gsvd(i_out, i_in), freqs, 'Hz'))), 'color', [0,0,0,0.2], ...
'HandleVisibility', 'off');
end
end
plot(freqs, abs(squeeze(freqresp(Gsvd(1, 2), freqs, 'Hz'))), 'color', [0,0,0,0.2], ...
'DisplayName', '$G_{SVD}(i,j)\ i \neq j$');
set(gca,'ColorOrderIndex',1)
for i_in_out = 1:3
plot(freqs, abs(squeeze(freqresp(Gsvd(i_in_out, i_in_out), freqs, 'Hz'))), 'DisplayName', sprintf('$G_{SVD}(%d,%d)$', i_in_out, i_in_out));
end
hold off;
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'log');
xlabel('Frequency [Hz]'); ylabel('Magnitude');
ylim([1e-10, 2e-4]);
leg = legend('location', 'northeast', 'FontSize', 8);
leg.ItemTokenSize(1) = 18;
%% Simscape model with relative motion sensor at alternative positions
mdl = 'detail_control_decoupling_test_model';
open(mdl)
deq = 0.2; % Length of the actuators [m]
% Input/Output definition
clear io; io_i = 1;
io(io_i) = linio([mdl, '/F1'], 1, 'openinput'); io_i = io_i + 1;
io(io_i) = linio([mdl, '/F2'], 1, 'openinput'); io_i = io_i + 1;
io(io_i) = linio([mdl, '/F3'], 1, 'openinput'); io_i = io_i + 1;
io(io_i) = linio([mdl, '/Payload'], 1, 'openoutput'); io_i = io_i + 1;
io(io_i) = linio([mdl, '/Payload'], 2, 'openoutput'); io_i = io_i + 1;
io(io_i) = linio([mdl, '/Payload'], 3, 'openoutput'); io_i = io_i + 1;
G_L_alt = linearize(mdl, io);
G_L_alt.InputName = {'F1', 'F2', 'F3'};
G_L_alt.OutputName = {'d1', 'd2', 'd32'};
% SVD Decoupling with the new plant
wc = 2*pi*100; % Decoupling frequency [rad/s]
% System's response at the decoupling frequency
H1 = evalfr(G_L_alt, j*wc);
% Real approximation of G(j.wc)
D = pinv(real(H1'*H1));
H1 = pinv(D*real(H1'*diag(exp(j*angle(diag(H1*D*H1.'))/2))));
[U,S,V] = svd(H1);
Gsvd_alt = inv(U)*G_L_alt*inv(V');
%% Obtained plant after SVD decoupling - Relative motion sensors are not collocated with the actuators
figure;
hold on;
for i_in = 1:3
for i_out = [i_in+1:3]
plot(freqs, abs(squeeze(freqresp(Gsvd_alt(i_out, i_in), freqs, 'Hz'))), 'color', [0,0,0,0.2], ...
'HandleVisibility', 'off');
end
end
plot(freqs, abs(squeeze(freqresp(Gsvd_alt(1, 2), freqs, 'Hz'))), 'color', [0,0,0,0.2], ...
'DisplayName', '$G_{SVD}(i,j)\ i \neq j$');
set(gca,'ColorOrderIndex',1)
for i_in_out = 1:3
plot(freqs, abs(squeeze(freqresp(Gsvd_alt(i_in_out, i_in_out), freqs, 'Hz'))), 'DisplayName', sprintf('$G_{SVD}(%d,%d)$', i_in_out, i_in_out));
end
hold off;
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'log');
xlabel('Frequency [Hz]'); ylabel('Magnitude');
ylim([5e-11, 7e-5]);
leg = legend('location', 'southwest', 'FontSize', 8);
leg.ItemTokenSize(1) = 18;

View File

@@ -0,0 +1,238 @@
%% Clear Workspace and Close figures
clear; close all; clc;
%% Intialize Laplace variable
s = zpk('s');
%% Path for functions, data and scripts
addpath('./src/'); % Path for functions
%% Colors for the figures
colors = colororder;
%% Initialize Frequency Vector
freqs = logspace(-1, 3, 1000);
%% Analytical Complementary Filters - Effect of alpha
freqs_study = logspace(-2, 2, 1000);
alphas = [0.1, 1, 10];
w0 = 2*pi*1;
s = tf('s');
figure;
hold on;
for i = 1:length(alphas)
alpha = alphas(i);
Hh2 = (s/w0)^2*((s/w0)+1+alpha)/(((s/w0)+1)*((s/w0)^2 + alpha*(s/w0) + 1));
Hl2 = ((1+alpha)*(s/w0)+1)/(((s/w0)+1)*((s/w0)^2 + alpha*(s/w0) + 1));
plot(freqs_study, abs(squeeze(freqresp(Hh2, freqs_study, 'Hz'))), 'color', colors(i,:), 'DisplayName', sprintf('$\\alpha = %g$', alphas(i)));
plot(freqs_study, abs(squeeze(freqresp(Hl2, freqs_study, 'Hz'))), 'color', colors(i,:), 'HandleVisibility', 'off');
end
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'log');
xlabel('Relative Frequency $\frac{\omega}{\omega_0}$'); ylabel('Magnitude');
hold off;
ylim([1e-3, 20]);
leg = legend('location', 'northeast', 'FontSize', 8);
leg.ItemTokenSize(1) = 18;
%% Analytical Complementary Filters - Effect of w0
freqs_study = logspace(-1, 3, 1000);
alpha = [1];
w0s = [2*pi*1, 2*pi*10, 2*pi*100];
s = tf('s');
figure;
hold on;
for i = 1:length(w0s)
w0 =w0s(i);
Hh2 = (s/w0)^2*((s/w0)+1+alpha)/(((s/w0)+1)*((s/w0)^2 + alpha*(s/w0) + 1));
Hl2 = ((1+alpha)*(s/w0)+1)/(((s/w0)+1)*((s/w0)^2 + alpha*(s/w0) + 1));
plot(freqs_study, abs(squeeze(freqresp(Hh2, freqs_study, 'Hz'))), 'color', colors(i,:), 'DisplayName', sprintf('$\\omega_0 = %g$ Hz', w0/2/pi));
plot(freqs_study, abs(squeeze(freqresp(Hl2, freqs_study, 'Hz'))), 'color', colors(i,:), 'HandleVisibility', 'off');
end
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'log');
xlabel('Frequency [Hz]'); ylabel('Magnitude');
hold off;
xlim([freqs_study(1), freqs_study(end)]); ylim([1e-3, 20]);
leg = legend('location', 'southeast', 'FontSize', 8);
leg.ItemTokenSize(1) = 18;
%% Test model
freqs = logspace(0, 3, 1000); % Frequency Vector [Hz]
m = 20; % mass [kg]
k = 1e6; % stiffness [N/m]
c = 1e2; % damping [N/(m/s)]
% Plant dynamics
G = 1/(m*s^2 + c*s + k);
% Uncertainty weight
wI = generateWF('n', 2, 'w0', 2*pi*50, 'G0', 0.1, 'Ginf', 10, 'Gc', 1);
%% Bode plot of the plant with dynamical uncertainty
figure;
tiledlayout(3, 1, 'TileSpacing', 'Compact', 'Padding', 'None');
% Magnitude
ax1 = nexttile([2,1]);
hold on;
plot(freqs, abs(squeeze(freqresp(G, freqs, 'Hz'))), 'k-', 'DisplayName', 'G');
plotMagUncertainty(wI, freqs, 'G', G, 'DisplayName', '$\Pi_i$');
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'log');
ylabel('Magnitude [m/N]'); set(gca, 'XTickLabel',[]);
ylim([1e-8, 7e-5]);
hold off;
leg = legend('location', 'northeast', 'FontSize', 8);
leg.ItemTokenSize(1) = 18;
% Phase
ax2 = nexttile;
hold on;
plotPhaseUncertainty(wI, freqs, 'G', G);
plot(freqs, 180/pi*unwrap(angle(squeeze(freqresp(G, freqs, 'Hz')))), 'k-');
set(gca,'xscale','log');
yticks(-360:90:90);
ylim([-270 45]);
xlabel('Frequency [Hz]'); ylabel('Phase [deg]');
hold off;
linkaxes([ax1,ax2],'x');
xlim([freqs(1), freqs(end)]);
%% Analytical Complementary Filters
w0 = 2*pi*20;
alpha = 1;
Hh = (s/w0)^2*((s/w0)+1+alpha)/(((s/w0)+1)*((s/w0)^2 + alpha*(s/w0) + 1));
Hl = ((1+alpha)*(s/w0)+1)/(((s/w0)+1)*((s/w0)^2 + alpha*(s/w0) + 1));
%% Specifications
figure;
hold on;
plot([1, 100], [0.01, 100], ':', 'color', colors(2,:));
plot([300, 1000], [0.01, 0.01], ':', 'color', colors(1,:));
plot(freqs, 1./abs(squeeze(freqresp(wI, freqs, 'Hz'))), ':', 'color', colors(1,:));
plot(freqs, abs(squeeze(freqresp(Hl, freqs, 'Hz'))), '-', 'color', colors(1,:));
plot(freqs, abs(squeeze(freqresp(Hh, freqs, 'Hz'))), '-', 'color', colors(2,:));
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'log');
xlabel('Frequency [Hz]'); ylabel('Magnitude');
hold off;
xlim([freqs(1), freqs(end)]);
ylim([1e-3, 10]);
xticks([0.1, 1, 10, 100, 1000]);
%% Obtained controller
omega = 2*pi*1000;
K = 1/(Hh*G) * 1/((1+s/omega+(s/omega)^2));
K = zpk(minreal(K));
%% Bode plot of the controller K
figure;
tiledlayout(3, 1, 'TileSpacing', 'Compact', 'Padding', 'None');
% Magnitude
ax1 = nexttile([2, 1]);
plot(freqs, abs(squeeze(freqresp(K*Hl, freqs, 'Hz'))), 'k-');
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'log');
ylabel('Magnitude'); set(gca, 'XTickLabel',[]);
ylim([8e3, 1e8])
% Phase
ax2 = nexttile;
plot(freqs, 180/pi*angle(squeeze(freqresp(K*Hl, freqs, 'Hz'))), 'k-');
set(gca,'xscale','log');
yticks(-180:45:180);
ylim([-180 45]);
xlabel('Frequency [Hz]'); ylabel('Phase [deg]');
linkaxes([ax1,ax2],'x');
xlim([freqs(1), freqs(end)]);
num_delta_points = 50;
theta = linspace(0, 2*pi, num_delta_points);
delta_points = exp(1j * theta);
% Get frequency responses for all components
G_resp = squeeze(freqresp(G, freqs, 'Hz'));
K_resp = squeeze(freqresp(K, freqs, 'Hz'));
Hl_resp = squeeze(freqresp(Hl, freqs, 'Hz'));
wI_resp = squeeze(freqresp(wI, freqs, 'Hz'));
% Calculate nominal responses
nom_L = G_resp .* K_resp .* Hl_resp;
nom_S = 1 ./ (1 + nom_L);
nom_T = nom_L ./ (1 + nom_L);
% Store all the points in the complex plane that L can take
loop_region_points = zeros(length(freqs), num_delta_points);
% Initialize arrays to store magnitude bounds
S_mag_min = ones(length(freqs), 1) * inf;
S_mag_max = zeros(length(freqs), 1);
T_mag_min = ones(length(freqs), 1) * inf;
T_mag_max = zeros(length(freqs), 1);
% Calculate magnitude bounds for all delta values
for i = 1:num_delta_points
% Perturbed loop gain
loop_perturbed = nom_L .* (1 + wI_resp .* delta_points(i));
loop_region_points(:,i) = loop_perturbed;
% Perturbed sensitivity function
S_perturbed = 1 ./ (1 + loop_perturbed);
S_mag = abs(S_perturbed);
% Update S magnitude bounds
S_mag_min = min(S_mag_min, S_mag);
S_mag_max = max(S_mag_max, S_mag);
% Perturbed complementary sensitivity function
T_perturbed = loop_perturbed ./ (1 + loop_perturbed);
T_mag = abs(T_perturbed);
% Update T magnitude bounds
T_mag_min = min(T_mag_min, T_mag);
T_mag_max = max(T_mag_max, T_mag);
end
% At frequencies where |wI| > 1, T min is zero
T_mag_min(abs(wI_resp)>1) = 1e-10;
%% Nyquist plot to check Robust Stability
figure;
hold on;
plot(real(squeeze(freqresp(G*K*Hl, freqs, 'Hz'))), imag(squeeze(freqresp(G*K*Hl, freqs, 'Hz'))), 'k', 'DisplayName', '$L(j\omega)$ - Nominal');
plot(alphaShape(real(loop_region_points(:)), imag(loop_region_points(:)), 0.1), 'FaceColor', [0, 0, 0], 'EdgeColor', 'none', 'FaceAlpha', 0.3, 'DisplayName', '$L(j\omega)$ - $\forall G \in \Pi_i$');
plot(-1, 0, 'k+', 'MarkerSize', 5, 'HandleVisibility', 'off');
hold off;
grid on;
axis equal
xlim([-1.4, 0.2]); ylim([-1.2, 0.4]);
xticks(-1.4:0.2:0.2); yticks(-1.2:0.2:0.4);
xlabel('Real Part'); ylabel('Imaginary Part');
leg = legend('location', 'southeast', 'FontSize', 8, 'NumColumns', 1);
leg.ItemTokenSize(1) = 18;
%% Robust Performance
figure;
hold on;
plot(freqs, abs(nom_S), 'color', colors(2,:), 'DisplayName', '$|S|$ - Nom.');
plot(freqs, abs(nom_T), 'color', colors(1,:), 'DisplayName', '$|T|$ - Nom.');
patch([freqs, fliplr(freqs)], [S_mag_max', fliplr(S_mag_min')], colors(2,:), 'FaceAlpha', 0.2, 'EdgeColor', 'none', 'HandleVisibility', 'off');
patch([freqs, fliplr(freqs)], [T_mag_max', fliplr(T_mag_min')], colors(1,:), 'FaceAlpha', 0.2, 'EdgeColor', 'none', 'HandleVisibility', 'off');
plot([1, 100], [0.01, 100], ':', 'color', colors(2,:), 'DisplayName', 'Specs.');
plot([300, 1000], [0.01, 0.01], ':', 'color', colors(1,:), 'DisplayName', 'Specs.');
plot(freqs, 1./abs(squeeze(freqresp(wI, freqs, 'Hz'))), ':', 'color', colors(1,:), 'HandleVisibility', 'off');
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'log');
hold off;
xlabel('Frequency [Hz]'); ylabel('Magnitude');
xlim([freqs(1), freqs(end)]);
ylim([1e-4, 5]);
xticks([0.1, 1, 10, 100, 1000]);
leg = legend('location', 'southwest', 'FontSize', 8, 'NumColumns', 3);
leg.ItemTokenSize(1) = 18;