This repository has been archived on 2025-04-18. You can view files and clone it, but cannot push or open issues or pull requests.
phd-test-bench-struts/matlab/test_struts_3_simscape_model.m

476 lines
17 KiB
Matlab

%% Clear Workspace and Close figures
clear; close all; clc;
%% Intialize Laplace variable
s = zpk('s');
%% Path for functions, data and scripts
addpath('./mat/'); % Path for data
addpath('./src/'); % Path for functions
addpath('./STEPS/'); % Path for Simscape Model
%% Linearization options
opts = linearizeOptions;
opts.SampleTime = 0;
%% Open Simscape Model
mdl = 'test_struts_simscape'; % Name of the Simulink File
open(mdl); % Open Simscape Model
%% Colors for the figures
colors = colororder;
%% Input/Output definition of the Model
clear io; io_i = 1;
io(io_i) = linio([mdl, '/u'], 1, 'openinput'); io_i = io_i + 1; % DAC Voltage
io(io_i) = linio([mdl, '/Vs'], 1, 'openoutput'); io_i = io_i + 1; % Sensor Voltage
io(io_i) = linio([mdl, '/de'], 1, 'openoutput'); io_i = io_i + 1; % Encoder
io(io_i) = linio([mdl, '/da'], 1, 'openoutput'); io_i = io_i + 1; % Interferometer
%% Frequency vector [Hz]
freqs = logspace(1, log10(2000), 1000);
%% Load measured FRF for comparison
load('meas_struts_frf.mat', 'f', 'enc_frf', 'int_frf', 'iff_frf', 'strut_nums');
%% Initialize strut with 2DoF model for the APA300ML and identify the dynamics
n_hexapod = struct();
n_hexapod.flex_bot = initializeBotFlexibleJoint('type', '4dof');
n_hexapod.flex_top = initializeTopFlexibleJoint('type', '4dof');
n_hexapod.actuator = initializeAPA('type', '2dof');
c_granite = 0; % Do not take into account damping added by the air bearing
% Run the linearization
Gs_2dof = exp(-s*1e-4)*linearize(mdl, io, 0.0, opts);
Gs_2dof.InputName = {'u'};
Gs_2dof.OutputName = {'Vs', 'de', 'da'};
%% Initialize strut with "flexible" model for the APA300ML and identify the dynamics
n_hexapod = struct();
n_hexapod.flex_bot = initializeBotFlexibleJoint('type', '4dof');
n_hexapod.flex_top = initializeTopFlexibleJoint('type', '4dof');
n_hexapod.actuator = initializeAPA('type', 'flexible');
c_granite = 100; % Do not take into account damping added by the air bearing
% Run the linearization
Gs_flex = exp(-s*1e-4)*linearize(mdl, io, 0.0, opts);
Gs_flex.InputName = {'u'};
Gs_flex.OutputName = {'Vs', 'de', 'da'};
%% Compare the FRF and identified dynamics from u to Vs and da
figure;
tiledlayout(3, 1, 'TileSpacing', 'Compact', 'Padding', 'None');
ax1a = nexttile([2,1]);
hold on;
plot(f, abs(int_frf(:, 1)), 'color', [0,0,0,0.2], ...
'DisplayName', 'FRF');
for i = 2:length(strut_nums)
plot(f, abs(int_frf(:, i)), 'color', [0,0,0,0.2], ...
'HandleVisibility', 'off');
end
plot(freqs, abs(squeeze(freqresp(Gs_2dof('da', 'u'), freqs, 'Hz'))), '-', ...
'color', colors(1,:), 'DisplayName', '2DoF Model')
plot(freqs, abs(squeeze(freqresp(Gs_flex('da', 'u'), freqs, 'Hz'))), '-', ...
'color', colors(2,:), 'DisplayName', 'Flex. Model')
hold off;
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'log');
ylabel('Amplitude $d_a/u$ [m/V]'); set(gca, 'XTickLabel',[]);
hold off;
ylim([1e-8, 1e-3]);
leg = legend('location', 'southwest', 'FontSize', 8, 'NumColumns', 1);
leg.ItemTokenSize(1) = 15;
ax2a = nexttile;
hold on;
for i = 1:length(strut_nums)
plot(f, 180/pi*angle(int_frf(:, i)), 'color', [0,0,0,0.2]);
end
plot(freqs, 180/pi*angle(squeeze(freqresp(Gs_2dof('da', 'u'), freqs, 'Hz'))), '-', 'color', colors(1,:))
plot(freqs, 180/pi*angle(squeeze(freqresp(Gs_flex('da', 'u'), freqs, 'Hz'))), '-', 'color', colors(2,:))
hold off;
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'lin');
xlabel('Frequency [Hz]'); ylabel('Phase [deg]');
hold off;
yticks(-360:90:360); ylim([-180, 180]);
linkaxes([ax1a,ax2a],'x');
xlim([10, 2e3]);
xticks([1e1, 1e2, 1e3]);
%% Compare the FRF and identified dynamics from u to Vs and da
figure;
tiledlayout(3, 1, 'TileSpacing', 'Compact', 'Padding', 'None');
ax1a = nexttile([2,1]);
hold on;
plot(f, abs(enc_frf(:, 1)), 'color', [0,0,0,0.2], ...
'DisplayName', 'FRF');
for i = 2:length(strut_nums)
plot(f, abs(enc_frf(:, i)), 'color', [0,0,0,0.2], ...
'HandleVisibility', 'off');
end
plot(freqs, abs(squeeze(freqresp(Gs_2dof('de', 'u'), freqs, 'Hz'))), '-', ...
'color', colors(1,:), 'DisplayName', '2DoF Model')
plot(freqs, abs(squeeze(freqresp(Gs_flex('de', 'u'), freqs, 'Hz'))), '-', ...
'color', colors(2,:), 'DisplayName', 'Flex. Model')
hold off;
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'log');
ylabel('Amplitude $d_e/u$ [m/V]'); set(gca, 'XTickLabel',[]);
hold off;
ylim([1e-8, 1e-3]);
leg = legend('location', 'southwest', 'FontSize', 8, 'NumColumns', 1);
leg.ItemTokenSize(1) = 15;
ax2a = nexttile;
hold on;
for i = 1:length(strut_nums)
plot(f, 180/pi*angle(enc_frf(:, i)), 'color', [0,0,0,0.2]);
end
plot(freqs, 180/pi*angle(squeeze(freqresp(Gs_2dof('de', 'u'), freqs, 'Hz'))), '-', 'color', colors(1,:))
plot(freqs, 180/pi*angle(squeeze(freqresp(Gs_flex('de', 'u'), freqs, 'Hz'))), '-', 'color', colors(2,:))
hold off;
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'lin');
xlabel('Frequency [Hz]'); ylabel('Phase [deg]');
hold off;
yticks(-360:90:360); ylim([-180, 180]);
linkaxes([ax1a,ax2a],'x');
xlim([10, 2e3]);
xticks([1e1, 1e2, 1e3]);
%% Compare the FRF and identified dynamics from u to Vs and da
figure;
tiledlayout(3, 1, 'TileSpacing', 'Compact', 'Padding', 'None');
ax1a = nexttile([2,1]);
hold on;
plot(f, abs(iff_frf(:, i)), 'color', [0,0,0,0.2], ...
'DisplayName', 'FRF');
for i = 1:length(strut_nums)
plot(f, abs(iff_frf(:, i)), 'color', [0,0,0,0.2], ...
'HandleVisibility', 'off');
end
plot(freqs, abs(squeeze(freqresp(Gs_2dof('Vs', 'u'), freqs, 'Hz'))), '-', ...
'color', colors(1,:), 'DisplayName', '2DoF Model')
plot(freqs, abs(squeeze(freqresp(Gs_flex('Vs', 'u'), freqs, 'Hz'))), '-', ...
'color', colors(2,:), 'DisplayName', 'Flex. Model')
hold off;
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'log');
ylabel('Amplitude $V_s/u$ [V/V]'); set(gca, 'XTickLabel',[]);
hold off;
ylim([1e-2, 1e2]);
leg = legend('location', 'southeast', 'FontSize', 8, 'NumColumns', 1);
leg.ItemTokenSize(1) = 15;
ax2a = nexttile;
hold on;
for i = 1:length(strut_nums)
plot(f, 180/pi*angle(iff_frf(:, i)), 'color', [0,0,0,0.2]);
end
set(gca,'ColorOrderIndex',1);
plot(freqs, 180/pi*angle(squeeze(freqresp(Gs_2dof('Vs', 'u'), freqs, 'Hz'))), '-', 'color', colors(1,:))
plot(freqs, 180/pi*angle(squeeze(freqresp(Gs_flex('Vs', 'u'), freqs, 'Hz'))), '-', 'color', colors(2,:))
hold off;
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'lin');
xlabel('Frequency [Hz]'); ylabel('Phase [deg]');
hold off;
yticks(-360:90:360); ylim([-180, 180]);
linkaxes([ax1a,ax2a],'x');
xlim([10, 2e3]);
xticks([1e1, 1e2, 1e3]);
%% Effect of a misalignment in Y-Direction
% Considered misalignment in the Y direction
dy_aligns = [-0.5, -0.1, 0.1, 0.5]*1e-3; % [m]
% Transfer functions from u to de for all the misalignment in y direction
Gs_dy_align = {zeros(length(dy_aligns), 1)};
for i = 1:length(dy_aligns)
n_hexapod.actuator = initializeAPA('type', 'flexible', 'd_align_bot', [0; dy_aligns(i); 0], 'd_align_top', [0; dy_aligns(i); 0]);
G = exp(-s*1e-4)*linearize(mdl, io, 0.0, opts);
G.InputName = {'u'};
G.OutputName = {'Vs', 'de', 'da'};
Gs_dy_align(i) = {G};
end
%% Effect of a misalignment in X-Direction
% Considered misalignment in the X direction
dx_aligns = [-0.1, -0.05, 0.05, 0.1]*1e-3; % [m]
% Transfer functions from u to de for all the misalignment in x direction
Gs_dx_align = {zeros(length(dx_aligns), 1)};
for i = 1:length(dx_aligns)
n_hexapod.actuator = initializeAPA('type', 'flexible', 'd_align_bot', [dx_aligns(i); 0; 0], 'd_align_top', [dx_aligns(i); 0; 0]);
G = exp(-s*1e-4)*linearize(mdl, io, 0.0, opts);
G.InputName = {'u'};
G.OutputName = {'Vs', 'de', 'da'};
Gs_dx_align(i) = {G};
end
%% Transfer function from Vs to de - effect of x-misalignment
figure;
tiledlayout(3, 1, 'TileSpacing', 'Compact', 'Padding', 'None');
ax1 = nexttile([2,1]);
hold on;
for i = 1:length(dy_aligns)
plot(freqs, abs(squeeze(freqresp(Gs_dy_align{i}('de', 'u'), freqs, 'Hz'))), ...
'DisplayName', sprintf('$d_y = %.1f$ [mm]', 1e3*dy_aligns(i)));
end
plot(freqs, abs(squeeze(freqresp(Gs_flex('de', 'u'), freqs, 'Hz'))), 'k-', ...
'DisplayName', 'aligned');
hold off;
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'log');
ylabel('Amplitude $d_e/u$ [m/V]'); set(gca, 'XTickLabel',[]);
hold off;
ylim([1e-8, 1e-3]);
leg = legend('location', 'southwest', 'FontSize', 8, 'NumColumns', 1);
leg.ItemTokenSize(1) = 15;
ax2 = nexttile;
hold on;
for i = 1:length(dy_aligns)
plot(freqs, 180/pi*angle(squeeze(freqresp(Gs_dy_align{i}('de', 'u'), freqs, 'Hz'))));
end
plot(freqs, 180/pi*angle(squeeze(freqresp(Gs_flex('de', 'u'), freqs, 'Hz'))), 'k-');
hold off;
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'lin');
xlabel('Frequency [Hz]'); ylabel('Phase [deg]');
hold off;
yticks(-360:90:360); ylim([-180, 180]);
linkaxes([ax1,ax2],'x');
xlim([10, 2e3]);
xticks([1e1, 1e2, 1e3]);
%% Transfer function from Vs to de - effect of x-misalignment
figure;
tiledlayout(3, 1, 'TileSpacing', 'Compact', 'Padding', 'None');
ax1 = nexttile([2,1]);
hold on;
for i = 1:length(dx_aligns)
plot(freqs, abs(squeeze(freqresp(Gs_dx_align{i}('de', 'u'), freqs, 'Hz'))), ...
'DisplayName', sprintf('$d_x = %.1f$ [mm]', 1e3*dx_aligns(i)));
end
plot(freqs, abs(squeeze(freqresp(Gs_flex('de', 'u'), freqs, 'Hz'))), 'k-', ...
'DisplayName', 'aligned');
hold off;
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'log');
ylabel('Amplitude $d_e/u$ [m/V]'); set(gca, 'XTickLabel',[]);
hold off;
ylim([1e-8, 1e-3]);
leg = legend('location', 'southwest', 'FontSize', 8, 'NumColumns', 1);
leg.ItemTokenSize(1) = 15;
ax2 = nexttile;
hold on;
for i = 1:length(dx_aligns)
plot(freqs, 180/pi*angle(squeeze(freqresp(Gs_dx_align{i}('de', 'u'), freqs, 'Hz'))));
end
plot(freqs, 180/pi*angle(squeeze(freqresp(Gs_flex('de', 'u'), freqs, 'Hz'))), 'k-');
hold off;
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'lin');
xlabel('Frequency [Hz]'); ylabel('Phase [deg]');
hold off;
yticks(-360:90:360); ylim([-180, 180]);
linkaxes([ax1,ax2],'x');
xlim([10, 2e3]);
xticks([1e1, 1e2, 1e3]);
%% Measurement of the y misalignment between the APA and the flexible joints
% Mesured struts
strut_nums = [1, 2, 3, 4, 5];
% Measured height differences in [mm]
% R ("red" side), B ("black" side)
% R Top B Top R Bot B Bot
strut_align = [[-0.40, -0.60, -0.16, -0.82] % Strut 1
[-0.67, -0.30, -0.34, -0.63] % Strut 2
[-0.07, -0.88, -0.16, -0.79] % Strut 3
[-0.48, -0.46, 0.07, -1.00] % Strut 4
[-0.33, -0.64, -0.48, -0.52]]; % Strut 5
% Verification that the thickness difference between the APA shell and the flexible joints is 1mm
thichness_diff_top = strut_align(:,1) + strut_align(:,2); % [mm]
thichness_diff_bot = strut_align(:,1) + strut_align(:,2); % [mm]
% Estimation of the dy misalignment
dy_bot = (strut_align(:,1) - strut_align(:,2))/2; % [mm]
dy_top = (strut_align(:,3) - strut_align(:,4))/2; % [mm]
%% Idenfity the dynamics from u to de - misalignement estimated from measurement
Gs_y_align = {zeros(size(strut_align,1), 1)};
% Measured dy alignment
strut_align = 1e-3*[[-0.60, -0.82, -0.40, -0.16]
[-0.30, -0.63, -0.67, -0.34]
[-0.88, -0.79, -0.07, -0.16]
[-0.48, 0.07, -0.46, -1.00]
[-0.33, -0.48, -0.64, -0.52]
[-0.34, -0.42, -0.63, -0.57]];
for i = 1:size(strut_align,1)
n_hexapod.actuator = initializeAPA('type', 'flexible', ...
'd_align_bot', [0; strut_align(i, 2) - strut_align(i, 4); 0], ...
'd_align_top', [0; strut_align(i, 1) - strut_align(i, 3); 0]);
G = exp(-s*1e-4)*linearize(mdl, io, 0.0, opts);
G.InputName = {'u'};
G.OutputName = {'Vs', 'de', 'da'};
Gs_y_align(i) = {G};
end
%% Idenfity the dynamics from u to de - misalignement tuned to have the best match
d_aligns = [[-0.05, -0.3, 0];
[ 0, 0.5, 0];
[-0.1, -0.3, 0];
[ 0, 0.3, 0];
[-0.05, 0.05, 0]]'*1e-3;
% Idenfity the transfer function from actuator to encoder for all cases
Gs_xy_align = {zeros(size(d_aligns,2), 1)};
for i = 1:5
n_hexapod.actuator = initializeAPA('type', 'flexible', 'd_align_top', d_aligns(:,i), 'd_align_bot', d_aligns(:,i));
G = exp(-s*1e-4)*linearize(mdl, io, 0.0, opts);
G.InputName = {'u'};
G.OutputName = {'Vs', 'de', 'da'};
Gs_xy_align(i) = {G};
end
%% Comparison of the plants (encoder output) when tuning the misalignment
figure;
tiledlayout(1, 3, 'TileSpacing', 'Compact', 'Padding', 'None');
ax1 = nexttile();
hold on;
plot(f, abs(enc_frf(:, 1)), 'DisplayName', 'Measurement');
plot(freqs, abs(squeeze(freqresp(Gs_y_align{1}('de', 'u'), freqs, 'Hz'))), ...
'DisplayName', '$d_y$ from meas');
plot(freqs, abs(squeeze(freqresp(Gs_xy_align{1}('de', 'u'), freqs, 'Hz'))), ...
'DisplayName', 'Tuned $d_x$, $d_y$');
hold off;
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'log');
xlabel('Frequency [Hz]'); ylabel('Amplitude [m/V]');
leg = legend('location', 'southwest', 'FontSize', 8, 'NumColumns', 1);
leg.ItemTokenSize(1) = 15;
title('Strut 1');
xticks([1e1, 1e2, 1e3]);
ax2 = nexttile();
hold on;
plot(f, abs(enc_frf(:, 2)), 'DisplayName', 'Measurement');
plot(freqs, abs(squeeze(freqresp(Gs_y_align{2}('de', 'u'), freqs, 'Hz'))), ...
'DisplayName', '$d_y$ from meas');
plot(freqs, abs(squeeze(freqresp(Gs_xy_align{2}('de', 'u'), freqs, 'Hz'))), ...
'DisplayName', 'Tuned $d_x$, $d_y$');
hold off;
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'log');
xlabel('Frequency [Hz]'); set(gca, 'YTickLabel',[]);
leg = legend('location', 'southwest', 'FontSize', 8, 'NumColumns', 1);
leg.ItemTokenSize(1) = 15;
title('Strut 2');
xticks([1e1, 1e2, 1e3]);
ax3 = nexttile();
hold on;
plot(f, abs(enc_frf(:, 3)), 'DisplayName', 'Measuremnet');
plot(freqs, abs(squeeze(freqresp(Gs_y_align{3}('de', 'u'), freqs, 'Hz'))), ...
'DisplayName', '$d_y$ from meas');
plot(freqs, abs(squeeze(freqresp(Gs_xy_align{3}('de', 'u'), freqs, 'Hz'))), ...
'DisplayName', 'Tuned $d_x$, $d_y$');
hold off;
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'log');
xlabel('Frequency [Hz]'); set(gca, 'YTickLabel',[]);
leg = legend('location', 'southwest', 'FontSize', 8, 'NumColumns', 1);
leg.ItemTokenSize(1) = 15;
title('Strut 3');
xticks([1e1, 1e2, 1e3]);
linkaxes([ax1,ax2,ax3],'xy');
xlim([10, 2e3]); ylim([1e-8, 1e-3]);
%% Measurement of the y misalignment between the APA and the flexible joints after strut better alignment
% Numbers of the measured legs
strut_align_nums = [1 2 3 4 5 6];
% Measured height differences in [mm]
% R ("red" side), B ("black" side)
% R Top B Top R Bot B Bot
strut_align = [[-0.54, -0.50, -0.50, -0.52] % strut 1
[-0.44, -0.55, -0.49, -0.49] % strut 2
[-0.48, -0.50, -0.50, -0.46] % strut 3
[-0.45, -0.51, -0.51, -0.45] % strut 4
[-0.50, -0.50, -0.50, -0.50] % strut 5
[-0.50, -0.49, -0.43, -0.54]]; % strut 6
% Verification that the thickness difference between the APA shell and the flexible joints is 1mm
thichness_diff_top = strut_align(:,1) + strut_align(:,2); % [mm]
thichness_diff_bot = strut_align(:,1) + strut_align(:,2); % [mm]
% Estimation of the dy misalignment
dy_bot = (strut_align(:,1) - strut_align(:,2))/2; % [mm]
dy_top = (strut_align(:,3) - strut_align(:,4))/2; % [mm]
%% New dynamical identified with re-aligned struts
% Load the identification data
leg_noise = {};
for i = 1:length(strut_align_nums)
leg_noise(i) = {load(sprintf('frf_struts_align_%i_noise.mat', strut_align_nums(i)), 'u', 'Vs', 'de')};
end
% Parameters for Frequency Analysis
Ts = 1e-4; % Sampling Time [s]
Nfft = floor(1/Ts); % Number of points for the FFT computation
win = hanning(Nfft); % Hanning window
Noverlap = floor(Nfft/2); % Overlap between frequency analysis
% Only used to have the frequency vector "f"
[~, f] = tfestimate(leg_noise{1}.u, leg_noise{1}.de, win, Noverlap, Nfft, 1/Ts);
% Transfer function from u to de (encoder)
enc_frf_aligned = zeros(length(f), length(strut_align_nums));
for i = 1:length(strut_align_nums)
enc_frf_aligned(:, i) = tfestimate(leg_noise{i}.u, leg_noise{i}.de, win, Noverlap, Nfft, 1/Ts);
end
%% Bode plot of the FRF from u to de
figure;
hold on;
plot(f, abs(enc_frf(:, 1)), 'color', [colors(1,:), 0.5], ...
'DisplayName', 'Initial alignment');
for i = 1:length(strut_nums)
plot(f, abs(enc_frf(:, i)), 'color', [colors(1,:), 0.5], ...
'HandleVisibility', 'off');
end
plot(f, abs(enc_frf_aligned(:, 1)), 'color', [colors(2,:), 0.5], ...
'DisplayName', 'With positioning pin');
for i = 1:length(strut_align_nums)
plot(f, abs(enc_frf_aligned(:, i)), 'color', [colors(2,:), 0.5], ...
'HandleVisibility', 'off');
end
hold off;
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'log');
xlabel('Frequency [Hz]'); ylabel('Amplitude $d_e/u$ [m/V]');
legend('location', 'southwest', 'FontSize', 8, 'NumColumns', 1);
xlim([10, 2e3]);
ylim([1e-8, 1e-3]);