% Code to plot simulation results from sdl_gear_variable
%% Plot Description:
%
% The plot below shows the input and output shaft speeds for a variable
% ratio gear that is driven by an oscillating torque source.  As the ratio
% increases, the effective inertia decreases and the input shaft maximum
% speed increases.

% Copyright 2016 The MathWorks, Inc.

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

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

% Get simulation results
simlog_t = simlog_sdl_gear_variable.Inertia_Base.w.series.time;
simlog_wB = simlog_sdl_gear_variable.Inertia_Base.w.series.values('rad/s');
simlog_wF = simlog_sdl_gear_variable.Inertia_Follower.w.series.values('rad/s');
simlog_r = simlog_sdl_gear_variable.Variable_Ratio_Gear.ratio.series.values;

temp_colorOrder = get(groot,'defaultAxesColorOrder');

% Plot results
yyaxis left
plot(simlog_t, simlog_wB, 'LineWidth', 1)
hold on
plot(simlog_t, simlog_wF,'-', 'LineWidth', 1,'Color',temp_colorOrder(3,:))
hold off
ylabel('Shaft Speed (rad/s)')
yyaxis right
plot(simlog_t, simlog_r, 'LineWidth', 1)
title('Gear Ratio')
grid on
set(gca,'YLim',...
    [min(simlog_r)-(max(simlog_r)-min(simlog_r))*0.05...
     max(simlog_r)+(max(simlog_r)-min(simlog_r))*0.05])
ylabel('Gear Ratio')
xlabel('Time (s)')
title('Shaft Speed (rad/s)')
legend({'Input Shaft','Output Shaft','Gear Ratio'},'Location','NorthWest');


% Remove temporary variables
clear simlog_t
clear simlog_wB simlog_wF simlog_r temp_colorOrder
