Implementation:CARLA simulator Carla ROS2 Interface
| Knowledge Sources | |
|---|---|
| Domains | ROS Integration, Middleware |
| Last Updated | 2026-02-15 05:00 GMT |
Overview
The ROS2 class provides the native ROS 2 integration interface for CARLA, managing publishers and subscribers for all sensor data, transforms, and control messages.
Description
carla::ros2::ROS2 is a singleton class (accessed via GetInstance()) that bridges CARLA sensor data and actor state to the ROS 2 ecosystem. It manages:
Publisher creation and data publishing for all sensor types:
- Camera: RGB, depth, semantic segmentation, instance segmentation, optical flow, DVS events, normals
- Lidar: standard and semantic point clouds
- Radar: detection data
- IMU, GNSS: inertial and GPS data
- Collision, obstacle detection: event data
- Vehicle: speedometer, control commands
- Generic: float, string, and image data
Key methods:
Enable(bool)-- globally enable/disable ROS 2 publishingSetFrame()/SetTimestamp()-- synchronize timingProcessDataFromCamera(),ProcessDataFromLidar(),ProcessDataFromRadar(), etc. -- publish sensor dataProcessDataFromTransform()-- publish TF dataProcessDataFromControl()-- publish vehicle control data
Subscriber management:
AddSubscriber()/RemoveSubscriber()-- register for external control inputGetVehicleControl()-- retrieve control commands from ROS subscribers
The class creates DDS publishers/subscribers using the Fast-DDS library for the ROS 2 middleware layer, with topic names derived from actor names and types.
Usage
Use this class to bridge CARLA simulation data to ROS 2 nodes. It is activated via the CARLA server configuration and automatically publishes data for all active sensors.
Code Reference
Source Location
- Repository: CARLA
- File:
LibCarla/source/carla/ros2/ROS2.h
Signature
class ROS2 {
public:
static std::shared_ptr<ROS2> GetInstance();
void Enable(bool enable);
bool IsEnabled();
void SetFrame(const uint64_t frame);
void SetTimestamp(double timestamp);
// Sensor data publishing
void ProcessDataFromCamera(uint64_t sensor_type, ...);
void ProcessDataFromLidar(uint64_t sensor_type, ...);
void ProcessDataFromSemanticLidar(uint64_t sensor_type, ...);
void ProcessDataFromRadar(uint64_t sensor_type, ...);
void ProcessDataFromIMU(uint64_t sensor_type, ...);
void ProcessDataFromGNSS(uint64_t sensor_type, ...);
void ProcessDataFromObstacleDetection(uint64_t sensor_type, ...);
void ProcessDataFromCollisionSensor(uint64_t sensor_type, ...);
void ProcessDataFromDVS(uint64_t sensor_type, ...);
// Transform and control
void ProcessDataFromTransform(uint64_t sensor_type, ...);
void ProcessDataFromControl(uint64_t sensor_type, ...);
// Subscriber management
void AddSubscriber(void *actor, const std::string &topic_name);
void RemoveSubscriber(void *actor);
bool GetVehicleControl(void *actor, rpc::VehicleControl &control);
};
Import
#include "carla/ros2/ROS2.h"
I/O Contract
| Input | Type | Description |
|---|---|---|
uint64_t | Type identifier for the sensor
| ||
| Various | Raw sensor data buffers (images, point clouds, etc.) | ||
uint64_t | Current simulation frame number
| ||
double | Current simulation timestamp
|
| Output | Type | Description |
|---|---|---|
| DDS publishers | Sensor data published to ROS 2 topics | ||
rpc::VehicleControl | Control commands received from ROS subscribers
|
Usage Examples
auto ros2 = carla::ros2::ROS2::GetInstance();
ros2->Enable(true);
ros2->SetFrame(frame_number);
ros2->SetTimestamp(sim_time);
// Publish camera data
ros2->ProcessDataFromCamera(sensor_type, stream_id, transform,
image_width, image_height, fov, image_data);