function helperPlotAEBResults(logsout)
%helperPlotAEBResults A helper function for plotting the results of the AEB
% demo.
%   This is a helper function for example purposes and may be removed or
%   modified in the future.
%
% The function assumes that the demo outputs the Simulink log, logsout,
% containing several signals.

% Copyright 2018 The MathWorks, Inc.

%% Get the data from simulation
ttc = logsout.getElement('TTC'); % TTC : time-to-contact
FCWtime = logsout.getElement('FCWstoppingTime'); % stopping times
PB1time = logsout.getElement('PB1stoppingTime');
PB2time = logsout.getElement('PB2stoppingTime');
FBtime  = logsout.getElement('FBstoppingTime');

FCWactivate = logsout.getElement('FCWactivate'); % AEB status flags
AEBstatus = logsout.getElement('AEBstatus');
collision = logsout.getElement('collision');
egoCarStop = logsout.getElement('egoCarStop');

acceleration = logsout.getElement('<ax>');  % acceleration of ego car
ego_velocity = logsout.getElement('ego_velocity'); % velocity of ego car
driver_set_velocity = logsout.getElement('driver_set_velocity'); % driver-set velocity

headway = logsout.getElement('headway'); % headway between ego car and MIO

%% Plot the results
figure('position',[100 100 720 600]);
set(gcf,'Numbertitle','off');
set(gcf,'Name','AEB simulation result');

% TTC vs. stopping times
ax(1) = subplot(5,1,1);
hold on;
plot(FCWtime.Values.time,FCWtime.Values.Data,'c');
plot(PB1time.Values.time,PB1time.Values.Data,'g');
plot(PB2time.Values.time,PB2time.Values.Data,'m');
plot(FBtime.Values.time, FBtime.Values.Data, 'r');
plot(ttc.Values.time,ttc.Values.Data,'b','LineWidth',2);
tmax = round(max(FCWtime.Values.Data))+0.5;
ylim([0,tmax]);
legend({'FCWtime','PB1time','PB2time','FBtime','TTC'},...
    'Location','northwest',...
    'FontName','Arial',...
    'FontSize',6);
title('TTC vs. Stopping Time')
ylabel('sec')
grid on;

% AEBstatus flags
ax(2) = subplot(5,1,2);
hold on;
stairs(FCWactivate.Values.time,FCWactivate.Values.Data,'m','LineWidth',1.5);
stairs(AEBstatus.Values.time,AEBstatus.Values.Data,'b','LineWidth',2);
stairs(collision.Values.time,collision.Values.Data,'r');
stairs(egoCarStop.Values.time,egoCarStop.Values.Data,'g');
legend({'FCWactivate','AEBstatus','collision','egoCarStop'},...
    'Location','northwest',...
    'FontName','Arial',...    
    'FontSize',6);
title('FCW and AEB Status (0:NoAEB, 1:PB1, 2:PB2, 3:FB)')
ylabel('Status')
grid on;

% Acceleration
ax(3) = subplot(5,1,3);
hold on;
plot([acceleration.Values.time(1) acceleration.Values.time(end)],[0 0],'k--');
plot(acceleration.Values.time,acceleration.Values.Data,'b','LineWidth',2);
title('Ego Car Acceleration');
ylabel('g'); 
grid on;

% ego velocity
ax(4) = subplot(5,1,4);
hold on;
plot(ego_velocity.Values.time,ego_velocity.Values.Data,'b','LineWidth',2);
plot(driver_set_velocity.Values.time,driver_set_velocity.Values.Data,'r--');
ylim([0,inf]);
legend({'ego velocity','set velocity'},...
    'Location','best',...
    'FontName','Arial',...    
    'FontSize',6);
title('Ego Car Velocity')
ylabel('m/s')
grid on;

% headway
ax(5) = subplot(5,1,5);
hold on;
plot(headway.Values.time,headway.Values.Data,'b','LineWidth',2);
ylim([0,inf]);
title('Headway')
xlabel('time (sec)')
ylabel('meter')
grid on;

linkaxes(ax,'x');
