% Code to plot simulation results from sdl_capstan
%% Plot Description:
%
% The plots show:
%  * The relative velocity between the capstan and the belt along with the
%    holding force speed
%  * The capstan torque required to produce a change in the rotational speed
%    and to resist the holding and load forces
%  * How the load force eventually exceeds the capstan force limit and causes slip

% Copyright 2017 The MathWorks, Inc.

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

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

% Get simulation results
simlog_t = simlog_sdl_capstan.Capstan.rel_velocity.series.time;
simlog_vRel = simlog_sdl_capstan.Capstan.rel_velocity.series.values('m/s');
simlog_vHold = simlog_sdl_capstan.Capstan.A.v.series.values('m/s');
simlog_tS = simlog_sdl_capstan.Capstan.tS.series.values('N*m');
simlog_fB = simlog_sdl_capstan.Capstan.fB.series.values('N');
simlog_fLoad = simlog_sdl_capstan.Ideal_Force_Source1.S.series.values('N');

% Plot results
simlog_handles(1) = subplot(3, 1, 1);
plot(simlog_t, simlog_vRel, 'LineWidth', 1)
hold on
plot(simlog_t, simlog_vHold, 'LineWidth', 1)
hold off
ylabel('Speed (m/s)')
grid on
set(gca,'YLim',[-5 15])
set(gca,'YTick',(-5:5:15));
title('Slip and holding force speeds')
legend({'Slip','Holding force'},'Location','NorthWest');

simlog_handles(2) = subplot(3, 1, 2);
plot(simlog_t, simlog_tS, 'LineWidth', 1)
ylabel('Torque (N*m)')
grid on
title('Capstan torque')

simlog_handles(3) = subplot(3, 1, 3);
plot(simlog_t, simlog_fB, 'LineWidth', 1)
hold on
plot(simlog_t, simlog_fLoad, 'LineWidth', 1)
hold off
ylabel('Force (N)')
grid on
title('Capstan and load forces')
legend({'Capstan','Load'},'Location','SouthWest');

linkaxes(simlog_handles, 'x')

% Remove temporary variables
clear simlog_t simlog_handles
clear simlog_vRel simlog_vHold simlog_tS simlog_fB simlog_fLoad
