Skip to content

Commit d8633af

Browse files
committed
Use a separate thread to pop k4abt_tracker result. Fix microsoft#109.
1 parent e35f236 commit d8633af

File tree

2 files changed

+86
-52
lines changed

2 files changed

+86
-52
lines changed

include/azure_kinect_ros_driver/k4a_ros_device.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ class K4AROSDevice
8686
sensor_msgs::PointCloud2Ptr& point_cloud);
8787

8888
void framePublisherThread();
89+
#if defined(K4A_BODY_TRACKING)
90+
void bodyPublisherThread();
91+
#endif
8992
void imuPublisherThread();
9093

9194
// Gets a timestap from one of the captures images
@@ -155,6 +158,8 @@ class K4AROSDevice
155158
#if defined(K4A_BODY_TRACKING)
156159
// Body tracker
157160
k4abt::tracker k4abt_tracker_;
161+
std::atomic_int16_t k4abt_tracker_queue_size_;
162+
std::thread body_publisher_thread_;
158163
#endif
159164

160165
std::chrono::nanoseconds device_to_realtime_offset_{0};

src/k4a_ros_device.cpp

Lines changed: 81 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ K4AROSDevice::K4AROSDevice(const NodeHandle& n, const NodeHandle& p)
3838
// clang-format off
3939
#if defined(K4A_BODY_TRACKING)
4040
k4abt_tracker_(nullptr),
41+
k4abt_tracker_queue_size_(0),
4142
#endif
4243
// clang-format on
4344
node_(n),
@@ -252,6 +253,13 @@ K4AROSDevice::~K4AROSDevice()
252253
// Start tearing down the publisher threads
253254
running_ = false;
254255

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+
255263
// Join the publisher thread
256264
ROS_INFO("Joining camera publisher thread");
257265
frame_publisher_thread_.join();
@@ -328,6 +336,9 @@ k4a_result_t K4AROSDevice::startCameras()
328336

329337
// Start the thread that will poll the cameras and publish frames
330338
frame_publisher_thread_ = thread(&K4AROSDevice::framePublisherThread, this);
339+
#if defined(K4A_BODY_TRACKING)
340+
body_publisher_thread_ = thread(&K4AROSDevice::bodyPublisherThread, this);
341+
#endif
331342

332343
return K4A_RESULT_SUCCEEDED;
333344
}
@@ -977,8 +988,6 @@ void K4AROSDevice::framePublisherThread()
977988
if (params_.body_tracking_enabled &&
978989
(body_marker_publisher_.getNumSubscribers() > 0 || body_index_map_publisher_.getNumSubscribers() > 0))
979990
{
980-
capture_time = timestampToROS(capture.get_depth_image().get_device_timestamp());
981-
982991
if (!k4abt_tracker_.enqueue_capture(capture))
983992
{
984993
ROS_ERROR("Error! Add capture to tracker process queue failed!");
@@ -987,56 +996,7 @@ void K4AROSDevice::framePublisherThread()
987996
}
988997
else
989998
{
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_;
10401000
}
10411001
}
10421002
#endif
@@ -1183,6 +1143,75 @@ void K4AROSDevice::framePublisherThread()
11831143
}
11841144
}
11851145

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+
11861215
k4a_imu_sample_t K4AROSDevice::computeMeanIMUSample(const std::vector<k4a_imu_sample_t>& samples)
11871216
{
11881217
// Compute mean sample

0 commit comments

Comments
 (0)