Traffic Signal Follower with RoadRunner Scenario
This example shows how to design a traffic signal observer in Simulink® and cosimulate it with RoadRunner Scenario. It then shows how to design and cosimulate a traffic signal follower that adjusts the ego vehicle speed based on traffic signal data.
Introduction
RoadRunner Scenario is an interactive editor that enables you to design scenarios for simulating and testing automated driving systems. You can place vehicles, define their paths and interactions in the scenario, and then simulate the scenario in the editor. RoadRunner Scenario supports in-editor playback for scenario visualization and connecting to other simulators, such as MATLAB® and Simulink, for cosimulation.
This example shows how to configure traffic signals in RoadRunner, read traffic signal data from RoadRunner Scenario using the MATLAB API, and cosimulate RoadRunner Scenario with Simulink to model the behavior of a traffic signal observer and a traffic signal follower. A traffic signal observer monitors the states of traffic signals, such as red, yellow, or green lights, without making any decisions or taking actions based on this data. Conversely, a traffic signal follower actively responds to these signals, adjusting its behavior accordingly, such as stopping at red lights or moving at green.
In this example, you:
Set Up Environment — Configure MATLAB to interact with RoadRunner Scenario.
Explore Scene and Scenario — Explore the scene and the scenario that define actions for the ego vehicle and other actors.
Explore Traffic Signal Observer Model — Explore the traffic signal observer model, which reads and visualizes traffic signal data.
Simulate Traffic Signal Observer — Simulate the traffic signal observer, and visualize the traffic signal data.
Explore Traffic Signal Follower Model — Explore the traffic signal follower model, which interprets traffic signal data and uses decision logic to regulate the ego vehicle speed.
Simulate Traffic Signal Follower — Simulate the traffic signal follower, visualize the traffic signal data, and observe how the ego vehicle reacts to the traffic signal follower.
Set Up Environment
This section shows how to set up the RoadRunner environment to cosimulate with RoadRunner Scenario.
Start the RoadRunner application interactively by using the roadrunnerSetup
function. When the function opens a dialog box, specify the RoadRunner Project Folder and RoadRunner Installation Folder locations.
rrApp = roadrunnerSetup;
The rrApp
RoadRunner object enables you to interact with RoadRunner from the MATLAB workspace. You can open the scenario and update scenario variables using this object. For more information on this object, see roadrunner
.
This example uses these files that you must add to the RoadRunner project.
FourWayJunctionWithSignal.rrscene
— Scene file that describes a four-way junction.scenario_01_TrafficSignalObserver.rrscenario
— RoadRunner Scenario file that describes actors and their trajectories in theFourWayJunctionWithSignal
scene. The scenario contains an ego vehicle observing a traffic signal from a nearby location.scenario_02_TrafficSignalFollower.rrscenario
— RoadRunner scenario file that describes actors and their trajectories in theFourWayJunctionWithSignal
scene. The scenario contains five target vehicles and one ego vehicle. The target vehicles obey the traffic signals by using theTraffic Signal
condition in the RoadRunner Scenario Logic editor. A Simulink model controls the ego vehicle adjusting the speed of the vehicle based on the traffic signal state.TrafficSignalObserver.rrbehavior.rrmeta
— Behavior file that associates the traffic signal observer behavior, implemented using Simulink, to the ego vehicle in the RoadRunner scenario.TrafficSignalFollower.rrbehavior.rrmeta
— Behavior file that associates the traffic signal follower behavior, implemented using Simulink, to the ego vehicle in the RoadRunner scenario.
Copy these files to the RoadRunner project.
copyfile("FourWayJunctionWithSignal.rrscene",fullfile(rrApp.status.Project.Filename,"Scenes/")); copyfile("scenario_01_TrafficSignalObserver.rrscenario",fullfile(rrApp.status.Project.Filename,"Scenarios/")) copyfile("scenario_02_TrafficSignalFollower.rrscenario",fullfile(rrApp.status.Project.Filename,"Scenarios/")) copyfile("TrafficSignalFollower.rrbehavior.rrmeta",fullfile(rrApp.status.Project.Filename,"Assets","Behaviors/")) copyfile("TrafficSignalObserver.rrbehavior.rrmeta",fullfile(rrApp.status.Project.Filename,"Assets","Behaviors/"))
Explore Scene and Scenario
This section explains the scene and scenario. It also shows how to configure traffic signals in RoadRunner for cosimulation with Simulink.
Open the scene.
openScene(rrApp,"FourWayJunctionWithSignal.rrscene");
The scene contains a four-way junction with traffic signals. In this scene the traffic signal at the junction has three phases: Left-Turn
, Straight
, and Straight-UnprotectedLeftTurn
. Each of these phases has a Go
, Attention
, and Stop
interval. The interval timing for the Left-Turn
phase consists of an 8-second Go
interval, a 2-second Attention
interval, and a 1-second Stop
interval. For the Straight
and Straight-UnprotectedLeftTurn
phases, the timing includes a 10-second Go
interval, a 3-second Attention
interval, and a 1-second Stop
interval. You can configure the phase sequence and the timing of the phase intervals using the Signal Tool (RoadRunner). RoadRunner provides tools to design realistic junctions with traffic signals that are ready to use in simulation environments. To learn more about these tools, see Junctions and Traffic Signals (RoadRunner).
Open the scenario_01_TrafficSignalObserver
scenario.
openScenario(rrApp,"scenario_01_TrafficSignalObserver");
The scenario contains an ego vehicle located a few meters from the junction, following a designated path. The speed of the ego vehicle is set to 0.1 m/s
, enabling it to move toward the junction very slowly. As it moves, the ego vehicle also observes the traffic signals.
To simulate traffic signals in RoadRunner Scenario, you must convert traffic signal heads at the junction of the scene to traffic signal actors. For more information on how to convert traffic signal heads to traffic signal actors, see Traffic Signal Tool (RoadRunner Scenario).
Open the scenario_02_TrafficSignalFollower
scenario.
openScenario(rrApp,"scenario_02_TrafficSignalFollower");
This scenario contains a blue ego vehicle and five target vehicles. Both the ego and target vehicles approach the signal at 8 m/s
. The target vehicles obey the traffic signal by using the Traffic Signal
and Distance To Actor
conditions. The behavior model attached to the ego vehicle enables it to follow the given path and adhere to the traffic signal.
Connect to the RoadRunner Scenario server for cosimulation by using the createSimulation
function, and enable data logging.
rrSim = createSimulation(rrApp);
set(rrSim,Logging="on")
Connection status: 1 Connected to RoadRunner Scenario server on localhost:54322, with client id {0ae32c94-952d-4007-aad9-009acb22a0e0}
rrSim
is a ScenarioSimulation
object. Use this object to set variables and to read scenario-related information. Set the simulation to run at a step size of 0.1
.
Ts = 0.1; set(rrSim,StepSize=Ts)
Explore Traffic Signal Observer Model
This example uses the TrafficSignalObserver
model to show how to read and visualize traffic signal data from RoadRunner Scenario. This model defines the actor behavior for the ego vehicle, enabling it to follow a custom path and observe traffic signal states.
Open the model.
open_system("TrafficSignalObserver.slx")
The model contains RoadRunner Scenario, RoadRunner Scenario Reader, and RoadRunner Scenario Writer blocks, which configure, read from, and write to RoadRunner Scenario, as well as these blocks:
All Traffic Signal Spec
— MATLAB System block of a System object™ that reads traffic signal static information, such as signal position, bulb state, and turn configurations from the RoadRunner scenario by using thegetAttribute
function of theActorSimulation
object and packs the signal information into a bus,BusTrafficSignalSpec
.All Traffic Signal Runtime
— MATLAB System block of a System object that reads the traffic signal runtime information, such as bulb state and time left from the RoadRunner scenario by using thegetAttribute
function of theActorSimulation
object and packs the signal information into a bus,BusTrafficSignalRuntime
.Path Following
— This subsystem enables the ego vehicle to follow the path specified in the RoadRunner Scenario. It takes the ego pose and the path as input and writes back the updated ego pose to RoadRunner Scenario.Visualization
— This subsystem visualizes the traffic signal and vehicle runtime information.
In addition to the RoadRunner Scenario block, the model contains these RoadRunner Scenario Reader blocks and a RoadRunner Scenario Writer block:
Path Action
— RoadRunner Scenario Reader block that reads the path of the ego vehicle.Ego Vehicle Runtime
— RoadRunner Scenario Reader block that reads the runtime information of ego vehicle.All Vehicle Runtime
— RoadRunner Scenario Reader block that reads the runtime information of all the vehicles.RoadRunner Scenario Writer — Writes the ego vehicle runtime information to RoadRunner Scenario.
Simulate Traffic Signal Observer
This example uses the helperSLTrafficSignalExampleSetup
helper function to load the scenario and initialize model parameters, such as vehicle specifications, actor IDs, and Simulink buses.
helperSLTrafficSignalExampleSetup(rrApp,rrSim,"scenarioFileName","scenario_01_TrafficSignalObserver")
Simulate the scenario, and visualize the traffic signal states and vehicle runtime information.
set(rrSim,SimulationCommand="Start") while strcmp(get(rrSim,"SimulationStatus"),"Running") pause(1) end
The visualization displays these plots and tables:
Traffic Signal and Vehicle Plot — Displays the positions of vehicles and the traffic signal states overlaid on a
roadrunnerHDMap
(RoadRunner). The plot displays traffic signal linked to the lane that contains the ego vehicle using a hexagon marker, and displays all the traffic signals using circular markers.All Traffic Signal Runtime — Displays the actor ID, traffic signal state, time left for the current state, and the turn configuration for each traffic signal head.
Traffic Phases and Intervals — Displays the phases and intervals of the traffic signal controller associated with the ego vehicle.
Ego Relevant Traffic Signal — Displays the actor ID, current state, time left for the current state, and turn configuration for the traffic signal head linked to the lane that contains the ego vehicle.
During the simulation, the ego vehicle approaches the junction very slowly while observing the traffic signal as it changes states. The observer model does not have any decision logic that enables it to interact with the traffic signals. The visualization displays all runtime information related to the traffic signal. The traffic signal head with actor ID 6, displayed in the plot as a hexagon marker, indicates the current state of the traffic signal linked to the ego vehicle lane.
Explore Traffic Signal Follower Model
This section introduces the traffic signal follower model, which shows how to use traffic signal data to apply decision logic that controls the speed of the ego vehicle, ensuring that it adheres to traffic signals.
Open the TrafficSignalFollower
model.
open_system("TrafficSignalFollower.slx")
The TrafficSignalFollower
model shows how to use traffic signal information to design a traffic signal follower. In addition to the blocks used in the TrafficSignalObserver
model, this model contain these blocks:
Ego Traffic Signal
— MATLAB System block of a System object that reads the traffic signal data relevant to the ego vehicle.Traffic Signal Follower
— MATLAB System block of a System object that processes the traffic signal information to adjust the speed of the ego vehicle by passing it to thePath Following
block.
Simulate Traffic Signal Follower
Initialize the TrafficSignalFollower
model using the helperSLTrafficSignalExampleSetup
function, and load the scenario_02_TrafficSignalFollower
scenario.
helperSLTrafficSignalExampleSetup(rrApp,rrSim,"scenarioFileName","scenario_02_TrafficSignalFollower");
Simulate the scenario, and visualize the traffic signal states and vehicle runtime information.
set(rrSim,SimulationCommand="Start") while strcmp(get(rrSim,"SimulationStatus"),"Running") pause(1) end
During the simulation, the ego vehicle follows the specified path at a set speed of 8 m/s
. The ego vehicle adjusts its speed based on the signal state read from the RoadRunner scenario. When the ego vehicle approaches the junction and the signal is red, the traffic signal follower reduces the speed of the ego vehicle, bringing it to a stop near the stop line. After the signal turns green, the traffic signal follower increases the speed of the ego vehicle, enabling it to cross the junction and continue following the specified path. All the target vehicles in the scenario scenario_02_TrafficSignalFollower
follow the traffic signals according to the states of their respective signals.
See Also
Blocks
Objects
Functions
roadrunnerSetup
|openScene
|openScenario
|open_system
(Simulink)
Tools
- Signal Tool (RoadRunner) | Traffic Signal Tool (RoadRunner Scenario)
Related Topics
- Traffic Light Negotiation
- Traffic Light Negotiation with Unreal Engine Visualization
- Traffic Light Negotiation Using Vehicle-to-Everything Communication
- Intersection Movement Assist Using Vehicle-to-Vehicle Communication
- Generate V2X MAP Message from RoadRunner
- Trajectory Follower with RoadRunner Scenario
- Speed Action Follower with RoadRunner Scenario
- Lateral Offset Follower with High-Fidelity Vehicle Dynamics
- Generate Scenario Variations Using gRPC API (RoadRunner Scenario)