% Code to plot simulation results from sh_diesel_injection
%% Plot Description:
%
% The plots below shows the extension of each injector pump. When the
% extension is 0, all of the fuel has been expelled from the pump.  The
% plot below shows that the firing order of the cylinders is 1-3-4-2.

% Copyright 2015 The MathWorks, Inc.

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

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

% Get simulation results
simlog_t = simlog_sh_diesel_injection.Cam_Shaft.Parabolic_Cam_1.Angle_Sensor.A.series.time;
simlog_cam1h = simlog_sh_diesel_injection.Cam_Shaft.Position_Source1.PS_Subtract.I1.series.values('m');
simlog_cam2h = simlog_sh_diesel_injection.Cam_Shaft.Position_Source2.PS_Subtract.I1.series.values('m');
simlog_cam3h = simlog_sh_diesel_injection.Cam_Shaft.Position_Source3.PS_Subtract.I1.series.values('m');
simlog_cam4h = simlog_sh_diesel_injection.Cam_Shaft.Position_Source4.PS_Subtract.I1.series.values('m');

% Plot results
temp_camspd = str2num(get_param([bdroot '/Spd'],'Value'));
plot(simlog_t*temp_camspd, simlog_cam1h+0.01*3, 'LineWidth', 1)
hold on
plot(simlog_t*temp_camspd, simlog_cam3h+0.01*2, 'LineWidth', 1)
plot(simlog_t*temp_camspd, simlog_cam4h+0.01, 'LineWidth', 1)
plot(simlog_t*temp_camspd, simlog_cam2h, 'LineWidth', 1)
hold off
title('Injector Pump Extension')
ylabel('Extension (m)')
set(gca,'XLim',[0 2*pi])
xlabel('Cam Rotation (rad)');
set(gca,'YTickLabel',{'' '' '' '' '' '' '0' '' 'max'})
legend({'Cyl 1','Cyl 3','Cyl 4','Cyl 2'},'Location','Best');

% Remove temporary variables
clear simlog_t simlog_cam1h simlog_cam2h simlog_cam3h simlog_cam4h temp_camspd
