% Code to plot simulation results from sh_cyl_closed_loop
%% Plot Description:
%
% The plots below shows the measured and commanded position of the piston as
% well as the pressures in the hydraulic cylinder.

% Copyright 2015 The MathWorks, Inc.

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

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

% Get simulation results
simlog_t = simlog_sh_cyl_closed_loop.PS_Subtract.I1.series.time;
simlog_cmd = simlog_sh_cyl_closed_loop.PS_Subtract.I1.series.values('m');
simlog_pos = simlog_sh_cyl_closed_loop.PS_Subtract.I2.series.values('m');
simlog_pA = simlog_sh_cyl_closed_loop.Cylinder.A.p.series.values('Pa');
simlog_pB = simlog_sh_cyl_closed_loop.Cylinder.B.p.series.values('Pa');

% Plot results
simlog_handles(1) = subplot(2, 1, 1);
plot(simlog_t, simlog_cmd, 'LineWidth', 1)
hold on
plot(simlog_t, simlog_pos, 'LineWidth', 1,'LineStyle','--')
hold off
grid on
title('Rod Position')
ylabel('Position (m)')
legend({'Command','Position'},'Location','Best');
simlog_handles(2) = subplot(2, 1, 2);
plot(simlog_t, simlog_pA, 'LineWidth', 1)
hold on
plot(simlog_t, simlog_pB, 'LineWidth', 1)
hold off
grid on
title('Cylinder Pressures')
ylabel('Pressure (Pa)')
xlabel('Time (s)')
legend({'Side A','Side B'},'Location','Best');

linkaxes(simlog_handles, 'x')

% Remove temporary variables
clear simlog_t simlog_cmd simlog_pos simlog_pA simlog_pB simlog_handles
