% Code to plot simulation results from sdl_transmission_5spd
%% Plot Description:
%
% The plot below shows the selector lever positions for each of the
% double-sided synchronizers.  Their positions dictate which of the gears
% are engaged and determines the gear ratio for the transmission.

% Copyright 2016 The MathWorks, Inc.

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

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

% Get simulation results
simlog_t = simlog_sdl_transmission_5spd.Load_Inertia.I.w.series.time;
simlog_x1Sync1or2 = simlog_sdl_transmission_5spd.Transmission.Dbl_Synchro_1or2.X1.series.values('mm');
simlog_x1Sync3or4 = simlog_sdl_transmission_5spd.Transmission.Dbl_Synchro_3or4.X1.series.values('mm');
simlog_x1Sync5orR = simlog_sdl_transmission_5spd.Transmission.Dbl_Synchro_5orR.X1.series.values('mm');

temp_Gear = logsout_sdl_transmission_5spd.get('Gear');

temp_colororder = get(gca,'defaultAxesColorOrder');

% Plot results
simlog_handles(1) = subplot(2, 1, 1);
plot(simlog_t, simlog_x1Sync1or2, 'LineWidth', 3)
hold on
plot(simlog_t, simlog_x1Sync3or4, 'LineWidth', 2)
plot(simlog_t, simlog_x1Sync5orR, 'LineWidth', 1,'Color',temp_colororder(5,:))
hold off
grid on
title('Selector Lever Position')
ylabel('Position (mm)')
legend({'1 or 2','3 or 4','5 or R'},'Location','Best');

simlog_handles(2) = subplot(2, 1, 2);
plot(temp_Gear.Values.Time, temp_Gear.Values.Data, 'LineWidth', 1,'Color',temp_colororder(4,:))
grid on
set(gca,'YLim',[-1.1 5.1])
set(gca,'YTick',[-1:1:5])
set(gca,'YTickLabel',{'R' 'N' '1' '2' '3' '4' '5'})
title('Selected Gear (N: Neutral, R:Reverse)')
ylabel('Gear');
xlabel('Time (s)')

% Remove temporary variables
clear simlog_t 
clear temp_colororder
clear simlog_x1Sync1or2 simlog_x1Sync3or4 simlog_x1Sync5orR temp_legstr

