Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Scenario.mat
Binary file not shown.
93 changes: 67 additions & 26 deletions SensorFusionUsingSyntheticRadarandVisionDataWorkshop.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,69 @@
scenario = drivingScenario;
scenario.SampleTime = 0.01;

roadCenters = [0 0; 50 0; 100 0; 250 20; 500 40];
road(scenario, roadCenters, 'lanes',lanespec(2));

% Create the ego vehicle that travels at 25 m/s along the road. Place the
% vehicle on the right lane by subtracting off half a lane width (1.8 m)
% from the centerline of the road.
egoCar = vehicle(scenario, 'ClassID', 1);
path(egoCar, roadCenters(2:end,:) - [0 1.8], 25); % On right lane

% Add a car in front of the ego vehicle
leadCar = vehicle(scenario, 'ClassID', 1);
path(leadCar, [70 0; roadCenters(3:end,:)] - [0 1.8], 25); % On right lane

% Add a car that travels at 35 m/s along the road and passes the ego vehicle
passingCar = vehicle(scenario, 'ClassID', 1);
waypoints = [0 -1.8; 50 1.8; 100 1.8; 250 21.8; 400 32.2; 500 38.2];
path(passingCar, waypoints, 35);

% Add a car behind the ego vehicle
chaseCar = vehicle(scenario, 'ClassID', 1);
path(chaseCar, [25 0; roadCenters(1:end,:)] - [0 1.8], 25); % On right lane

roadCenters = [12.1 12.8 0;
22.8 20.7 0;
36.5 21.5 0;
41.6 14.7 0;
38.1 6.3 0;
34 3.5 0;
33.7 -18.2 0;
12.5 -22.3 0;
12.1 12.8 0];
laneSpecification = lanespec(2, 'Width', 3.925);
road(scenario, roadCenters, 'Lanes', laneSpecification);

% Add the ego car
egoCar = vehicle(scenario, ...
'ClassID', 1, ...
'Position', [30.2 20.6 0]);
waypoints = [30.2 20.6 0;
31.5 20.3 0;
38.6 17.5 0;
39 10.6 0;
34.6 6.3 0;
30.3 2.5 0;
29.9 -5.5 0;
32.2 -16 0;
29.2 -21.7 0;
23.7 -23.5 0;
17.2 -22.8 0;
13 -20.4 0;
9.2 -15.9 0;
4.1 -14.1 0;
2.3 -5.8 0;
2.9 3.3 0;
7.5 11.7 0;
16.1 19.7 0;
21.1 21.8 0;
26.9 19.9 0;
31.5 20.6 0];
speed = 60;
trajectory(egoCar, waypoints, speed);

% Add the non-ego actors
truck = vehicle(scenario, ...
'ClassID', 2, ...
'Length', 8.2, ...
'Width', 2.5, ...
'Height', 3.5, ...
'Position', [27.1 -23.2 0]);
waypoints = [27.1 -23.2 0;
17.5 -22.7 0;
10.3 -18.2 0;
6.1 -7.4 0;
9.1 4.4 0;
13.4 11.1 0;
20.2 17.8 0;
34.5 20.7 0;
39.5 15.7 0;
38.2 8.6 0;
30.4 2.9 0;
30.4 -9.4 0;
32.2 -17.5 0;
28.8 -21.7 0];
speed = 22;
trajectory(truck, waypoints, speed);
sensors = cell(8,1);
% Front-facing long-range radar sensor at the center of the front bumper of the car.
sensors{1} = radarDetectionGenerator('SensorIndex', 1, 'Height', 0.2, 'MaxRange', 174, ...
Expand All @@ -35,13 +76,13 @@

% Rear-left-facing short-range radar sensor at the left rear wheel well of the car.
sensors{3} = radarDetectionGenerator('SensorIndex', 3, 'Height', 0.2, 'Yaw', 120, ...
'SensorLocation', [0, egoCar.Width/2], 'MaxRange', 30, 'ReferenceRange', 50, ...
'FieldOfView', [90, 5], 'AzimuthResolution', 10, 'RangeResolution', 1.25);
'SensorLocation', [0, egoCar.Width/2], 'MaxRange', 15, 'ReferenceRange', 50, ...
'FieldOfView', [100, 5], 'AzimuthResolution', 10, 'RangeResolution', 1.25);

% Rear-right-facing short-range radar sensor at the right rear wheel well of the car.
sensors{4} = radarDetectionGenerator('SensorIndex', 4, 'Height', 0.2, 'Yaw', -120, ...
'SensorLocation', [0, -egoCar.Width/2], 'MaxRange', 30, 'ReferenceRange', 50, ...
'FieldOfView', [90, 5], 'AzimuthResolution', 10, 'RangeResolution', 1.25);
'SensorLocation', [0, -egoCar.Width/2], 'MaxRange', 15, 'ReferenceRange', 50, ...
'FieldOfView', [100, 5], 'AzimuthResolution', 10, 'RangeResolution', 1.25);

% Front-left-facing short-range radar sensor at the left front wheel well of the car.
sensors{5} = radarDetectionGenerator('SensorIndex', 5, 'Height', 0.2, 'Yaw', 60, ...
Expand Down
162 changes: 162 additions & 0 deletions generateSensorData.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
function [allData, scenario, sensors] = generateSensorData()
%generateSensorData - Returns sensor detections
% allData = generateSensorData returns sensor detections in a structure
% with time for an internally defined scenario and sensor suite.
%
% [allData, scenario, sensors] = generateSensorData optionally returns
% the drivingScenario and detection generator objects.

% Generated by MATLAB(R) 9.5 and Automated Driving System Toolbox 1.3.
% Generated on: 22-Nov-2018 18:51:15

% Create the drivingScenario object and ego car
[scenario, egoCar] = createDrivingScenario;

% Create all the sensors
[sensors, numSensors] = createSensors(scenario);

allData = struct('Time', {}, 'ActorPoses', {}, 'ObjectDetections', {}, 'LaneDetections', {});
running = true;
while running

% Generate the target poses of all actors relative to the ego car
poses = targetPoses(egoCar);
time = scenario.SimulationTime;

objectDetections = {};
laneDetections = [];
isValidTime = false(1, numSensors);

% Generate detections for each sensor
for sensorIndex = 1:numSensors
[objectDets, numObjects, isValidTime(sensorIndex)] = sensors{sensorIndex}(poses, time);
objectDetections = [objectDetections; objectDets(1:numObjects)]; %#ok<AGROW>
end

% Aggregate all detections into a structure for later use
if any(isValidTime)
allData(end + 1) = struct( ...
'Time', scenario.SimulationTime, ...
'ActorPoses', actorPoses(scenario), ...
'ObjectDetections', {objectDetections}, ...
'LaneDetections', {laneDetections}); %#ok<AGROW>
end

% Advance the scenario one time step and exit the loop if the scenario is complete
running = advance(scenario);
end

% Restart the driving scenario to return the actors to their initial positions.
restart(scenario);

% Release all the sensor objects so they can be used again.
for sensorIndex = 1:numSensors
release(sensors{sensorIndex});
end

%%%%%%%%%%%%%%%%%%%%
% Helper functions %
%%%%%%%%%%%%%%%%%%%%

% Units used in createSensors and createDrivingScenario
% Distance/Position - meters
% Speed - meters/second
% Angles - degrees
% RCS Pattern - dBsm

function [sensors, numSensors] = createSensors(scenario)
% createSensors Returns all sensor objects to generate detections

% Assign into each sensor the physical and radar profiles for all actors
profiles = actorProfiles(scenario);
sensors{1} = radarDetectionGenerator('SensorIndex', 1, ...
'SensorLocation', [2.8 -0.9], ...
'Yaw', -90, ...
'MaxRange', 15, ...
'FieldOfView', [120 5], ...
'ActorProfiles', profiles);
sensors{2} = visionDetectionGenerator('SensorIndex', 2, ...
'SensorLocation', [3.7 0], ...
'MaxRange', 100, ...
'DetectorOutput', 'Objects only', ...
'Intrinsics', cameraIntrinsics([1814.81018227767 1814.81018227767],[320 240],[480 640]), ...
'ActorProfiles', profiles);
sensors{3} = radarDetectionGenerator('SensorIndex', 3, ...
'SensorLocation', [2.8 0.9], ...
'Yaw', 90, ...
'MaxRange', 15, ...
'FieldOfView', [120 5], ...
'ActorProfiles', profiles);
numSensors = 3;

function [scenario, egoCar] = createDrivingScenario
% createDrivingScenario Returns the drivingScenario defined in the Designer

% Construct a drivingScenario object.
scenario = drivingScenario;

% Add all road segments
roadCenters = [12.1 12.8 0;
22.8 20.7 0;
36.5 21.5 0;
41.6 14.7 0;
38.1 6.3 0;
34 3.5 0;
33.7 -18.2 0;
12.5 -22.3 0;
12.1 12.8 0];
laneSpecification = lanespec(2, 'Width', 3.925);
road(scenario, roadCenters, 'Lanes', laneSpecification);

% Add the ego car
egoCar = vehicle(scenario, ...
'ClassID', 1, ...
'Position', [30.2 20.6 0]);
waypoints = [30.2 20.6 0;
31.5 20.3 0;
38.6 17.5 0;
39 10.6 0;
34.6 6.3 0;
30.3 2.5 0;
29.9 -5.5 0;
32.2 -16 0;
29.2 -21.7 0;
23.7 -23.5 0;
17.2 -22.8 0;
13 -20.4 0;
9.2 -15.9 0;
4.1 -14.1 0;
2.3 -5.8 0;
2.9 3.3 0;
7.5 11.7 0;
16.1 19.7 0;
21.1 21.8 0;
26.9 19.9 0;
31.5 20.6 0];
speed = 60;
trajectory(egoCar, waypoints, speed);

% Add the non-ego actors
truck = vehicle(scenario, ...
'ClassID', 2, ...
'Length', 8.2, ...
'Width', 2.5, ...
'Height', 3.5, ...
'Position', [27.1 -23.2 0]);
waypoints = [27.1 -23.2 0;
17.5 -22.7 0;
10.3 -18.2 0;
6.1 -7.4 0;
9.1 4.4 0;
13.4 11.1 0;
20.2 17.8 0;
34.5 20.7 0;
39.5 15.7 0;
38.2 8.6 0;
30.4 2.9 0;
30.4 -9.4 0;
32.2 -17.5 0;
28.8 -21.7 0];
speed = 20;
trajectory(truck, waypoints, speed);