Skip to content
This repository was archived by the owner on Jul 26, 2024. It is now read-only.

Commit f80aca0

Browse files
authored
Merge branch 'melodic' into patch-1
2 parents d9efea6 + 3912711 commit f80aca0

File tree

2 files changed

+88
-93
lines changed

2 files changed

+88
-93
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
@@ -82,6 +82,9 @@ class K4AROSDevice
8282
sensor_msgs::PointCloud2Ptr& point_cloud);
8383

8484
void framePublisherThread();
85+
#if defined(K4A_BODY_TRACKING)
86+
void bodyPublisherThread();
87+
#endif
8588
void imuPublisherThread();
8689

8790
// Gets a timestap from one of the captures images
@@ -157,6 +160,8 @@ class K4AROSDevice
157160
#if defined(K4A_BODY_TRACKING)
158161
// Body tracker
159162
k4abt::tracker k4abt_tracker_;
163+
std::atomic_int16_t k4abt_tracker_queue_size_;
164+
std::thread body_publisher_thread_;
160165
#endif
161166

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

src/k4a_ros_device.cpp

Lines changed: 83 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ K4AROSDevice::K4AROSDevice(const NodeHandle& n, const NodeHandle& p)
5656
// clang-format off
5757
#if defined(K4A_BODY_TRACKING)
5858
k4abt_tracker_(nullptr),
59+
k4abt_tracker_queue_size_(0),
5960
#endif
6061
// clang-format on
6162
node_(n),
@@ -312,6 +313,13 @@ K4AROSDevice::~K4AROSDevice()
312313
// Start tearing down the publisher threads
313314
running_ = false;
314315

316+
#if defined(K4A_BODY_TRACKING)
317+
// Join the publisher thread
318+
ROS_INFO("Joining body publisher thread");
319+
body_publisher_thread_.join();
320+
ROS_INFO("Body publisher thread joined");
321+
#endif
322+
315323
// Join the publisher thread
316324
ROS_INFO("Joining camera publisher thread");
317325
frame_publisher_thread_.join();
@@ -388,6 +396,9 @@ k4a_result_t K4AROSDevice::startCameras()
388396

389397
// Start the thread that will poll the cameras and publish frames
390398
frame_publisher_thread_ = thread(&K4AROSDevice::framePublisherThread, this);
399+
#if defined(K4A_BODY_TRACKING)
400+
body_publisher_thread_ = thread(&K4AROSDevice::bodyPublisherThread, this);
401+
#endif
391402

392403
return K4A_RESULT_SUCCEEDED;
393404
}
@@ -596,7 +607,6 @@ k4a_result_t K4AROSDevice::getRgbPointCloudInDepthFrame(const k4a::capture& capt
596607

597608
point_cloud->header.frame_id = calibration_data_.tf_prefix_ + calibration_data_.depth_camera_frame_;
598609
point_cloud->header.stamp = timestampToROS(k4a_depth_frame.get_device_timestamp());
599-
printTimestampDebugMessage("RGB point cloud", point_cloud->header.stamp);
600610

601611
return fillColorPointCloud(calibration_data_.point_cloud_image_, calibration_data_.transformed_rgb_image_,
602612
point_cloud);
@@ -629,7 +639,6 @@ k4a_result_t K4AROSDevice::getRgbPointCloudInRgbFrame(const k4a::capture& captur
629639

630640
point_cloud->header.frame_id = calibration_data_.tf_prefix_ + calibration_data_.rgb_camera_frame_;
631641
point_cloud->header.stamp = timestampToROS(k4a_depth_frame.get_device_timestamp());
632-
printTimestampDebugMessage("RGB point cloud", point_cloud->header.stamp);
633642

634643
return fillColorPointCloud(calibration_data_.point_cloud_image_, k4a_bgra_frame, point_cloud);
635644
}
@@ -646,7 +655,6 @@ k4a_result_t K4AROSDevice::getPointCloud(const k4a::capture& capture, sensor_msg
646655

647656
point_cloud->header.frame_id = calibration_data_.tf_prefix_ + calibration_data_.depth_camera_frame_;
648657
point_cloud->header.stamp = timestampToROS(k4a_depth_frame.get_device_timestamp());
649-
printTimestampDebugMessage("Point cloud", point_cloud->header.stamp);
650658

651659
// Tranform depth image to point cloud
652660
calibration_data_.k4a_transformation_.depth_image_to_point_cloud(k4a_depth_frame, K4A_CALIBRATION_TYPE_DEPTH,
@@ -758,7 +766,6 @@ k4a_result_t K4AROSDevice::getImuFrame(const k4a_imu_sample_t& sample, sensor_ms
758766
{
759767
imu_msg->header.frame_id = calibration_data_.tf_prefix_ + calibration_data_.imu_frame_;
760768
imu_msg->header.stamp = timestampToROS(sample.acc_timestamp_usec);
761-
printTimestampDebugMessage("IMU", imu_msg->header.stamp);
762769

763770
// The correct convention in ROS is to publish the raw sensor data, in the
764771
// sensor coordinate frame. Do that here.
@@ -989,7 +996,6 @@ void K4AROSDevice::framePublisherThread()
989996
else if (result == K4A_RESULT_SUCCEEDED)
990997
{
991998
capture_time = timestampToROS(capture.get_ir_image().get_device_timestamp());
992-
printTimestampDebugMessage("IR image", capture_time);
993999

9941000
// Re-sychronize the timestamps with the capture timestamp
9951001
ir_raw_camera_info.header.stamp = capture_time;
@@ -1021,7 +1027,6 @@ void K4AROSDevice::framePublisherThread()
10211027
else if (result == K4A_RESULT_SUCCEEDED)
10221028
{
10231029
capture_time = timestampToROS(capture.get_depth_image().get_device_timestamp());
1024-
printTimestampDebugMessage("Depth image", capture_time);
10251030

10261031
// Re-sychronize the timestamps with the capture timestamp
10271032
depth_raw_camera_info.header.stamp = capture_time;
@@ -1053,7 +1058,6 @@ void K4AROSDevice::framePublisherThread()
10531058
else if (result == K4A_RESULT_SUCCEEDED)
10541059
{
10551060
capture_time = timestampToROS(capture.get_depth_image().get_device_timestamp());
1056-
printTimestampDebugMessage("Depth image", capture_time);
10571061

10581062
depth_rect_frame->header.stamp = capture_time;
10591063
depth_rect_frame->header.frame_id = calibration_data_.tf_prefix_ + calibration_data_.rgb_camera_frame_;
@@ -1067,11 +1071,9 @@ void K4AROSDevice::framePublisherThread()
10671071

10681072
#if defined(K4A_BODY_TRACKING)
10691073
// Publish body markers when body tracking is enabled and a depth image is available
1070-
if (params_.body_tracking_enabled &&
1074+
if (params_.body_tracking_enabled && k4abt_tracker_queue_size_ < 3 &&
10711075
(body_marker_publisher_.getNumSubscribers() > 0 || body_index_map_publisher_.getNumSubscribers() > 0))
10721076
{
1073-
capture_time = timestampToROS(capture.get_depth_image().get_device_timestamp());
1074-
10751077
if (!k4abt_tracker_.enqueue_capture(capture))
10761078
{
10771079
ROS_ERROR("Error! Add capture to tracker process queue failed!");
@@ -1080,56 +1082,7 @@ void K4AROSDevice::framePublisherThread()
10801082
}
10811083
else
10821084
{
1083-
k4abt::frame body_frame = k4abt_tracker_.pop_result();
1084-
if (body_frame == nullptr)
1085-
{
1086-
ROS_ERROR_STREAM("Pop body frame result failed!");
1087-
ros::shutdown();
1088-
return;
1089-
}
1090-
else
1091-
{
1092-
if (body_marker_publisher_.getNumSubscribers() > 0)
1093-
{
1094-
// Joint marker array
1095-
MarkerArrayPtr markerArrayPtr(new MarkerArray);
1096-
auto num_bodies = body_frame.get_num_bodies();
1097-
for (size_t i = 0; i < num_bodies; ++i)
1098-
{
1099-
k4abt_body_t body = body_frame.get_body(i);
1100-
for (int j = 0; j < (int) K4ABT_JOINT_COUNT; ++j)
1101-
{
1102-
MarkerPtr markerPtr(new Marker);
1103-
getBodyMarker(body, markerPtr, j, capture_time);
1104-
markerArrayPtr->markers.push_back(*markerPtr);
1105-
}
1106-
}
1107-
body_marker_publisher_.publish(markerArrayPtr);
1108-
}
1109-
1110-
if (body_index_map_publisher_.getNumSubscribers() > 0)
1111-
{
1112-
// Body index map
1113-
ImagePtr body_index_map_frame(new Image);
1114-
result = getBodyIndexMap(body_frame, body_index_map_frame);
1115-
1116-
if (result != K4A_RESULT_SUCCEEDED)
1117-
{
1118-
ROS_ERROR_STREAM("Failed to get body index map");
1119-
ros::shutdown();
1120-
return;
1121-
}
1122-
else if (result == K4A_RESULT_SUCCEEDED)
1123-
{
1124-
// Re-sychronize the timestamps with the capture timestamp
1125-
body_index_map_frame->header.stamp = capture_time;
1126-
body_index_map_frame->header.frame_id =
1127-
calibration_data_.tf_prefix_ + calibration_data_.depth_camera_frame_;
1128-
1129-
body_index_map_publisher_.publish(body_index_map_frame);
1130-
}
1131-
}
1132-
}
1085+
++k4abt_tracker_queue_size_;
11331086
}
11341087
}
11351088
#endif
@@ -1155,7 +1108,6 @@ void K4AROSDevice::framePublisherThread()
11551108
}
11561109

11571110
capture_time = timestampToROS(capture.get_color_image().get_device_timestamp());
1158-
printTimestampDebugMessage("Color image", capture_time);
11591111

11601112
rgb_jpeg_frame->header.stamp = capture_time;
11611113
rgb_jpeg_frame->header.frame_id = calibration_data_.tf_prefix_ + calibration_data_.rgb_camera_frame_;
@@ -1181,7 +1133,6 @@ void K4AROSDevice::framePublisherThread()
11811133
}
11821134

11831135
capture_time = timestampToROS(capture.get_color_image().get_device_timestamp());
1184-
printTimestampDebugMessage("Color image", capture_time);
11851136

11861137
rgb_raw_frame->header.stamp = capture_time;
11871138
rgb_raw_frame->header.frame_id = calibration_data_.tf_prefix_ + calibration_data_.rgb_camera_frame_;
@@ -1209,7 +1160,6 @@ void K4AROSDevice::framePublisherThread()
12091160
}
12101161

12111162
capture_time = timestampToROS(capture.get_color_image().get_device_timestamp());
1212-
printTimestampDebugMessage("Color image", capture_time);
12131163

12141164
rgb_rect_frame->header.stamp = capture_time;
12151165
rgb_rect_frame->header.frame_id = calibration_data_.tf_prefix_ + calibration_data_.depth_camera_frame_;
@@ -1276,6 +1226,76 @@ void K4AROSDevice::framePublisherThread()
12761226
}
12771227
}
12781228

1229+
#if defined(K4A_BODY_TRACKING)
1230+
void K4AROSDevice::bodyPublisherThread()
1231+
{
1232+
while (running_ && ros::ok() && !ros::isShuttingDown())
1233+
{
1234+
if (k4abt_tracker_queue_size_ > 0)
1235+
{
1236+
k4abt::frame body_frame = k4abt_tracker_.pop_result();
1237+
--k4abt_tracker_queue_size_;
1238+
1239+
if (body_frame == nullptr)
1240+
{
1241+
ROS_ERROR_STREAM("Pop body frame result failed!");
1242+
ros::shutdown();
1243+
return;
1244+
}
1245+
else
1246+
{
1247+
auto capture_time = timestampToROS(body_frame.get_device_timestamp());
1248+
1249+
if (body_marker_publisher_.getNumSubscribers() > 0)
1250+
{
1251+
// Joint marker array
1252+
MarkerArrayPtr markerArrayPtr(new MarkerArray);
1253+
auto num_bodies = body_frame.get_num_bodies();
1254+
for (size_t i = 0; i < num_bodies; ++i)
1255+
{
1256+
k4abt_body_t body = body_frame.get_body(i);
1257+
for (int j = 0; j < (int) K4ABT_JOINT_COUNT; ++j)
1258+
{
1259+
MarkerPtr markerPtr(new Marker);
1260+
getBodyMarker(body, markerPtr, j, capture_time);
1261+
markerArrayPtr->markers.push_back(*markerPtr);
1262+
}
1263+
}
1264+
body_marker_publisher_.publish(markerArrayPtr);
1265+
}
1266+
1267+
if (body_index_map_publisher_.getNumSubscribers() > 0)
1268+
{
1269+
// Body index map
1270+
ImagePtr body_index_map_frame(new Image);
1271+
auto result = getBodyIndexMap(body_frame, body_index_map_frame);
1272+
1273+
if (result != K4A_RESULT_SUCCEEDED)
1274+
{
1275+
ROS_ERROR_STREAM("Failed to get body index map");
1276+
ros::shutdown();
1277+
return;
1278+
}
1279+
else if (result == K4A_RESULT_SUCCEEDED)
1280+
{
1281+
// Re-sychronize the timestamps with the capture timestamp
1282+
body_index_map_frame->header.stamp = capture_time;
1283+
body_index_map_frame->header.frame_id =
1284+
calibration_data_.tf_prefix_ + calibration_data_.depth_camera_frame_;
1285+
1286+
body_index_map_publisher_.publish(body_index_map_frame);
1287+
}
1288+
}
1289+
}
1290+
}
1291+
else
1292+
{
1293+
std::this_thread::sleep_for(std::chrono::milliseconds{ 20 });
1294+
}
1295+
}
1296+
}
1297+
#endif
1298+
12791299
k4a_imu_sample_t K4AROSDevice::computeMeanIMUSample(const std::vector<k4a_imu_sample_t>& samples)
12801300
{
12811301
// Compute mean sample
@@ -1503,33 +1523,3 @@ void K4AROSDevice::updateTimestampOffset(const std::chrono::microseconds& k4a_de
15031523
std::floor(alpha * (device_to_realtime - device_to_realtime_offset_).count())));
15041524
}
15051525
}
1506-
1507-
void printTimestampDebugMessage(const std::string& name, const ros::Time& timestamp)
1508-
{
1509-
ros::Duration lag = ros::Time::now() - timestamp;
1510-
static std::map<const std::string, std::pair<ros::Duration, ros::Duration>> map_min_max;
1511-
auto it = map_min_max.find(name);
1512-
if (it == map_min_max.end())
1513-
{
1514-
map_min_max[name] = std::make_pair(lag, lag);
1515-
it = map_min_max.find(name);
1516-
}
1517-
else
1518-
{
1519-
auto& min_lag = it->second.first;
1520-
auto& max_lag = it->second.second;
1521-
if (lag < min_lag)
1522-
{
1523-
min_lag = lag;
1524-
}
1525-
if (lag > max_lag)
1526-
{
1527-
max_lag = lag;
1528-
}
1529-
}
1530-
1531-
ROS_DEBUG_STREAM(name << " timestamp lags ros::Time::now() by\n"
1532-
<< std::setw(23) << lag.toSec() * 1000.0 << " ms. "
1533-
<< "The lag ranges from " << it->second.first.toSec() * 1000.0 << "ms"
1534-
<< " to " << it->second.second.toSec() * 1000.0 << "ms.");
1535-
}

0 commit comments

Comments
 (0)