Crate carla

Crate carla 

Source
Expand description

Rust client library for CARLA autonomous driving simulator.

This crate provides safe, idiomatic Rust bindings to the CARLA simulator, allowing you to control vehicles, sensors, and the simulation environment for autonomous driving research and development.

§Overview

CARLA (Car Learning to Act) is an open-source simulator for autonomous driving research. This crate wraps the C++ client library, providing a type-safe and ergonomic Rust interface.

Supported CARLA Versions: 0.9.14, 0.9.15, 0.9.16 (default: 0.9.16)

§Quick Start

use carla::{client::Client, rpc::VehicleControl};

// Connect to the CARLA server
let client = Client::connect("localhost", 2000, None);
let mut world = client.world();

// Get the blueprint library
let blueprint_library = world.blueprint_library();

// Control a vehicle
let mut control = vehicle.control();
control.throttle = 0.5;
vehicle.apply_control(&control);

§Core Concepts

§Client and World

The client::Client is the entry point for connecting to a CARLA server. The client::World represents the simulation world and provides methods for spawning actors, accessing the map, and controlling simulation settings.

§Actors

Everything in the simulation is an client::Actor:

§Sensors and Data

Attach sensors to vehicles to collect data:

§Traffic Manager

The traffic_manager::TrafficManager provides autopilot functionality for creating realistic traffic scenarios with multiple vehicles.

§Module Organization

  • client - Core client, world, and actor types
  • sensor - Sensor types and sensor data
  • traffic_manager - Traffic management and autopilot
  • agents - Navigation agents for autonomous vehicle control
  • geom - Geometry types (vectors, transforms, locations)
  • rpc - RPC types for configuration and control
  • road - Road network and waypoint navigation
  • prelude - Commonly used traits and extension methods

§Thread Safety

Most types in this crate implement Send and Sync, allowing them to be safely shared across threads. However, note that:

  • The CARLA server connection is not thread-safe by default
  • Sensor callbacks run on separate threads
  • Use appropriate synchronization when sharing mutable state

See the individual type documentation for specific thread safety guarantees.

§Simulation Modes

CARLA can run in two modes:

  • Synchronous Mode: The simulation is stepped by the client, providing deterministic behavior. Use client::World::tick() to advance the simulation.
  • Asynchronous Mode: The simulation runs freely at the server’s pace. The client receives updates as they occur.

For deterministic simulations and precise control, use synchronous mode.

§Python API Correspondence

This crate closely follows the CARLA Python API, making it easy to translate Python examples to Rust:

PythonRust
carla.Client()client::Client::connect()
client.get_world()client::Client::world()
world.spawn_actor()client::World::spawn_actor()
vehicle.apply_control()client::Vehicle::apply_control()

§Examples

§Spawning and Controlling a Vehicle

use carla::client::Client;

let client = Client::connect("localhost", 2000, None);
let mut world = client.world();

// Apply vehicle control
let mut control = vehicle.control();
control.throttle = 1.0;
control.steer = -0.5;
vehicle.apply_control(&control);

§Attaching a Camera Sensor

use carla::{client::Client, sensor::data::Image};

let client = Client::connect("localhost", 2000, None);
let mut world = client.world();

// Listen for camera images
sensor.listen(|data| {
    if let Ok(image) = Image::try_from(data) {
        println!("Received image: {}x{}", image.width(), image.height());
    }
});

§Using the Traffic Manager

use carla::client::Client;

let client = Client::connect("localhost", 2000, None);
let mut world = client.world();
let mut traffic_manager = client.instance_tm(None);


// Enable autopilot and configure behavior
vehicle.set_autopilot(true);
traffic_manager.set_percentage_speed_difference(&vehicle, -20.0);

§Feature Flags

This crate currently has no optional features. All functionality is enabled by default.

§See Also

  • carla_sys - Low-level FFI bindings (re-exported by this crate)
  • prelude - Import commonly used traits with use carla::prelude::*

Re-exports§

pub use error::CarlaError;
pub use error::ConnectionError;
pub use error::InternalError;
pub use error::MapError;
pub use error::OperationError;
pub use error::ResourceError;
pub use error::ResourceType;
pub use error::Result;
pub use error::SensorError;
pub use error::ValidationError;
pub use carla_sys;

Modules§

agents
CARLA navigation agents system for autonomous vehicle control.
client
Client library for the CARLA simulator.
error
Error types for CARLA operations.
geom
Geometry types and utilities.
prelude
Convenient re-exports of commonly used traits and extension methods.
road
Road network and navigation types based on OpenDRIVE.
rpc
Remote Procedure Call (RPC) types for client-server communication.
sensor
Sensor data types for processing sensor measurements.
traffic_manager
Traffic manager for controlling autopilot vehicles with realistic behavior.