% Code to plot simulation results from sdl_hybrid_two_mode
%% Plot Description:
%
% The plot below shows the gear ratio of the two mode hybrid powertrain
% transmission. The two electric motors enable the set of planetary gears
% to act as a continuously variable transmission.

% 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('h2_sdl_hybrid_two_mode', 'var') || ...
        ~isgraphics(h2_sdl_hybrid_two_mode, 'figure')
    h2_sdl_hybrid_two_mode = figure('Name', 'sdl_hybrid_two_mode');
end
figure(h2_sdl_hybrid_two_mode)
clf(h2_sdl_hybrid_two_mode)

% Get simulation results
simlog_t = simlog_sdl_hybrid_two_mode.Engine.Engine.F.w.series.time;
simlog_wEngine = simlog_sdl_hybrid_two_mode.Engine.Engine.F.w.series.values('rpm');
simlog_wDriveshaft = simlog_sdl_hybrid_two_mode.Transmission.Planetary_Gear_3.C.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_wDriveshaft./(simlog_wEngine), 'LineWidth', 1)
ylabel('Gear Ratio')
yyaxis right
plot(simlog_t, simlog_vVeh, 'LineWidth', 1)
ylabel('Vehicle Speed (km/hr)')
grid on
title('Gear Ratio and Vehicle Speed')
xlabel('Time (s)');

% Remove temporary variables
clear simlog_t simlog_handles
clear simlog_wMA simlog_wMB simlog_wEngine
clear simlog_wDriveshaft simlog_vVeh
