@@ -38,6 +38,7 @@ K4AROSDevice::K4AROSDevice(const NodeHandle& n, const NodeHandle& p)
38
38
// clang-format off
39
39
#if defined(K4A_BODY_TRACKING)
40
40
k4abt_tracker_ (nullptr ),
41
+ k4abt_tracker_queue_size_(0 ),
41
42
#endif
42
43
// clang-format on
43
44
node_ (n),
@@ -252,6 +253,13 @@ K4AROSDevice::~K4AROSDevice()
252
253
// Start tearing down the publisher threads
253
254
running_ = false ;
254
255
256
+ #if defined(K4A_BODY_TRACKING)
257
+ // Join the publisher thread
258
+ ROS_INFO (" Joining body publisher thread" );
259
+ body_publisher_thread_.join ();
260
+ ROS_INFO (" Body publisher thread joined" );
261
+ #endif
262
+
255
263
// Join the publisher thread
256
264
ROS_INFO (" Joining camera publisher thread" );
257
265
frame_publisher_thread_.join ();
@@ -328,6 +336,9 @@ k4a_result_t K4AROSDevice::startCameras()
328
336
329
337
// Start the thread that will poll the cameras and publish frames
330
338
frame_publisher_thread_ = thread (&K4AROSDevice::framePublisherThread, this );
339
+ #if defined(K4A_BODY_TRACKING)
340
+ body_publisher_thread_ = thread (&K4AROSDevice::bodyPublisherThread, this );
341
+ #endif
331
342
332
343
return K4A_RESULT_SUCCEEDED;
333
344
}
@@ -977,8 +988,6 @@ void K4AROSDevice::framePublisherThread()
977
988
if (params_.body_tracking_enabled &&
978
989
(body_marker_publisher_.getNumSubscribers () > 0 || body_index_map_publisher_.getNumSubscribers () > 0 ))
979
990
{
980
- capture_time = timestampToROS (capture.get_depth_image ().get_device_timestamp ());
981
-
982
991
if (!k4abt_tracker_.enqueue_capture (capture))
983
992
{
984
993
ROS_ERROR (" Error! Add capture to tracker process queue failed!" );
@@ -987,56 +996,7 @@ void K4AROSDevice::framePublisherThread()
987
996
}
988
997
else
989
998
{
990
- k4abt::frame body_frame = k4abt_tracker_.pop_result ();
991
- if (body_frame == nullptr )
992
- {
993
- ROS_ERROR_STREAM (" Pop body frame result failed!" );
994
- ros::shutdown ();
995
- return ;
996
- }
997
- else
998
- {
999
- if (body_marker_publisher_.getNumSubscribers () > 0 )
1000
- {
1001
- // Joint marker array
1002
- MarkerArrayPtr markerArrayPtr (new MarkerArray);
1003
- auto num_bodies = body_frame.get_num_bodies ();
1004
- for (size_t i = 0 ; i < num_bodies; ++i)
1005
- {
1006
- k4abt_body_t body = body_frame.get_body (i);
1007
- for (int j = 0 ; j < (int ) K4ABT_JOINT_COUNT; ++j)
1008
- {
1009
- MarkerPtr markerPtr (new Marker);
1010
- getBodyMarker (body, markerPtr, j, capture_time);
1011
- markerArrayPtr->markers .push_back (*markerPtr);
1012
- }
1013
- }
1014
- body_marker_publisher_.publish (markerArrayPtr);
1015
- }
1016
-
1017
- if (body_index_map_publisher_.getNumSubscribers () > 0 )
1018
- {
1019
- // Body index map
1020
- ImagePtr body_index_map_frame (new Image);
1021
- result = getBodyIndexMap (body_frame, body_index_map_frame);
1022
-
1023
- if (result != K4A_RESULT_SUCCEEDED)
1024
- {
1025
- ROS_ERROR_STREAM (" Failed to get body index map" );
1026
- ros::shutdown ();
1027
- return ;
1028
- }
1029
- else if (result == K4A_RESULT_SUCCEEDED)
1030
- {
1031
- // Re-sychronize the timestamps with the capture timestamp
1032
- body_index_map_frame->header .stamp = capture_time;
1033
- body_index_map_frame->header .frame_id =
1034
- calibration_data_.tf_prefix_ + calibration_data_.depth_camera_frame_ ;
1035
-
1036
- body_index_map_publisher_.publish (body_index_map_frame);
1037
- }
1038
- }
1039
- }
999
+ ++k4abt_tracker_queue_size_;
1040
1000
}
1041
1001
}
1042
1002
#endif
@@ -1183,6 +1143,75 @@ void K4AROSDevice::framePublisherThread()
1183
1143
}
1184
1144
}
1185
1145
1146
+ #if defined(K4A_BODY_TRACKING)
1147
+ void K4AROSDevice::bodyPublisherThread ()
1148
+ {
1149
+ while (running_ && ros::ok () && !ros::isShuttingDown ())
1150
+ {
1151
+ if (k4abt_tracker_queue_size_ > 0 )
1152
+ {
1153
+ k4abt::frame body_frame = k4abt_tracker_.pop_result ();
1154
+ --k4abt_tracker_queue_size_;
1155
+ auto capture_time = timestampToROS (body_frame.get_device_timestamp ());
1156
+
1157
+ if (body_frame == nullptr )
1158
+ {
1159
+ ROS_ERROR_STREAM (" Pop body frame result failed!" );
1160
+ ros::shutdown ();
1161
+ return ;
1162
+ }
1163
+ else
1164
+ {
1165
+ if (body_marker_publisher_.getNumSubscribers () > 0 )
1166
+ {
1167
+ // Joint marker array
1168
+ MarkerArrayPtr markerArrayPtr (new MarkerArray);
1169
+ auto num_bodies = body_frame.get_num_bodies ();
1170
+ for (size_t i = 0 ; i < num_bodies; ++i)
1171
+ {
1172
+ k4abt_body_t body = body_frame.get_body (i);
1173
+ for (int j = 0 ; j < (int ) K4ABT_JOINT_COUNT; ++j)
1174
+ {
1175
+ MarkerPtr markerPtr (new Marker);
1176
+ getBodyMarker (body, markerPtr, j, capture_time);
1177
+ markerArrayPtr->markers .push_back (*markerPtr);
1178
+ }
1179
+ }
1180
+ body_marker_publisher_.publish (markerArrayPtr);
1181
+ }
1182
+
1183
+ if (body_index_map_publisher_.getNumSubscribers () > 0 )
1184
+ {
1185
+ // Body index map
1186
+ ImagePtr body_index_map_frame (new Image);
1187
+ auto result = getBodyIndexMap (body_frame, body_index_map_frame);
1188
+
1189
+ if (result != K4A_RESULT_SUCCEEDED)
1190
+ {
1191
+ ROS_ERROR_STREAM (" Failed to get body index map" );
1192
+ ros::shutdown ();
1193
+ return ;
1194
+ }
1195
+ else if (result == K4A_RESULT_SUCCEEDED)
1196
+ {
1197
+ // Re-sychronize the timestamps with the capture timestamp
1198
+ body_index_map_frame->header .stamp = capture_time;
1199
+ body_index_map_frame->header .frame_id =
1200
+ calibration_data_.tf_prefix_ + calibration_data_.depth_camera_frame_ ;
1201
+
1202
+ body_index_map_publisher_.publish (body_index_map_frame);
1203
+ }
1204
+ }
1205
+ }
1206
+ }
1207
+ else
1208
+ {
1209
+ std::this_thread::sleep_for (std::chrono::milliseconds{20 });
1210
+ }
1211
+ }
1212
+ }
1213
+ #endif
1214
+
1186
1215
k4a_imu_sample_t K4AROSDevice::computeMeanIMUSample (const std::vector<k4a_imu_sample_t >& samples)
1187
1216
{
1188
1217
// Compute mean sample
0 commit comments