Implementation:CARLA simulator Carla TrafficManager Configuration
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Traffic Behavior Modeling, Simulation Configuration |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Concrete tool for configuring the Traffic Manager's global and per-vehicle behavior parameters, provided by the CARLA simulator.
Description
The TrafficManager object exposes multiple configuration methods that control how NPC vehicles drive. These methods are grouped into:
- Global settings -- Apply to all vehicles managed by this Traffic Manager instance
- Per-vehicle settings -- Override global defaults for specific vehicles
- Mode settings -- Control synchronous execution, hybrid physics, and determinism
The key configuration methods covered here are:
- set_global_distance_to_leading_vehicle(distance) -- Sets the minimum following distance (in meters) for all managed vehicles.
- global_percentage_speed_difference(percentage) -- Adjusts the target speed of all managed vehicles relative to the speed limit. Positive values slow down, negative values speed up.
- set_synchronous_mode(mode) -- Enables or disables synchronous execution, ensuring the TM processes updates in lockstep with world ticks.
- set_hybrid_physics_mode(enabled) -- Enables or disables hybrid physics, where vehicles far from the hero vehicle use simplified kinematic movement.
- set_hybrid_physics_radius(radius) -- Sets the distance (in meters) from the hero vehicle within which full physics is applied.
- set_random_device_seed(seed) -- Sets the random seed for deterministic traffic behavior.
Usage
Call these methods on the TrafficManager object after obtaining it via Client.get_trafficmanager(). Configuration should ideally be done before spawning NPC vehicles, though most parameters can be changed at runtime.
Code Reference
Source Location
- Repository: CARLA
- File:
LibCarla/source/carla/trafficmanager/TrafficManager.h - Lines: L150-360
- Python Binding:
PythonAPI/carla/src/TrafficManager.cpp, L76-113
Signature
TrafficManager.set_global_distance_to_leading_vehicle(distance: float) -> None
TrafficManager.global_percentage_speed_difference(percentage: float) -> None
TrafficManager.set_synchronous_mode(mode: bool) -> None
TrafficManager.set_hybrid_physics_mode(enabled: bool) -> None
TrafficManager.set_hybrid_physics_radius(radius: float) -> None
TrafficManager.set_random_device_seed(seed: int) -> None
Import
import carla
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| distance | float | Yes | Minimum following distance in meters. Default is 2.5m. Typical range: 1.0 to 10.0. |
| percentage | float | Yes | Speed difference as a percentage of the speed limit. Positive values reduce speed (e.g., 30.0 = 30% slower). Negative values increase speed (e.g., -10.0 = 10% faster). Range: -100.0 to 100.0. |
| mode | bool | Yes | True to enable synchronous mode, False to disable. Should match the world's synchronous mode setting. |
| enabled | bool | Yes | True to enable hybrid physics mode, False for full physics on all vehicles. |
| radius | float | Yes | Distance in meters from the hero vehicle within which full physics simulation is applied. Default is 50.0m. Vehicles outside this radius use kinematic movement. |
| seed | int | Yes | Random seed for deterministic behavior. Any integer value. Use the same seed across runs for reproducibility. |
Outputs
| Name | Type | Description |
|---|---|---|
| return (all methods) | None | All configuration methods return None. Changes take effect on the next simulation tick. |
Usage Examples
Basic Example
import carla
client = carla.Client('localhost', 2000)
client.set_timeout(10.0)
world = client.get_world()
# Initialize Traffic Manager
traffic_manager = client.get_trafficmanager(8000)
# Configure global behavior
traffic_manager.set_global_distance_to_leading_vehicle(3.0) # 3 meter gap
traffic_manager.global_percentage_speed_difference(20.0) # 20% below speed limit
Full Deterministic Configuration
import carla
client = carla.Client('localhost', 2000)
client.set_timeout(10.0)
world = client.get_world()
# Enable synchronous mode on the world
settings = world.get_settings()
settings.synchronous_mode = True
settings.fixed_delta_seconds = 0.05 # 20 Hz fixed time step
world.apply_settings(settings)
# Configure Traffic Manager for deterministic simulation
traffic_manager = client.get_trafficmanager(8000)
traffic_manager.set_synchronous_mode(True)
traffic_manager.set_random_device_seed(42)
# Set driving behavior
traffic_manager.set_global_distance_to_leading_vehicle(2.5)
traffic_manager.global_percentage_speed_difference(10.0)
print("Deterministic traffic configuration complete")
High-Performance Configuration with Hybrid Physics
import carla
client = carla.Client('localhost', 2000)
client.set_timeout(10.0)
world = client.get_world()
# Synchronous mode
settings = world.get_settings()
settings.synchronous_mode = True
settings.fixed_delta_seconds = 0.05
world.apply_settings(settings)
traffic_manager = client.get_trafficmanager(8000)
traffic_manager.set_synchronous_mode(True)
traffic_manager.set_random_device_seed(0)
# Enable hybrid physics for large-scale traffic
# Only vehicles within 70m of the hero vehicle get full physics
traffic_manager.set_hybrid_physics_mode(True)
traffic_manager.set_hybrid_physics_radius(70.0)
# Moderate driving behavior
traffic_manager.set_global_distance_to_leading_vehicle(2.0)
traffic_manager.global_percentage_speed_difference(15.0)
print("Hybrid physics enabled: full physics within 70m of hero vehicle")
Per-Vehicle Configuration Overrides
import carla
import random
client = carla.Client('localhost', 2000)
client.set_timeout(10.0)
world = client.get_world()
traffic_manager = client.get_trafficmanager(8000)
traffic_manager.set_synchronous_mode(True)
tm_port = traffic_manager.get_port()
# Set conservative global defaults
traffic_manager.set_global_distance_to_leading_vehicle(3.5)
traffic_manager.global_percentage_speed_difference(20.0)
# Spawn vehicles (batch spawn code omitted for brevity)
# Assume 'vehicles' is a list of spawned vehicle actors
for vehicle in vehicles:
vehicle.set_autopilot(True, tm_port)
# Make 10% of vehicles aggressive drivers
if random.random() < 0.1:
# Per-vehicle overrides: faster and closer following
traffic_manager.distance_to_leading_vehicle(vehicle, 1.5)
traffic_manager.vehicle_percentage_speed_difference(vehicle, -15.0)
# Allow lane changes and ignore distance to other vehicles
traffic_manager.auto_lane_change(vehicle, True)
traffic_manager.ignore_lights_percentage(vehicle, 5.0)
else:
# Standard drivers keep global defaults
traffic_manager.auto_lane_change(vehicle, True)
Related Pages
Implements Principle
Uses Heuristic
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment