% Code to plot simulation results from sdl_double_cardan_joint
%% Plot Description:
%
% The plots below show the shaft speeds in double cardan joints.  In each
% case, the output and input shaft speeds are identical.  The intermediate
% shaft speed varies with shaft angle.

% Copyright 2016 The MathWorks, Inc.

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

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

% Get simulation results
simlog_t = simlog_sdl_double_cardan_joint.Double_Cardan_Opposite_Angles.Universal_Joint_1.B.w.series.time;
simlog_wOpB = simlog_sdl_double_cardan_joint.Double_Cardan_Opposite_Angles.Universal_Joint_1.B.w.series.values('rad/s');
simlog_wOpI = simlog_sdl_double_cardan_joint.Double_Cardan_Opposite_Angles.Universal_Joint_1.F.w.series.values('rad/s');
simlog_wOpF = simlog_sdl_double_cardan_joint.Double_Cardan_Opposite_Angles.Universal_Joint_2.F.w.series.values('rad/s');

simlog_wHaB = simlog_sdl_double_cardan_joint.Double_Cardan_Half_Angles.Universal_Joint_1.B.w.series.values('rad/s');
simlog_wHaI = simlog_sdl_double_cardan_joint.Double_Cardan_Half_Angles.Universal_Joint_1.F.w.series.values('rad/s');
simlog_wHaF = simlog_sdl_double_cardan_joint.Double_Cardan_Half_Angles.Universal_Joint_2.F.w.series.values('rad/s');

% Plot results
simlog_handles(1) = subplot(2, 1, 1);
plot(simlog_t, simlog_wOpB, 'LineWidth', 2)
hold on
plot(simlog_t, simlog_wOpF, '--','LineWidth', 2)
plot(simlog_t, simlog_wOpI, 'LineWidth', 1)
hold off
grid on
title('Double Cardan with Opposite Angles')
ylabel('Speed (rad/s)')
legend({'Input Shaft','Output Shaft','Intermediate'},'Location','Best');

simlog_handles(2) = subplot(2, 1, 2);
plot(simlog_t, simlog_wHaB, 'LineWidth', 2)
hold on
plot(simlog_t, simlog_wHaF, '--','LineWidth', 2)
plot(simlog_t, simlog_wHaI, 'LineWidth', 1)
hold off
grid on
title('Double Cardan with Half Angles')
ylabel('Speed (rad/s)')
legend({'Input Shaft','Output Shaft','Intermediate'},'Location','Best');
xlabel('Time (s)')

linkaxes(simlog_handles, 'x')

% Remove temporary variables
clear simlog_t simlog_handles
clear simlog_wHaB simlog_wHaF simlog_wHaI
clear simlog_wOpB simlog_wOpF simlog_wOpI
