% Code to plot simulation results from sdl_transmission_4spd_ravigneaux
%% Plot Description:
%
% The plot below shows shaft speeds and overall transmission efficiency as
% the transmission is cycled through each of its gears.  The input shaft is
% turned at a constant speed.  The table in the lower right shows the
% steady-state values for gear ratio and efficiency for each gear.

% Copyright 2016 The MathWorks, Inc.

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

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

% Get simulation results
simlog_t = simlog_sdl_transmission_4spd_ravigneaux.Transmission.Inertia_Input.w.series.time;
simlog_wInput = simlog_sdl_transmission_4spd_ravigneaux.Transmission.Inertia_Input.w.series.values('rpm');
simlog_wOutput = simlog_sdl_transmission_4spd_ravigneaux.Transmission.Inertia_Ring.w.series.values('rpm');

temp_GearData = logsout_sdl_transmission_4spd_ravigneaux.get('Gear_Data');
temp_TransEff = logsout_sdl_transmission_4spd_ravigneaux.get('Transmission_Efficiency');

% Plot results
yyaxis left
plot(simlog_t, simlog_wInput, 'LineWidth', 1)
hold on
plot(simlog_t, simlog_wOutput, 'LineWidth', 2)
hold off
ylabel('Speed (RPM)')
grid on
yyaxis right
plot(temp_TransEff.Values.Time, temp_TransEff.Values.Data, 'LineWidth', 1)
set(gca,'YLim',[-0.1 1.1])
ylabel('Efficiency (0-1)')
title('Shaft Speeds and Transmission Efficiency')
legend({'Input','Output','Efficiency'},'Location','Best');
xlabel('Time (s)')%

temp_gearStr = char(num2str(temp_GearData.Values.Data(:,3),'%i'));
temp_ratioStr = char(num2str(temp_GearData.Values.Data(:,2),'%1.2f'));
temp_effStr = char(num2str(temp_GearData.Values.Data(:,1)*100,'%2.2f'));

temp_labelstr = {'Gear  Ratio   Effic.',...
    ['   ' temp_gearStr(1,:) '   ' temp_ratioStr(1,:) '   %' temp_effStr(1,:)],...
    ['   ' temp_gearStr(2,:) '   ' temp_ratioStr(2,:) '   %' temp_effStr(2,:)],...
    ['   ' temp_gearStr(3,:) '   ' temp_ratioStr(3,:) '   %' temp_effStr(3,:)],...
    ['   ' temp_gearStr(4,:) '   ' temp_ratioStr(4,:) '   %' temp_effStr(4,:)],...
    ['   ' temp_gearStr(5,:) '   ' temp_ratioStr(5,:) '   %' temp_effStr(5,:)],...
    ['   ' temp_gearStr(6,:) '   ' temp_ratioStr(6,:) '   %' temp_effStr(6,:)],...
    };

text(0.675,0.4,temp_labelstr,'units','normalized','VerticalAlignment','top','BackgroundColor','white');

% Remove temporary variables
clear simlog_t
clear simlog_wInput simlog_wOutput temp_TransEff temp_labelstr
clear temp_GearData temp_gearStr temp_ratioStr temp_effStr

