Main Content

addSensors

Add sensors to specific host vehicle

Since R2023a

Description

addSensors(scenario,sensors,hostVehicleID) adds all the sensors to the vehicle with unique ID hostVehicleID, in the driving scenario scenario.

example

Examples

collapse all

In this example, you will add sensors to a driving scenario using the addSensors function and get ground-truth target poses based on the individual sensor inputs. Then, you process the ground-truth target poses into detections and visualize them.

Set Up Driving Scenario and Bird's-Eye-Plot

Create a driving scenario with an ego vehicle and two target vehicles. One target vehicle is in the front and the other is to the left of the ego-vehicle.

[scenario, egovehicle] = helperCreateDrivingScenario;

Configure a vision sensor to be mounted at the front of the ego vehicle.

visionSensor = visionDetectionGenerator(SensorIndex=1,SensorLocation=[4.0 0],Height=1.1,Pitch=1.0,DetectorOutput="Objects only");

Configure an ultrasonic sensor to be mounted at the left side of the ego-vehicle.

leftUltrasonic = ultrasonicDetectionGenerator(SensorIndex=2,MountingLocation=[0.5 1 0.2],MountingAngles=[90 0 0],FieldOfView=[70 35],UpdateRate=100);

Create a bird's-eye-plot to visualize the driving scenario.

[detPlotter,lmPlotter,olPlotter,lulrdPlotter,luldetPlotter,bepAxes] = helperCreateBEP(visionSensor,leftUltrasonic);

Figure BEP contains an axes object. The axes object with xlabel X (m), ylabel Y (m) contains 7 objects of type patch, line. One or more of the lines displays its values using only markers These objects represent Vision Coverage area, Ultrasonic Coverage area, Object detections, Lane markings, Left Ultrasonic Ranges, Point-On-Target (Left Ultrasonic).

Add Sensors and Simulate Driving Scenario

Add both vision and ultrasonic sensors to the driving scenario using the addSensors function. You can add sensors to any vehicle in the driving scenario using the addSensors function by specifying the actor ID of the desired vehicle. In this example, specify the ego-vehicle actor ID.

addSensors(scenario,{visionSensor,leftUltrasonic},egovehicle.ActorID);

Simulate the driving scenario. Note that you get separate target poses based on individual sensors by specifying their respective sensor IDs as inputs to the targetPoses function. This syntax returns the ground-truth poses of targets only within the range of the specified sensor. You then pass the ground-truth poses to their respective sensor models to generate detections and visualize them.

legend(bepAxes,'show')
lookaheadDistance = 0:0.5:60;
while advance(scenario)
    
    lb = laneBoundaries(egovehicle,'XDistance',lookaheadDistance,'LocationType','inner');
    [lmv,lmf] = laneMarkingVertices(egovehicle);

    % Get ground-truth poses of targets in the range of vision and ultrasonic sensors separately  
    tgtposeVision = targetPoses(scenario,visionSensor.SensorIndex);
    tgtposeUltrasonic = targetPoses(scenario,leftUltrasonic.SensorIndex);
    
    % Obtain detections based on targets only in range
    [obdets,obValid] = visionSensor(tgtposeVision,scenario.SimulationTime);
    [lobdets,lobValid] = leftUltrasonic(tgtposeUltrasonic,scenario.SimulationTime);

    helperPlotBEPVision(egovehicle,lmv,lmf,obdets,obValid,detPlotter,lmPlotter,olPlotter)
    helperPlotBEPUltrasonic(lobdets,lobValid,leftUltrasonic,lulrdPlotter,luldetPlotter)
end

Figure BEP contains an axes object. The axes object with xlabel X (m), ylabel Y (m) contains 7 objects of type patch, line. One or more of the lines displays its values using only markers These objects represent Vision Coverage area, Ultrasonic Coverage area, Object detections, Lane markings, Left Ultrasonic Ranges, Point-On-Target (Left Ultrasonic).

Helper Functions

helperCreateDrivingScenario creates a driving scenario by specifying the road and vehicle properties.

function [scenario, egovehicle] = helperCreateDrivingScenario
    scenario = drivingScenario;
    roadCenters = [-120 30 0;-60 0 0;0 0 0; 60 0 0; 120 30 0; 180 60 0];
    lspc = lanespec(3);
    road(scenario,roadCenters,Lanes=lspc);

    % Create an ego vehicle that travels in the center lane at a velocity of 30 m/s.
    egovehicle = vehicle(scenario,ClassID=1);
    egopath = [1.5 0 0; 60 0 0; 111 25 0];
    egospeed = 30;
    smoothTrajectory(egovehicle,egopath,egospeed);

    % Add a target vehicle that travels ahead of the ego vehicle at 30.5 m/s in the right lane, and changes lanes close to the ego vehicle.    
    ftargetcar = vehicle(scenario,ClassID=1);
    ftargetpath = [8 2; 60 -3.2; 120 33];
    ftargetspeed = 40;
    smoothTrajectory(ftargetcar,ftargetpath,ftargetspeed);

    % Add a second target vehicle that travels in the left lane at 32m/s.    
    ltargetcar = vehicle(scenario,ClassID=1);
    ltargetpath = [-5.0 3.5 0; 60 3.5 0; 111 28.5 0];
    ltargetspeed = 30;
    smoothTrajectory(ltargetcar,ltargetpath,ltargetspeed);
end

helperCreateBEP creates a bird's-eye-plot for visualing the driving scenario simulation.

function [detPlotter, lmPlotter, olPlotter, lulrdPlotter,luldetPlotter,bepAxes] = helperCreateBEP(visionSensor,leftUltrasonic)
    figureName = "BEP";
    Figure = findobj('Type','Figure',Name=figureName);
    if isempty(Figure)
        screenSize = double(get(groot,'ScreenSize'));
        Figure = figure(Name=figureName);
        Figure.Position = [screenSize(3)*0.17 screenSize(4)*0.15 screenSize(3)*0.4 screenSize(4)*0.6];
        Figure.NumberTitle = 'off';
        Figure.MenuBar = 'none';
        Figure.ToolBar = 'none';
    end
    clf(Figure);
    bepAxes = axes(Figure);
    grid(bepAxes,'on');
    legend(bepAxes,'hide');
    
    bep = birdsEyePlot(Parent=bepAxes,XLim=[-20 60],YLim=[-35 35]);
    caPlotterV = coverageAreaPlotter(bep,DisplayName="Vision Coverage area",FaceColor="b");
    caPlotterU = coverageAreaPlotter(bep,DisplayName="Ultrasonic Coverage area",FaceColor="m");
    detPlotter = detectionPlotter(bep,DisplayName="Object detections");
    lmPlotter = laneMarkingPlotter(bep,DisplayName="Lane markings");
    olPlotter = outlinePlotter(bep);
    plotCoverageArea(caPlotterV,visionSensor.SensorLocation,...
        visionSensor.MaxRange,visionSensor.Yaw, ...
        visionSensor.FieldOfView(1));
    plotCoverageArea(caPlotterU,leftUltrasonic.MountingLocation(1:2),...
        leftUltrasonic.DetectionRange(3),leftUltrasonic.MountingAngles(1), ...
        leftUltrasonic.FieldOfView(1));
    
    lulrdPlotter = rangeDetectionPlotter(bep,DisplayName="Left Ultrasonic Ranges",LineStyle="-");
    luldetPlotter = detectionPlotter(bep,DisplayName="Point-On-Target (Left Ultrasonic)",MarkerFaceColor="k");
end

helperPlotBEPVision plots ego vehicle outlines, lanes and vision detections on the bird's-eye-plot.

function helperPlotBEPVision(egovehicle,lmv,lmf,obdets,obValid,detPlotter, lmPlotter, olPlotter)
    plotLaneMarking(lmPlotter,lmv,lmf)
    
    [objposition,objyaw,objlength,objwidth,objoriginOffset,color] = targetOutlines(egovehicle);
    plotOutline(olPlotter,objposition,objyaw,objlength,objwidth, ...
        OriginOffset=objoriginOffset,Color=color)

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

end

helperPlotBEPUltrasonic plots ultrasonic range measurements and points on targets.

function helperPlotBEPUltrasonic(lobdets,lobValid,leftUltrasonic,lulrdPlotter,luldetPlotter)
    
    if ~isempty(lobdets) && lobValid
        lranges = lobdets{1}.Measurement;
        plotRangeDetection(lulrdPlotter,lranges,leftUltrasonic.FieldOfView(1),leftUltrasonic.MountingLocation,leftUltrasonic.MountingAngles)
        plotDetection(luldetPlotter,lobdets{1}.ObjectAttributes{1}.PointOnTarget(1:2)')
    end

end

In this example, you will add sensors to a driving scenario using the addSensors function. Then, you obtain sensor measurements without providing any ground-truth actor pose inputs and visualize them.

Set Up Driving Scenario and Bird's-Eye-Plot

Create a driving scenario with an ego vehicle and two target vehicles. One target vehicle is in the front and the other is to the left of the ego-vehicle.

[scenario, egovehicle] = helperCreateDrivingScenario;

Configure a lidar sensor to be mounted at the center of the roof's front edge of the ego vehicle.

lidarSensor = lidarPointCloudGenerator(SensorIndex=1,SensorLocation=[1.5 0],ActorProfiles=actorProfiles(scenario));

Create a bird's-eye-plot to visualize the driving scenario.

[pcPlotter, lmPlotter, olPlotter, bepAxes] = helperCreateBEP;

Figure BEP contains an axes object. The axes object with xlabel X (m), ylabel Y (m) contains 3 objects of type line, patch. One or more of the lines displays its values using only markers These objects represent Lidar Point Cloud, Lane markings.

Add Sensors and Simulate Driving Scenario

Add the lidar sensor to the driving scenario using the addSensors function. You can add sensors to any vehicle in the driving scenario using the addSensors function by specifying the actor ID of the desired vehicle. For this example, specify the ego-vehicle actor ID.

addSensors(scenario,lidarSensor,egovehicle.ActorID);

Simulate the driving scenario. Note that you are not providing any ground-truth actor pose inputs when you call lidarSensor object. The sensor automatically returns the point cloud based on the sensor parameters and actors within the sensor range.

legend(bepAxes,'show')
lookaheadDistance = 0:0.5:60;

while advance(scenario)
    
    lb = laneBoundaries(egovehicle,'XDistance',lookaheadDistance,'LocationType','inner');
    [lmv,lmf] = laneMarkingVertices(egovehicle);

    % Obtain lidar point cloud without any actor pose inputs
    [ptCloud,isValidTime] = lidarSensor();

    if isValidTime
        % Plot point cloud, vehicle outlines and lane markings
        plotPointCloud(pcPlotter,ptCloud);
        
        [objposition,objyaw,objlength,objwidth,objoriginOffset,color] = targetOutlines(egovehicle);
        plotOutline(olPlotter,objposition,objyaw,objlength,objwidth, ...
            OriginOffset=objoriginOffset,Color=color)

        plotLaneMarking(lmPlotter,lmv,lmf)
    end
end

Figure BEP contains an axes object. The axes object with xlabel X (m), ylabel Y (m) contains 3 objects of type line, patch. One or more of the lines displays its values using only markers These objects represent Lidar Point Cloud, Lane markings.

Helper Functions

helperCreateDrivingScenario creates a driving scenario by specifying the road and vehicle properties.

function [scenario, egovehicle] = helperCreateDrivingScenario
scenario = drivingScenario;
roadCenters = [-120 30 0;-60 0 0;0 0 0; 60 0 0; 120 30 0; 180 60 0];
lspc = lanespec(3);
road(scenario,roadCenters,Lanes=lspc);

% Create an ego vehicle that travels in the center lane at a velocity of 30 m/s.
egovehicle = vehicle(scenario,ClassID=1,Mesh=driving.scenario.carMesh);
egopath = [1.5 0 0; 60 0 0; 111 25 0];
egospeed = 30;
smoothTrajectory(egovehicle,egopath,egospeed);

% Add a target vehicle that travels ahead of the ego vehicle at 30.5 m/s in the right lane, and changes lanes close to the ego vehicle.    
ftargetcar = vehicle(scenario,ClassID=1,Mesh=driving.scenario.carMesh);
ftargetpath = [8 2; 60 -3.2; 120 33];
ftargetspeed = 40;
smoothTrajectory(ftargetcar,ftargetpath,ftargetspeed);

% Add a second target vehicle that travels in the left lane at 32m/s.    
ltargetcar = vehicle(scenario,ClassID=1,Mesh=driving.scenario.truckMesh);
ltargetpath = [-5.0 3.5 0; 60 3.5 0; 111 28.5 0];
ltargetspeed = 30;
smoothTrajectory(ltargetcar,ltargetpath,ltargetspeed);
end

helperCreateBEP creates a bird's-eye-plot for visualing the driving scenario simulation.

function [pcPlotter, lmPlotter, olPlotter,bepAxes] = helperCreateBEP()
figureName = "BEP";
Figure = findobj('Type','Figure',Name=figureName);
if isempty(Figure)
    screenSize = double(get(groot,'ScreenSize'));
    Figure = figure(Name=figureName);
    Figure.Position = [screenSize(3)*0.17 screenSize(4)*0.15 screenSize(3)*0.4 screenSize(4)*0.6];
    Figure.NumberTitle = 'off';
    Figure.MenuBar = 'none';
    Figure.ToolBar = 'none';
end
clf(Figure);
bepAxes = axes(Figure);
grid(bepAxes,'on');
legend(bepAxes,'hide');

bep = birdsEyePlot(Parent=bepAxes,XLim=[-20 60],YLim=[-35 35]);
pcPlotter = pointCloudPlotter(bep,DisplayName='Lidar Point Cloud');
lmPlotter = laneMarkingPlotter(bep,DisplayName="Lane markings");
olPlotter = outlinePlotter(bep);

end

Input Arguments

collapse all

Driving scenario, specified as a drivingScenario object.

Sensors to add to the driving scenario, specified as one of the following:

Each detection generator must have a unique sensor ID specified by its SensorIndex property.

Actor ID of the vehicle in the driving scenario, specified as a positive scalar. The function adds sensors to the vehicle corresponding to this ID in the scenario. You can access the actor ID using the ActorID property of the vehicle object.

hostVehicle = vehicle(scenario,ClassID=1);
hostVehicleID = vehicle.ActorID

Version History

Introduced in R2023a