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.

Implementation:CARLA simulator Carla BehaviorAgent Car Following Manager

From Leeroopedia
Knowledge Sources
Domains Traffic Flow Theory, Car Following, Autonomous Driving
Last Updated 2026-02-15 00:00 GMT

Overview

Concrete tool for managing longitudinal speed when following a lead vehicle provided by the CARLA simulator agents library. The car_following_manager() method computes a safe vehicle control command based on the Time-To-Collision (TTC) with a detected lead vehicle, applying behavior-profile-specific safety thresholds and speed adaptation.

Description

The car_following_manager() method is called by BehaviorAgent.run_step() when a vehicle obstacle has been detected ahead on the route. It:

  1. Retrieves the current speed of both the ego vehicle and the lead vehicle using get_speed()
  2. Computes the relative closing velocity in m/s
  3. Calculates the Time-To-Collision (TTC) as distance divided by closing velocity
  4. Applies a three-tier decision:
    • Emergency brake if TTC is below the behavior's safety_time threshold
    • Speed adaptation if within the proximity zone -- reduces target speed to match or lag behind the lead vehicle
    • Gradual deceleration otherwise -- reduces target speed by the behavior's speed_decrease parameter
  5. Delegates the adapted target speed to the PID controller via LocalPlanner.run_step()

The method accepts an optional debug flag that prints TTC and speed information to the console for tuning and analysis.

Usage

Use the car-following manager when the ego vehicle detects a slower vehicle ahead and needs to adapt its speed to maintain a safe following distance. This is automatically invoked by BehaviorAgent.run_step() but can be called directly for custom behavior implementations.

Code Reference

Source Location

  • Repository: CARLA
  • File: PythonAPI/carla/agents/navigation/behavior_agent.py
  • Lines: L196-237

Signature

def car_following_manager(self, vehicle, distance, debug=False):
    """
    Module in charge of car-following behaviors when there's
    someone in front of us.

    :param vehicle: car to follow (carla.Actor)
    :param distance: distance from vehicle in meters (float)
    :param debug: boolean for debug info
    :return: carla.VehicleControl
    """

Import

from agents.navigation.behavior_agent import BehaviorAgent
# Method accessed as agent.car_following_manager(vehicle, distance)

I/O Contract

Inputs

Name Type Required Description
vehicle carla.Actor Yes The lead vehicle to follow. Its speed is queried via get_speed() to compute the relative velocity.
distance float Yes Distance in meters between the ego vehicle and the lead vehicle. Typically provided by the obstacle detection routine.
debug bool No (default: False) When True, prints debug information including TTC, ego speed, and lead vehicle speed to the console.

Outputs

Name Type Description
control carla.VehicleControl Vehicle control command with adapted throttle/brake/steering. May be an emergency brake command (throttle=0, brake=1) if TTC is critical, or a PID-generated command with reduced target speed otherwise.

Usage Examples

Basic Example

import carla
from agents.navigation.behavior_agent import BehaviorAgent

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

# Spawn ego vehicle
bp = world.get_blueprint_library().find('vehicle.tesla.model3')
spawn_point = world.get_map().get_spawn_points()[0]
vehicle = world.spawn_actor(bp, spawn_point)

# Create a cautious behavior agent
agent = BehaviorAgent(vehicle, behavior='cautious')
agent.set_destination(world.get_map().get_spawn_points()[80].location)

while not agent.done():
    # The run_step() method internally calls car_following_manager
    # when a lead vehicle is detected
    control = agent.run_step(debug=True)
    vehicle.apply_control(control)
    world.tick()

Direct Usage for Custom Behavior

from agents.navigation.behavior_agent import BehaviorAgent

agent = BehaviorAgent(vehicle, behavior='normal')
agent.set_destination(destination)

# Manually invoke car-following when you detect a lead vehicle
vehicle_list = world.get_actors().filter('vehicle.*')
detected, lead_vehicle, distance = agent._vehicle_obstacle_detected(
    vehicle_list=vehicle_list,
    max_distance=30.0
)

if detected and lead_vehicle is not None:
    # Use car-following manager directly
    control = agent.car_following_manager(
        vehicle=lead_vehicle,
        distance=distance,
        debug=True
    )
    vehicle.apply_control(control)
else:
    # No obstacle, proceed normally
    control = agent.run_step()
    vehicle.apply_control(control)

Comparing Behavior Profiles

# Create agents with different behaviors for comparison
cautious_agent = BehaviorAgent(vehicle_1, behavior='cautious')
normal_agent = BehaviorAgent(vehicle_2, behavior='normal')
aggressive_agent = BehaviorAgent(vehicle_3, behavior='aggressive')

# At the same following distance and lead speed:
# - Cautious: larger speed reduction, earlier braking
# - Normal: moderate speed reduction
# - Aggressive: minimal speed reduction, later braking

Related Pages

Implements Principle

Uses Heuristic

Page Connections

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