% Code to plot simulation results from sscfluids_front_loader
%% Plot Description:
%
% The plots below show the pressure difference in the lift and tilt
% cylinders and the position of the in each cylinder.

% Copyright 2015-2020 The MathWorks, Inc.

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

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

% Get simulation results
simlog_t = simlog_sscfluids_front_loader.Inertia.w.series.time;
simlog_liftcylpA = simlog_sscfluids_front_loader.Lift_Actuator.Cylinder.A.p.series.values('Pa');
simlog_liftcylpB = simlog_sscfluids_front_loader.Lift_Actuator.Cylinder.B.p.series.values('Pa');
simlog_liftcylx = simlog_sscfluids_front_loader.Lift_Actuator.Cylinder.hard_stop.x.series.values('m');
simlog_tiltcylpA = simlog_sscfluids_front_loader.Tilt_Actuator.Cylinder.A.p.series.values('Pa');
simlog_tiltcylpB = simlog_sscfluids_front_loader.Tilt_Actuator.Cylinder.B.p.series.values('Pa');
simlog_tiltcylx = simlog_sscfluids_front_loader.Tilt_Actuator.Cylinder.hard_stop.x.series.values('m');

% Plot results
simlog_handles(1) = subplot(2, 1, 1);
plot(simlog_t, simlog_liftcylpA-simlog_liftcylpB, 'LineWidth', 1)
hold on
plot(simlog_t, simlog_tiltcylpA-simlog_tiltcylpB, 'LineWidth', 1)
hold off
grid on
title('Pressure Difference in Cylinder')
ylabel('Pressure (Pa)')
legend({'Lift','Tilt'},'Location','Best');

simlog_handles(2) = subplot(2, 1, 2);
plot(simlog_t, simlog_liftcylx, 'LineWidth', 1)
hold on
plot(simlog_t, simlog_tiltcylx, 'LineWidth', 1)
hold off
grid on
title('Cylinder Position')
ylabel('Position (m)')
xlabel('Time (s)')
legend({'Lift','Tilt'},'Location','Best');

linkaxes(simlog_handles, 'x')

% Remove temporary variables
clear simlog_t simlog_liftcylpA simlog_liftcylpB simlog_tiltcylpA simlog_tiltcylpB 
clear simlog_liftcylx simlog_tiltcylx simlog_handles
