Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:CARLA simulator Carla Client Get Trafficmanager

From Leeroopedia
Knowledge Sources
Domains Autonomous Driving Simulation, Traffic Management
Last Updated 2026-02-15 00:00 GMT

Overview

Concrete tool for obtaining a Traffic Manager instance from the CARLA client, provided by the CARLA simulator.

Description

The Client.get_trafficmanager() method retrieves or creates a Traffic Manager (TM) instance bound to a specified network port. The Traffic Manager is CARLA's built-in system for controlling NPC vehicle behavior through autopilot. When called, this method checks if a TM instance already exists on the given port; if so, it returns a handle to the existing instance. Otherwise, it initializes a new TM that connects to the CARLA server and begins managing vehicles registered to it.

The returned TrafficManager object provides methods to:

  • Register and unregister vehicles for autopilot control
  • Configure global and per-vehicle driving behavior (speed, lane changes, distance keeping)
  • Enable synchronous mode to lock TM updates to simulation ticks
  • Activate hybrid physics mode for performance optimization

Internally, the C++ implementation delegates to carla::traffic_manager::TrafficManager, which spawns a multi-threaded stage pipeline (Localization, Collision, Traffic Light, Motion Planning, Vehicle Light) that processes all registered vehicles each tick.

Usage

Call get_trafficmanager() early in your traffic generation script, before spawning any NPC vehicles. The returned TM handle is then used to configure traffic behavior and is referenced when setting vehicles to autopilot mode.

Code Reference

Source Location

  • Repository: CARLA
  • File: LibCarla/source/carla/client/Client.h
  • Lines: L112-114
  • Python Binding: PythonAPI/carla/src/Client.cpp, L216

Signature

Client.get_trafficmanager(port: int = 8000) -> TrafficManager

Import

import carla

I/O Contract

Inputs

Name Type Required Description
port int No (default: 8000) The network port on which the Traffic Manager listens. Each unique port creates a separate TM instance, allowing multiple TMs to coexist with different configurations.

Outputs

Name Type Description
return carla.TrafficManager A handle to the Traffic Manager instance bound to the specified port. Provides methods for configuring NPC vehicle behavior, synchronous mode, hybrid physics, and other traffic parameters.

Usage Examples

Basic Example

import carla

# Connect to the CARLA server
client = carla.Client('localhost', 2000)
client.set_timeout(10.0)

# Obtain a Traffic Manager on the default port (8000)
traffic_manager = client.get_trafficmanager(8000)

# Configure synchronous mode to match the simulation
traffic_manager.set_synchronous_mode(True)

# Set a global speed reduction of 30% below posted speed limits
traffic_manager.global_percentage_speed_difference(30.0)

Multiple Traffic Manager Instances

import carla

client = carla.Client('localhost', 2000)
client.set_timeout(10.0)

# Create two TM instances with different driving styles
tm_cautious = client.get_trafficmanager(8000)
tm_aggressive = client.get_trafficmanager(8001)

# Cautious drivers: 40% slower than speed limit, large following distance
tm_cautious.global_percentage_speed_difference(40.0)
tm_cautious.set_global_distance_to_leading_vehicle(5.0)

# Aggressive drivers: 10% faster than speed limit, small following distance
tm_aggressive.global_percentage_speed_difference(-10.0)
tm_aggressive.set_global_distance_to_leading_vehicle(1.0)

Integration with Vehicle Spawning

import carla

client = carla.Client('localhost', 2000)
client.set_timeout(10.0)
world = client.get_world()

# Get TM and its port for batch commands
traffic_manager = client.get_trafficmanager(8000)
tm_port = traffic_manager.get_port()

# Spawn a vehicle and register it with the TM
blueprint = world.get_blueprint_library().find('vehicle.tesla.model3')
spawn_point = world.get_map().get_spawn_points()[0]
vehicle = world.spawn_actor(blueprint, spawn_point)

# Enable autopilot managed by this specific TM
vehicle.set_autopilot(True, tm_port)

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment