% Code to plot simulation results from sh_cyl_digital
%% Plot Description:
%
% The plots below show the pressures in each cylinder chamber and the
% extension of each cylinder. Six of the eight possible positions are
% shown during this cycle.

% Copyright 2015 The MathWorks, Inc.

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

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

% Get simulation results
simlog_t = simlog_sh_cyl_digital.Load_Spring.x.series.time;
simlog_cylpA1 = simlog_sh_cyl_digital.Digital_Hydraulic_Cylinder.Piston_1.Piston_Side_A.A.p.series.values('Pa');
simlog_cylpA2 = simlog_sh_cyl_digital.Digital_Hydraulic_Cylinder.Piston_2.Piston_Side_A.A.p.series.values('Pa');
simlog_cylpA3 = simlog_sh_cyl_digital.Digital_Hydraulic_Cylinder.Piston_3.Piston_Side_A.A.p.series.values('Pa');
simlog_cylpB = simlog_sh_cyl_digital.Digital_Hydraulic_Cylinder.Piston_3.Piston_Side_B.A.p.series.values('Pa');
simlog_cylp1x = simlog_sh_cyl_digital.Digital_Hydraulic_Cylinder.Piston_1_C_Hard_Stop.x.series.values('m');
simlog_cylp2x = simlog_sh_cyl_digital.Digital_Hydraulic_Cylinder.Piston_2_1_Hard_Stop.x.series.values('m');
simlog_cylp3x = simlog_sh_cyl_digital.Digital_Hydraulic_Cylinder.Piston_3_2_Hard_Stop.x.series.values('m');
simlog_cylx = simlog_sh_cyl_digital.Load_Spring.x.series.values('m');

% Plot results
simlog_handles(1) = subplot(2, 1, 1);
plot(simlog_t, simlog_cylpA1, 'LineWidth', 1)
hold on
plot(simlog_t, simlog_cylpA2, 'LineWidth', 1)
plot(simlog_t, simlog_cylpA3, 'LineWidth', 1)
plot(simlog_t, simlog_cylpB, 'LineWidth', 1)
hold off
grid on
title('Cylinder Pressure')
ylabel('Pressure (Pa)')
legend({'Chamber A1','Chamber A2','Chamber A3','Chamber B'},'Location','Best');

simlog_handles(2) = subplot(2, 1, 2);
plot(simlog_t, simlog_cylp1x, 'LineWidth', 1)
hold on
plot(simlog_t, simlog_cylp2x, 'LineWidth', 1)
plot(simlog_t, simlog_cylp3x, 'LineWidth', 1)
plot(simlog_t, simlog_cylx, 'LineWidth', 1)
hold off
grid on
title('Positions of Pistons and Rod')
ylabel('Position (m)')
xlabel('Time (s)')
legend({'Piston 1','Piston 2','Piston 3','Cylinder Rod'},'Location','Best');

linkaxes(simlog_handles, 'x')

% Remove temporary variables
clear simlog_t simlog_cylpA simlog_cylpB simlog_cylx simlog_handles
clear simlog_cylp1x simlog_cylp2x simlog_cylp3x
clear simlog_cylpA1 simlog_cylpA2 simlog_cylpA3
