Main Content

Add Sensors to RoadRunner Scenario Using MATLAB

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 R2024a\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.

copyfile("TrajectoryCutIn-longRun.rrscenario",fullfile(rrProjectPath,"Scenarios/"))
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 sensor models for vision, radar and lidar sensors to add to the ego vehicle using visionDetectionGenerator, drivingRadarDataGenerator and lidarPointCloudGenerator objects. Specify unique IDs for each sensor.

visionSensor = visionDetectionGenerator(SensorIndex=1, ...
            SensorLocation=[2.4 0], MaxRange=50, ...
            DetectorOutput="Lanes and objects", ...
            UpdateInterval=stepSize);

radarSensor = drivingRadarDataGenerator(SensorIndex=2,...
    MountingLocation=[1.8 0 0.2], FieldOfView=[80 5],...
    AzimuthResolution=1,UpdateRate=1/stepSize);

lidarSensor = lidarPointCloudGenerator(SensorIndex=3,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,{visionSensor,radarSensor,lidarSensor},egoVehicleID);

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

[visionDetPlotter,radarDetPlotter,pcPlotter,lbGTPlotter,lbDetPlotter,bepAxes] = helperSetupBEP(visionSensor,radarSensor);

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","Step");
pause(0.1)
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)
        
        % Get detections from vision and radar sensors
        [visionDets,numVisionDets,visionDetsValid,lbDets,numLbDets,lbDetsValid] = visionSensor(tgtPoses,gTruthLbs,simTime);
        [radarDets,numRadarDets,radarDetsValid] = radarSensor(tgtPoses,simTime);

        % Get point cloud from lidar sensor
        [ptCloud,ptCloudValid] = lidarSensor();

        % Plot ground-truth and detected lane boundaries
        helperPlotLaneBoundaries(lbGTPlotter,gTruthLbs)
       
        
        % Plot vision and radar detections
        if visionDetsValid
            detPos = cellfun(@(d)d.Measurement(1:2),visionDets,UniformOutput=false);
            detPos = vertcat(zeros(0,2),cell2mat(detPos')');
            plotDetection(visionDetPlotter,detPos)
        end

        if lbDetsValid
            plotLaneBoundary(lbDetPlotter,vertcat(lbDets.LaneBoundaries))
        end

        if radarDetsValid
            detPos = cellfun(@(d)d.Measurement(1:2),radarDets,UniformOutput=false);
            detPos = vertcat(zeros(0,2),cell2mat(detPos')');
            plotDetection(radarDetPlotter,detPos)
        end

        % Plot lidar point cloud
        if ptCloudValid
            plotPointCloud(pcPlotter,ptCloud);
        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.

See Also

Objects

Functions

Related Topics