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 ROS2 Bridge

From Leeroopedia
Knowledge Sources
Domains ROS2, Middleware, Sensor Data Publishing
Last Updated 2026-02-15 05:00 GMT

Overview

ROS2.cpp implements the ROS2 bridge singleton that publishes CARLA simulation data (sensor readings, clock, transforms) to ROS2 topics and subscribes to vehicle control commands from ROS2 nodes.

Description

The ROS2 class is a singleton (accessed via a shared static _instance) that bridges CARLA's internal data streams to the ROS2 ecosystem. The implementation includes:

  • Sensor publisher registry -- Supports a comprehensive set of sensor types defined by the ESensors enum: CollisionSensor, DepthCamera, NormalsCamera, DVSCamera, GnssSensor, InertialMeasurementUnit, LaneInvasionSensor, ObstacleDetectionSensor, OpticalFlowCamera, Radar, RayCastSemanticLidar, RayCastLidar, RssSensor, SceneCaptureCamera, SemanticSegmentationCamera, InstanceSegmentationCamera, WorldObserver, and GBuffer cameras.
  • Clock publishing -- SetTimestamp splits a double timestamp into seconds and nanoseconds, publishes via CarlaClockPublisher.
  • Frame management -- SetFrame processes incoming frames, checks for new vehicle control messages from the _controller subscriber, and dispatches them to registered actor callbacks.
  • Actor naming -- AddActorRosName and AddActorParentRosName manage the mapping between CARLA actors and their ROS topic names/hierarchies.
  • Publisher types -- Instantiates specialized publishers including CarlaRGBCameraPublisher, CarlaDepthCameraPublisher, CarlaNormalsCameraPublisher, CarlaOpticalFlowCameraPublisher, CarlaSSCameraPublisher, CarlaISCameraPublisher, CarlaDVSCameraPublisher, CarlaLidarPublisher, CarlaSemanticLidarPublisher, CarlaRadarPublisher, CarlaIMUPublisher, CarlaGNSSPublisher, CarlaMapSensorPublisher, CarlaSpeedometerSensor, CarlaTransformPublisher, CarlaCollisionPublisher, and CarlaLineInvasionPublisher.
  • Subscriber support -- CarlaEgoVehicleControlSubscriber for receiving vehicle controls, and optionally BasicSubscriber when compiled with WITH_ROS2_DEMO.

Usage

The ROS2 bridge is activated by calling Enable(true) on the singleton instance. Once enabled, it automatically publishes sensor data and clock information each frame. Vehicle control subscribers allow external ROS2 nodes to control CARLA vehicles.

Code Reference

Source Location

  • Repository: CARLA
  • File: LibCarla/source/carla/ros2/ROS2.cpp (897 lines)

Signature

namespace carla {
namespace ros2 {

class ROS2 {
public:
  static std::shared_ptr<ROS2> _instance;

  void Enable(bool enable);
  void SetFrame(uint64_t frame);
  void SetTimestamp(double timestamp);
  void AddActorRosName(void *actor, std::string ros_name);
  void AddActorParentRosName(void *actor, void* parent);
  void RemoveActorRosName(void *actor);
  void RemoveActorCallback(void *actor);
  // ... additional publisher/subscriber management methods
};

} // namespace ros2
} // namespace carla

Import

#include "carla/ros2/ROS2.h"

I/O Contract

Inputs

Name Type Required Description
enable bool Yes Whether to enable or disable the ROS2 bridge
frame uint64_t Yes Current simulation frame number
timestamp double Yes Current simulation timestamp in seconds
actor void* Yes Opaque pointer to a CARLA actor for naming/callback registration
ros_name std::string Yes ROS2 topic name for the actor

Outputs

Name Type Description
ROS2 topics Various ROS2 message types Published sensor data, clock, and transform messages
VehicleControl carla::ros2::VehicleControl Vehicle control messages received from ROS2 subscribers

Usage Examples

// Enable the ROS2 bridge
auto ros2 = carla::ros2::ROS2::_instance;
ros2->Enable(true);

// Each simulation tick
ros2->SetFrame(frame_number);
ros2->SetTimestamp(simulation_time);

// Register actor ROS names
ros2->AddActorRosName(actor_ptr, "ego_vehicle");

Related Pages

Page Connections

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