% Code to plot simulation results from sscfluids_rotact_sequencing
%% Plot Description:
%
% This plot shows the check valve areas that maintain the sequence of
% operations in the two rotary actuators. When rotary actuator 1 rotates,
% the meter-in check valve to chamber A of rotary actuator 2 remains closed
% until rotary actuator 1 completes its stroke. It then opens to enable
% flow into rotary actuator 2 so that it can rotate. The same sequence
% occurs when rotary actuator 1 rotates in the opposite direction.

% Copyright 2019 The MathWorks, Inc.

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

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

plotValveArea(simlog_sscfluids_rotact_sequencing)



% Plot valve area in check valves
function plotValveArea(simlog)

% Get simulation results
t = simlog.Sequencing_Valves.Check_Valve_Ai.valve_area.series.time;
area_Ai = simlog.Sequencing_Valves.Check_Valve_Ai.valve_area.series.values('m^2');
area_Ao = simlog.Sequencing_Valves.Check_Valve_Ao.valve_area.series.values('m^2');
area_Bi = simlog.Sequencing_Valves.Check_Valve_Bi.valve_area.series.values('m^2');
area_Bo = simlog.Sequencing_Valves.Check_Valve_Bo.valve_area.series.values('m^2');

% Plot results
handles(1) = subplot(2, 1, 1);
plot(t, area_Ai, '-', 'LineWidth', 1)
hold on
plot(t, area_Bo, '--', 'LineWidth', 1)
hold off
grid on
legend('Valve A in', 'Valve B out', 'Location', 'best')
ylabel('Area (m^2)')
title('Check Valve Areas')

handles(2) = subplot(2, 1, 2);
plot(t, area_Bi, '-', 'LineWidth', 1)
hold on
plot(t, area_Ao, '--', 'LineWidth', 1)
hold off
grid on
legend('Valve B in', 'Valve A out', 'Location', 'best')
ylabel('Area (m^2)')
xlabel('Time (s)')

linkaxes(handles, 'x')

end