Implementation:CARLA simulator Carla ConstantVelocityAgent
| Knowledge Sources | |
|---|---|
| Domains | Agent Navigation, Autonomous Driving |
| Last Updated | 2026-02-15 05:00 GMT |
Overview
A Python agent that navigates the CARLA scene at a fixed velocity, extending BasicAgent with constant velocity physics and collision-responsive behavior.
Description
The ConstantVelocityAgent class extends BasicAgent to drive a vehicle at a constant speed using CARLA's enable_constant_velocity physics mode. The agent converts the target speed from km/h to m/s and applies it as a forward velocity vector. It sets up a collision sensor that automatically stops the constant velocity on impact. After a configurable restart_time, the agent resumes constant velocity driving. During each run_step(), the agent checks for vehicle obstacles (using _vehicle_obstacle_detected) and red traffic lights (using _affected_by_traffic_light). When a hazard is detected, the constant velocity is temporarily reduced to the hazard speed (or zero for traffic lights). The optional use_basic_behavior flag allows falling back to BasicAgent steering logic when the constant velocity is stopped. Key methods include set_target_speed(speed), stop_constant_velocity(), restart_constant_velocity(), and destroy_sensor().
Usage
Use this agent for scenarios requiring a vehicle to maintain a steady cruising speed while still reacting to traffic lights and other vehicles. It is suitable for highway-like driving or testing constant-speed behavior.
Code Reference
Source Location
- Repository: CARLA
- File: PythonAPI/carla/agents/navigation/constant_velocity_agent.py
Signature
class ConstantVelocityAgent(BasicAgent):
def __init__(self, vehicle, target_speed=20, opt_dict={}, map_inst=None, grp_inst=None)
def set_target_speed(self, speed)
def stop_constant_velocity(self)
def restart_constant_velocity(self)
def run_step(self)
def destroy_sensor(self)
Import
from agents.navigation.constant_velocity_agent import ConstantVelocityAgent
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| vehicle | carla.Vehicle | Yes | Vehicle actor to control |
| target_speed | float | No | Target speed in km/h (default: 20) |
| opt_dict | dict | No | Options: restart_time (float), use_basic_behavior (bool) |
| map_inst | carla.Map | No | Pre-loaded map instance to avoid expensive calls |
| grp_inst | GlobalRoutePlanner | No | Pre-loaded route planner instance |
Outputs
| Name | Type | Description |
|---|---|---|
| control | carla.VehicleControl | Steering and throttle control for the current step |
Usage Examples
import carla
from agents.navigation.constant_velocity_agent import ConstantVelocityAgent
client = carla.Client('localhost', 2000)
world = client.get_world()
# Spawn a 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 agent with 30 km/h target
agent = ConstantVelocityAgent(vehicle, target_speed=30, opt_dict={'restart_time': 5.0})
# Set a destination
destination = world.get_map().get_spawn_points()[10].location
agent.set_destination(destination)
# Simulation loop
while True:
if agent.done():
break
control = agent.run_step()
vehicle.apply_control(control)
# Cleanup
agent.destroy_sensor()
vehicle.destroy()