% Code to plot simulation results from sdl_transmission_helicopter_base
%% Plot Description:
%
% The plots below show the torque supplied by the engine and the torque
% applied to the rotor shafts.  The engine is permitted to stall after 6
% seconds.  Induced and profile drag on the spinning rotors act to slow
% them down.

% Copyright 2016-2019 The MathWorks, Inc.

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

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

% Get simulation results
simlog_t = simlog_sdl_transmission_helicopter_base.Inertia_Main_Rotor.t.series.time;
simlog_trqMainR = simlog_sdl_transmission_helicopter_base.Inertia_Main_Rotor.t.series.values('N*m');
simlog_trqTailR = simlog_sdl_transmission_helicopter_base.Inertia_Tail_Rotor.t.series.values('N*m');
simlog_trqEng = simlog_sdl_transmission_helicopter_base.Engine.trq.series.values('N*m');
simlog_thrEng = simlog_sdl_transmission_helicopter_base.Engine.T.series.values('N*m');

% Plot results
simlog_handles(1) = subplot(2, 1, 1);
plot(simlog_t, simlog_trqEng, 'LineWidth', 1)
hold on
plot(simlog_t, -simlog_trqMainR, 'LineWidth', 1)
plot(simlog_t, -simlog_trqTailR, 'LineWidth', 1)
hold off
grid on
title('Shaft Torque')
ylabel('Torque (N*m)')
legend({'Engine','Main Rotor','Tail Rotor'},'Location','Best');

simlog_handles(2) = subplot(2, 1, 2);
plot(simlog_t, simlog_thrEng, 'LineWidth', 1)
grid on
title('Engine Throttle')
ylabel('Throttle (0-1)')
xlabel('Time (s)')

linkaxes(simlog_handles, 'x')

% Remove temporary variables
clear simlog_t simlog_handles
clear simlog_trqEng simlog_trqMainR simlog_trqTailR simlog_thrEng
