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 Vehicle Set Autopilot

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

Overview

Concrete tool for enabling or disabling AI-driven autopilot control on a vehicle actor provided by the CARLA simulator.

Description

The Vehicle.set_autopilot method registers or unregisters a vehicle with the CARLA Traffic Manager (TM). When enabled, the Traffic Manager takes full control of the vehicle's steering, throttle, and braking, driving it according to traffic rules, road topology, and configurable behavior parameters. When disabled, control is returned to the client, and the vehicle will coast to a stop unless the client begins sending VehicleControl commands.

The tm_port parameter specifies which Traffic Manager instance should control this vehicle. The default port is 8000, corresponding to the default Traffic Manager. By using different ports, you can create multiple Traffic Manager instances with different behavior profiles and assign vehicles to them selectively.

Usage

Use set_autopilot to enable AI driving on NPC vehicles after spawning them. This is the standard approach for populating the simulation with background traffic. Call it once after spawning; the Traffic Manager handles the vehicle continuously until autopilot is disabled or the vehicle is destroyed.

Code Reference

Source Location

  • Repository: CARLA
  • File: PythonAPI/carla/src/Actor.cpp
  • Lines: L175-178

Signature

Vehicle.set_autopilot(enabled: bool = True, tm_port: int = 8000) -> None

Import

import carla

I/O Contract

Inputs

Name Type Required Description
enabled bool No Whether to enable (True) or disable (False) autopilot. Defaults to True.
tm_port int No The port number of the Traffic Manager instance that should control this vehicle. Defaults to 8000.

Outputs

Name Type Description
return None This method returns nothing. The effect is that the Traffic Manager begins or stops controlling the vehicle.

Usage Examples

Basic Example

import carla
import random
import time

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

blueprint_library = world.get_blueprint_library()
vehicle_blueprints = blueprint_library.filter("vehicle.*")

spawn_points = world.get_map().get_spawn_points()
random.shuffle(spawn_points)

# Spawn NPC vehicles and enable autopilot
npc_vehicles = []
for i in range(20):
    bp = random.choice(vehicle_blueprints)
    vehicle = world.try_spawn_actor(bp, spawn_points[i])
    if vehicle is not None:
        # Enable autopilot on the default Traffic Manager
        vehicle.set_autopilot(True)
        npc_vehicles.append(vehicle)

print(f"Spawned {len(npc_vehicles)} NPC vehicles with autopilot enabled")

# Let traffic run for a while
time.sleep(30)

# Disable autopilot on all vehicles (they will coast to a stop)
for vehicle in npc_vehicles:
    vehicle.set_autopilot(False)

# Cleanup
for vehicle in npc_vehicles:
    vehicle.destroy()

Configurable Traffic Behavior Example

import carla
import random

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

# Get the default Traffic Manager and configure it
traffic_manager = client.get_trafficmanager(8000)
traffic_manager.set_global_distance_to_leading_vehicle(2.5)
traffic_manager.set_synchronous_mode(True)  # Match world sync mode

blueprint_library = world.get_blueprint_library()
vehicle_blueprints = [bp for bp in blueprint_library.filter("vehicle.*")
                      if int(bp.get_attribute("number_of_wheels")) == 4]

spawn_points = world.get_map().get_spawn_points()
random.shuffle(spawn_points)

npc_vehicles = []
for i in range(30):
    bp = random.choice(vehicle_blueprints)
    if bp.has_attribute("color"):
        color = random.choice(bp.get_attribute("color").recommended_values)
        bp.set_attribute("color", color)

    vehicle = world.try_spawn_actor(bp, spawn_points[i])
    if vehicle is not None:
        vehicle.set_autopilot(True, tm_port=8000)
        npc_vehicles.append(vehicle)

        # Customize per-vehicle behavior
        # Some vehicles drive 20% slower than the speed limit
        traffic_manager.vehicle_percentage_speed_difference(vehicle, 20.0)

        # 10% chance to ignore traffic lights (simulating inattentive drivers)
        traffic_manager.ignore_lights_percentage(vehicle, 10.0)

        # Allow automatic lane changes
        traffic_manager.auto_lane_change(vehicle, True)

print(f"Traffic scenario: {len(npc_vehicles)} NPC vehicles with varied behavior")

# Run synchronous simulation
settings = world.get_settings()
settings.synchronous_mode = True
settings.fixed_delta_seconds = 0.05
world.apply_settings(settings)

for tick in range(2000):  # Run for 100 simulated seconds
    world.tick()

# Cleanup
for vehicle in npc_vehicles:
    vehicle.set_autopilot(False)
    vehicle.destroy()

Related Pages

Implements Principle

Page Connections

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