% Code to plot simulation results from sh_hydrostatic_trans
%% Plot Description:
%
% The plots below show the flow rate through the hydrostatic transmission
% and the pressure drop across the motor.  As the motor reaches its nominal
% pressure, flow is diverted through a pressure relief valve.

% Copyright 2015 The MathWorks, Inc.

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

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

% Get simulation results
simlog_t = simlog_sh_hydrostatic_trans.Motor.flow_rate.series.time;
simlog_motorq = simlog_sh_hydrostatic_trans.Motor.flow_rate.series.values('m^3/s');
simlog_srcq = simlog_sh_hydrostatic_trans.Flow_Rate_Source.q.series.values('m^3/s');
simlog_prvBq = simlog_sh_hydrostatic_trans.Pressure_Relief_Valve_A.flow_rate.series.values('m^3/s');
simlog_motordp = simlog_sh_hydrostatic_trans.Motor.pressure_diff.series.values('Pa');

% Plot results
simlog_handles(1) = subplot(2, 1, 1);
plot(simlog_t, simlog_motorq, 'LineWidth', 1)
hold on
plot(simlog_t, simlog_prvBq, 'LineWidth', 1)
plot(simlog_t, simlog_srcq, 'LineWidth', 1,'LineStyle','--')
hold off
grid on
title('Flow Rates')
ylabel('Flow Rate (m^3/s)')
legend({'Motor','Pressure Relief Valve A','Source'},'Location','Best');

simlog_handles(2) = subplot(2, 1, 2);
plot(simlog_t, simlog_motordp, 'LineWidth', 1)
grid on
title('Motor Pressure Difference')
ylabel('Pressure (Pa)')
xlabel('Time (s)')
set(gca,'YLim',[-1e7 1.25e7])
linkaxes(simlog_handles, 'x')

% Remove temporary variables
clear simlog_t simlog_srcq simlog_motorq simlog_prvBq simlog_motordp simlog_handles
