%% Test Open-Loop ADAS Algorithm Using Driving Scenario
% This example shows how to test an open-loop ADAS (advanced driver
% assistance system) algorithm in Simulink(R). In an open-loop ADAS
% algorithm, the ego vehicle behavior is predefined and does not change as
% the scenario advances during simulation. 
%
% To test the algorithm, 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 example, you read in a scenario by using
% a Scenario Reader block, and then visualize the scenario and sensor
% detections 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
% scenarios that you access through the Driving Scenario Designer app. For
% more details on these scenarios, see
% <docid:driving_ug#mw_a8459f52-ba65-4354-a24b-3eb233ada699 Generate
% Synthetic Detections from a Prebuilt Driving Scenario>.
%
% Open the scenario file in the app.
%
%   drivingScenarioDesigner('LeftTurnScenario.mat')
%
% To simulate the scenario, click *Run*. In this scenario, the ego vehicle
% travels north and goes straight through an intersection. A vehicle coming
% from the left side of the intersection turns left and ends up in front of
% the ego vehicle.
%
% <<../LeftTurnScenario.png>>
%
% The ego vehicle has these sensors:
%
% * A front-facing radar for generating object detections
% * A front-facing camera and rear-facing camera for generating object and
% lane boundary detections
% * A lidar on the center of its roof for generating point cloud data of
% the scenario
%
% <<../LeftTurnScenarioSensors.png>>
%
%% Inspect Model
% The model in this example was generated from the app by selecting *Export
% > Export Simulink Model*. In the model, a
% <docid:driving_ref#mw_0abe0f52-f25a-4829-babb-d9bafe8fdbf3 Scenario
% Reader> block reads the actors and roads from the scenario file and
% outputs the non-ego actors and lane boundaries. Open the model.

open_system('OpenLoopWithScenarios.slx')

%%
% In the <docid:driving_ref#mw_0abe0f52-f25a-4829-babb-d9bafe8fdbf3
% 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.
%
% The Scenario Reader block outputs the poses of the non-ego actors in the
% scenario and the left-lane and right-lane boundaries of the ego vehicle.
% To output all lane boundaries of the road on which the ego vehicle is
% traveling, select the corresponding option for the *Lane boundaries to
% output* parameter.
%
% The actors, lane boundaries, and ego vehicle pose are passed to a
% subsystem containing the sensor blocks. Open the subsystem.

open_system('OpenLoopWithScenarios/Detection Generators')

%%
% The <docid:driving_ref#mw_535e4736-f89b-4dc7-a609-b8c323c4ce32 Driving Radar
% Data Generator>,
% <docid:driving_ref#mw_2fa86dfd-52db-4f59-8781-7d954ae3e8dc Vision
% Detection Generator>, and
% <docid:driving_ref#mw_b87c4ea6-1e17-45b6-8b0c-62226f5a1b28 Lidar Point
% Cloud Generator> blocks produce synthetic detections from the scenario.
% You can fuse this sensor data to generate tracks, such as in the
% open-loop example
% <docid:driving_ug#mw_8ae0eb21-efca-4a6d-9a4a-0415a638c554 Sensor Fusion
% Using Synthetic Radar and Vision Data in Simulink>.
%
% The outputs of the sensor blocks in this model are 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.
%
% Because this model is open loop, the ego vehicle behavior does not change
% as the simulation advances. Therefore, the *Source of ego vehicle*
% parameter is set to |Scenario|, and the block reads the predefined ego
% vehicle pose and trajectory from the scenario file. For vehicle
% controllers and other closed-loop models, set the *Source of ego vehicle*
% parameter to |Input port|. With this option, you specify an ego vehicle
% that is defined in the model as an input to the Scenario Reader block.
% For an example, see
% <docid:driving_ug#mw_953c7fae-43f3-430f-99d4-a5f673caa1ba Test
% Closed-Loop ADAS Algorithm Using Generated Scenario>.
%% Visualize Simulation
% To visualize the scenario and sensor detections, use the
% <docid:driving_ref#mw_59742eb7-dce8-4938-9c2e-44d34c7b8891 Bird's-Eye
% Scope>. On the Simulink toolstrip, under *Review Results*, click
% *Bird's-Eye Scope*. Then, in the scope, click *Find Signals* and run the
% simulation.
%
% <<../DrivingScenarioOnBirdsEyeScope.png>>
%
%% Update Simulation Settings
% This model uses the default simulation stop time of 10 seconds. Because
% the scenario is only about 5 seconds long, the simulation continues to
% run in the Bird's-Eye Scope even after the scenario has ended. To
% synchronize the simulation and scenario stop times, on the Simulink model
% toolbar, set the simulation stop time to |5.2| seconds, which is the
% exact stop time of the app scenario. After you run the simulation, the
% app displays this value in the bottom-right corner of the scenario
% canvas.
% 
% If the simulation runs too fast in the Bird's-Eye Scope, you can slow
% down the simulation by using simulation pacing. On the Simulink
% toolstrip, select *Run > Simulation Pacing*. Select the *Enable pacing to
% slow down simulation* check box and decrease the simulation time to
% slightly less than 1 second per wall-clock second, such as 0.8 seconds.
% Then, rerun the simulation in the Bird's-Eye Scope.