Traffic lights examples
Example: how SUT and NPC drive through the same traffic-light-controlled junction
In the below example, the SUT and an NPC both drive on the same path through a traffic-light-controlled junction connection. The NPC stops with its front 5 meters before the stop line (regardless of the traffic-light's state) and then continues driving.
import "$FTX/env/basic/exe_platforms/model_ssp/config/model_sumo_config.osc"
extend test_config:
set map = "$FTX_PACKAGES/maps/M510_FTX_TrafficSignals_Simple.xodr"
extend sim_config:
set tl_api_level = logical
extend top.main:
car1: vehicle
# traffic-light controlled path that crosses a junction
route_with_tl: roads_follow_in_junction_with_tl
# a path (longer than 5m plus sut-length) that ends at the stop line of the traffic light
path_to_stop_line: road_with_traffic_light with:
keep(it.length > sut.car.bbox.length + 5m)
keep(it.id == route_with_tl.internal_road.road_with_traffic_light_id)
do parallel(duration: [15..25]s, overlap: equal, run_mode: best_effort):
car1_drive: serial:
# stop with front of car 5 meters before the stop line
car1.drive() with:
along(path_to_stop_line, end_offset: 5m + sut.car.bbox.length / 2, at: end)
speed([0..2]kph, at: end)
# drive through the junction
car1.drive() with:
car1_dest: along(route_with_tl.out_road, at: end)
override(car1_dest, run_mode: best_effort) # may be limitted by SUT driving in front
car1_speed: speed(30kph, at: end)
override(car1_speed, run_mode: best_effort) # may be limitted by SUT driving in front
sut_drive: serial:
sut.car.drive() with:
along(route_with_tl.in_road, at: start)
sut_dest: along(route_with_tl.out_road, at: end)
override(sut_dest, run_mode: best_effort) # may be limitted by NPC driving in front
Example: How to directly control the color of an individual traffic light bulb in declarative code
The below example sets the traffic-light at the beginning of the scenario with the red bulb on and all other bulbs off (only the green bulb needs to be actively turned off because it is initially on due to the default state). Then, after the NPC stops, we turn the red bulb off and the green bulb on.
import "$FTX/env/basic/exe_platforms/model_ssp/config/model_sumo_config.osc"
extend test_config:
set map = "$FTX_PACKAGES/maps/M510_FTX_TrafficSignals_Simple.xodr"
extend sim_config:
set tl_api_level = logical
extend top.main:
tl: traffic_light
car1: vehicle
# traffic-light controlled path that crosses a junction
route_with_tl: roads_follow_in_junction_with_tl
# a path (longer than 5m plus sut-length) that ends at the stop line of the traffic light
path_to_stop_line: road_with_traffic_light with:
keep(it.length > sut.car.bbox.length + 5m)
keep(it.id == route_with_tl.internal_road.road_with_traffic_light_id)
do parallel(duration: [15..25]s, overlap: equal, run_mode: best_effort):
car1_drive: serial:
# start with red light
r1: tl.set_light_bulb(path_to_stop_line.traffic_light_id, red, is_on)
g1: tl.set_light_bulb(path_to_stop_line.traffic_light_id, green, is_off)
# stop with front of car 5 meters before the stop line
car1.drive() with:
along(path_to_stop_line, end_offset: 5m + sut.car.bbox.length / 2, at: end)
speed([0..2]kph, at: end)
# change the light to green
r2: tl.set_light_bulb(path_to_stop_line.traffic_light_id, red, is_off)
g2: tl.set_light_bulb(path_to_stop_line.traffic_light_id, green, is_on)
# drive through the junction
car1.drive() with:
car1_dest: along(route_with_tl.out_road, at: end)
override(car1_dest, run_mode: best_effort) # may be limitted by SUT driving in front
car1_speed: speed(30kph, at: end)
override(car1_speed, run_mode: best_effort) # may be limitted by SUT driving in front
sut_drive: serial:
sut.car.drive() with:
along(route_with_tl.in_road, at: start)
sut_dest: along(route_with_tl.out_road, at: end)
override(sut_dest, run_mode: best_effort) # may be limitted by NPC driving in front
Example: How to control traffic lights by changing the semantic state of traffic lights procedurally for an internal road
The below example controls the traffic-lights using the map method update_internal_road_tl_state() to modify the semantic state.
import "$FTX/env/basic/exe_platforms/model_ssp/config/model_sumo_config.osc"
extend test_config:
set map = "$FTX_PACKAGES/maps/M510_FTX_TrafficSignals_Simple.xodr"
extend sim_config:
set tl_api_level = logical
extend top.main:
car1: vehicle
# traffic-light controlled path that crosses a junction
route_with_tl: roads_follow_in_junction_with_tl with:
keep(it.internal_road.can_be_protected == true)
stop_state: semantic_traffic_light_state with:
keep((route_with_tl.internal_road.valid_tl_stop_constant and it == stop_constant) or
(route_with_tl.internal_road.valid_tl_stop and it == stop))
event tl1
# on tl1 update the traffic-light state for the internal road to stop_state (stop or stop_constant for right turn)
on @tl1:
var res := map.update_internal_road_tl_state(route_with_tl.internal_road, stop_state)
event tl2
# on tl2 update the traffic-light state for the internal road to go_exclusive
on @tl2:
var res := map.update_internal_road_tl_state(route_with_tl.internal_road, go_exclusive)
# a path (longer than 5m plus sut-length) that ends at the stop line of the traffic light
path_to_stop_line: road_with_traffic_light with:
keep(it.length > sut.car.bbox.length + 5m)
keep(it.id == route_with_tl.internal_road.road_with_traffic_light_id)
do parallel(duration: [15..25]s, overlap: equal, run_mode: best_effort):
car1_drive: serial:
# start with red light
emit tl1
# stop with front of car 5 meters before the stop line
car1.drive() with:
along(path_to_stop_line, end_offset: 5m + sut.car.bbox.length / 2, at: end)
speed([0..2]kph, at: end)
# change the light to green
emit tl2
# drive through the junction
car1.drive() with:
car1_dest: along(route_with_tl.out_road, at: end)
override(car1_dest, run_mode: best_effort) # may be limitted by SUT driving in front
car1_speed: speed(30kph, at: end)
override(car1_speed, run_mode: best_effort) # may be limitted by SUT driving in front
sut_drive: serial:
sut.car.drive() with:
along(route_with_tl.in_road, at: start)
sut_dest: along(route_with_tl.out_road, at: end)
override(sut_dest, run_mode: best_effort) # may be limitted by NPC driving in front
Example: How to control traffic lights procedurally by changing the semantic state if all directions controlled by the traffic light
The below example shows how to control traffic-lights by changing the semantic state of all directions of a traffic-light using map.set_tl_direction_agnostic_state().
import "$FTX/env/basic/exe_platforms/model_ssp/config/model_sumo_config.osc"
extend test_config:
set map = "$FTX_PACKAGES/maps/M510_FTX_TrafficSignals_Simple.xodr"
extend sim_config:
set tl_api_level = logical
extend top.main:
car1: vehicle
# traffic-light controlled path that crosses a junction
route_with_tl: roads_follow_in_junction_with_tl
# a path (longer than 5m plus sut-length) that ends at the stop line of the traffic light
path_to_stop_line: road_with_traffic_light with:
keep(it.length > sut.car.bbox.length + 5m)
keep(it.id == route_with_tl.internal_road.road_with_traffic_light_id)
event tl1
# on tl1 update the traffic-light state to all_stop (all red lights on, all other lights off)
on @tl1: map.set_tl_direction_agnostic_state(path_to_stop_line.traffic_light_id, all_stop)
event tl2
# on tl2 update the traffic-light state to all_go (all green lights on, all other lights off)
on @tl2: map.set_tl_direction_agnostic_state(path_to_stop_line.traffic_light_id, all_go)
do parallel(duration: [15..25]s, overlap: equal, run_mode: best_effort):
car1_drive: serial:
# start with red light
emit tl1
# stop with front of car 5 meters before the stop line
car1.drive() with:
along(path_to_stop_line, end_offset: 5m + sut.car.bbox.length / 2, at: end)
speed([0..2]kph, at: end)
# change the light to green
emit tl2
# drive through the junction
car1.drive() with:
car1_dest: along(route_with_tl.out_road, at: end)
override(car1_dest, run_mode: best_effort) # may be limitted by SUT driving in front
car1_speed: speed(30kph, at: end)
override(car1_speed, run_mode: best_effort) # may be limitted by SUT driving in front
sut_drive: serial:
sut.car.drive() with:
along(route_with_tl.in_road, at: start)
sut_dest: along(route_with_tl.out_road, at: end)
override(sut_dest, run_mode: best_effort) # may be limitted by NPC driving in front
Example: How to modify a traffic light state in declarative code using the set_light_state action
import "$FTX/env/basic/exe_platforms/model_ssp/config/model_sumo_config.osc"
extend test_config:
set map = "$FTX_PACKAGES/maps/M510_FTX_TrafficSignals_Simple.xodr"
extend sim_config:
set tl_api_level = logical
extend top.main:
tl: traffic_light
car1: vehicle
# traffic-light controlled path that crosses a junction
route_with_tl: roads_follow_in_junction_with_tl
# a path (longer than 5m plus sut-length) that ends at the stop line of the traffic light
path_to_stop_line: road_with_traffic_light with:
keep(it.length > sut.car.bbox.length + 5m)
keep(it.id == route_with_tl.internal_road.road_with_traffic_light_id)
do parallel(duration: [15..25]s, overlap: equal, run_mode: best_effort):
car1_drive: serial:
# start with red light
tl.set_light_state(path_to_stop_line.traffic_light_id, red)
# stop with front of car 5 meters before the stop line
car1.drive() with:
along(path_to_stop_line, end_offset: 5m + sut.car.bbox.length / 2, at: end)
speed([0..2]kph, at: end)
# change the light to green
tl.set_light_state(path_to_stop_line.traffic_light_id, green)
# drive through the junction
car1.drive() with:
car1_dest: along(route_with_tl.out_road, at: end)
override(car1_dest, run_mode: best_effort) # may be limitted by SUT driving in front
car1_speed: speed(30kph, at: end)
override(car1_speed, run_mode: best_effort) # may be limitted by SUT driving in front
sut_drive: serial:
sut.car.drive() with:
along(route_with_tl.in_road, at: start)
sut_dest: along(route_with_tl.out_road, at: end)
override(sut_dest, run_mode: best_effort) # may be limitted by NPC driving in front