% Code to plot simulation results from sdl_vehicle_sdl_and_sl
%% Plot Description:
%
% The plot below compares the results of the Simscape Driveline and
% Simulink models.  When the models are equivalent, the results are
% identical.  Meshing losses and physical effects can be easily added to the
% Simscape Driveline model by enabling them in the block dialog boxes.

% Copyright 2016 The MathWorks, Inc.

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

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

% Get simulation results
simlog_t = simlog_sdl_vehicle_sdl_and_sl.Vehicle_Body.v.series.time;
simlog_vSpd_dl=simlog_sdl_vehicle_sdl_and_sl.Vehicle_Body.v.series.values('km/hr');    % Simscape
lgsout_vSpd_sl=get(logsout_sdl_vehicle_sdl_and_sl,'vehSpd_sl');    % Simulink

% Plot results
plot(simlog_t, simlog_vSpd_dl, 'LineWidth', 3)
hold on
plot(lgsout_vSpd_sl.Values.Time, lgsout_vSpd_sl.Values.Data,'c--','LineWidth', 1)
hold off
grid on
title('Vehicle Speed')
ylabel('Speed (km/hr)')
legend({'Simscape Driveline','Simulink'},'Location','Best');
xlabel('Time (s)');

% Remove temporary variables
clear simlog_t simlog_vSpd_dl lgsout_vSpd_sl

