Main Content

Simulate RoadRunner Scenarios with Traffic Signals in MATLAB

You can simulate a RoadRunner scenario containing traffic signal actors in MATLAB® to run and test automated driving workflows. For example, you can retrieve information about the current run-time state of a traffic signal, and accordingly slow down or halt a moving vehicle at a traffic signal junction. A traffic signal controller is an abstract entity that connects all traffic signal actors of a junction together and synchronizes their behavior. The traffic signal controller acts as a parent to all the connected traffic signal actors.

To simulate a scenario with traffic signal actors in MATLAB, you must first establish a connection between RoadRunner and MATLAB and then convert the traffic signal heads at the junction to actors (ActorSimulation objects) by clicking the Create Traffic Signal Actors button in the Traffic Signal Tool (RoadRunner Scenario).

This example simulates a RoadRunner Scenario containing traffic signal actors in MATLAB.

Prerequisites

The example assumes that:

  1. You have a RoadRunner license and the product is installed. For more information, see Install and Activate RoadRunner (RoadRunner).

  2. You have a RoadRunner Scenario license and the product is installed. For more information, see Get RoadRunner Updates and Upgrades (RoadRunner).

  3. You have created a RoadRunner project folder named Traffic_Signal. For more information, see RoadRunner Project and Scene System (RoadRunner).

  4. You have modified a scene file to place a traffic signal controller at a junction. For example, open the sample scene file FourWaySignal file from the Scenes folder of the Traffic_Signal project and signalize a traffic signal junction using the Four-way Protected Left option. For more information, see Signal Tool (RoadRunner).

  5. You have created a new scenario file using the modified scene file as base and converted traffic signals to actor objects. For example, create a scenario file named TrafficSignalJunction using the modified FourWaySignal scene file as base and convert traffic signals to ActorSimulation objects. For more information, see Traffic Signal Tool (RoadRunner Scenario).

Set Up MATLAB - RoadRunner Cosimulation Environment

  1. At the MATLAB Command Window, specify the path to your local RoadRunner installation folder. This code snippet uses the default installation path of the RoadRunner application on Windows®. You can change the path to match the location of your RoadRunner installation folder.

    RRInstallationFolder = "C:\Program Files\RoadRunner "...
      + matlabRelease.Release + "\bin\win64";
  2. Set the project location to the path of your RoadRunner project folder.

    rrProjectLocation = "C:\Traffic_Signal";
    
  3. Create a roadrunner object to launch the RoadRunner application and open the project at the specified project location.

    rrApp = roadrunner(rrProjectLocation,"InstallationFolder",...
      RRInstallationFolder);
  4. Open the scene file that contains a signalized junction.

    openScene(rrApp,"FourWaySignal");
  5. Open the scenario file built on the scene file with a signalized junction and containing traffic signal actors.

    openScenario(rrApp,"TrafficSignalJunction");
  6. Connect to the RoadRunner Scenario server to enable cosimulation.

    ss = rrApp.createSimulation();
  7. Start the scenario.

    set(ss,"SimulationCommand","Start");

Get a Traffic Signal Actor

  1. Get the list of ActorSimulation objects corresponding to all the actors in the scenario.

     actors = ss.get("ActorSimulation");
  2. Iterate over the list and get individual ActorSimulation objects.

    ts1 = actors{1};
    ts2 = actors{2};
    ts3 = actors{3};
    ts4 = actors{4};
  3. Check if the retrieved ActorSimulation objects are traffic signal actors by using the getAttribute function of the ActorSimulation object. If the returned type for an actor is TrafficSignal, then it is a traffic signal.

    ts3actortype_uint8 = getAttribute(ts3,"ActorType");
    ts3actortype = char(ts3actortype_uint8);

    If your selection is not a traffic signal actor, then select a different ActorSimulation object from the list.

  4. Get the ActorModel object corresponding to the ActorSimulation object that represents the selected traffic signal actor.

     am3 = get(ts3,"ActorModel")

Determine Number of Bulbs in the Traffic Signal

Determine the number of bulbs in traffic signal t3:

  1. Get the static attributes of a traffic signal by using the getAttribute function of its corresponding ActorModel object. The static attributes are returned in a structure of type TrafficSignalSpec.

    attr3 = getAttribute(am3,"TrafficSignalSpec");
  2. Get the BulbConfigurations structure that is part of TrafficSignalSpec, which details all possible bulb configurations.

    bc3 = attr3.BulbConfigurations;
    
  3. Get the BulbConfiguration structure within the BulbConfigurations structure. The dimensions of the resultant structure array indicate the number of bulbs of a traffic signal. For example, a 1-by-3 structure array corresponds to three bulbs, a 1-by-4 structure array corresponds to four bulbs, and so on.

    bulbconfig1 = bc3(1).BulbConfiguration

Determine Configuration Index of Traffic Signal with Green Bulb Switched On

Determine the configuration index assigned to a signal when its green bulb is switched on, by checking every bulb configuration.

You can check a bulb configuration by assigning the BulbName and BulbState field values of a BulbConfiguration structure to a descriptive variable. For example, the variable bulbconfig1_bulb2_state holds the bulb state of the second bulb, in the first configuration. In this way, for every configuration, you can retrieve the state of each bulb.

  1. Check the first bulb configuration:

    bulbconfig1_bulb1_namevector = bulbconfig1(1).BulbName
    bulbconfig1_bulb1_namevector = 1×11 uint8 row vector
       108   105   103   104   116    95   103   114   101   101   110
    
    bulbconfig1_bulb1_name = char(bulbconfig1_bulb1_namevector)
    bulbconfig1_bulb1_name = 'light_green'
    bulbconfig1_bulb1_state = bulbconfig1(1).BulbState
    bulbconfig1_bulb1_state = 
      EnumBulbState enumeration
    
        Off
    
    bulbconfig1_bulb2_namevector = bulbconfig1(2).BulbName
    bulbconfig1_bulb2_namevector = 1×9 uint8 row vector
       108   105   103   104   116    95   114   101   100
    
    bulbconfig1_bulb2_name = char(bulbconfig1_bulb2_namevector)
    bulbconfig1_bulb2_name = 'light_red'
    bulbconfig1_bulb2_state = bulbconfig1(2).BulbState
    bulbconfig1_bulb2_state = 
      EnumBulbState enumeration
    
        On
    
    bulbconfig1_bulb3_namevector = bulbconfig1(3).BulbName
    bulbconfig1_bulb3_namevector = 1×12 \...
      uint8 row vector
       108   105   103   104   116    95   121...  
       101   108   108   111   119
    
    bulbconfig1_bulb3_name = char(bulbconfig1_bulb3_namevector)
    bulbconfig1_bulb3_name = 'light_yellow'
    bulbconfig1_bulb3_state = bulbconfig1(3).BulbState
    bulbconfig1_bulb3_state = 
      EnumBulbState enumeration
    
        Off
    

  2. Check the second bulb configuration:

    bulbconfig2 = bc3(2).BulbConfiguration;
    bulbconfig2_bulb1_namevector = bulbconfig2(1).BulbName
    bulbconfig2_bulb1_namevector = 1×11 uint8 row vector
       108   105   103   104   116    95   103   114   101   101   110
    
    bulbconfig2_bulb1_name = char(bulbconfig2_bulb1_namevector)
    bulbconfig2_bulb1_name = 'light_green'
    bulbconfig2_bulb1_state = bulbconfig2(1).BulbState
    bulbconfig2_bulb1_state = 
      EnumBulbState enumeration
    
        Off
    
    bulbconfig2_bulb2_namevector = bulbconfig2(2).BulbName
    bulbconfig2_bulb2_namevector = 1×9 uint8 row vector
       108   105   103   104   116    95   114   101   100
    
    bulbconfig2_bulb2_name = char(bulbconfig2_bulb2_namevector)
    bulbconfig2_bulb2_name = 'light_red'
    bulbconfig2_bulb2_state = bulbconfig2(2).BulbState
    bbulbconfig2_bulb2_state = 
      EnumBulbState enumeration
    
        Off
    
    bulbconfig2_bulb3_namevector = bulbconfig2(3).BulbName
    bulbconfig2_bulb3_namevector = 1×12 uint8...
       row vector
       108   105   103   104   116    95   121   101...
        108   108   111   119
    
    bulbconfig2_bulb3_name = char(bulbconfig2_bulb3_namevector)
    bulbconfig2_bulb3_name = 'light_yellow'
    bulbconfig2_bulb3_state = bulbconfig2(3).BulbState
    bulbconfig2_bulb3_state = 
      EnumBulbState enumeration
    
        On
    

  3. Check the third bulb configuration:

    bulbconfig3 = bc3(3).BulbConfiguration;
    bulbconfig3_bulb1_namevector = bulbconfig3(1).BulbName
    bulbconfig3_bulb1_namevector = 1×11 uint8 row vector
       108   105   103   104   116    95   103   114   101   101   110
    
    bulbconfig3_bulb1_name = char(bulbconfig3_bulb1_namevector)
    bulbconfig3_bulb1_name = 'light_green'
    bulbconfig3_bulb1_state = bulbconfig3(1).BulbState
    bulbconfig3_bulb1_state = 
      EnumBulbState enumeration
    
        On
    
    bulbconfig3_bulb2_namevector = bulbconfig3(2).BulbName
    bulbconfig3_bulb2_namevector = 1×9 uint8 row vector
       108   105   103   104   116    95   114   101   100
    
    bulbconfig3_bulb2_name = char(bulbconfig3_bulb2_namevector)
    bulbconfig3_bulb2_name = 'light_red'
    bulbconfig3_bulb2_state = bulbconfig3(2).BulbState
    bulbconfig3_bulb2_state = 
      EnumBulbState enumeration
    
        Off
    
    bulbconfig3_bulb3_namevector = bulbconfig3(3).BulbName
    bulbconfig1_bulb3_namevector = 1×12...
       uint8 row vector
       108   105   103   104   116    95...  
       121   101   108   108   111   119
    
    bulbconfig3_bulb3_name = char(bulbconfig3_bulb3_namevector)
    bulbconfig3_bulb3_name = 'light_yellow'
    bulbconfig3_bulb3_state = bulbconfig3(3).BulbState
    bulbconfig3_bulb3_state = 
      EnumBulbState enumeration
    
        Off
    

  4. Check the fourth bulb configuration:

    bulbconfig4 = bc3(4).BulbConfiguration;
    bulbconfig4_bulb1_namevector = bulbconfig4(1).BulbName
    bulbconfig4_bulb1_namevector = 1×11 uint8 row vector
       108   105   103   104   116    95   103   114   101   101   110
    
    bulbconfig4_bulb1_name = char(bulbconfig4_bulb1_namevector)
    bulbconfig4_bulb1_name = 'light_green'
    bulbconfig4_bulb1_state = bulbconfig4(1).BulbState
    bulbconfig4_bulb1_state = 
      EnumBulbState enumeration
    
        Off
    
    bulbconfig4_bulb2_namevector = bulbconfig4(2).BulbName
    bulbconfig4_bulb2_namevector = 1×9 uint8 row vector
       108   105   103   104   116    95   114   101   100
    
    bulbconfig4_bulb2_name = char(bulbconfig4_bulb2_namevector)
    bulbconfig4_bulb2_name = 'light_red'
    bulbconfig4_bulb2_state = bulbconfig4(2).BulbState
    bulbconfig4_bulb2_state = 
      EnumBulbState enumeration
    
        On
    
    bulbconfig4_bulb3_namevector = bulbconfig4(3).BulbName
    bulbconfig4_bulb3_namevector = 1×12...
       uint8 row vector
       108   105   103   104   116    95...
         121   101   108   108   111   119
    
    bulbconfig4_bulb3_name = char(bulbconfig4_bulb3_namevector)
    bulbconfig4_bulb3_name = 'light_yellow'
    bulbconfig4_bulb3_state = bulbconfig4(3).BulbState
    bulbconfig4_bulb3_state = 
      EnumBulbState enumeration
    
        On
    

  5. Once you determine the configuration that represents a green light, get the corresponding configuration number. For instance, the outputs above indicate that the green bulb is switched on in the third configuration. The third configuration has the following values for each of its traffic signal bulbs:

    • light_greenOn

    • light_redOff

    • light_yellowOff

    Hence, get the configuration number for the third configuration:

    green_config_number = bc3(3).ConfigurationNumber
    green_config_number = uint32
      3

    This means that for the configuration number 3, the specified traffic signal is green. You may use the configuration number to get the corresponding turn configuration. The returned turn configuration lists the allowed turns at the junction, for the specified traffic signal, in this configuration.

See Also

Functions

Objects

Related Topics