%% Sensor Fusion Using Synthetic Radar and Vision Data in Simulink
% This example shows how to implement a synthetic data simulation for
% tracking and sensor fusion in Simulink(R) with Automated Driving
% Toolbox(TM). It closely follows the
% <docid:driving_ug#mw_4b0a3201-1e7b-487f-961c-8f0ebf14df97 Sensor
% Fusion Using Synthetic Radar and Vision Data> MATLAB(R) example.
%
% Copyright 2017-2018 The MathWorks, Inc.

%% Introduction
% Simulating synthetic radar and vision detections provides the ability to
% create rare and potentially dangerous events and test the vehicle
% algorithms with them. This example covers the entire synthetic data
% workflow in Simulink.

%% Setup and Overview of the Model
% Prior to running this example, the
% <docid:driving_ref#mw_07e6310f-b9c9-4f4c-b2f9-51e31d407766 Driving
% Scenario Designer> app was used to create the same scenario defined in
% <docid:driving_ug#mw_4b0a3201-1e7b-487f-961c-8f0ebf14df97 Sensor
% Fusion Using Synthetic Radar and Vision Data>. The roads and actors from
% this scenario were then saved to the scenario file |OpenLoop.mat|.
%
% The <docid:driving_ref#mw_0abe0f52-f25a-4829-babb-d9bafe8fdbf3 Scenario
% Reader> block reads the actor pose data from the saved file. The block
% converts the actor poses from the world coordinates of the scenario into
% ego vehicle coordinates. The actor poses are streamed on a bus generated
% by the block.
%
% The actor poses are used by the Sensor Simulation subsystem, which
% generates synthetic radar and vision detections. The simulated detections
% are concatenated at the input to the Multi-Object Tracker block, whose
% output is a list of confirmed tracks. Finally, the Bird's-Eye Scope
% visualizes the actors, the vision and radar detections, the confirmed
% tracks and the road boundaries. The following sections describe the main
% blocks of this model.

%%
close
open_system('SyntheticDataSimulinkExample');

%% Simulating Sensor Detections
% In this example, you simulate an ego vehicle that has 6 radar sensors and
% 2 vision sensors covering the 360 degrees field of view. The sensors have
% some overlap and some coverage gap. The ego vehicle is equipped with a
% long-range radar sensor and a vision sensor on both the front and the
% back of the vehicle. Each side of the vehicle has two short-range radar
% sensors, each covering 90 degrees. One sensor on each side covers from
% the middle of the vehicle to the back. The other sensor on each side
% covers from the middle of the vehicle forward.
%
% When you open the Sensor Simulation subsystem, you can see the two
% <docid:driving_ref#mw_2fa86dfd-52db-4f59-8781-7d954ae3e8dc Vision
% Detection Generator> blocks, configured to generate detections from the
% front and the back of the ego vehicle. The output from the vision
% detection generators is connected to a
% <docid:driving_ref#mw_d736c020-7eef-4c00-9248-9762e9746af5 Detection
% Concatenation> block. Next, the subsystem contains six
% <docid:driving_ref#mw_09922365-011c-47bd-8b62-feadcb8af9d8 Radar
% Detection Generator> blocks, configured as described in the previous
% paragraph. The outputs of the radar detection generators are concatenated
% and then clustered using a Detection Clustering block.

%%
open_system('SyntheticDataSimulinkExample/Sensor Simulation')
%% Tracking and Sensor Fusion
% The detections from the vision and radar sensors must first be
% concatenated to form a single input to the
% <docid:driving_ref#mw_1cb02a28-0c4b-491e-aa45-55747c487a25 Multi-Object
% Tracker> block. The concatenation is done using an additional Detection
% Concatenation block.
%
% The Multi-Object Tracker block is responsible for fusing the data from
% all the detections and tracking the objects around the ego vehicle. The
% multi-object tracker is configured with the same parameters that were
% used in the corresponding MATLAB example,
% <docid:driving_ug#mw_4b0a3201-1e7b-487f-961c-8f0ebf14df97 Sensor
% Fusion Using Synthetic Radar and Vision Data>. The output from the
% Multi-Object Tracker block is a list of confirmed tracks.

%% Creating and Propagating Buses
% The inputs and outputs from the various blocks in this example are all
% |<docid:simulink_ref#bvh6_eb-1 Simulink.Bus>| objects. To simplify compiling the model and creating the buses,
% all the Vision Detection Generator, Radar Detection Generator,
% Multi-Object Tracker, and Detection Concatenation blocks have a property
% that defines the source of the output bus name. When set to |'Auto'|, the
% buses are created automatically and their names are propagated to the
% block that consumes this bus as an input. When set to |'Property'|, you can
% define the name of the output bus. The following images show the
% detections bus, a single detection bus, the tracks bus, and a single
% track bus.
%%
% 
% <<../DetectionsBus.png>>
% 
%%
% 
% <<../SingleDetectionBus.png>>
% 
%%
% 
% <<../TracksBus.png>>
% 
%%
% 
% <<../SingleTrackBus.png>>
% 

%% Display
% The <docid:driving_ref#mw_59742eb7-dce8-4938-9c2e-44d34c7b8891 Bird's-Eye
% Scope> is a model-level visualization tool that you can open from the
% Simulink toolstrip. On the *Simulation* tab, under *Review Results*,
% click *Bird's-Eye Scope*. After opening the scope, click *Find Signals*
% to set up the signals. Then run the simulation to display the actors,
% vision and radar detections, tracks, and road boundaries. The following
% image shows the bird's-eye scope for this example.
% 
% <<../BirdsEyeScope.png>>
% 

%%
close_system('SyntheticDataSimulinkExample')
