% Code to plot simulation results from sh_pump_control_load
%% Plot Description:
%
% The plots below show the behavior of the motor.  The motor speed varies
% as the load torque is applied.  The valve controls pressure to the motor
% such that it can spin forward, backward, or be locked in place.

% Copyright 2015 The MathWorks, Inc.

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

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

% Get simulation results
simlog_t = simlog_sh_pump_control_load.Motor.S.w.series.time;
simlog_motorw = simlog_sh_pump_control_load.Motor.S.w.series.values('rpm');
simlog_loadtrq = simlog_sh_pump_control_load.Load_Torque.t.series.values('N*m');
simlog_valveSA = simlog_sh_pump_control_load.Valve.x3_Position_Valve_Actuator.A.series.values('V');
simlog_valveSB = simlog_sh_pump_control_load.Valve.x3_Position_Valve_Actuator.B.series.values('V');

% Plot results
simlog_handles(1) = subplot(3, 1, 1);
plot(simlog_t, simlog_motorw, 'LineWidth', 1)
grid on
title('Motor Speed')
ylabel('Speed (rpm)')

simlog_handles(2) = subplot(3, 1, 2);
plot(simlog_t, simlog_loadtrq, 'LineWidth', 1)
grid on
title('Load Torque')
ylabel('Torque (N*m)')

simlog_handles(3) = subplot(3, 1, 3);
plot(simlog_t, simlog_valveSA, 'LineWidth', 1)
hold on
plot(simlog_t, simlog_valveSB, 'LineWidth', 1)
hold off
grid on
title('Valve Control')
ylabel('Signal (V)')
xlabel('Time (s)')
legend({'Signal A','Signal B'},'Location','Best');

linkaxes(simlog_handles, 'x')

% Remove temporary variables
clear simlog_t simlog_motorw simlog_loadtrq simlog_valveSA simlog_valveSB simlog_handles
