% Code to plot simulation results from sh_power_steering
%% Plot Description:
%
% The plots below compare the amount of effort required when the power
% steering is turned on and turned off.  For the same steering sequence,
% the amount of torqure required when the power steering is turned off is
% much higher.  The power steering is turned off by setting the gain in
% Valve Opening to 0.

% Copyright 2015 The MathWorks, Inc.

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

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

% Get current value for twist-to-valve opening gain
temp_vo_gain = get_param([bdroot '/Valve Opening'],'gain');

% Turn power steering on, simulate, and save results
set_param([bdroot '/Valve Opening'],'gain','-0.0212');
sim(bdroot)

simlog_on_t = simlog_sh_power_steering.Cylinder.A.p.series.time;
simlog_on_whlphi = simlog_sh_power_steering.Steering_Angle.Motion_Sensor.phi.series.values('deg');
simlog_on_whltrq = simlog_sh_power_steering.Steering_Velocity_Source.t.series.values('N*m');

% Turn power steering off, simulate, and save results
set_param([bdroot '/Valve Opening'],'gain','0');
sim(bdroot)
simlog_off_t = simlog_sh_power_steering.Cylinder.A.p.series.time;
simlog_off_whlphi = simlog_sh_power_steering.Steering_Angle.Motion_Sensor.phi.series.values('deg');
simlog_off_whltrq = simlog_sh_power_steering.Steering_Velocity_Source.t.series.values('N*m');

% Plot results
simlog_handles(1) = subplot(2, 1, 1);
plot(simlog_on_t, simlog_on_whltrq, 'LineWidth', 1)
hold on
plot(simlog_off_t, simlog_off_whltrq, 'LineWidth', 1,'LineStyle','--')
hold off
grid on
title('Steering Wheel Torque')
ylabel('Torque (N*m)')
legend({'Power Steer On','Power Steer Off'},'Location','Best');

simlog_handles(2) = subplot(2, 1, 2);
plot(simlog_on_t, simlog_on_whlphi, 'LineWidth', 1)
hold on
plot(simlog_off_t, simlog_off_whlphi, 'LineWidth', 1,'LineStyle','--')
hold off
grid on
title('Steering Wheel Angle')
ylabel('Angle (deg)')
xlabel('Time (s)')
legend({'Power Steer On','Power Steer Off'},'Location','Best');

% Reset original value of gain
set_param([bdroot '/Valve Opening'],'gain',temp_vo_gain);

linkaxes(simlog_handles, 'x')

% Remove temporary variables
clear simlog_off_t simlog_off_whlphi simlog_off_whltrq simlog_on_t simlog_on_whlphi simlog_on_whltrq simlog_handles