Skip to content

Add support for cameras over ROS 2 - #866

Closed
Yadunund wants to merge 4 commits into
huggingface:mainfrom
Yadunund:ros2_camera
Closed

Yadunund wants to merge 4 commits into
huggingface:mainfrom
Yadunund:ros2_camera

Conversation

@Yadunund

@Yadunund Yadunund commented Mar 16, 2025 •

Copy link
Copy Markdown
Contributor

What this does

Capture images over ROS 2!

There are so many cameras with ROS 2 drivers! Support all of them in lerobot with a single camera type that this PR introduces!

ros2camera

This PR introduced the ROS2Camera which can be configured using the ROS2CameraConfig class.

  • Each topic is treated as a separate camera and requires an instance of ROS2Camera
  • A single ROS 2 node is used to create subscriptions for all topics/cameras.
  • Assumes each image is published as a sensor_msgs::msg::Image message.
  • Supports subscribing to depth images although recording a dataset is not supported yet (see existing todo in image_array_to_pil_image)

Example camera config:

    cameras: dict[str, CameraConfig] = field(
        default_factory=lambda: {
            "rs_camera_color": ROS2CameraConfig(
                topic="/rs/color/image_raw",
                fps=30,
                width=640,
                height=480,
                channels=3,
            ),
            "rs_camera_depth": ROS2CameraConfig(
                topic="/rs/depth/image_rect_raw",
                fps=30,
                width=640,
                height=480,
                channels=1,
            ),
            "oak_color_camera": ROS2CameraConfig(
                topic="/oak/rgb/image_raw",
                fps=10,
                width=1280,
                height=720,
                encoding="rgb8",
                channels=3,
            ),
            "oak_camera_depth": ROS2CameraConfig(
                topic="/oak/stereo/image_raw",
                fps=10,
                width=1280,
                height=720,
                channels=1,
            ),
        }
    )

See dataset recorded from Realsense and OakD cameras:

Other datasets and policies generated using this pipeline.

Note: All parameters apart from topic (fps, width, height, etc) have no effect as these are dictated by the ROS 2 driver. However they are needed as attributes in the ROS2CameraConfig class by other parts of the stack before the first frame is read. Also looking for suggestions on how to improve the design there.

How it was tested

Start a ROS 2 camera driver, eg. Realsense

source /opt/ros/jazzy/setup.bash
ros2 launch realsense2_camera rs_launch.py rgb_camera.color_profile:=640x480x30 depth_module.depth_profile:=640x480x30

Start a ROS 2 camera driver, eg. OakD (depthai)

source /opt/ros/jazzy/setup.bash
ros2 launch depthai_ros_driver camera.launch.py

Update cameras dict in of the robot configs to include a ROS2CameraConfig entries as described above. Remove the depth topics as this is not yet supported in image_array_to_pil_image

Source ROS 2 and record an episode.

source /opt/ros/jazzy/setup.bash
python3 lerobot/scripts/control_robot.py   --robot.type=so100   --control.type=record   --control.fps=30   --control.single_task="Test ROS2Camera"   --control.repo_id=${HF_USER}/test_ros2_camera   --control.tags='["so100"]'   --control.warmup_time_s=10   --control.episode_time_s=10   --control.reset_time_s=5   --control.num_episodes=1   --control.push_to_hub=true --control.display_data=true

TODO

  • Add mock

@Yadunund
Yadunund marked this pull request as draft March 16, 2025 06:54
@imstevenpmwork imstevenpmwork added enhancement Suggestions for new features or improvements robots Issues concerning robots HW interfaces labels Mar 17, 2025
@imstevenpmwork
imstevenpmwork self-requested a review March 17, 2025 09:35

@youliangtan youliangtan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Yadunund Happy to review this. Drop down a few comments here

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we support multiple images here?

The test code is located in here:

@pytest.mark.parametrize("camera_type, mock", TEST_CAMERA_TYPES)
@require_camera
def test_save_images_from_cameras(tmp_path, request, camera_type, mock):
# TODO(rcadene): refactor
if camera_type == "opencv":
from lerobot.common.robot_devices.cameras.opencv import save_images_from_cameras
elif camera_type == "intelrealsense":
from lerobot.common.robot_devices.cameras.intelrealsense import save_images_from_cameras
# Small `record_time_s` to speedup unit tests
save_images_from_cameras(tmp_path, record_time_s=0.02, mock=mock)

@Yadunund

Yadunund commented May 4, 2025 •

Copy link
Copy Markdown
Contributor Author

@youliangtan thanks a lot for the review.

Correct me if i'm wrong, but the torch.from_numpy() calls here and here only work on a single ndarray. If async_read() returns a tuple, that call would throw an error.

Which makes me wonder how the existing intelrealsense implementation works without an error as its async_read() implementation returns a tuple of ndarrays if use_depth=True.

To handle tuples, I would imagine the implementation in manipulator.py should be something like

if isinstance(images[name], tuple):
    # Handle tuple case
    images[name] = tuple(torch.from_numpy(arr) for arr in images[name] if isinstance(arr, np.ndarray))
else:
    # Handle single ndarray case
    images[name] = torch.from_numpy(images[name])

This brings up the question again on whether we should have users include a separate ROS2CameraConfig for every ROS 2 topic. This would treat each topic as a separate camera whose aync_read() call would return a single ndarray. I can make the changes you suggested to treat the ROS 2 node as a static instance that is shared across cameras.

Alternatively, we could have users configure a single ROS2CameraConfig with a config schema that captures information about every topic like below

            "camera": ROS2CameraConfig([
                {
                    "topic": "/camera/color/image_raw",
                    "fps": 30,
                    "width": 640,
                    "height": 480,
                },
                {
                    "topic": "/camera/depth/image_rect_raw",
                    "fps": 30,
                    "width": 640,
                    "height": 480,
                },
                ]
            ),

Then async_read() will return a tuple of ndarrays and manipulator.py will handle the tuple case as described above.

What do you think?

Yadunund and others added 3 commits May 3, 2025 20:44
Signed-off-by: Yadunund <yadunund@gmail.com>
Signed-off-by: Yadunund <yadunund@gmail.com>
@Yadunund
Yadunund force-pushed the ros2_camera branch 2 times, most recently from f2ad7f6 to 5ccaaba Compare May 4, 2025 08:18
@Yadunund

Yadunund commented May 4, 2025

Copy link
Copy Markdown
Contributor Author

I went ahead with having a separate ROS2CameraConfig for every ROS 2 topic! I've updated the PR description and included a recorded dataset.

Signed-off-by: Yadunund <yadunund@gmail.com>
@youliangtan

youliangtan commented May 5, 2025 •

Copy link
Copy Markdown
Contributor

Good catch @Yadunund. I just checked the code, I think the realsense impl with config.use_depth = True is not being used, and I do think this is a bug when depth is enabled. Maybe should raise an issue ticket for this 🤔

Also, I think all cameras in the original lerobot code should derive from the abstract class Camera, which specified the required functions and data types.

Btw, feel free change this from draft to ready-to-review 👍

@Woojin-Crive

Copy link
Copy Markdown
Contributor

I’m working on the same feature as well. It would be great if our source code could contribute something to the development.
main...ROBOTIS-GIT:lerobot:feature-ros-wrapper

@masonhensley

Copy link
Copy Markdown

I was planning on working on a Luxonix Oak Depth PR next week. This is a better approach.

Nice!

@Yadunund

Yadunund commented Jun 23, 2025 •

Copy link
Copy Markdown
Contributor Author

@Woojin-Crive ,

I starting working on the refactor of this PR to adapt to the new lerobot codebase. The changes are https://github.com/Yadunund/lerobot/tree/ros2_camera-refactor. I'll update this PR / open a new one after i've had a chance to test the changes. But if anyone want's to give it a spin, i'd appreciate it a lot!

@masonhensley I think the cornerstone of ROS is standardizing interfaces for different classes of hardware so having a generic "ROS2" integration that works across different camera brands over these interfaces is likely more scalable as you've pointed out.

Related, @ycheng517 has a PR open to command robots over ROS 2 control interfaces: #1280. That could also use some review / testing :)

@pkooij

pkooij commented Oct 21, 2026

Copy link
Copy Markdown
Member

Thanks for the ROS 2 camera work! We’d prefer the ROS-dependent backend in a community camera plugin. Please coordinate with the ROS 2 integration discussion in #4368 and list the package on our third-party sensors page. Closing this native backend proposal.

@pkooij pkooij closed this Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Suggestions for new features or improvements robots Issues concerning robots HW interfaces

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants