% Code to plot simulation results from sh_cyl_servovalve
%% Plot Description:
%
% The plots below show the flow rates in the valve and the position of
% the piston.  

% Copyright 2015 The MathWorks, Inc.

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

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

% Get simulation results
simlog_t = simlog_sh_cyl_servovalve.Cylinder.A.p.series.time;
simlog_valqPA = simlog_sh_cyl_servovalve.Servovalve.Main_Valve.Orifice_PA.Orifice.flow_rate.series.values('m^3/s');
simlog_valqPB = simlog_sh_cyl_servovalve.Servovalve.Main_Valve.Orifice_PB.Orifice.flow_rate.series.values('m^3/s');
simlog_valqAT = simlog_sh_cyl_servovalve.Servovalve.Main_Valve.Orifice_AT.Orifice.flow_rate.series.values('m^3/s');
simlog_valqBT = simlog_sh_cyl_servovalve.Servovalve.Main_Valve.Orifice_BT.Orifice.flow_rate.series.values('m^3/s');
simlog_cylx = simlog_sh_cyl_servovalve.Cylinder.hs.x.series.values('m');

% Plot results
simlog_handles(1) = subplot(2, 1, 1);
plot(simlog_t, simlog_valqPA, 'LineWidth', 1)
hold on
plot(simlog_t, simlog_valqPB, 'LineWidth', 1)
plot(simlog_t, simlog_valqAT, 'LineWidth', 1)
plot(simlog_t, simlog_valqBT, 'LineWidth', 1)
hold off
grid on
title('Valve Orifice Flow Rate')
ylabel('Flow Rate (m^3/s)')
legend({'PA','PB','AT','BT'},'Location','Best');

simlog_handles(2) = subplot(2, 1, 2);
plot(simlog_t, simlog_cylx, 'LineWidth', 1)
grid on
title('Piston Position')
ylabel('Position (m)')
xlabel('Time (s)')

linkaxes(simlog_handles, 'x')

% Remove temporary variables
clear simlog_t simlog_valqPA simlog_valqPB simlog_valqAT simlog_valqBT simlog_cylx simlog_handles
