% Code to plot simulation results from sdl_flexible_shaft
%% Plot Description:
%
% The plots below show the amount of twist in each compliance element of
% the flexible shafts as a clutch between them is locked and unlocked.
% Though the stiffness and dimensions are the same for each shaft, their
% behavior is slightly different because they are subjected to different
% loads.  The motor shaft is driven by a motor, and the load shaft is
% connected to a viscous damper.

% Copyright 2016 The MathWorks, Inc.

% Generate simulation results if they don't exist
if ~exist('simlog_sdl_flexible_shaft', 'var')
    sim('sdl_flexible_shaft')
end

% Reuse figure if it exists, else create new figure
if ~exist('h2_sdl_flexible_shaft', 'var') || ...
        ~isgraphics(h2_sdl_flexible_shaft, 'figure')
    h2_sdl_flexible_shaft = figure('Name', 'sdl_flexible_shaft');
end
figure(h2_sdl_flexible_shaft)
clf(h2_sdl_flexible_shaft)

% Get simulation results
simlog_t = simlog_sdl_flexible_shaft.Clutch.P.series.time;

temp_simlog_ShaftLoadAbsPhi= simlog_sdl_flexible_shaft.Shaft_Load.fsTorsion.phi.series.values('deg'); 
temp_dim_simlog_ShaftLoadAbsPhi= simlog_sdl_flexible_shaft.Shaft_Load.fsTorsion.phi.series.dimension;
temp_numNodes_ShaftLoad= temp_dim_simlog_ShaftLoadAbsPhi(1);
simlog_ShaftLoadTotPhi= (reshape(temp_simlog_ShaftLoadAbsPhi', temp_numNodes_ShaftLoad, numel(simlog_t) ))'; 
simlog_ShaftLoadSprPhi= -diff(simlog_ShaftLoadTotPhi, 1, 2);

temp_simlog_ShaftMotorAbsPhi= simlog_sdl_flexible_shaft.Shaft_Motor.fsTorsion.phi.series.values('deg'); 
temp_dim_simlog_ShaftMotorAbsPhi= simlog_sdl_flexible_shaft.Shaft_Motor.fsTorsion.phi.series.dimension;
temp_numNodes_ShaftMotor= temp_dim_simlog_ShaftMotorAbsPhi(1);
simlog_ShaftMotorTotPhi= (reshape(temp_simlog_ShaftMotorAbsPhi', temp_numNodes_ShaftMotor, numel(simlog_t) ))'; 
simlog_ShaftMotorSprPhi= -diff(simlog_ShaftMotorTotPhi, 1, 2);

% Plot results
simlog_handles(1) = subplot(2, 1, 1);
plot(simlog_t, simlog_ShaftLoadSprPhi, 'LineWidth', 1)
grid on
title(['Twist of Compliance Elements 1-' num2str( temp_numNodes_ShaftLoad-1 ) ' in Load Shaft'])
ylabel('Twist (deg)')

simlog_handles(2) = subplot(2, 1, 2);
plot(simlog_t, simlog_ShaftMotorSprPhi, 'LineWidth', 1)
grid on
title(['Twist of Compliance Elements 1-' num2str( temp_numNodes_ShaftMotor-1 ) ' in Motor Shaft'])
ylabel('Twist (deg)')
xlabel('Time (s)')

linkaxes(simlog_handles, 'x')

% Remove temporary variables
clear simlog_t simlog_handles simlog_ShaftLoadSprPhi simlog_ShaftLoadTotPhi
clear simlog_ShaftMotorSprPhi simlog_ShaftMotorTotPhi
clear temp_ShaftLoadids temp_ShaftLoadGBEInds simlog_ShaftLoadSprPhi temp_spri
clear temp_ShaftMotorids temp temp_ShaftMotorSprInds simlog_ShaftMotorSprPhi
clear temp_dim_simlog_ShaftLoadAbsPhi temp_dim_simlog_ShaftMotorAbsPhi
clear temp_numNodes_ShaftLoad temp_numNodes_ShaftMotor
clear temp_simlog_ShaftLoadAbsPhi temp_simlog_ShaftMotorAbsPhi
