%% Clear Workspace and Close figures
clear; close all; clc;

%% Intialize Laplace variable
s = zpk('s');

addpath('flexor_025/');

open('flexor_025.slx');

% Import Mass Matrix, Stiffness Matrix, and Interface Nodes Coordinates
% We first extract the stiffness and mass matrices.

K = readmatrix('flex025_mat_K.CSV');
M = readmatrix('flex025_mat_M.CSV');



% #+caption: First 10x10 elements of the Mass matrix
% #+RESULTS:
% |  0.006 |  8e-09 | -2e-08 | -1e-10 |  3e-05 |  3e-08 |  0.003 | -3e-09 |  9e-09 |  2e-12 |
% |  8e-09 |   0.02 |  1e-07 | -3e-05 |  1e-11 |  6e-10 |  1e-08 |  0.003 | -5e-08 |  3e-09 |
% | -2e-08 |  1e-07 |   0.01 | -6e-08 | -6e-11 | -8e-12 | -1e-07 |  1e-08 |  0.003 | -1e-08 |
% | -1e-10 | -3e-05 | -6e-08 |  1e-06 |  7e-14 |  6e-13 |  1e-10 |  1e-06 | -1e-08 |  3e-10 |
% |  3e-05 |  1e-11 | -6e-11 |  7e-14 |  2e-07 |  1e-10 |  3e-08 | -7e-12 |  6e-11 | -6e-16 |
% |  3e-08 |  6e-10 | -8e-12 |  6e-13 |  1e-10 |  5e-07 |  1e-08 | -5e-10 | -1e-11 |  1e-13 |
% |  0.003 |  1e-08 | -1e-07 |  1e-10 |  3e-08 |  1e-08 |   0.02 | -2e-08 |  1e-07 | -4e-12 |
% | -3e-09 |  0.003 |  1e-08 |  1e-06 | -7e-12 | -5e-10 | -2e-08 |  0.006 | -8e-08 |  3e-05 |
% |  9e-09 | -5e-08 |  0.003 | -1e-08 |  6e-11 | -1e-11 |  1e-07 | -8e-08 |   0.01 | -6e-08 |
% |  2e-12 |  3e-09 | -1e-08 |  3e-10 | -6e-16 |  1e-13 | -4e-12 |  3e-05 | -6e-08 |  2e-07 |


% Then, we extract the coordinates of the interface nodes.

[int_xyz, int_i, n_xyz, n_i, nodes] = extractNodes('flex025_out_nodes_3D.txt');

% Identification of the parameters using Simscape
% The flexor is now imported into Simscape and its parameters are estimated using an identification.


m = 1;



% The dynamics is identified from the applied force/torque to the measured displacement/rotation of the flexor.

%% Name of the Simulink File
mdl = 'flexor_025';

%% Input/Output definition
clear io; io_i = 1;
io(io_i) = linio([mdl, '/T'], 1, 'openinput');  io_i = io_i + 1;
io(io_i) = linio([mdl, '/D'], 1, 'openoutput'); io_i = io_i + 1;

G = linearize(mdl, io);

% Simpler Model
% Let's now model the flexible joint with a "perfect" Bushing joint as shown in Figure [[fig:flexible_joint_simscape]].

% #+name: fig:flexible_joint_simscape
% #+caption: Bushing Joint used to model the flexible joint
% [[file:figs/flexible_joint_simscape.png]]

% The parameters of the Bushing joint (stiffnesses) are estimated from the Stiffness matrix that was computed from the FEM.

Kx = K(1,1); % [N/m]
Ky = K(2,2); % [N/m]
Kz = K(3,3); % [N/m]
Krx = K(4,4); % [Nm/rad]
Kry = K(5,5); % [Nm/rad]
Krz =  K(6,6); % [Nm/rad]



% The dynamics from the applied force/torque to the measured displacement/rotation of the flexor is identified again for this simpler model.

%% Name of the Simulink File
mdl = 'flexor_025_simplified';

%% Input/Output definition
clear io; io_i = 1;
io(io_i) = linio([mdl, '/T'], 1, 'openinput');  io_i = io_i + 1;
io(io_i) = linio([mdl, '/D'], 1, 'openoutput'); io_i = io_i + 1;

Gs = linearize(mdl, io);



% The two obtained dynamics are compared in Figure


freqs = logspace(0, 5, 1000);

figure;
tiledlayout(1, 2, 'TileSpacing', 'None', 'Padding', 'None');

ax1 = nexttile;
hold on;
set(gca,'ColorOrderIndex',1)
plot(freqs, abs(squeeze(freqresp(G(1,1), freqs, 'Hz'))), '-', 'DisplayName', '$D_x/F_x$');
set(gca,'ColorOrderIndex',1)
plot(freqs, abs(squeeze(freqresp(Gs(1,1), freqs, 'Hz'))), '--', 'HandleVisibility', 'off');
set(gca,'ColorOrderIndex',2)
plot(freqs, abs(squeeze(freqresp(G(2,2), freqs, 'Hz'))), '-', 'DisplayName', '$D_y/F_y$');
set(gca,'ColorOrderIndex',2)
plot(freqs, abs(squeeze(freqresp(Gs(2,2), freqs, 'Hz'))), '--', 'HandleVisibility', 'off');
set(gca,'ColorOrderIndex',3)
plot(freqs, abs(squeeze(freqresp(G(3,3), freqs, 'Hz'))), '-', 'DisplayName', '$D_z/F_z$');
set(gca,'ColorOrderIndex',3)
plot(freqs, abs(squeeze(freqresp(Gs(3,3), freqs, 'Hz'))), '--', 'HandleVisibility', 'off');
hold off;
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'log');
xlabel('Frequency [Hz]'); ylabel('Amplitude [m/N]');
hold off;
legend('location', 'southwest');

ax2 = nexttile;
hold on;
set(gca,'ColorOrderIndex',1)
plot(freqs, abs(squeeze(freqresp(G(4,4), freqs, 'Hz'))), '-', 'DisplayName', '$R_x/M_x$');
set(gca,'ColorOrderIndex',1)
plot(freqs, abs(squeeze(freqresp(Gs(4,4), freqs, 'Hz'))), '--', 'HandleVisibility', 'off');
set(gca,'ColorOrderIndex',2)
plot(freqs, abs(squeeze(freqresp(G(5,5), freqs, 'Hz'))), '-', 'DisplayName', '$R_y/M_y$');
set(gca,'ColorOrderIndex',2)
plot(freqs, abs(squeeze(freqresp(Gs(5,5), freqs, 'Hz'))), '--', 'HandleVisibility', 'off');
set(gca,'ColorOrderIndex',3)
plot(freqs, abs(squeeze(freqresp(G(6,6), freqs, 'Hz'))), '-', 'DisplayName', '$R_z/M_z$');
set(gca,'ColorOrderIndex',3)
plot(freqs, abs(squeeze(freqresp(Gs(6,6), freqs, 'Hz'))), '--', 'HandleVisibility', 'off');
hold off;
set(gca, 'XScale', 'log'); set(gca, 'YScale', 'log');
xlabel('Frequency [Hz]'); ylabel('Amplitude [rad/Nm]');
hold off;
legend('location', 'southwest');

% Comparison with a stiffer Flexible Joint
% The stiffness matrix with the flexible joint with a "hinge" size of 0.50mm is loaded.

K_050 = readmatrix('flex050_mat_K.CSV');
