% Code to plot simulation results from sdl_vehicle_manual_transmission
%% Plot Description:
%
% The plots below show the driver inputs to the engine and transmission.  
% The top plot shows the forces applied to the synchronizers to select the
% active gear, and the bottom plot shows the inputs to the throttle and
% clutch.

% Copyright 2016 The MathWorks, Inc.

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

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

% Get simulation results
simlog_syncFrcLogsout = logsout_sdl_vehicle_manual_transmission.get('Synchronizer_Forces');
simlog_clutchLogsout = logsout_sdl_vehicle_manual_transmission.get('Clutch');
simlog_throttleLogsout = logsout_sdl_vehicle_manual_transmission.get('Throttle');

% Plot results
simlog_handles(1) = subplot(2, 1, 1);
plot(simlog_syncFrcLogsout.Values.Time,simlog_syncFrcLogsout.Values.Data, 'LineWidth', 1)
hold off
grid on
title('Force on Synchronizers')
ylabel('Speed (RPM)')
legend({'Syn 4','Syn 3','Syn 2','Syn 1'},'Location','Best');

simlog_handles(2) = subplot(2, 1, 2);
yyaxis left
plot(simlog_throttleLogsout.Values.Time, simlog_throttleLogsout.Values.Data, 'LineWidth', 1)
ylabel('Throttle (0-1)');
set(gca,'YLim',[0 1.1])
hold on
yyaxis right
plot(simlog_clutchLogsout.Values.Time, simlog_clutchLogsout.Values.Data, 'LineWidth', 1)
ylabel('Clutch (N)')
hold off
grid on
title('Pedal Inputs')

xlabel('Time (s)')
%legend({'Throttle','Clutch'},'Location','Best');

linkaxes(simlog_handles, 'x')

% Remove temporary variables
clear simlog_t simlog_handles
clear simlog_syncFrcLogsout simlog_throttleLogsout simlog_clutchLogsout simlog_cam4h temp_camspd
