Main Content

SensorSimulation

Add sensors and access sensor data from scenario simulation

Since R2023a

Description

A SensorSimulation object represents a sensor in a scenario simulation. Use a sensorSimulation object to:

  • Add sensors to actors in a scenario

  • Retrieve sensor detection and lane boundary data during simulation

Creation

Retrieve the SensorSimulation object from a scenario simulation by using the get function of a Simulink.ScenarioSimulation object with the "SensorSimulation" option.

Object Functions

addSensorsAdd sensors to vehicle actors in RoadRunner scenario
targetPosesGet positions and orientations of targets in sensor range relative to host vehicle
laneBoundariesGet lane boundaries relative to host vehicle

Examples

collapse all

Define sensor models in MATLAB®, and add them to vehicle actors in a RoadRunner Scenario. Then, obtain ground truth measurements from RoadRunner Scenario, process them into detections for visualization.

Set Up RoadRunner Scenario — MATLAB Interface

Configure your RoadRunner installation and project folder properties. Open the RoadRunner app.

rrInstallationPath = "C:\Program Files\RoadRunner R2022b\bin\win64";
rrProjectPath = "D:\RR\TestProjects";

s = settings;
s.roadrunner.application.InstallationFolder.PersonalValue = rrInstallationPath;
rrApp = roadrunner(rrProjectPath);

To open the scenario this example uses, you must add the TrajectoryCutIn-longRun.rrscenario file from the example folder to your RoadRunner project folder. Then, open the scenario.

openScenario(rrApp,"TrajectoryCutIn-longRun")

Create a ScenarioSimulation object to connect MATLAB to the RoadRunner Scenario simulation and set the step size.

scenarioSim = createSimulation(rrApp);
stepSize = 0.1;
set(scenarioSim,"StepSize",stepSize);

Create a SensorSimulation object to control the sensor configuration for the RoadRunner Scenario simulation.

sensorSim = get(scenarioSim,"SensorSimulation");

Configure Sensors and Add to RoadRunner Scenario

Configure the sensor model consisting of one vision sensor at the front of the ego vehicle using the visionDetectionGenerator object. If you want to add multiple sensors, specify unique IDs for each sensor.

sensorID = 1;
sensor = visionDetectionGenerator(SensorIndex=sensorID, ...
            SensorLocation=[2.4 0], ...
            MaxRange=50, ...
            DetectorOutput="Objects only", ...
            UpdateInterval=stepSize);

Add the sensor to the ego vehicle actor in the RoadRunner scenario. Specify the Actor ID property for the vehicle.

egoVehicleID = 1;
addSensors(sensorSim,sensor,egoVehicleID)

Set up bird's-eye-plot for visualization.

[lbPlotter,detPlotter,bepAxes] = helperSetupBEP(sensor);

Simulate RoadRunner Scenario and Visualize Sensor Data

Use the ScenarioSimulation object to step through the RoadRunner scenario. Retrieve target poses in the sensor range using the targetPoses function, and process them into detections using the sensor model. Visualize detections and ground truth lane boundaries using birdsEyePlot.

simTime = 0.0;
set(scenarioSim,"SimulationCommand","Start");
set(scenarioSim,"SimulationCommand","Pause");
pause(0.5)
legend(bepAxes,"show")

while ~isequal(get(scenarioSim,"SimulationStatus"),"Stopped")

    % Get ground truth target poses and lane boundaries from the sensor
    tgtPoses = targetPoses(sensorSim,1);
    gTruthLbs = laneBoundaries(sensorSim,1,OutputOption="EgoAdjacentLanes",inHostCoordinate=true);
    
    if ~isempty(gTruthLbs)
        
        % Plot ground-truth lane boundaries
        helperPlotLaneBoundaries(lbPlotter,gTruthLbs)
        
        % Generate detections
        [objDets,numObjDets,objDetsValid] = sensor(tgtPoses,simTime);
        
        % Plot detections
        if objDetsValid
            detPos = cellfun(@(d)d.Measurement(1:2),objDets,UniformOutput=false);
            detPos = vertcat(zeros(0,2),cell2mat(detPos')');
            plotDetection(detPlotter,detPos)
        end
        
    end     

    if ~isequal(get(scenarioSim,"SimulationStatus"),"Stopped")
        set(scenarioSim,"SimulationCommand","Step");
    end

    simTime = simTime + stepSize;
    pause(0.1)
end

Helper Functions

helperSetupBEP function creates a bird's-eye-plot and configures all the plotters for visualization.

helperPlotLaneBoundaries function plots the lane boundaries on the birds'eye-plot.

Define sensor models in Simulink™, and add them to vehicle actors in a RoadRunner Scenario. Then, obtain ground truth measurements from RoadRunner Scenario, process them into detections in Simulink for visualization.

Set Up RoadRunner Scenario — Simulink Interface

Configure your RoadRunner installation and project folder properties. Open the RoadRunner app.

rrInstallationPath = "C:\Program Files\RoadRunner R2022b\bin\win64";
rrProjectPath = "D:\RR\TestProjects";

s = settings;
s.roadrunner.application.InstallationFolder.PersonalValue = rrInstallationPath;
rrApp = roadrunner(rrProjectPath);

To open the scenario this example uses, you must add these files from the example folder to your RoadRunner project folder:

  1. straightCurvedFourLaneRoad.rrscene — RoadRunner scene file that describes the four lane highway.

  2. SensorDerectionSimulation.rrscenario — RoadRunner scenario file that describes actors and their trajectories on the four lane highway scene.

  3. SensorIntegration.rrbehavior.rrmeta — Behavior file that associates the sensor detection processing and visualization behavior using Simulink, to the ego vehicle in the RoadRunner scenario.

copyfile("straightCurvedFourLaneRoad.rrscene",fullfile(rrProjectPath,"Scenes/"));
copyfile("SensorDetectionSimulation.rrscenario",fullfile(rrProjectPath,"Scenarios/"))
copyfile("SensorIntegration.rrbehavior.rrmeta",fullfile(rrProjectPath,"Assets","Behaviors/"))

Open the scenario and create a ScenarioSimulation object to connect Simulink with the RoadRunner Scenario.

openScenario(rrApp,"SensorDetectionSimulation")
rrSim = createSimulation(rrApp);

Specify the step size for the RoadRunner scenario simulation. The Simulink model also uses the same step size. Units are in seconds.

simStepSize = 0.1;
set(rrSim,"StepSize",simStepSize);

Inspect Simulink Model and Simulate Scenario

The Simulink model rrScenarioSimWithSensors configures sensor models for the vision and radar sensors to be added to the ego vehicle using Vision Detection Generator and Radar Detection Generator blocks respectively. The model reads ground truth data from the scenario and processes that into detections and visualizes the detections. It also reads the path defined for the ego vehicle from the RoadRunner Scenario, implements the path follower logic and controls the ego vehicle motion along the path.

Load the bus definitions and open the model.

load("busDefinitionsForRRSim.mat")
modelName = 'rrScenarioSimWithSensors';
open_system(modelName)

Create a SensorSimulation object. Specify the block paths for the radar and vision sensors in the model.

sensorSim = get(rrSim,"SensorSimulation");
visionSensorBlkPath = [modelName,'/Vision Detection Generator'];
radarSensorBlkPath = [modelName,'/Driving Radar Data Generator'];

Add the two sensors to the ego vehicle using the addSensors function.

egoVehicleId = 1;
addSensors(sensorSim,{visionSensorBlkPath,radarSensorBlkPath},egoVehicleId);

Get the actor profiles of all the actors in the scenario for visualization.

rrScenario = getScenario(rrSim);
actorProfiles = helperGetActorProfiles(rrScenario);
numActors = length(actorProfiles);
numTargets = numActors - 1;

Start the scenario Simulation. You can visualize the object detections and lane detections alongside ground truth values.

set(rrSim,"SimulationCommand","Start")

Version History

Introduced in R2023a