@@ -145,11 +145,21 @@ K4AROSDevice::K4AROSDevice(const NodeHandle &n, const NodeHandle &p) : k4a_devic
145145
146146K4AROSDevice::~K4AROSDevice ()
147147{
148- stopCameras ();
149- stopImu () ;
148+ // Start tearing down the publisher threads
149+ running_ = false ;
150150
151+ // Join the publisher thread
152+ ROS_INFO (" Joining camera publisher thread" );
151153 frame_publisher_thread_.join ();
154+ ROS_INFO (" Camera publisher thread joined" );
155+
156+ // Join the publisher thread
157+ ROS_INFO (" Joining IMU publisher thread" );
152158 imu_publisher_thread_.join ();
159+ ROS_INFO (" IMU publisher thread joined" );
160+
161+ stopCameras ();
162+ stopImu ();
153163}
154164
155165
@@ -196,32 +206,16 @@ k4a_result_t K4AROSDevice::startImu()
196206
197207void K4AROSDevice::stopCameras ()
198208{
199- // Start tearing down the publisher thread
200- running_ = false ;
201-
202209 // Stop the K4A SDK
203210 ROS_INFO (" Stopping K4A device" );
204211 k4a_device_.stop_cameras ();
205212 ROS_INFO (" K4A device stopped" );
206-
207- // Join the publisher thread
208- ROS_INFO (" Joining camera publisher thread" );
209- frame_publisher_thread_.join ();
210- ROS_INFO (" Camera publisher thread joined" );
211213}
212214
213215
214216void K4AROSDevice::stopImu ()
215217{
216- // Start tearing down the publisher thread
217- running_ = false ;
218-
219218 k4a_device_.stop_imu ();
220-
221- // Join the publisher thread
222- ROS_INFO (" Joining IMU publisher thread" );
223- imu_publisher_thread_.join ();
224- ROS_INFO (" IMU publisher thread joined" );
225219}
226220
227221
@@ -681,46 +675,53 @@ void K4AROSDevice::framePublisherThread()
681675
682676 if (params_.depth_enabled )
683677 {
684- // IR images are available in all depth modes
685- result = getIrFrame (capture, ir_raw_frame);
686-
687- if (result != K4A_RESULT_SUCCEEDED)
678+ // Only do compute if we have subscribers
679+ if (ir_raw_publisher_.getNumSubscribers () > 0 )
688680 {
689- ROS_ERROR_STREAM (" Failed to get raw IR frame" );
690- ros::shutdown ();
691- return ;
692- }
693-
694- // Re-sychronize the timestamps with the capture timestamp
695- ir_raw_camera_info.header .stamp = capture_time;
696- ir_raw_frame->header .stamp = capture_time;
697- ir_raw_frame->header .frame_id = calibration_data_.depth_camera_frame_ ;
698-
699- ir_raw_publisher_.publish (ir_raw_frame);
700- ir_raw_camerainfo_publisher_.publish (ir_raw_camera_info);
701-
702- // Depth images are not available in PASSIVE_IR mode
703- if (calibration_data_.k4a_calibration_ .depth_mode != K4A_DEPTH_MODE_PASSIVE_IR)
704- {
705- result = getDepthFrame (capture, depth_raw_frame);
706-
681+ // IR images are available in all depth modes
682+ result = getIrFrame (capture, ir_raw_frame);
683+
707684 if (result != K4A_RESULT_SUCCEEDED)
708685 {
709- ROS_ERROR_STREAM (" Failed to get raw depth frame" );
686+ ROS_ERROR_STREAM (" Failed to get raw IR frame" );
710687 ros::shutdown ();
711688 return ;
712689 }
713690
714691 // Re-sychronize the timestamps with the capture timestamp
715- depth_raw_camera_info .header .stamp = capture_time;
716- depth_raw_frame ->header .stamp = capture_time;
717- depth_raw_frame ->header .frame_id = calibration_data_.depth_camera_frame_ ;
692+ ir_raw_camera_info .header .stamp = capture_time;
693+ ir_raw_frame ->header .stamp = capture_time;
694+ ir_raw_frame ->header .frame_id = calibration_data_.depth_camera_frame_ ;
718695
719- depth_raw_publisher_.publish (depth_raw_frame);
720- depth_raw_camerainfo_publisher_.publish (depth_raw_camera_info);
696+ ir_raw_publisher_.publish (ir_raw_frame);
697+ ir_raw_camerainfo_publisher_.publish (ir_raw_camera_info);
698+ }
721699
700+ // Depth images are not available in PASSIVE_IR mode
701+ if (calibration_data_.k4a_calibration_ .depth_mode != K4A_DEPTH_MODE_PASSIVE_IR)
702+ {
703+ if (depth_raw_publisher_.getNumSubscribers () > 0 )
704+ {
705+ result = getDepthFrame (capture, depth_raw_frame);
706+
707+ if (result != K4A_RESULT_SUCCEEDED)
708+ {
709+ ROS_ERROR_STREAM (" Failed to get raw depth frame" );
710+ ros::shutdown ();
711+ return ;
712+ }
713+
714+ // Re-sychronize the timestamps with the capture timestamp
715+ depth_raw_camera_info.header .stamp = capture_time;
716+ depth_raw_frame->header .stamp = capture_time;
717+ depth_raw_frame->header .frame_id = calibration_data_.depth_camera_frame_ ;
718+
719+ depth_raw_publisher_.publish (depth_raw_frame);
720+ depth_raw_camerainfo_publisher_.publish (depth_raw_camera_info);
721+ }
722+
722723 // We can only rectify the depth into the color co-ordinates if the color camera is enabled!
723- if (params_.color_enabled )
724+ if (params_.color_enabled && (depth_rect_publisher_. getNumSubscribers () > 0 ) )
724725 {
725726 result = getDepthFrame (capture, depth_rect_frame, true /* rectified */ );
726727
@@ -744,25 +745,31 @@ void K4AROSDevice::framePublisherThread()
744745
745746 if (params_.color_enabled )
746747 {
747- result = getRbgFrame (capture, rgb_raw_frame);
748-
749- if (result != K4A_RESULT_SUCCEEDED)
748+ if (rgb_raw_publisher_.getNumSubscribers () > 0 )
750749 {
751- ROS_ERROR_STREAM (" Failed to get RGB frame" );
752- ros::shutdown ();
753- return ;
754- }
750+ result = getRbgFrame (capture, rgb_raw_frame);
751+
752+ if (result != K4A_RESULT_SUCCEEDED)
753+ {
754+ ROS_ERROR_STREAM (" Failed to get RGB frame" );
755+ ros::shutdown ();
756+ return ;
757+ }
755758
756- rgb_raw_frame->header .stamp = capture_time;
757- rgb_raw_frame->header .frame_id = calibration_data_.rgb_camera_frame_ ;
758- rgb_raw_publisher_.publish (rgb_raw_frame);
759+ rgb_raw_frame->header .stamp = capture_time;
760+ rgb_raw_frame->header .frame_id = calibration_data_.rgb_camera_frame_ ;
761+ rgb_raw_publisher_.publish (rgb_raw_frame);
759762
760- // Re-synchronize the header timestamps since we cache the camera calibration message
761- rgb_raw_camera_info.header .stamp = capture_time;
762- rgb_raw_camerainfo_publisher_.publish (rgb_raw_camera_info);
763+ // Re-synchronize the header timestamps since we cache the camera calibration message
764+ rgb_raw_camera_info.header .stamp = capture_time;
765+ rgb_raw_camerainfo_publisher_.publish (rgb_raw_camera_info);
766+ }
763767
764- // We can only rectify the color into the depth co-ordinates if the color camera is enabled!
765- if (params_.depth_enabled && (calibration_data_.k4a_calibration_ .depth_mode != K4A_DEPTH_MODE_PASSIVE_IR))
768+ // We can only rectify the color into the depth co-ordinates if the depth camera is
769+ // enabled and processing depth data
770+ if (params_.depth_enabled &&
771+ (calibration_data_.k4a_calibration_ .depth_mode != K4A_DEPTH_MODE_PASSIVE_IR) &&
772+ (rgb_rect_publisher_.getNumSubscribers () > 0 ))
766773 {
767774 result = getRbgFrame (capture, rgb_rect_frame, true /* rectified */ );
768775
@@ -783,35 +790,38 @@ void K4AROSDevice::framePublisherThread()
783790 }
784791 }
785792
786- if (params_. rgb_point_cloud )
793+ if (pointcloud_publisher_. getNumSubscribers () > 0 )
787794 {
788- result = getRgbPointCloud (capture, point_cloud);
789-
790- if (result != K4A_RESULT_SUCCEEDED)
795+ if (params_.rgb_point_cloud )
791796 {
792- ROS_ERROR_STREAM (" Failed to get RGB Point Cloud" );
793- ros::shutdown ();
794- return ;
797+ result = getRgbPointCloud (capture, point_cloud);
798+
799+ if (result != K4A_RESULT_SUCCEEDED)
800+ {
801+ ROS_ERROR_STREAM (" Failed to get RGB Point Cloud" );
802+ ros::shutdown ();
803+ return ;
804+ }
795805 }
796- }
797- else if (params_.point_cloud )
798- {
799- result = getPointCloud (capture, point_cloud);
800-
801- if (result != K4A_RESULT_SUCCEEDED)
806+ else if (params_.point_cloud )
802807 {
803- ROS_ERROR_STREAM (" Failed to get Point Cloud" );
804- ros::shutdown ();
805- return ;
808+ result = getPointCloud (capture, point_cloud);
809+
810+ if (result != K4A_RESULT_SUCCEEDED)
811+ {
812+ ROS_ERROR_STREAM (" Failed to get Point Cloud" );
813+ ros::shutdown ();
814+ return ;
815+ }
806816 }
807- }
808817
809- if (params_.point_cloud || params_.rgb_point_cloud )
810- {
811- point_cloud->header .stamp = capture_time;
812- pointcloud_publisher_.publish (point_cloud);
818+ if (params_.point_cloud || params_.rgb_point_cloud )
819+ {
820+ point_cloud->header .stamp = capture_time;
821+ pointcloud_publisher_.publish (point_cloud);
822+ }
813823 }
814-
824+
815825 ros::spinOnce ();
816826 loop_rate.sleep ();
817827 }
0 commit comments