From 680f9ec477daffc5b705c2eb7ec4e83f0b50627d Mon Sep 17 00:00:00 2001 From: Kenji Brameld Date: Tue, 19 Aug 2025 18:27:39 +0000 Subject: [PATCH] use point cloud transport to allow compressed point clouds to be sent across topics --- realsense2_camera/CMakeLists.txt | 9 ++++++--- realsense2_camera/include/pointcloud_filter.h | 9 ++++++--- realsense2_camera/package.xml | 7 ++++--- realsense2_camera/src/pointcloud_filter.cpp | 8 ++++---- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/realsense2_camera/CMakeLists.txt b/realsense2_camera/CMakeLists.txt index f8a66a6a1..f1ab891b0 100644 --- a/realsense2_camera/CMakeLists.txt +++ b/realsense2_camera/CMakeLists.txt @@ -120,6 +120,7 @@ find_package(tf2_ros REQUIRED) find_package(tf2 REQUIRED) find_package(diagnostic_updater REQUIRED) find_package(OpenCV REQUIRED COMPONENTS core) +find_package(point_cloud_transport REQUIRED) find_package(realsense2 2.56) if (BUILD_ACCELERATE_GPU_WITH_GLSL) @@ -263,6 +264,7 @@ set(dependencies tf2 tf2_ros diagnostic_updater + point_cloud_transport ) set (targets @@ -278,6 +280,7 @@ set (targets tf2::tf2 tf2_ros::tf2_ros diagnostic_updater::diagnostic_updater + point_cloud_transport::point_cloud_transport ) if (BUILD_ACCELERATE_GPU_WITH_GLSL) @@ -293,7 +296,7 @@ if(USE_LIFECYCLE_NODE) find_package(rclcpp_lifecycle REQUIRED) find_package(lifecycle_msgs REQUIRED) list(APPEND dependencies rclcpp_lifecycle lifecycle_msgs) - list(APPEND targets + list(APPEND targets rclcpp_lifecycle::rclcpp_lifecycle lifecycle_msgs::lifecycle_msgs__rosidl_typesupport_cpp) add_definitions(-DUSE_LIFECYCLE_NODE) @@ -342,13 +345,13 @@ install( ) # Install launch files -install(DIRECTORY +install(DIRECTORY launch DESTINATION share/${PROJECT_NAME} ) # Install example files -install(DIRECTORY +install(DIRECTORY examples DESTINATION share/${PROJECT_NAME} ) diff --git a/realsense2_camera/include/pointcloud_filter.h b/realsense2_camera/include/pointcloud_filter.h index 94c8b0896..81fdc47b9 100644 --- a/realsense2_camera/include/pointcloud_filter.h +++ b/realsense2_camera/include/pointcloud_filter.h @@ -17,9 +17,11 @@ #include #include #include +#include +#include +#include #include #include -#include #include "named_filter.h" namespace realsense2_camera @@ -28,7 +30,7 @@ namespace realsense2_camera { public: PointcloudFilter(std::shared_ptr filter, RosNodeBase& node, std::shared_ptr parameters, rclcpp::Logger logger, bool is_enabled=false); - + void setPublisher(); void Publish(rs2::points pc, const rclcpp::Time& t, const rs2::frameset& frameset, const std::string& frame_id); @@ -41,7 +43,8 @@ namespace realsense2_camera bool _allow_no_texture_points; bool _ordered_pc; std::mutex _mutex_publisher; - rclcpp::Publisher::SharedPtr _pointcloud_publisher; + std::shared_ptr _pct; + std::shared_ptr _pointcloud_publisher; std::string _pointcloud_qos; }; } diff --git a/realsense2_camera/package.xml b/realsense2_camera/package.xml index 44a8b4d98..a7b7622d9 100644 --- a/realsense2_camera/package.xml +++ b/realsense2_camera/package.xml @@ -6,12 +6,12 @@ RealSense camera package allowing access to Intel D400 3D cameras LibRealSense ROS Team Apache License 2.0 - + http://www.ros.org/wiki/RealSense https://github.com/intel-ros/realsense/issues LibRealSense ROS Team - + ament_cmake eigen builtin_interfaces @@ -34,6 +34,7 @@ tf2 tf2_ros diagnostic_updater + point_cloud_transport ament_cmake_gtest launch_testing ament_cmake_pytest @@ -45,7 +46,7 @@ python3-requests tf2_ros_py ros2topic - + launch_ros ros_environment diff --git a/realsense2_camera/src/pointcloud_filter.cpp b/realsense2_camera/src/pointcloud_filter.cpp index 27c924542..4104c8bb9 100644 --- a/realsense2_camera/src/pointcloud_filter.cpp +++ b/realsense2_camera/src/pointcloud_filter.cpp @@ -69,9 +69,9 @@ void PointcloudFilter::setPublisher() std::lock_guard lock_guard(_mutex_publisher); if ((_is_enabled) && (!_pointcloud_publisher)) { - _pointcloud_publisher = _node.create_publisher("~/depth/color/points", - rclcpp::QoS(rclcpp::QoSInitialization::from_rmw(qos_string_to_qos(_pointcloud_qos)), - qos_string_to_qos(_pointcloud_qos))); + _pct = std::make_shared(_node.shared_from_this()); + _pointcloud_publisher = std::make_shared( + _pct->advertise("~/depth/color/points", qos_string_to_qos(_pointcloud_qos))); } else if ((!_is_enabled) && (_pointcloud_publisher)) { @@ -92,7 +92,7 @@ void PointcloudFilter::Publish(rs2::points pc, const rclcpp::Time& t, const rs2: { { std::lock_guard lock_guard(_mutex_publisher); - if ((!_pointcloud_publisher) || (!(_pointcloud_publisher->get_subscription_count()))) + if ((!_pointcloud_publisher) || (!(_pointcloud_publisher->getNumSubscribers()))) return; } rs2_stream texture_source_id = static_cast(_filter->get_option(rs2_option::RS2_OPTION_STREAM_FILTER));