Main Content

Design a Curved Road Programmatically using MATLAB Scene Authoring Functions

This example shows how to create a curved road with multiple segments programmatically using road layouts, lanes, lane boundaries, and lane markings.

To run this example, you must:

  • Have an Automated Driving Toolbox® license.

  • Have a RoadRunner® license and the product is installed.

  • Have created a RoadRunner project folder.

The steps to construct a curved road segment are:

  • Create a segmented road

  • Add lanes to the road

  • Add lane markings to the lanes

Start RoadRunner Programmatically

To use MATLAB® scene authoring functions to control RoadRunner programmatically, use the roadrunner object. By default, roadrunner opens RoadRunner from the default installation folder for the platform you are using (either Windows® or Linux®). These are the default installation locations by platform:

  • Windows – C:\Program Files\RoadRunner R20NNx\bin\win64

  • Linux, Ubuntu® – /usr/local/RoadRunner_R20NNx/bin/glnxa64

R20NNx is the MATLAB version for the release you are using.

If your RoadRunner installation is at a different location than the default location, use MATLAB settings API to customize the default value of the RoadRunner installation folder.

Access Scene Authoring APIs in MATLAB

Create a roadrunner object, specifying the path to an existing project. For example, this code shows the path to a project, on a Windows machine, located at "C:\RR\MyProject". This code assumes that RoadRunner is installed in the default location, and returns an object, rrApp, that provides functions for performing basic tasks such as opening, closing, and saving scenes and projects.

If you are opening RoadRunner from MATLAB for the first time, or if you have changed the RoadRunner installation location since you last opened it from MATLAB, you can use the roadrunnerSetup function to specify new default project and installation folders to use when opening RoadRunner. You can save these folders between MATLAB sessions by selecting the Across MATLAB sessions option from the corresponding drop down.

rrApp = roadrunner(ProjectFolder="C:\RR\MyProject"); 

Create a new scene in RoadRunner by using the newScene function, specifying the roadrunner object rrApp.

newScene(rrApp);

Create a RoadRunner authoring API object, rrAPI, that references the object for the current RoadRunner instance rrApp. The rrApi object enables you to programmatically author scenes, such as by adding and modifying road and lane components, using MATLAB.

rrApi = roadrunnerAPI(rrApp);

Extract the Scene object for your scene from the Scene property of the authoring API object rrApi. The extracted Scene object enables you to specify the scene in which to add scene components, such as roads and lanes.

scn = rrApi.Scene;

Extract the Project object for your RoadRunner project from the Project property of the authoring API object rrApi. The extracted Project object enables you to specify the project folder for the current RoadRunner session from which to retrieve asset objects. You can use the asset objects to assign markings to the lanes in your scene.

prj = rrApi.Project;

Create a Segmented Curved Road

Specify the control points of the starting point forward direction of a road as positions along the X- and the Y- axes of the RoadRunner local coordinate system. Then, use the addSegmentedRoad function to add a segmented curve type road to the scene.

startPoint = [-100 0];
startRoadDirection = [1 0];
rrRoad = addSegmentedRoad(scn,startPoint,startRoadDirection);

Extract the SegmentedRoadCurve object from the HorizontalCurve property of the road rrRoad. The extracted object enables you to add curved road segments such as lines, arcs, spirals, and parametric cubics to the horizontal curve of the road. Then, use the addLine function to create a line segment and add it to the horizontal curve.

segmentedCurve = rrRoad.HorizontalCurve;
addLine(segmentedCurve,20);

Use the addSpiral function to create a spiral segment and add it to the horizontal curve. Specify the length of the spiral segment as 50 m, the start curvature as -1/10 m-1, and the end curvature as 1/10 m-1.

addSpiral(segmentedCurve,50,-1/10,1/10);

Then, use the addArc function to create an arc segment and add it to the horizontal curve. Specify the length of the arc segment as 60 m and its curvature as -1/20 m-1.

addArc(segmentedCurve,60,-1/20);

Use the addSpiral function to create a spiral segment and add it to the horizontal curve. Specify the length of the spiral segment as 40 m, the start curvature as 1/20 m-1, and the end curvature as 1/15 m-1.

addSpiral(segmentedCurve,40,1/20,1/15);

Use the addLine function to create a line segment and add it to the horizontal curve.

addLine(segmentedCurve,10);

Add Lanes to the Road

Extract the reference lane of the road from the ReferenceLane property of the road object segmentedCurve. The reference lane defines the center lane, or reference line, of a road in a RoadRunner scene. This lane has no width and serves as the basis for positioning all other lanes, which RoadRunner arranges outward from the reference line.

refLane = rrRoad.ReferenceLane;

Use the extracted reference lane object refLane to add a driving lane on each side of the reference lane of the road using the addLaneToLeft and addLaneToRight functions. Then, use the LaneType and TravelDirection properties of the added lanes to specify the type and travel direction of each lane.

lane1 = addLaneToLeft(refLane);
lane1.LaneType = "Driving";
lane1.TravelDirection = "Forward";
lane2 = addLaneToRight(refLane);
lane2.LaneType = "Driving";
lane2.TravelDirection = "Backward";

Add Shoulder Lanes to the Road

Add one shoulder lane to the left of the first lane and another shoulder lane to the right of the second lane.

shoulderLane1 = addLaneToLeft(lane1);
shoulderLane1.LaneType = "Shoulder";
shoulderLane2 = addLaneToRight(lane2);
shoulderLane2.LaneType = "Shoulder";

Extract the width profile for the shoulder lane by specifying the WidthProfile property of the lanes. By specifying a width of 1.25 m at the start and end of the final node of the width profile, the width profile uniformly modifies the shoulder lanes to a width of 1.25 m.

shoulderLane1WidthProfile = shoulderLane1.WidthProfile;
shoulderLane1NodeEnd = shoulderLane1WidthProfile.Nodes(1);
shoulderLane1NodeEnd.EndWidth = 1.25;
shoulderLane1NodeStart = shoulderLane1WidthProfile.Nodes(2);
shoulderLane1NodeStart.StartWidth = 1.25;
shoulderLane2WidthProfile = shoulderLane2.WidthProfile;
shoulderLane2NodeEnd = shoulderLane2WidthProfile.Nodes(1);
shoulderLane2NodeEnd.EndWidth = 1.25;
shoulderLane2NodeStart = shoulderLane2WidthProfile.Nodes(2);
shoulderLane2NodeStart.StartWidth = 1.25;

Add Lane Markings to the Roads and Lanes

Use the getAsset function to extract a lane marking style object, which represents the DashedSingleWhite.rrlms asset, from the project prj. To define the marking profile of the reference lane, first extract the lane marking profile object refLaneMarkingProfile of the reference lane object refLane. Then, extract the lane marking span object refLaneSpan, which represents the span on which to place the lane marking, from the Spans property of the lane marking profile object refLaneMarkingProfile. Lastly, set the LaneMarkingStyle property of the extracted span object to mark the reference lane with the dashed single white marking style.

dashedWhiteMarkingStyle = getAsset(prj,"<PROJECT>/Assets/Markings/DashedSingleWhite.rrlms","LaneMarkingStyle");
refLaneMarkingProfile = refLane.LaneMarkingProfile;
refLaneSpan = refLaneMarkingProfile.Spans(1);
refLaneSpan.LaneMarkingStyle = dashedWhiteMarkingStyle;

Extract the solid single white marking style object dashedWhiteMarkingStyle, and mark the boundaries of the driving lanes.

solidWhiteMarkingStyle = getAsset(prj,"<PROJECT>/Assets/Markings/SolidSingleWhite.rrlms","LaneMarkingStyle");
lane1MarkingProfile = lane1.LaneMarkingProfile;
lane1Span = lane1MarkingProfile.Spans;
lane1Span.LaneMarkingStyle = solidWhiteMarkingStyle;
lane2MarkingProfile = lane2.LaneMarkingProfile;
lane2Span = lane2MarkingProfile.Spans;
lane2Span.LaneMarkingStyle = solidWhiteMarkingStyle;

See Also

| | |