Main Content

Add Lidar Sensor Model with Simulated Weather Effects to RoadRunner Scenario

Since R2024a

This example shows how to integrate a lidar sensor model into RoadRunner Scenario, generate lidar point cloud data in foggy weather conditions, and export lidar sensor simulation data to a rosbag file.

Introduction

The lidarSensor enables you to simulate high-fidelity point cloud data. You can configure the lidar sensor model to simulate scan patterns of popular sensors, such as Velodyne® HDL32E and Ouster® OS0-128. You can also simulate effects of motion distortion as well as weather conditions.

In this example, you configure the lidar sensor model in MATLAB® to simulate scan pattern of Velodyne HDL32E sensor and add foggy weather effects to point cloud data. Then, you add the lidar sensor to the vehicle actors in RoadRunner Scenario, which is an interactive editor that enables you to design scenarios for simulating and testing automated driving systems. You can obtain generated point cloud data from RoadRunner Scenario and visualize it in MATLAB. You can also export point cloud data to a rosbag file.

Set Up RoadRunner Environment

Set up an environment for cosimulating a RoadRunner scenario with MATLAB.

Specify the path to your local RoadRunner installation folder. This code shows the path for the default installation location on Windows®.

rrInstallationPath = "C:\Program Files\RoadRunner R2023b\bin\win64";

Specify the path to your RoadRunner project. This code shows the path to a sample project folder on Windows.

rrProjectPath = "D:\Projects\RRProject";

Update the path for the RoadRunner installation folder by first getting the root object within the settings hierarchical tree. For more information, see SettingsGroup.

s = settings;
s.roadrunner.application.InstallationFolder.PersonalValue = rrInstallationPath;

Open RoadRunner using the specified path to your project.

rrApp = roadrunner(rrProjectPath);

The roadrunner object rrApp enables you to interact with RoadRunner from the MATLAB workspace. You can open a RoadRunner scenario using this object.

This example uses these files, which you must add to the RoadRunner project.

  1. SimpleRRScene.rrscene — RoadRunner scene file that describes a simple RoadRunner scene. This scene is based on the ScenarioBasic.rrscene scene included with RoadRunner. It contains a four-way intersection with traffic signals, a roundabout, and several roads of varying lengths and curve types.

  2. SimpleRRScenario.rrscenario — RoadRunner scenario file that specifies two actors and defines their trajectories.

Copy the RoadRunner scene and scenario files to the RoadRunner project. For more information about the RoadRunner environment, see RoadRunner Project and Scene System (RoadRunner).

copyfile("SimpleRRScene.rrscene",fullfile(rrProjectPath,"Scenes/"))
copyfile("SimpleRRScenario.rrscenario",fullfile(rrProjectPath,"Scenarios/"))

Open the SimpleRRScenario.rrscenario scenario.

openScenario(rrApp,"SimpleRRScenario")

The scenario contains two vehicles. The white lead car follows the lane-following built-in behavior, and the yellow ego car follows the predefined path assigned in RoadRunner.

Create a ScenarioSimulation (Automated Driving Toolbox) object to connect MATLAB to RoadRunner Scenario.

rrSim = createSimulation(rrApp);

Specify the step size for the RoadRunner scenario simulation. Units are in seconds.

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

Configure Lidar Sensor and Add to RoadRunner Scenario

Configure a lidar sensor model to add to the ego vehicle by using the lidarSensor object. To simulate a lidar scan pattern similar to a Velodyne HDL32E lidar sensor, specify the Model property as "HDL32E". Specify an azimuth resolution for the sensor of 0.3 degrees.

lidar = lidarSensor(Model="HDL32E",AzimuthResolution=0.3);

To simulate the effect of foggy weather on the point cloud data, modify the FogVisibility property of the lidarSensor object. The property specifies the visible distance in fog. Units are in meters. A higher value indicates better visibility and a lower fog impact. You must not specify a FogVisibility property value greater than 1000.

lidar.FogVisibility = 700;

To control the sensor configuration from the RoadRunner Scenario simulation object, retrieve a SensorSimulation (Automated Driving Toolbox) object. Then, use the addSensors (Automated Driving Toolbox) function of the object to attach the lidar sensor to the ego vehicle.

sensorSim = get(rrSim,"SensorSimulation");
egoVehicleId = 1;
addSensors(sensorSim,lidar,egoVehicleId);

Simulate Scenario and Export Lidar Point Cloud Data to Rosbag File

Run the RoadRunner Scenario simulation for 10 seconds. The code visualizes the 3-D point cloud data during the simulation and saves the simulated point clouds to a rosbag file.

set(rrSim,SimulationCommand="Start")
set(rrSim,SimulationCommand="Pause")
pause(0.5)
set(rrSim,SimulationCommand="Step")
pause(0.5)

% Create pcplayer object
player = pcplayer([-250 250],[-250 250],[-5 50],ColorSource="Intensity");

simTime = 0;

% Simulation stop time in seconds
stopTime = 10.0;

% Create rosbagwriter object
bagwriter = rosbagwriter("lidar_simulation.bag");

% Run the simulation for stopTime seconds or until RoadRunner simulation is stopped
while simTime<stopTime && ~isequal(get(rrSim,"SimulationStatus"),"Stopped")

    [ptCloud,isValid] = lidar(simTime);
    
    simTime = simTime + simStepSize;
    if(isValid)
        % Visualize generated point cloud
        view(player,ptCloud)
        
        % Convert pointCloud object to ROS PointCloud2 message format and
        % write to rosbag file
        ros_ptCloud2 = hCreateROSPointCloud2Message(ptCloud);
        if(~isempty(ros_ptCloud2))
            write(bagwriter,"/lidar",simTime,ros_ptCloud2)
        end
    end

    set(rrSim,SimulationCommand="Step")
end

Clear the bagwriter variable, stop the RoadRunner simulation, and close the RoadRunner instance.

clear bagwriter
set(rrSim,SimulationCommand="Stop")
close(rrApp)

Verify the created rosbag file by reading and visualizing point cloud data from it.

% Create pcplayer object
player = pcplayer([-250 250],[-250 250],[-5 50],ColorSource="Intensity");

% Create rosbagreader object and read messages
bagreader = rosbagreader("lidar_simulation.bag");
rosmsgs = readMessages(bagreader,DataFormat="struct");

% Loop through messages, convert to pointCloud type, and visualize data
for i = 1:numel(rosmsgs)
    
    % Read XYZ and Intensity fields from ROS PointCloud2 object and create pointCloud object
    xyz = rosReadXYZ(rosmsgs{i});
    intensities = rosReadField(rosmsgs{i},"intensity");
    ptCloud = pointCloud(xyz,Intensity=intensities);
    
    % Visualize point cloud data
    view(player,ptCloud)

    pause(0.1)
end

Figure Point Cloud Player contains an axes object. The axes object with xlabel X, ylabel Y contains an object of type scatter.

% Clear the bagreader variable
clear bagreader

See Also

Objects

Related Topics