% Code to plot simulation results from sh_cyl_closed_circuit
%% Plot Description:
%
% The plots below shows the position of the piston and the flow rate through the
% pump.

% Copyright 2015 The MathWorks, Inc.

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

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

% Get simulation results
simlog_t = simlog_sh_cyl_closed_circuit.Cylinder.A.p.series.time;
simlog_posMeas = simlog_sh_cyl_closed_circuit.Cylinder.hs.x.series.values('m');
simlog_posCmd = simlog_sh_cyl_closed_circuit.PS_Subtract.I1.series.values('m');
simlog_pumpQ = simlog_sh_cyl_closed_circuit.Pump.flow_rate.series.values('m^3/s');

% Plot results
simlog_handles(1) = subplot(2, 1, 1);
plot(simlog_t, simlog_posMeas, 'LineWidth', 1)
hold on
plot(simlog_t, simlog_posCmd, 'LineWidth', 1)
hold off
grid on
title('Piston Position')
ylabel('Position (m)')
legend({'Measured','Command'},'Location','Best');

simlog_handles(2) = subplot(2, 1, 2);
plot(simlog_t, simlog_pumpQ, 'LineWidth', 1)
grid on
title('Pump Flow Rate')
ylabel('Flow Rate (m^3/s)')
xlabel('Time (s)')

linkaxes(simlog_handles, 'x')

% Remove temporary variables
clear simlog_t simlog_posMeas simlog_posCmd simlog_pumpQ simlog_handles
