@@ -525,6 +525,7 @@ void BaseRealSenseNode::frame_callback(rs2::frame frame)
525525 if (f.is <rs2::labeled_points>())
526526 {
527527 publishLabeledPointCloud (frame.as <rs2::labeled_points>(), t);
528+ publishMetadata (f, t, OPTICAL_FRAME_ID (sip));
528529 continue ;
529530 }
530531 if (f.is <rs2::points>())
@@ -595,6 +596,7 @@ void BaseRealSenseNode::frame_callback(rs2::frame frame)
595596 ROS_DEBUG (" Single labeled point cloud frame arrived (%s, %d). frame_number: %llu ; frame_TS: %f ; ros_TS(NSec): %lu" ,
596597 rs2_stream_to_string (stream_type), stream_index, frame.get_frame_number (), frame_time, t.nanoseconds ());
597598 publishLabeledPointCloud (frame.as <rs2::labeled_points>(), t);
599+ publishMetadata (frame, t, OPTICAL_FRAME_ID (sip));
598600 }
599601 _synced_imu_publisher->Resume ();
600602} // frame_callback
@@ -801,10 +803,18 @@ void BaseRealSenseNode::publishPointCloud(rs2::points pc, const rclcpp::Time& t,
801803 _pc_filter->Publish (pc, t, frameset, frame_id);
802804}
803805
806+ bool BaseRealSenseNode::isSCStream (const stream_index_pair& sip)
807+ {
808+ const rs2_stream stream = sip.first ;
809+ return (stream == RS2_STREAM_SAFETY || stream == RS2_STREAM_OCCUPANCY || stream == RS2_STREAM_LABELED_POINT_CLOUD);
810+ }
811+
804812void BaseRealSenseNode::publishLabeledPointCloud (rs2::labeled_points pc, const rclcpp::Time& t){
805813 if (0 == _labeled_pointcloud_publisher->get_subscription_count ())
806814 return ;
807815
816+ ROS_DEBUG (" Publishing Labeled Point Cloud Frame" );
817+
808818 // Create the PointCloud message
809819 sensor_msgs::msg::PointCloud2::UniquePtr msg_pointcloud = std::make_unique<sensor_msgs::msg::PointCloud2>();
810820
@@ -824,6 +834,9 @@ void BaseRealSenseNode::publishLabeledPointCloud(rs2::labeled_points pc, const r
824834 sensor_msgs::PointCloud2Iterator<float > iter_label (*msg_pointcloud, " label" );
825835 const rs2::vertex* vertex = pc.get_vertices ();
826836 const uint8_t * label = pc.get_labels ();
837+ msg_pointcloud->width = 320 ;
838+ msg_pointcloud->height = 180 ;
839+ msg_pointcloud->point_step = 3 *sizeof (float ) + sizeof (uint8_t );
827840 msg_pointcloud->row_step = msg_pointcloud->width * msg_pointcloud->point_step ;
828841 msg_pointcloud->data .resize (msg_pointcloud->height * msg_pointcloud->row_step );
829842
@@ -835,8 +848,9 @@ void BaseRealSenseNode::publishLabeledPointCloud(rs2::labeled_points pc, const r
835848 *iter_label = *label;
836849 ++iter_x; ++iter_y; ++iter_z; ++iter_label;
837850 }
851+
838852 msg_pointcloud->header .stamp = t;
839- msg_pointcloud->header .frame_id = " camera_link " ;
853+ msg_pointcloud->header .frame_id = OPTICAL_FRAME_ID (LABELED_POINT_CLOUD) ;
840854
841855 // Publish the PointCloud message
842856 _labeled_pointcloud_publisher->publish (std::move (msg_pointcloud));
@@ -918,58 +932,56 @@ void BaseRealSenseNode::publishFrame(rs2::frame f, const rclcpp::Time& t,
918932 image = fix_depth_scale (image, _depth_scaled_image[stream]);
919933 }
920934
921- if (info_publishers.find (stream) == info_publishers.end () ||
922- image_publishers.find (stream) == image_publishers.end ())
923- {
924- // Stream is already disabled.
925- return ;
926- }
927- auto & info_publisher = info_publishers.at (stream);
928- auto & image_publisher = image_publishers.at (stream);
929- if (0 != info_publisher->get_subscription_count () ||
930- 0 != image_publisher->get_subscription_count ())
935+ bool is_sc_stream = isSCStream (stream);
936+
937+ // if stream is on, and is not a SC stream, publish camereaInfo
938+ if (!is_sc_stream && info_publishers.find (stream) != info_publishers.end ())
931939 {
932- auto & cam_info = _camera_info .at (stream);
933- if (cam_info. width != width )
940+ auto & info_publisher = info_publishers .at (stream);
941+ if ( 0 != info_publisher-> get_subscription_count () )
934942 {
935- updateStreamCalibData (f.get_profile ().as <rs2::video_stream_profile>());
936- }
937- cam_info.header .stamp = t;
943+ auto & cam_info = _camera_info.at (stream);
944+ if (cam_info.width != width)
945+ {
946+ updateStreamCalibData (f.get_profile ().as <rs2::video_stream_profile>());
947+ }
948+ cam_info.header .stamp = t;
938949
939- // don't publish camera info messages for SC streams
940- auto stream_type = f.get_profile ().stream_type ();
941- if (stream_type != RS2_STREAM_SAFETY &&
942- stream_type != RS2_STREAM_OCCUPANCY &&
943- stream_type != RS2_STREAM_LABELED_POINT_CLOUD)
944- {
945950 info_publisher->publish (cam_info);
946951 }
952+ }
947953
948- // Prepare image topic to be published
949- // We use UniquePtr for allow intra-process publish when subscribers of that type are available
950- sensor_msgs::msg::Image::UniquePtr img (new sensor_msgs::msg::Image ());
951-
952- if (!img)
954+ if (image_publishers.find (stream) != image_publishers.end ())
955+ {
956+ auto &image_publisher = image_publishers.at (stream);
957+ if (0 != image_publisher->get_subscription_count ())
953958 {
954- ROS_ERROR ( " sensor image message allocation failed, frame was dropped " );
955- return ;
956- }
959+ // Prepare image topic to be published
960+ // We use UniquePtr for allow intra-process publish when subscribers of that type are available
961+ sensor_msgs::msg::Image::UniquePtr img ( new sensor_msgs::msg::Image ());
957962
958- // Convert the CV::Mat into a ROS image message (1 copy is done here)
959- cv_bridge::CvImage (std_msgs::msg::Header (), _encoding.at (bpp), image).toImageMsg (*img);
960-
961- // Convert OpenCV Mat to ROS Image
962- img->header .frame_id = OPTICAL_FRAME_ID (stream);
963- img->header .stamp = t;
964- img->height = height;
965- img->width = width;
966- img->is_bigendian = false ;
967- img->step = width * bpp;
968- // Transfer the unique pointer ownership to the RMW
969- sensor_msgs::msg::Image* msg_address = img.get ();
970- image_publisher->publish (std::move (img));
971-
972- ROS_DEBUG_STREAM (rs2_stream_to_string (f.get_profile ().stream_type ()) << " stream published, message address: " << std::hex << msg_address);
963+ if (!img)
964+ {
965+ ROS_ERROR (" sensor image message allocation failed, frame was dropped" );
966+ return ;
967+ }
968+
969+ // Convert the CV::Mat into a ROS image message (1 copy is done here)
970+ cv_bridge::CvImage (std_msgs::msg::Header (), _encoding.at (bpp), image).toImageMsg (*img);
971+
972+ // Convert OpenCV Mat to ROS Image
973+ img->header .frame_id = OPTICAL_FRAME_ID (stream);
974+ img->header .stamp = t;
975+ img->height = height;
976+ img->width = width;
977+ img->is_bigendian = false ;
978+ img->step = width * bpp;
979+ // Transfer the unique pointer ownership to the RMW
980+ sensor_msgs::msg::Image *msg_address = img.get ();
981+ image_publisher->publish (std::move (img));
982+
983+ ROS_DEBUG_STREAM (rs2_stream_to_string (f.get_profile ().stream_type ()) << " stream published, message address: " << std::hex << msg_address);
984+ }
973985 }
974986 if (is_publishMetadata)
975987 {
0 commit comments