% Code to plot simulation results from sdl_hybrid_two_mode
%% Plot Description:
%
% The plot below shows the speeds of the engine, motor-generator shafts,
% and vehicle speed as it progresses through each of the modes of a two
% mode hybrid powertrain. The modes include fixed gears and split modes
% where the flow of power is split at the input to the powertrain or both
% at the input and output (compound split).

% Copyright 2016-2017 The MathWorks, Inc.

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

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

% Get simulation results
simlog_t = simlog_sdl_hybrid_two_mode.Transmission.Inertia_MA.w.series.time;
simlog_wMA = simlog_sdl_hybrid_two_mode.Transmission.Inertia_MA.w.series.values('rpm');
simlog_wMB = simlog_sdl_hybrid_two_mode.Transmission.Inertia_MB.w.series.values('rpm');
simlog_wEngine = simlog_sdl_hybrid_two_mode.Engine.Engine.F.w.series.values('rpm');
simlog_vVeh = simlog_sdl_hybrid_two_mode.Vehicle_Body.Vehicle_Body.v.series.values('km/hr');

% Plot results
yyaxis left
plot(simlog_t, simlog_wEngine, 'LineWidth', 1)
ylabel('Shaft Speed (RPM)')
hold on
plot(simlog_t, simlog_wMA, 'LineWidth', 1)
plot(simlog_t, simlog_wMB,'-.', 'LineWidth', 1)
hold off
yyaxis right
plot(simlog_t, simlog_vVeh, 'LineWidth', 1)
ylabel('Vehicle Speed (km/hr)')
grid on
title('Shaft Speeds and Vehicle Speed')
legend({'Engine','Motor A','Motor B','Vehicle'},'Location','NorthWest');
xlabel('Time (s)');

% Remove temporary variables
clear simlog_t
clear simlog_wMA simlog_wMB simlog_wEngine simlog_vVeh
