8.14 Traffic lights

8.14.1 Overview

This chapter explains the implementation of traffic lights and how it enables the manipulation of traffic signals in ASAM OpenSCENARIO. The concept allows for the management of traffic lights within the map file, using semantic states and or bulb states.

8.14.2 Traffic light bulbs

ASAM OpenSCENARIO provides enumeration types for icon shape, color, and state to manage and define traffic light bulbs. The structure traffic_light_bulb encapsulates the properties of an individual traffic light bulb.

8.14.2.1 Enum bulb_icon

Available icons for traffic lights. Note that this enum only describes the icons, with the actual colors being irrelevant. For visual representations of the icons, refer to the OpenDRIVE traffic signal code number and table found in OpenDRIVE traffic signal files. Images of the icons can be found at usepastel.com/link/omxxxgy3/#/opendrive-group/opendrive-antora-gen/ASAM_OpenDRIVE_Signal_reference/latest/signal-catalog/01_road_signals/road_signals.html.

Basic information
Table 299. Basic information of enum bulb_icon

Instantiable

yes

Values
Table 300. Enum bulb_icon
Value Comment

unknown

Unknown icon

circle

OpenDRIVE Type 1000020, 1000001, 1000008, 1000012

pedestrian_walking

OpenDRIVE Type 1000002-20 (a walking pedestrian)

pedestrian_standing

OpenDRIVE Type 1000002-10 (a pedestrian in standing position)

tram

OpenDRIVE Type 1000014 (tram)

bus

OpenDRIVE Type 1000015 (bus)

bicycle

OpenDRIVE Type 1000013-20 (bicycle)

horse_rider

OpenDRIVE Type 1000015 (a person riding a horse)

person_bicycle

OpenDRIVE Type 1000015 (a person riding a bicylce)

bicycle_left

OpenDRIVE Type 1000013-50 (a bicycle as seen driving to the left)

bicycle_right

OpenDRIVE Type 1000013-80 (a bicycle as seen driving to the right)

arrow_left

OpenDRIVE Type 1000020-10 (an arrow pointing left)

arrow_right

OpenDRIVE Type 1000020-20 (an arrow pointing right)

arrow_straight

OpenDRIVE Type 1000020-30 (an arrow pointing straight ahead)

arrow_left_straight

OpenDRIVE Type 1000020-40

arrow_right_straight

OpenDRIVE Type 1000020-50

arrow_diagonal_left

OpenDRIVE Type 1000020-60

arrow_diagonal_right

OpenDRIVE Type 1000020-70

arrow_u_turn_left

OpenDRIVE Type 1000008-80 (an arrow indicating a u-turn to the left)

arrow_u_turn_right

OpenDRIVE Type 1000008-90 (an arrow indicating a u-turn to the left)

arrow_left_right

OpenDRIVE Type 1000012-100

lane_arrow_down

OpenDRIVE Type 1000021

lane_arrow_down_right

OpenDRIVE Type 1000022-10

lane_arrow_down_left

OpenDRIVE Type 1000022-20

lane_cross

OpenDRIVE Type 1000022-23

txt_walk

A textual description meaning "walk"

txt_dont_walk

A textual description meaning "do not walk"

countdown

Numbers for counting down

pt_horizontal_bar

Public transportation OpenDRIVE Type F-0

pt_vertical_bar

Public transportation OpenDRIVE Type F-1

pt_slash_bar

Public transportation OpenDRIVE Type F-2

pt_backslash_bar

Public transportation OpenDRIVE Type F-3

pt_small_circle

Public transportation OpenDRIVE Type F-4

pt_triangle

Public transportation OpenDRIVE Type F-5

switch_x

Public transportation OpenDRIVE Type W-0

switch_v_flipped

Public transportation OpenDRIVE Type W-1

switch_v_left

Public transportation OpenDRIVE Type W-2

switch_v_right

Public transportation OpenDRIVE Type W-3

switch_t

Public transportation OpenDRIVE Type A-1

switch_a

Public transportation OpenDRIVE Type A-X

switch_bar_v

Public transportation OpenDRIVE Type W-14

switch_bar_v_flipped

Public transportation OpenDRIVE Type W-11

switch_bar_v_right

Public transportation OpenDRIVE Type W-12

switch_bar_v_left

Public transportation OpenDRIVE Type W-13

switch_dotted_circle

Public transportation OpenDRIVE Type A-2B

8.14.2.2 Enum bulb_color

The color of a traffic light bulb.

Basic information
Table 301. Basic information of enum bulb_color

Instantiable

yes

Values
Table 302. Enum bulb_color
Value Comment

unknown

Unknown color

red

Red color

yellow

Yellow color

green

Green color

blue

Blue color

white

White color

8.14.2.3 Enum bulb_state

The state of a traffic light bulb.

Basic information
Table 303. Basic information of enum bulb_state

Instantiable

yes

Values
Table 304. Enum bulb_state
Value Comment

unknown

Unknown state

is_off

Bulb is off

is_on

Bulb is on

is_flashing

Bulb is flashing

8.14.2.4 Struct traffic_light_bulb

A traffic light bulb.

Basic information
Table 305. Basic information of struct traffic_light_bulb

Instantiable

yes

Parameters
Table 306. Struct traffic_light_bulb
Parameter Type Mandatory Description

map_id

string

no

Unique identifier of the traffic light bulb in the context of the map actor

icon

bulb_icon

yes

The type of traffic light bulb icon

color

bulb_color

yes

The color of the traffic light bulb

icon_positive

bool

yes

True if icon is illuminated (and background is dark), false if icon is dark (and background is illuminated).

State variables
Table 307. State variables of struct traffic_light_bulb
Variable Type Mandatory Description

state

bulb_state

yes

State of the traffic light bulb

Example of icon_positive==false (left) and icon_positive==true (right)
Figure 26. Example of icon_positive==false (left) and icon_positive==true (right)

8.14.3 Semantic states

The semantic state of a signal belongs to the rules governing traffic behavior in relation to the signal at a specific point in time. For instance, a signal might be in a semantic stop state, indicating that compliant traffic must not cross a designated stop line associated with the signal. Alternatively, it could be in a semantic caution state, allowing compliant traffic to proceed past the signal but requiring it to yield to other traffic and pedestrians.

8.14.3.1 Enum semantic_traffic_light_state

Rules imposed on traffic obeying the signal at a given point in time.

Basic information
Table 308. Basic information of enum semantic_traffic_light_state

Instantiable

yes

Values
Table 309. Enum semantic_traffic_light_state
Value Comment

off

Traffic light is turned off

stop

Stop motion

attention

Traffic signal is going to change to go

caution

Obeying traffic may pass the signal but must yield to other traffic

stop_attention

Traffic signal is going to switch to stop

go

Obeying traffic is allowed to drive

go_exclusive

Obeying traffic has exclusive rights to pass. Other crossing traffic members have semantic state stop.

non_functional

Non-functional traffic light. Can be used when a traffic light is in an unrecognizable state (for example, when red and green lights are on together).

The mapping between semantic and physical (bulb) states can vary, especially across regional standards. The traffic_light struct uses the semantic_state_to_state and state_to_semantic_state methods to enable conversion between these representations. The specific conversion mapping can be defined in a separate file, such as 'xxx_regional_settings.osc'.

8.14.4 Traffic light

The traffic_light struct represents a singular traffic light box, such as one with red, yellow, and green bulbs. The order of the bulbs reflects the arrangement of the traffic light. For vertically oriented traffic light boxes the order is from top to bottom. For horizontally oriented traffic light boxes the order is from left to right.

8.14.4.1 Struct traffic_light

A single traffic light box.

Basic information
Table 310. Basic information of struct traffic_light

Instantiable

yes

Parameters
Table 311. Struct traffic_light
Parameter Type Mandatory Description

map_id

string

no

Unique identifier of the traffic light box in the context of the map actor

bulbs

list of traffic_light_bulb

yes

List of traffic light bulbs that are contained in the traffic light box

pose

pose_3d

yes

The physical position and orientation of the traffic light box

height

length

yes

Height of the traffic light box

width

length

yes

Width of the traffic light box

group

traffic_light_group

yes

The traffic light group the traffic light box belongs to

8.14.4.1.1 Methods

Method state_equal()

Checks whether the current bulb states of the traffic light bulbs of this traffic light are equal to the bulb states of the traffic light bulbs in bulbs.

Prototype
extend traffic_light:
    def state_equal(bulbs: list of traffic_light_bulb)-> bool
Return value

True if the current traffic light bulb states of the traffic light are equal to the bulb states of the bulbs in bulbs, otherwise false.

Parameters
Table 312. Parameter for method state_equal()
Parameter Type Description

bulbs

list of traffic_light_bulb

List of traffic light bulbs to be compared to the bulbs of the current traffic light in regard to their state.

Method semantic_state_to_state()

Converts the semantic state to a list of traffic light bulbs with the correct bulb icons, colors and states.

Prototype
extend traffic_light:
    def semantic_state_to_state(state: semantic_traffic_light_state)-> list of traffic_light_bulb
Return value

A list of elements of type traffic_light_bulb, ordered according to the position of the bulbs in the traffic light.

Parameters
Table 313. Parameter for method semantic_state_to_state()
Parameter Type Description

state

semantic_traffic_light_state

state of the traffic light in semantic representation

Method state_to_semantic_state()

Converts a list of traffic light bulbs to a semantic state of a traffic light by evaluating their icons, colors, and states.

Prototype
extend traffic_light:
    def state_to_semantic_state(bulbs: list of traffic_light_bulb)-> semantic_traffic_light_state
Return value

The semantic state of the traffic light.

Parameters
Table 314. Parameter for method state_state_to_semantic_stateequal()
Parameter Type Description

bulbs

list of traffic_light_bulb

A list of traffic light bulbs, ordered according to the position of the bulbs in the traffic light.

The struct traffic_light_group describes a set of traffic light boxes that control the same element of the road. This can either be a single traffic light, or multiple traffic lights that duplicate each other. An example are multiple traffic light boxes facing the same segment of the road, but hanging at different positions. All traffic lights in the traffic light group must have exactly the same state at any given moment of time, unless there is a malfunction or one of the bulbs is burned.

8.14.4.2 Struct traffic_light_group

A set of traffic light boxes that control the same incoming lanes.

Basic information
Table 315. Basic information of struct traffic_light_group

Instantiable

yes

Parameters
Table 316. Struct traffic_light_group
Parameter Type Mandatory Description

map_id

string

no

Unique identifier of the traffic light group in the context of the map actor

bulbs

list of traffic_light_bulb

yes

Union of all traffic light bulbs that are contained in all traffic light boxes of the group

traffic_lights

list of traffic_light

yes

List of traffic lights in the traffic light group

cycle

traffic_light_cycle

yes

Activation_cycle may be null if not specified by the provided control plan (see map.set_traffic_lights_control_file)

8.14.4.2.1 Methods

Method state_equal()

Checks whether the current bulb states of the traffic light bulbs of this traffic light group are equal to the bulb states of the traffic light bulbs in bulbs.

Prototype
extend traffic_light:
    def state_equal(bulbs: list of traffic_light_bulb)-> bool
Return value

True if the current traffic light bulb states of the traffic light group are equal to the bulb states of the bulbs in bulbs, otherwise false.

Parameters
Table 317. Parameter for method state_equal()
Parameter Type Description

bulbs

list of traffic_light_bulb

List of traffic light bulbs to be compared to the bulbs of the current traffic light group in regard to their state.

8.14.5 Stop-line

A stop line, whether imaginary or real, indicates the position at which traffic is required to halt when the traffic light is in the semantic stop state. The marking of the stop line is defined by the following enumeration type.

8.14.5.1 Enum stop_line_marking

Type of imaginary or real line that describes the position where the traffic shall stop if the traffic light is in the semantic state stop.

Basic information
Table 318. Basic information of enum stop_line_marking

Instantiable

yes

Values
Table 319. Enum stop_line_marking
Value Comment

none

No line drawn on the ground.

solid

Stop line is a solid line.

broken

Stop line is a broken (dashed) line.

The following structure represents the stop_line of a traffic_light_group.

8.14.5.2 Struct traffic_light_stop_line

The stop_line of a traffic_light_group.

Basic information
Table 320. Basic information of struct traffic_light_stop_line

Instantiable

yes

Parents

route_element

Parameters
Table 321. Struct traffic_light_stop_line
Parameter Type Mandatory Description

traffic_light_group

traffic_light_group

yes

Group of traffic lights that is related to the stop line

route

route

yes

Route on which the stop line is located

rightmost_lane

uint

yes

The lane index for the rightmost relevant lane controlled by traffic_light_group

leftmost_lane

uint

yes

The lane index for the leftmost relevant lane controlled by the traffic_light_group

offset

length

yes

The position of the stop line in s-coordinates along the route

secondary_stop_offset

length

yes

The position of the secondary stop line in s-coordinates along the route. By default, only one stop line per traffic_light_group exists, and secondary_stop_offset == offset

primary_stop_line_marking

stop_line_marking

yes

Type of stop line marking

secondary_stop_line_marking

stop_line_marking

yes

Type of stop line marking for secondary stop line. By default, only one stop line per traffic_light_group exists, and secondary_stop_line_marking == primary_stop_line_marking

Inherited parameters and variables
Table 322. Inherited parameters and variables of struct traffic_light_stop_line
Parent Inherited parameters and variables

route

length, directionality, min_lanes, max_lanes, anchors

8.14.6 Traffic light phase

A phase of a traffic light is its state for a certain duration.

8.14.6.1 Struct traffic_light_phase

Definition of the states of a list of traffic light bulbs of a specific traffic light group for a defined time span.

Basic information
Table 323. Basic information of struct traffic_light_phase

Instantiable

yes

Parameters
Table 324. Struct traffic_light_phase
Parameter Type Mandatory Description

group

traffic_light_group

yes

Traffic light group that is addressed by the phase

bulbs_state

list of traffic_light_bulb

yes

List of traffic light bulbs that should be set for the specific group

duration

time

yes

Duration of the phase in seconds

A list of traffic_light_phase elements arranged in chronological order form the struct traffic_light_cycle.

8.14.6.2 Struct traffic_light_cycle

Different possible phases of type traffic_light_phase arranged in chronological order.

Basic information
Table 325. Basic information of struct traffic_light_cycle

Instantiable

yes

Parameters
Table 326. Struct traffic_light_cycle
Parameter Type Mandatory Description

phases

list of traffic_light_phase

yes

The different phases of the cycle in chronological order

synchronization_group_id

uint

yes

Identifier used to synchronize different traffic light groups. All traffic light groups with the same synchronization_group_id shall be synchronized, for example in a crossing: If traffic in one direction has a green light, the traffic in the perpendicular direction must have a red light

start_offset

time

yes

Temporal offset of the start of the cycle

8.14.7 Interaction with the map

The traffic lights are located in the map. The map itself is described by the map file.

extend map:
    traffic_light_groups: list of traffic_light_group
    traffic_light_control: list of traffic_light_cycle

Here, the traffic_light_groups is a list of all traffic_light_group defined in the map file. In case the map file or any other source contains information about the corresponding entities of traffic_light_cycle, these shall be kept in the traffic_light_control list. Note that traffic_light_control does not necessarily contain control plans for all traffic_light_groups and can also be empty.

The set_traffic_lights_control_file modifier is used to specify traffic light control cycles with an external file. See map modifier set_traffic_lights_control_file for details.

8.14.8 Actor for controlling traffic_lights

Traffic lights are controlled by an actor traffic_light_controller, which inherits from osc_actor.

8.14.8.1 Actor traffic_light_controller

Actor for controlling traffic lights and traffic light groups

Basic information
Table 327. Basic information of actor traffic_light_controller

Instantiable

yes

Parents

osc_actor

8.14.9 Actions for controlling traffic lights

8.14.9.1 Setting traffic light bulb states

The set_bulb_state action sets the state of a bulb of a single traffic light.

8.14.9.1.1 Action set_bulb_state

Set the state of a single traffic light bulb.

Basic information
Table 328. Basic information of action set_bulb_state

Action ending

The action is instantaneous.

Parameters
Table 329. Action set_bulb_state
Parameter Type Mandatory Description

traffic_light

traffic_light

no

The traffic light to which the bulb belongs. Some bulbs can be independent of traffic lights

bulb_color

bulb_color

yes

Color of the bulb

bulb_kind

bulb_icon

yes

Icon type of the bulb

bulb_state

bulb_state

yes

Bulb state to be set

sync

bool

yes

If true, bulb states of all traffic lights in the same synchronization group are set to a bulb state that is consistent according to map.traffic_light_control

This action is instantaneous, meaning that the state change is immediate and permanent unless another set action is executed. Here sync==true implies that all bulbs in the synchronization group will be set to a state that is consistent according to map.traffic_light_control cycles.

8.14.9.2. Setting traffic light states with a list of bulb_states

The set_state action sets the state of a single traffic light.

8.14.9.2.1 Action set_state

Set the state of a single traffic light.

Basic information
Table 330. Basic information of action set_state

Action ending

The action is instantaneous.

Parameters
Table 331. Action set_state
Parameter Type Mandatory Description

traffic_light

traffic_light

yes

The traffic light affected by the action

state

list of bulb_state

yes

List of bulb states to be set by the action

sync

bool

yes

If true, states of all traffic lights in the same synchronization group are set to a state that is consistent according to map.traffic_light_control

This action is instantaneous, meaning that the state change is immediate and permanent unless another set action is executed. Here sync==true implies that all bulbs in the synchronization group will be set to a state that is consistent according to map.traffic_light_control cycles.

8.14.9.3. Setting semantic_state of a traffic_light

The set_semantic_state action sets the semantic state of a single traffic light.

8.14.9.3.1 Action set_semantic_state

Set the semantic-state of a single traffic light. If the state is not applicable to the given traffic light, then its state is left unchanged.

Basic information
Table 332. Basic information of action set_semantic_state

Action ending

The action is instantaneous.

Parameters
Table 333. Action set_semantic_state
Parameter Type Mandatory Description

traffic_light

traffic_light

yes

The traffic light affected by the action

state

semantic_traffic_light_state

yes

Semantic state to be set by the action

sync

bool

yes

If true, states of all traffic lights in the same synchronization group are set to a state that is consistent according to map.traffic_light_control

This action is instantaneous, meaning that the state change is immediate and permanent unless another set action is executed. Here sync==true implies that all bulbs in the synchronization group will be set to a state that is consistent according to map.traffic_light_control cycles. If the state is not applicable to the traffic_light then its state will remain unchanged.

8.14.9.4. Setting state of a single bulb in a traffic_light_group

The set_group_bulb_state action sets the state of a bulb for all traffic lights of a traffic light group.

8.14.9.4.1 Action set_group_bulb_state

Set state of a single bulb for all traffic lights of a traffic light group.

Basic information
Table 334. Basic information of action set_group_bulb_state

Action ending

The action is instantaneous.

Parameters
Table 335. Action set_group_bulb_state
Parameter Type Mandatory Description

traffic_light

traffic_light

yes

The traffic light group affected by the action

bulb_color

bulb_color

yes

Color of the bulb

bulb_kind

bulb_icon

yes

Icon type of the bulb

bulb_state

bulb_state

yes

Bulb state to be set

sync

bool

yes

If true, states of all traffic lights in the same synchronization group are set to a state that is consistent according to map.traffic_light_control

This action is instantaneous, meaning that the state change is immediate and permanent unless another set action is executed. Here sync==true implies that all bulbs in the synchronization group will be set to a state that is consistent according to map.traffic_light_control cycles. If the state is not applicable to the traffic_light_group then its state will remain unchanged.

8.14.9.5. Setting state of a traffic_light_group with a list of bulb_states

The set_group_state action sets the state of all traffic lights of a traffic light group.

8.14.9.5.1 Action set_group_state

Set the state for all traffic lights in a traffic light group. If the state is not applicable to the given traffic light, then its state is left unchanged.

Basic information
Table 336. Basic information of action set_group_state

Action ending

The action is instantaneous.

Parameters
Table 337. Action set_group_state
Parameter Type Mandatory Description

group

traffic_light_group

yes

The traffic light group affected by the action

state

list of bulb_state

yes

List of bulb states to be set by the action

sync

bool

yes

If true, states of all traffic lights in the same synchronization group are set to a state that is consistent according to map.traffic_light_control

This action is instantaneous, meaning that the state change is immediate and permanent unless another set action is executed. Here sync==true implies that all bulbs in the synchronization group will be set to a state that is consistent according to map.traffic_light_control cycles. If the state is not applicable to the traffic_light_group then its state will remain unchanged.

8.14.9.6. Setting semantic state of a traffic_light_group

The set_group_semantic_state action sets the semantic state of all traffic lights of a traffic light group.

8.14.9.6.1 Action set_group_semantic_state

Set the semantic state for all traffic lights of a traffic light group.

Basic information
Table 338. Basic information of action set_group_semantic_state

Action ending

The action is instantaneous.

Parameters
Table 339. Action set_group_semantic_state
Parameter Type Mandatory Description

traffic_light_group

traffic_light_group

yes

The traffic light group affected by the action

state

semantic_traffic_light_state

yes

List of semantic states to be set by the action

sync

bool

yes

If true, states of all traffic lights in the same synchronization group are set to a state that is consistent according to map.traffic_light_control

This action is instantaneous, meaning that the state change is immediate and permanent unless another set action is executed. Here sync==true implies that all bulbs in the synchronization group will be set to a state that is consistent according to map.traffic_light_control cycles. If the state is not applicable to the traffic_light then its state will remain unchanged.

8.14.9.7 Setting activation cycles

The following action can be used to start the execution of traffic light cycles of any number of traffic_light_group.

8.14.9.7.1 Action play_cycles

This action receives a list of traffic light cycles to control the temporal sequence of the traffic light states of any number of traffic light groups. For each traffic light group the shall be only one cycle.

Basic information
Table 340. Basic information of action play_cycles

Action ending

The action ends when another action that affects the state of any traffic light that is controlled by the current cycles is invoked.

Parameters
Table 341. Action play_cycles
Parameter Type Mandatory Description

cycles

list of traffic_light_cycle

yes

list of traffic light cycles to be executed

The states of the traffic_lights in the groups will be changed according to the specified cycles.

8.14.10 Examples

8.14.10.1 Example 1

The following example shows how to control a traffic light by setting its bulb_states.

Code 74. Control a traffic light by its bulb states
scenario sut.traffic_light_bulb_state_control:
    # traffic light with 4 bulbs, 2 red, 1 yellow and 1 green.
    # This can be either 1 OpenDrive dynamic signal with with 4 bulbs in one line or 3 vertical and one on a side.
    # This can also be 4 separated OpenDrive traffic light signals with one bulb each.

    # define my_traffic_light, constrain it to be any traffic light on the map that has 4 bulbs,
    # with the first two being red, the third yellow and the fourth green
    my_traffic_light: traffic_light with:
        keep(it.bulbs.size() == 4)
        keep(it.bulbs[0].color == red)
        keep(it.bulbs[1].color == red)
        keep(it.bulbs[2].color == yellow)
        keep(it.bulbs[3].color == green)

    # define stop_line, constrain it to be the stop line of the traffic light group that my_traffic_light belongs to
    stop_line: traffic_light_stop_line with:
        keep(it.traffic_light_group == my_traffic_light.group)

    # define a traffic light controller
    my_traffic_light_controller: traffic_light_controller

    # define two times, yellow_time and red_time
    red_time: time with:
        keep(it in [5s..20s])

    yellow_time: time with:
        keep(it in [1s..3s])

    do parallel():
        # let sut travel the whole length of the road that stop_line is located on
        sut_drive: sut.car.drive() with:
            along(stop_line.road, start_offset: 0m, at: start)
            along(stop_line.road, end_offset: 0m, at: end)

        traffic_light_change: serial():
            # Start traffic light with the red bulb turned on
            set_red: my_traffic_light_controller.set_state(my_traffic_light, [is_on ,is_on, is_off, is_off])

            wait elapsed(red_time)

            # Turn off red bulb and turn on yellow bulb, then wait for 2s
            set_yellow: my_traffic_light_controller.set_state(my_traffic_light, [is_off, is_off, is_on, is_off])

            wait elapsed(yellow_time)

            # Turn off yellow bulb and turn on green bulb
            my_traffic_light_controller.set_state(my_traffic_light, [is_off, is_off, is_off, is_on])

8.14.10.2 Example 2

The following example shows how to control a traffic light by setting its semantic state.

Code 75. Control a traffic light by its semantic state
scenario sut.traffic_light_semantic_state_control:
    # define my_traffic_light, constrain it to be any traffic light on the map that has at least 3 bulbs
    my_traffic_light: traffic_light with:
        keep(it.bulbs.size() >= 3)

    # define stop_line, constrain it to be the stop line of the traffic light group that my_traffic_light belongs to
    stop_line: traffic_light_stop_line with:
        keep(it.traffic_light_group == my_traffic_light.group)

    # define a traffic light controller
    my_traffic_light_controller: traffic_light_controller

    # define a time stop_time
    stop_time: time with:
        keep(it in [5s..20s])

    do parallel():
        # let sut travel the whole length of the road that stop_line is located on
        sut_drive: sut.car.drive() with:
            along(stop_line.road, start_offset: 0m, at: start)
            along(stop_line.road, end_offset: 0m, at: end)

        traffic_light_change: serial():
            # Start traffic light in semantic state stop
            set_stop: my_traffic_light_controller.set_semantic_state(my_traffic_light, stop)

            wait elapsed(stop_time)

            # Change traffic_light to semantic state go_exclusive
            set_go_exclusive: my_traffic_light_controller.set_semantic_state(my_traffic_light, go_exclusive)

8.14.10.3 Example 3

This example demonstrates event-based triggering of traffic light states.

Code 76. Trigger traffic light states based on events
scenario sut.go_to_stop_by_event:
    # define my_traffic_light to be any traffic light on the map
    my_traffic_light: traffic_light

    # define stop_line, constrain it to be the stop line of the traffic light group that my_traffic_light belongs to
    stop_line: traffic_light_stop_line with:
        keep(it.traffic_light_group == my_traffic_light.group)

    # define a traffic light controller
    my_traffic_light_controller: traffic_light_controller

    # trigger event 'sut_at_my_traffic_light' when sut distance to stop_line is 10 m and semantic state of my_traffic_light is still go
    event sut_at_my_traffic_light is (sut.car.distance_along_route(stop_line.road, from: from_start) > (stop_line.offset - 10m)) and my_traffic_light.state_equal(my_traffic_light.semantic_state_to_state(go))

    do parallel():
        # let sut travel the whole length of the road that stop_line is located on
        sut_drive: sut.car.drive() with:
            along(stop_line.road, start_offset: 0m, at: start)
            along(stop_line.road, end_offset: 0m, at: end)

        traffic_light_change: serial():
            # Start traffic light in semantic state go
            set_go: my_traffic_light_controller.set_semantic_state(my_traffic_light, go)

            wait @sut_at_my_traffic_light

            # Change traffic_light to semantic state go_exclusive
            set_stop: my_traffic_light_controller.set_semantic_state(my_traffic_light, stop)

8.14.10.4 Example 4

This example shows how to let the traffic light controller play the traffic light control plan defined by the map.

Code 77. Execute the traffic light control plan of a map
scenario sut.drive_with_traffic_lights:

    # define my_traffic_light to be any traffic light on the map
    my_traffic_light: traffic_light

    # define stop_line, constrain it to be the stop line of the traffic light group that my_traffic_light belongs to
    stop_line: traffic_light_stop_line with:
        keep(it.traffic_light_group == my_traffic_light.group)

    # define a traffic light controller
    my_traffic_light_controller: traffic_light_controller

    do parallel():
        # play traffic light control plan for all traffic lights
        play_traffic_light_cycles: my_traffic_light_controller.play_cycles(map.traffic_light_control)
        # let sut travel the whole length of the road that stop_line is located on
        sut_drive: sut.car.drive() with:
            along(stop_line.road, start_offset: 0m, at: start)
            along(stop_line.road, end_offset: 0m, at: end)

8.14.10.5 Example 5

This example illustrates how to create a traffic light control plan for a map.

Code 78. Set up a traffic light control plan for a map
extend top.main:
    # cycles for traffic_light_group 42
    group42_cycle_phase1: traffic_light_phase with:
        # red for 30 seconds
        keep(it.group.map_id == "42")
        keep(it.bulbs_state == [is_on, is_off, is_off])
        keep(it.duration == 19s)
    group42_cycle_phase2: traffic_light_phase with:
        # yellow for 2 seconds
        keep(it.group.map_id == "42")
        keep(it.bulbs_state == [is_off, is_on, is_off])
        keep(it.duration == 2s)
    group42_cycle_phase3: traffic_light_phase with:
        # green for 30 seconds
        keep(it.group.map_id == "42")
        keep(it.bulbs_state == [is_off, is_off, is_on])
        keep(it.duration == 30s)

    group42_cycle: traffic_light_cycle with:
        keep(it.phases == [group42_cycle_phase1, group42_cycle_phase2, group42_cycle_phase3])

    # cycles for traffic_light_group 43
    group43_cycle_phase1: traffic_light_phase with:
        # green for 30 seconds
        keep(it.group.map_id == "43")
        keep(it.bulbs_state == [is_off, is_off, is_on])
        keep(it.duration == 19s)
    group43_cycle_phase2: traffic_light_phase with:
        # yellow for 2 seconds
        keep(it.group.map_id == "43")
        keep(it.bulbs_state == [is_off, is_on, is_off])
        keep(it.duration == 2s)
    group43_cycle_phase3: traffic_light_phase with:
        # red for 30 seconds
        keep(it.group.map_id == "43")
        keep(it.bulbs_state == [is_on, is_off, is_off])
        keep(it.duration == 30s)

    group43_cycle: traffic_light_cycle with:
        keep(it.phases == [group43_cycle_phase1, group43_cycle_phase2, group43_cycle_phase3])

    keep(map.traffic_light_control == [group42_cycle, group43_cycle])