% Code to plot simulation results from sdl_hybrid_power_split
%% Plot Description:
%
% The plot below shows the electrical losses from the motor, generator, and
% battery as the vehicle accelerates and decelerates.  The largest losses
% come from the motor and the battery.

% Copyright 2016 The MathWorks, Inc.

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

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

% Get simulation results
simlog_t = simlog_sdl_hybrid_power_split.Engine.Engine.P.series.time;
simlog_lossMotor = simlog_sdl_hybrid_power_split.Motor.PMSM.Motor_Equations.PS_Term_Losses.I.series.values;
simlog_lossGen = simlog_sdl_hybrid_power_split.Generator.PMSM.Motor_Equations.PS_Term_Losses.I.series.values;
simlog_lossBatt = simlog_sdl_hybrid_power_split.Battery.Calculations.R.O.series.values;
simlog_vVeh = simlog_sdl_hybrid_power_split.Vehicle_Body.Vehicle_Body.V.series.values('km/hr');

temp_colorOrder = get(groot,'defaultAxesColorOrder');

% Plot results
yyaxis left
plot(simlog_t, (simlog_lossBatt+simlog_lossMotor+simlog_lossGen)/1000, 'LineWidth', 1)
hold on
plot(simlog_t, simlog_lossMotor/1000, 'LineWidth', 1)
plot(simlog_t, simlog_lossGen/1000, '-.','LineWidth', 1)
plot(simlog_t, simlog_lossBatt/1000,'-','LineWidth', 1,'Color',temp_colorOrder(5,:));
ylabel('Electrical Losses (kW)')
yyaxis right
plot(simlog_t, simlog_vVeh, 'LineWidth', 1)
ylabel('Speed (km/hr)')
grid on
title('Electrical Losses and Vehicle Speed')
legend({'Total Losses','Motor Losses','Generator Losses','Battery Losses','Vehicle Speed'},'Location','NorthEast');
xlabel('Time (s)')

% Remove temporary variables
clear simlog_t temp_colorOrder
clear simlog_lossMotor simlog_lossGen simlog_lossBatt 
clear simlog_vVeh
