Main Content

Translocate Collision from Seed Scenario to Target Scene

This example shows how to translocate a collision from a RoadRunner seed scenario to a target scene.

Creating collisions in scenarios is useful for various automated driving test applications. However, creating a test scenario for each scene is not scalable, and is prone to human error. Unlike manual test scenario creation, this example explains how to translocate a collision from a seed scenario to a target scene using these steps.

In this example, you:

  • Set Up Environment — Configure MATLAB® settings to interact with RoadRunner Scenario.

  • Explore Seed Scenario — Explore the seed scenario using RoadRunner.

  • Extract Actor Properties from Seed Scenario — Extract ego and target actor properties from the seed scenario.

  • Extract Collision Information — Extract colliding ego and target actor waypoints and their travel distances.

  • Explore Target Scene and Fetch Locations to Translocate Collision — Explore the target scene and fetch filtered locations to translocate the collision.

  • Translocate Collision from Seed Scenario to Target Scene — Translocate the collision from the seed scenario to the target scene, and visualize the collision.

  • Import Waypoints into RoadRunner — Write translocated ego and target waypoints to CSV files and import them into RoadRunner.

Set Up Environment

This section shows how to set up the environment to cosimulate MATLAB with RoadRunner Scenario.

Ensure that no other session of RoadRunner is currently open.

if exist("rrApp","var")
    close(rrApp)
end

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

rrAppPath = "C:\Program Files\RoadRunner R2023a\bin\win64";

To update the MATLAB path for the RoadRunner installation folder, get the root object within the settings hierarchical tree, and update the TemporaryValue for the RoadRunner installation folder property.

For more information, see SettingsGroup.

s = settings;
s.roadrunner.application.InstallationFolder.TemporaryValue = rrAppPath;

Specify the path to your RoadRunner project. This code shows the path for a sample project folder location in Windows.

rrProjectPath = "C:\TestProject";

Open RoadRunner using the specified path to your project. The rrApp RoadRunner object enables you to interact with RoadRunner from the MATLAB workspace.

rrApp = roadrunner(rrProjectPath);

This example uses two files that you must add to the RoadRunner project.

seedScenario.rrscenario RoadRunner seed scenario, with a collision.

LargeFourWayIntersection Target scene, containing large four-way lanes, into which to translocate the collision.

Specify the filenames of the RoadRunner seed scenario and target scene.

seedScenario = "seedScenario.rrscenario";
targetScene = "LargeFourWayIntersection";

Copy the seedScenario file to the RoadRunner project. To learn more about the RoadRunner environment, see RoadRunner Project and Scene System (RoadRunner).

copyfile(seedScenario,fullfile(rrProjectPath,"Scenarios"))

Explore Seed Scenario

Open the seed scenario seedScenario.rrscenario.

openScenario(rrApp,seedScenario)

The scenario contains two vehicles. The red car is the ego vehicle and the blue car is the target vehicle. The ego vehicle has an initial speed of 0 m/s and accelerates to 16.2 m/s over a distance of 10 meters. The target vehicle has an initial of 0 m/s and accelerates to 15.4 m/s over a distance of 10 meters. Both vehicles travel on the path specified in the scenario, and they collide where their paths intersect.

Extract Actor Properties from Seed Scenario

Connect to the RoadRunner Scenario server from MATLAB for cosimulation using the createSimulation function.

rrSim = createSimulation(rrApp);

Extract properties from the seed scenario, and store these properties in a ScenarioDescriptor object, descriptor, by using the getScenarioDescriptor function.

scenario.SimulatorInstance = rrApp;
scenario.SimulationInstance = rrSim;
descriptor = getScenarioDescriptor(scenario, Simulator="RoadRunner");

Specify the colliding actor IDs as egoID and targetID.

egoID = 1;
targetID = 2;

Extract the properties for the actors specified by egoID and targetID by using the helperGetActorProfiles function.

egoData = helperGetActorProfiles(descriptor,egoID)
egoData = struct with fields:
          Time: [240×1 double]
     WayPoints: [240×3 double]
           Yaw: [240×1 double]
         Pitch: [240×1 double]
          Roll: [240×1 double]
      Velocity: [240×1 double]
    Dimensions: [1×1 struct]

targetData = helperGetActorProfiles(descriptor,targetID)
targetData = struct with fields:
          Time: [221×1 double]
     WayPoints: [221×3 double]
           Yaw: [221×1 double]
         Pitch: [221×1 double]
          Roll: [221×1 double]
      Velocity: [221×1 double]
    Dimensions: [1×1 struct]

Extract Collision Information

Get the collision information of the colliding actors by using the getCollisionData function.

c = getCollisionData(descriptor,Actor1ID=egoID,Actor2ID=targetID);

Extract the precollision actor waypoints for the ego and target vehicles by using the helperIntersectWaypoints function.

Note: This function assumes that the ego and target vehicle waypoints intersect.

[egoData.preCollisionWayPoints,targetData.preCollisionWayPoints] = helperIntersectWayPoints(c,egoData.WayPoints,targetData.WayPoints);

Compute the total travel distance of the ego and target actors by using the helperGetDistance function. This function calculates the travel distance of an actor based on the specified actor waypoints.

egoData.totalTravelDistance = helperGetDistance(egoData.WayPoints);
targetData.totalTravelDistance = helperGetDistance(targetData.WayPoints);

Compute the precollision travel distance of the ego and target actors by using the helperGetDistance function. Calculate the precollision travel distance using the extracted precollision actor waypoints.

egoData.preCollisionTravelDistance = helperGetDistance(egoData.preCollisionWayPoints);
targetData.preCollisionTravelDistance = helperGetDistance(targetData.preCollisionWayPoints);

Explore Target Scene and Fetch Locations to Translocate Collision

Get the RoadRunner HD Map of the seed scenario by specifying the roadrunner object as an input to the helperGetMap function.

sourceRRHDMap = helperGetMap(rrApp)
sourceRRHDMap = 
  roadrunnerHDMap with properties:

                Author: ""
          GeoReference: [0 0]
    GeographicBoundary: [2×3 double]
                 Lanes: [50×1 roadrunner.hdmap.Lane]
        LaneBoundaries: [79×1 roadrunner.hdmap.LaneBoundary]
            LaneGroups: [29×1 roadrunner.hdmap.LaneGroup]
          LaneMarkings: [1×1 roadrunner.hdmap.LaneMarking]
             Junctions: [1×1 roadrunner.hdmap.Junction]
          BarrierTypes: [0×1 roadrunner.hdmap.BarrierType]
              Barriers: [0×1 roadrunner.hdmap.Barrier]
             SignTypes: [1×1 roadrunner.hdmap.SignType]
                 Signs: [4×1 roadrunner.hdmap.Sign]
     StaticObjectTypes: [2×1 roadrunner.hdmap.StaticObjectType]
         StaticObjects: [6×1 roadrunner.hdmap.StaticObject]

The target scene LargeFourWayIntersection.rrscene contains numerous lanes on each side of the road intersecting at a junction at the center of the scene.

Get all intersecting lane locations from the target scene, based on the seed scenario, by using the helperGetLaneIntersections function. These intersecting lane locations are the potential locations to which to translocate the collision from the seed scenario.

[allLaneIntersections,targetRRHDMap] = helperGetLaneIntersections(rrProjectPath,seedScenario,targetScene);

Explore the target RoadRunner HD Map scene, which now contains lanes.

targetRRHDMap
targetRRHDMap = 
  roadrunnerHDMap with properties:

                Author: ""
          GeoReference: [0 0]
    GeographicBoundary: [2×3 double]
                 Lanes: [550×1 roadrunner.hdmap.Lane]
        LaneBoundaries: [710×1 roadrunner.hdmap.LaneBoundary]
            LaneGroups: [102×1 roadrunner.hdmap.LaneGroup]
          LaneMarkings: [4×1 roadrunner.hdmap.LaneMarking]
             Junctions: [2×1 roadrunner.hdmap.Junction]
          BarrierTypes: [2×1 roadrunner.hdmap.BarrierType]
              Barriers: [5×1 roadrunner.hdmap.Barrier]
             SignTypes: [14×1 roadrunner.hdmap.SignType]
                 Signs: [46×1 roadrunner.hdmap.Sign]
     StaticObjectTypes: [26×1 roadrunner.hdmap.StaticObjectType]
         StaticObjects: [701×1 roadrunner.hdmap.StaticObject]

Remove the intersecting lane locations where translocation is not viable by using the helperFilterintersection function.

filteredLaneIntersections = helperFilterIntersection(sourceRRHDMap,targetRRHDMap,allLaneIntersections,egoData,targetData);
disp(filteredLaneIntersections)
        X           Y       Lane1    Lane2    AssociatedJunction
    _________    _______    _____    _____    __________________

       7.2141    -10.697       2      157             1         
      0.23794    -10.228       2      167             1         
       3.7272     -10.45       2      184             1         
       243.41     171.74       3      132             2         
       7.8098    -4.9072       4      157             1         
       6.8915    -4.3148       4      173             1         
       3.9544    -1.9057       4      177             1         
       9.2719     7.1449     147      157             1         
      -12.727     9.0921     147      158             1         
       2.3035     7.7481     147      167             1         
      -16.218     9.3303     147      178             1         
       5.7861     7.4374     147      184             1         
       7.5548    -7.2119     149      157             1         
      0.58156    -6.7417     149      167             1         
      -17.049    -5.8496     149      178             1         
       4.0695    -6.9646     149      184             1         
      -13.011     4.9021     150      158             1         
      -4.4283    -9.8605     150      173             1         
      -14.005      5.678     150      174             1         
      -16.369     7.1864     150      178             1         
      -10.418     2.3508     150      426             1         
       7.4056    -8.6834     157      172             1         
       8.8584     3.6672     157      174             1         
       6.9208    -14.185     157      175             1         
       9.6607     10.625     157      550             1         
      -13.258     0.5114     158      166             1         
      -12.967      5.604     158      174             1         
      -13.855    -12.999     158      175             1         
      -13.234    0.97847     158      426             1         
      -12.455     12.578     158      550             1         
      -16.727     3.3889     165      166             1         
       -11.89     5.5233     165      174             1         
      -16.605     3.4322     165      178             1         
      -7.1255     -12.26     166      173             1         
      -8.0785    -8.5815     166      177             1         
      -13.573    0.83366     166      426             1         
       0.5859    -6.7016     167      173             1         
       1.8931     4.2733     167      174             1         
    -0.057722    -13.717     167      175             1         
       2.6851     11.226     167      550             1         
       2.9106    -5.6408     172      173             1         
       4.1095    -6.5981     172      184             1         
      -6.8792     12.123     172      550             1         
       4.2704    -5.1221     173      184             1         
      -16.459      5.841     174      178             1         
       5.3741     3.9606     174      184             1         
      -17.352    -12.843     175      178             1         
        3.432    -13.939     175      184             1         
      -15.947     12.818     178      550             1         
       6.1703     10.917     184      550             1         

Visualize the target scene by using the helperVisualizeLaneIntersection function. Specify the laneIntersectionIndex to overlay the lane intersection, to which you have selected to translocate the collision, onto the target scene. The visualized plot has marked selected lane intersection point with red colour and other lane intersection points are marked with blue colour.

helperVisualizeLaneIntersections(targetRRHDMap,filteredLaneIntersections)
laneIntersectionIndex = 1;
hold on
plot(filteredLaneIntersections.X(laneIntersectionIndex),filteredLaneIntersections.Y(laneIntersectionIndex),"r*")
hold off

Translocate Collision from Seed Scenario to Target Scene

Compute ego and target actor waypoints for the target scene by using the helperTranslocate function.

[newEgoWayPoints,newTargetWayPoints,targetRRHDMap] = helperTranslocate(targetRRHDMap,egoData,targetData,filteredLaneIntersections(laneIntersectionIndex,:));

Visualize the translocated collision for the target scene by using the helperVisualizeTranslocate function. The visualization contains the trajectory of the ego and target actors in the seed and target scenarios. The trajectory of the ego actor is represented red and the trajectory of the target actor is represented blue. The start waypoints of the trajectories are represented with green markers and the end waypoints are represented with red markers.

helperVisualizeTranslocate(sourceRRHDMap,egoData.WayPoints,targetData.WayPoints,targetRRHDMap,newEgoWayPoints,newTargetWayPoints)

Import Waypoints into RoadRunner

Write the translocated ego and target waypoints to CSV files to use to import them into RoadRunner.

writetable(newEgoWayPoints,"ego.csv")
writetable(newTargetWayPoints,"target.csv")

Open RoadRunner using the specified path to your project.

rrApp = roadrunner(rrProjectPath);

Open the target scene and create a new scenario.

openScene(rrApp,targetScene)
newScenario(rrApp)

Import the waypoints you saved as CSV files into RoadRunner by using the importScenario function.

importScenario(rrApp,fullfile(pwd,"ego.csv"),"CSV Trajectory")
importScenario(rrApp,fullfile(pwd,"target.csv"),"CSV Trajectory")

Further Exploration

In this example, you have explored translocating a collision from a seed scenario to the target scene LargeFourWayIntersection. You can also select the following target scenes for translocation by updating the targetScene variable.

  • DownTownIntersection — Contains urban road intersections.

You can also add speed variations to the ego vehicle in the seed scenario and perform translocation.To add speed variations, first specify a new speed for the ego vehicle. For example:

newEgoSpeed = 25; % Units are in meters per second.

Then compute the wait times of the ego and target actors required to preserve their collision by using the helperComputeWaitTime function.

[egoData.WaitTime,targetData.WaitTime] = helperComputeWaitTime(egoData,targetData,newEgoSpeed);

[newEgoWayPoints,newTargetWayPoints,targetRRHDMap] = helperTranslocate(targetRRHDMap,egoData,targetData,filteredLaneIntersections(laneIntersectionIndex,:),newEgoSpeed);

Finally, repeat the steps outlined in the Import Waypoints into RoadRunner section.

Helper Function

Creates a RoadRunner HD Map for a scene that contains a RoadRunner scenario.

function map = helperGetMap(rrApp)
% This function is used to get the RoadRunner HD Map of a
% RoadRunner Scenario.
rrSim = createSimulation(rrApp);
rrRawMap = rrSim.getMap;
map = matlabshared.roadrunner.createHDMap(rrRawMap.header,rrRawMap.map);
close(rrApp);
end

See Also

Functions

Related Topics