%% Test Closed-Loop ADAS Algorithm Using Driving Scenario
% This model shows how to test a closed-loop ADAS (advanced driver
% assistance system) algorithm in Simulink(R). In a closed-loop ADAS
% algorithm, the ego vehicle is controlled by changes in its scenario
% environment as the simulation advances.
%
% To test the scenario, you use a driving scenario that was saved from the
% <docid:driving_ref#mw_07e6310f-b9c9-4f4c-b2f9-51e31d407766 Driving
% Scenario Designer> app. In this model, you read in a scenario using a
% Scenario Reader block, and then visually verify the performance of the
% algorithm, an autonomous emergency braking (AEB) system, on the
% <docid:driving_ref#mw_59742eb7-dce8-4938-9c2e-44d34c7b8891 Bird's-Eye
% Scope>.
%
% Copyright 2018 The MathWorks, Inc.

%% Inspect Driving Scenario
% This example uses a driving scenario that is based on one of the prebuilt
% Euro NCAP test protocol scenarios that you can access through the Driving
% Scenario Designer app. For more details on these scenarios, see
% <docid:driving_ug#mw_10023df9-26a1-48cb-a736-c26b40b14caf Generate
% Synthetic Detections from a Euro NCAP Scenario>.
%
% Open the scenario file in the app.
%
%   drivingScenarioDesigner('AEB_PedestrianChild_Nearside_50width_overrun.mat')
%
% Click *Run* to simulate the scenario. In this scenario, the ego vehicle
% collides with a pedestrian child who is crossing the street.
%
% <<../AEB_PedestrianChild_Scenario.png>>
%
% In the model used in this example, you use an AEB sensor fusion algorithm
% to detect the pedestrian child and test whether the ego vehicle brakes in
% time to avoid a collision.
%% Inspect Model
% The model implements the AEB algorithm described in the
% <docid:driving_ug#mw_b60cf0e1-2485-4e0f-8f1f-8c3300484139 Autonomous
% Emergency Braking with Sensor Fusion> example and is configured to
% simulate a pedestrian collision scenario. Open the model.
%
open_system('AEBTestBenchExample')
%%
% A <docid:driving_ref#mw_0abe0f52-f25a-4829-babb-d9bafe8fdbf3 Scenario
% Reader> block reads the non-ego actors and roads from the specified
% scenario file and outputs the non-ego actors. The ego vehicle is passed
% into the block through an input port.
%
% The Scenario Reader block is located in the *Vehicle Environment > Actors
% and Sensor Simulation* subsystem. Open this subsystem.
%
open_system('AEBTestBenchExample/Vehicle and Environment/Actors and Sensor Simulation')
%%
% In the Scenario Reader block, the *Driving Scenario Designer file name*
% parameter specifies the name of the scenario file. You can specify a
% scenario file that is on the MATLAB(R) search path, such as the scenario
% file used in this example, or the full path to a scenario file.
% Alternatively, you can specify a |<docid:driving_ref#bvm8jbf
% drivingScenario>| object by setting *Source of driving scenario* to |From
% workspace| and then setting *MATLAB or model workspace variable name* to
% the name of a valid |drivingScenario| object workspace variable. In
% closed-loop simulations, specifying the |drivingScenario| object is
% useful because it enables you finer control over specifying the initial
% position of the ego vehicle in your model.
%
% The Scenario Reader block outputs the poses of the non-ego actors in the
% scenario. These poses are passed to vision and radar sensors, whose
% detections are used to determine the behavior of the AEB controller.
%
% The actor poses are output in vehicle coordinates, where: 
%
% * The _X_-axis points forward from the ego vehicle.
% * The _Y_-axis points to the left of the ego vehicle.
% * The origin is located at the center of the rear axle of the ego
% vehicle.
%
% Although this scenario includes a predefined ego vehicle, the Scenario
% Reader block is configured to ignore this ego vehicle definition.
% Instead, the ego vehicle is defined in the model and specified as an
% input to the Scenario Reader block (the *Source of ego vehicle* parameter
% is set to |Input port|). As the simulation advances, the AEB algorithm
% determines the pose and trajectory of the ego vehicle. If you are
% developing an open-loop algorithm, where the ego vehicle is predefined in
% the driving scenario, set the *Source of ego vehicle* parameter to
% |Scenario|. For an example, see
% <docid:driving_ug#mw_ceaa38ba-2dc5-4c22-bf2a-5da81514cc18 Test Open-Loop
% ADAS Algorithm Using Generated Scenario>.
%% Visualize Simulation
% To visualize the scenario, use the
% <docid:driving_ref#mw_59742eb7-dce8-4938-9c2e-44d34c7b8891 Bird's-Eye
% Scope>. From the Simulink toolstrip, under *Review Results*, click
% *Bird's-Eye Scope*. Then, in the scope, click *Find Signals* and run the
% simulation. With the AEB algorithm, the ego vehicle brakes in time to
% avoid a collision.
%
% <<../closedLoopADASBirdsEyeScope.png>>