% Code to plot simulation results from sdl_transmission_3spd_simpson
%% Plot Description:
%
% The plot below shows the speed of the input and output shafts of the
% Simpson 3-speed transmission.  This test cycles through four gear ratios,
% three for forward and one for reverse.  The states of the two clutches
% and two band brakes are also shown.

% Copyright 2016 The MathWorks, Inc.

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

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

% Get simulation results
simlog_t = simlog_sdl_transmission_3spd_simpson.Input_Speed.Motion_Sensor.W.series.time;
simlog_wIn = simlog_sdl_transmission_3spd_simpson.Input_Speed.Motion_Sensor.W.series.values('rpm');
simlog_wOut = simlog_sdl_transmission_3spd_simpson.Output_Speed.Motion_Sensor.W.series.values('rpm');

temp_transCtrl = logsout_sdl_transmission_3spd_simpson.get('Transmission_Control');

% Plot results
simlog_handles(1) = subplot(2, 1, 1);
plot(simlog_t, simlog_wIn, 'LineWidth', 2)
hold on
plot(simlog_t, simlog_wOut, 'LineWidth', 1)
hold off
grid on
title('Shaft Speed')
ylabel('Speed (RPM)')
legend({'Input','Output'},'Location','Best');

simlog_handles(2) = subplot(2, 1, 2);
plot(temp_transCtrl.Values.Time, temp_transCtrl.Values.Data./...
    (repmat((max(temp_transCtrl.Values.Data)+eps),size(temp_transCtrl.Values.Data,1),1))...
    +(repmat([3 2 1 0],size(temp_transCtrl.Values.Data,1),1)),...
    'LineWidth', 1);
set(gca,'YTick',0:4)
set(gca,'YTickLabel',{'' '' '' 'open' 'locked'})
title('Transmission Clutch and Brake States')
ylabel('State')
legend({'Clutch F','Clutch R','Brake Int','Brake Rev'},'Location','Best');
xlabel('Time (s)')

linkaxes(simlog_handles, 'x')

% Remove temporary variables
clear simlog_t simlog_handles
clear simlog_wIn simlog_wOut temp_transCtrl
