% Code to plot simulation results from sdl_transmission_4spd_crcr
%% Plot Description:
%
% The plot below shows the input and output shaft speeds of the
% transmission.  The clutch states are also plotted (locked or open),
% indicating the selected gear of the transmission.

% Copyright 2016 The MathWorks, Inc.

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

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

% Get simulation results
simlog_t = simlog_sdl_transmission_4spd_crcr.Inertia_Input_Shaft.w.series.time;
simlog_wInp = simlog_sdl_transmission_4spd_crcr.Inertia_Input_Shaft.w.series.values('rpm');
simlog_wOut = simlog_sdl_transmission_4spd_crcr.Inertia_Output_Shaft.w.series.values('rpm');

temp_pClutch = logsout_sdl_transmission_4spd_crcr.get('Clutch_Pressures');

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

simlog_handles(2) = subplot(2, 1, 2);
plot(temp_pClutch.Values.Time, temp_pClutch.Values.Data./...
    (repmat((max(temp_pClutch.Values.Data)+eps),size(temp_pClutch.Values.Data,1),1))...
    +(repmat((4:-1:0),size(temp_pClutch.Values.Data,1),1)),...
    'LineWidth', 1);
grid on
set(gca,'YLim',[-0.1 5.1])
set(gca,'YTick',(0:1:5));
set(gca,'YTickLabel',{'' '' '' '' 'open' 'locked' })
title('Clutch States')
ylabel('State')
xlabel('Time (s)')
legend({'A','B','C','D','R'},'Location','Best');

linkaxes(simlog_handles, 'x')

% Remove temporary variables
clear simlog_t simlog_handles
clear simlog_wInp simlog_wOut temp_pClutch
