Implementation:CARLA simulator Carla Manage Traffic Light Tool
| Knowledge Sources | |
|---|---|
| Domains | Simulation, Traffic, Utility |
| Last Updated | 2026-02-15 05:00 GMT |
Overview
Command-line utility script for controlling individual traffic lights in a CARLA simulation by OpenDRIVE ID, with support for timing, state, freeze, and group reset operations.
Description
This script (manage_traffic_light.py) provides fine-grained control over traffic lights identified by their OpenDRIVE ID. It defines a TRAFFIC_LIGHT_STATES dictionary mapping state names (Green, Yellow, Red, Off) to carla.TrafficLightState enum values. The get_traffic_light() function locates a traffic light by iterating through map topology to find junctions, then scanning all junction traffic lights for a matching OpenDRIVE ID. The main function supports: setting phase durations (-gt/--green-time, -yt/--yellow-time, -rt/--red-time), changing current state (-st/--state), freezing all traffic lights (-f/--freeze), and resetting the traffic light group (-rg/--reset-group). Each operation calls the corresponding method on the carla.TrafficLight actor.
Usage
Use this tool for debugging traffic light behavior, testing autonomous driving scenarios with specific traffic light configurations, or creating reproducible test conditions.
Code Reference
Source Location
- Repository: CARLA
- File: PythonAPI/util/manage_traffic_light.py
Signature
TRAFFIC_LIGHT_STATES = {
'Green': carla.TrafficLightState.Green,
'Yellow': carla.TrafficLightState.Yellow,
'Red': carla.TrafficLightState.Red,
'Off': carla.TrafficLightState.Off,
}
def get_traffic_light(world, tl_id):
"""Find a traffic light by OpenDRIVE ID via junction topology."""
def main():
"""Parse args and control the specified traffic light."""
Import
# Run as a command-line tool:
python manage_traffic_light.py --id "1234" -gt 30 -st Green
I/O Contract
| Argument | Type | Default | Description |
|---|---|---|---|
| --host | str | 127.0.0.1 | CARLA server IP address |
| -p, --port | int | 2000 | CARLA server TCP port |
| --id | str | required | OpenDRIVE ID of the traffic light |
| -gt, --green-time | int | -1 | Green phase duration in seconds |
| -yt, --yellow-time | int | -1 | Yellow phase duration in seconds |
| -rt, --red-time | int | -1 | Red phase duration in seconds |
| -st, --state | str | NONE | Set current state (Green, Yellow, Red, Off) |
| -f, --freeze | flag | False | Freeze all traffic lights |
| -rg, --reset-group | flag | False | Reset the traffic light group |
Usage Examples
# Set green time to 30 seconds
python manage_traffic_light.py --id "signal_1" -gt 30
# Change state to Red
python manage_traffic_light.py --id "signal_1" -st Red
# Set all phase times
python manage_traffic_light.py --id "signal_1" -gt 30 -yt 5 -rt 25
# Freeze all traffic lights
python manage_traffic_light.py --id "signal_1" -f
# Reset the traffic light group
python manage_traffic_light.py --id "signal_1" -rg