% Code to plot simulation results from sdl_vehicle_dual_clutch
%% Plot Description:
%
% The plots below show the engine and vehicle speed as it accelerates
% through the first three gears in the dual-clutch transmission.  The
% half-gear states indicate gear shifts are taking place by engaging and
% disengaging the dual clutches.

% Copyright 2016 The MathWorks, Inc.

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

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

% Get simulation results
simlog_t = simlog_sdl_vehicle_dual_clutch.Vehicle.Vehicle_Body.V.series.time;
simlog_vVeh = simlog_sdl_vehicle_dual_clutch.Vehicle.Vehicle_Body.V.series.values('mph');
simlog_wEngine = simlog_sdl_vehicle_dual_clutch.Engine.Engine.F.w.series.values('rpm');
simlog_pClOdd = simlog_sdl_vehicle_dual_clutch.Dual_Clutch_Transmission.Clutch_Odd_Gears.P.series.values('Pa');
simlog_pClEven = simlog_sdl_vehicle_dual_clutch.Dual_Clutch_Transmission.Clutch_Even_Gears.P.series.values('Pa');

temp_Gear = logsout_sdl_vehicle_dual_clutch.get('Gear_State');

% Plot results
simlog_handles(1) = subplot(2, 1, 1);
yyaxis left
plot(simlog_t, simlog_vVeh, 'LineWidth', 1)
ylabel('Speed (mph)')
yyaxis right
plot(simlog_t, simlog_wEngine, 'LineWidth', 1)
ylabel('Speed (RPM)')
grid on
title('Vehicle and Engine Speeds')
legend({'Vehicle','Engine'},'Location','Best');

simlog_handles(2) = subplot(2, 1, 2);
yyaxis left
plot(simlog_t, simlog_pClOdd, 'LineWidth', 1)
hold on
plot(simlog_t, simlog_pClEven, 'LineWidth', 1)
hold off
ylabel('Pressure (Pa)')
yyaxis right
plot(temp_Gear.Values.Time, temp_Gear.Values.Data, 'LineWidth', 1)
grid on
ylabel('Gear')
title('Clutch Pressures and Selected Gear')
ylabel('Gear')
xlabel('Time (s)')
legend({'Odd Clutch','Even Clutch','Gear'},'Location','Best');

linkaxes(simlog_handles, 'x')

% Remove temporary variables
clear simlog_t simlog_handles
clear simlog_vVeh simlog_wEngine simlog_pClOdd simlog_pClEven temp_Gear