% Code to plot simulation results from sdl_hybrid_power_split
%% Plot Description:
%
% The plot below shows the flow of power from the engine, motor, and
% generator as the vehicle accelerates and decelerates.  The generator
% supplies the DC network with a constant flow of power drawn from the
% engine.  The motor draws power from the battery to accelerate the vehicle
% and then uses regenerative braking to feed that power back to 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('h1_sdl_hybrid_power_split', 'var') || ...
        ~isgraphics(h1_sdl_hybrid_power_split, 'figure')
    h1_sdl_hybrid_power_split = figure('Name', 'sdl_hybrid_power_split');
end
figure(h1_sdl_hybrid_power_split)
clf(h1_sdl_hybrid_power_split)

% Get simulation results
simlog_t = simlog_sdl_hybrid_power_split.Engine.Engine.P.series.time;
simlog_pwrEngine = simlog_sdl_hybrid_power_split.Engine.Engine.P.series.values('kW');
simlog_pwrMotor = simlog_sdl_hybrid_power_split.Motor.PMSM.Motor_Equations.Mechanical_Power.O.series.values;
simlog_pwrGen = simlog_sdl_hybrid_power_split.Generator.PMSM.Motor_Equations.Mechanical_Power.O.series.values;
simlog_pwrBatt = simlog_sdl_hybrid_power_split.Battery.Calculations.Power.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_pwrMotor/1000, 'LineWidth', 1)
hold on
plot(simlog_t, simlog_pwrEngine, 'LineWidth', 1)
plot(simlog_t, simlog_pwrGen/1000, '-.','LineWidth', 1)
plot(simlog_t, simlog_pwrBatt/1000,'-','LineWidth', 1,'Color',temp_colorOrder(5,:));
ylabel('Power (kW)')
yyaxis right
plot(simlog_t, simlog_vVeh, 'LineWidth', 1)
ylabel('Speed (km/hr)')
grid on
title('Power and Vehicle Speed')
legend({'Motor Power','Engine Power','Generator Power','Battery Power','Vehicle Speed'},'Location','NorthEast');
xlabel('Time (s)')

% Remove temporary variables
clear simlog_t temp_colorOrder
clear simlog_pwrEngine simlog_pwrMotor simlog_pwrGen simlog_pwrBatt 
clear simlog_vVeh
