Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Workflow:CARLA simulator Carla Traffic Generation

From Leeroopedia
Knowledge Sources
Domains Autonomous_Driving, Traffic_Simulation
Last Updated 2026-02-15 12:00 GMT

Overview

End-to-end process for populating a CARLA simulation world with realistic NPC vehicle and pedestrian traffic using the Traffic Manager and batch spawn commands.

Description

This workflow covers spawning large numbers of NPC vehicles and pedestrians to create realistic urban traffic scenarios. Vehicles are spawned in batch using the command system and registered with the Traffic Manager for autonomous driving behavior. Pedestrians are spawned at random navigation mesh locations with AI controllers that direct them to walk between random destinations. The Traffic Manager provides configurable parameters for speed variation, safety distances, lane changes, and traffic light compliance to create diverse driving conditions.

Usage

Execute this workflow when you need to populate a CARLA world with background traffic for testing an ego vehicle, collecting training data with realistic traffic conditions, or evaluating driving algorithms in dense urban environments. Prerequisites include a running CARLA server with a loaded map.

Execution Steps

Step 1: Initialize Traffic Manager

Obtain a Traffic Manager instance from the client and configure its global parameters. Enable synchronous mode on the Traffic Manager to match the world settings. Set the global safety distance to leading vehicles and optionally set a random device seed for deterministic traffic behavior.

Key considerations:

  • Traffic Manager runs on a configurable port (default 8000)
  • Synchronous mode must be enabled on both the world and the Traffic Manager for deterministic results
  • Setting a random seed ensures reproducible traffic patterns across runs

Step 2: Configure Vehicle Blueprints

Filter the Blueprint Library for vehicle blueprints, optionally selecting specific vehicle generations or types. Exclude problematic blueprints (e.g., bicycles in certain scenarios). Configure attributes such as color (randomized) and driver ID to create visual variety.

Key considerations:

  • Vehicle blueprints can be filtered by generation (Gen1, Gen2, Gen3)
  • The 'number_of_wheels' attribute can filter for specific vehicle categories
  • Random color assignment creates visual diversity in traffic

Step 3: Batch Spawn NPC Vehicles

Retrieve all available spawn points from the map and shuffle them randomly. Create a batch of SpawnActor commands chained with SetAutopilot commands. Execute the batch synchronously using apply_batch_sync to spawn all vehicles efficiently in a single RPC call.

Key considerations:

  • The number of spawn points limits the maximum number of vehicles per map
  • Batch spawning with apply_batch_sync is significantly faster than individual spawn calls
  • The command chaining pattern SpawnActor().then(SetAutopilot()) atomically spawns and enables autopilot
  • Check batch results for failures (occupied spawn points) and track successful actor IDs

Step 4: Spawn Pedestrian Walkers

Filter blueprints for walker types, generate random spawn locations on the navigation mesh using get_random_location_from_navigation, and spawn pedestrians at these locations. For each walker, also spawn a WalkerAIController that will drive its behavior.

Key considerations:

  • Walker spawn locations come from the navigation mesh, not from fixed spawn points
  • Each walker needs a paired WalkerAIController spawned at the same location
  • Setting walker speed attributes creates natural variation (walking vs running)
  • Use world.set_pedestrians_seed() before spawning for deterministic pedestrian placement

Step 5: Initialize Walker AI Controllers

Start each WalkerAIController by calling controller.start(), then set a random navigation destination with controller.go_to_location() and a maximum speed with controller.set_max_speed(). The controllers use the Recast/Detour navigation mesh to pathfind to their destinations.

Key considerations:

  • Controllers must be started after a world tick to ensure proper initialization
  • get_random_location_from_navigation() provides valid pedestrian destinations on sidewalks
  • Walkers will navigate around obstacles and cross streets at designated crossings
  • Controllers must be stopped and destroyed before destroying the walkers themselves

Step 6: Configure Traffic Behavior

Fine-tune the Traffic Manager parameters to create the desired traffic conditions. Set global or per-vehicle speed differences, adjust vehicle light management, configure lane change behavior, and set traffic light compliance percentages.

Key considerations:

  • global_percentage_speed_difference controls deviation from speed limits (negative = faster)
  • Per-vehicle overrides take precedence over global settings
  • update_vehicle_lights(actor, True) enables automatic headlight/indicator management
  • ignore_lights_percentage and ignore_signs_percentage create more aggressive traffic

Step 7: Cleanup Traffic on Exit

When the simulation session ends, properly destroy all spawned actors. Stop walker controllers first, then destroy controllers, then destroy walkers and vehicles. In synchronous mode, disable synchronous mode on both the Traffic Manager and the world before cleanup.

Key considerations:

  • Always stop walker controllers before destroying walkers
  • Destroy actors in reverse order: controllers first, then walkers, then vehicles
  • Disable synchronous mode before cleanup to avoid deadlocks
  • Use client.apply_batch for efficient batch destruction

Execution Diagram

GitHub URL

Workflow Repository