% Code to plot simulation results from sh_cyl_rotating
%% Plot Description:
%
% The plots below show the pressure, speed, and extension of the rotating
% hydraulic cylinder.  A slightly higher pressure is seen in the second
% stroke due to the higher rotational speed of the cylinder.

% Copyright 2015 The MathWorks, Inc.

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

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

% Get simulation results
simlog_t = simlog_sh_cyl_rotating.Cylinder.A.p.series.time;
simlog_cylpA = simlog_sh_cyl_rotating.Cylinder.A.p.series.values('Pa');
simlog_cylw = simlog_sh_cyl_rotating.Rotating_Pipe.W.series.values('rpm');
simlog_cylx = simlog_sh_cyl_rotating.Cylinder.hs.x.series.values('m');

% Plot results
simlog_handles(1) = subplot(3, 1, 1);
plot(simlog_t, simlog_cylpA, 'LineWidth', 1)
grid on
title('Cylinder Pressure')
ylabel('Pressure (Pa)')
set(gca,'YLim',[-1e5 11e5]);

simlog_handles(2) = subplot(3, 1, 2);
plot(simlog_t, simlog_cylw, 'LineWidth', 1)
grid on
title('Cylinder Rotational Speed')
ylabel('Speed (rpm)')

simlog_handles(3) = subplot(3, 1, 3);
plot(simlog_t, simlog_cylx, 'LineWidth', 1)
grid on
title('Piston Position')
ylabel('Position (m)')
xlabel('Time (s)')
set(gca,'YLim',[-0.005 0.02]);

linkaxes(simlog_handles, 'x')

% Remove temporary variables
clear simlog_t simlog_cylpA simlog_cylw simlog_cylx simlog_handles
