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

Use a separate thread to pop k4abt_tracker result. Fix #109. #161

Merged
merged 5 commits into from
Oct 30, 2021

Conversation

zchenpds
Copy link
Contributor

@zchenpds zchenpds commented Nov 16, 2020

Fixes #109

Description of the changes:

  • Create a separate thread dedicated to popping the result from k4abt_tracker_ and publishing body_tracking_data following the suggestion from this post:

the tracker will process faster if you keep queuing the frame without waiting for previous frame to be finished. Because then the pipeline can process two frames at different stages at the same time.

Before submitting a Pull Request:

I tested changes on:

  • Windows
  • Linux
  • ROS1
  • ROS2

I measured the delay by adding ROS_INFO_STREAM("Body tracking delay: " << (ros::Time::now() - capture_time).toSec() * 1000.0 << " ms."); below Line 990

k4abt::frame body_frame = k4abt_tracker_.pop_result();
and run roslaunch azure_kinect_ros_driver driver.launch fps:=30 depth_mode:=WFOV_2X2BINNED body_tracking_enabled:=true. The delay increases quickly from 80 ms to around 280 ms on my laptop if/when I run some light-weight tasks such as opening a new webpage in Chrome browser or scrolling up/down in vscode editor. (Laptop specs: Intel® Core™ i7-9750H CPU @ 2.60GHz × 12 , GeForce RTX 2060, 32 GB RAM)

By moving

k4abt::frame body_frame = k4abt_tracker_.pop_result();
if (body_frame == nullptr)
{
ROS_ERROR_STREAM("Pop body frame result failed!");
ros::shutdown();
return;
}
else
{
if (body_marker_publisher_.getNumSubscribers() > 0)
{
// Joint marker array
MarkerArrayPtr markerArrayPtr(new MarkerArray);
auto num_bodies = body_frame.get_num_bodies();
for (size_t i = 0; i < num_bodies; ++i)
{
k4abt_body_t body = body_frame.get_body(i);
for (int j = 0; j < (int) K4ABT_JOINT_COUNT; ++j)
{
MarkerPtr markerPtr(new Marker);
getBodyMarker(body, markerPtr, j, capture_time);
markerArrayPtr->markers.push_back(*markerPtr);
}
}
body_marker_publisher_.publish(markerArrayPtr);
}
if (body_index_map_publisher_.getNumSubscribers() > 0)
{
// Body index map
ImagePtr body_index_map_frame(new Image);
result = getBodyIndexMap(body_frame, body_index_map_frame);
if (result != K4A_RESULT_SUCCEEDED)
{
ROS_ERROR_STREAM("Failed to get body index map");
ros::shutdown();
return;
}
else if (result == K4A_RESULT_SUCCEEDED)
{
// Re-sychronize the timestamps with the capture timestamp
body_index_map_frame->header.stamp = capture_time;
body_index_map_frame->header.frame_id =
calibration_data_.tf_prefix_ + calibration_data_.depth_camera_frame_;
body_index_map_publisher_.publish(body_index_map_frame);
}
}
}
to a separate thread, the delay was able to stay at around 80 ms regardless of what light-weight tasks I run.

Alternatively the delay can be measured by printTimestampDebugMessage("Body tracking", capture_time);, running roslaunch azure_kinect_ros_driver driver.launch fps:=30 depth_mode:=WFOV_2X2BINNED body_tracking_enabled:=true imu_rate_target:=3 and change log verbosity level to debug.

@ooeygui
Copy link
Member

ooeygui commented Nov 16, 2020

Thank you for the contribution!

We need to keep the ROS1 and ROS2 trees in sync, can you make an equivalent fix to the Foxy branch and test on ROS2?

@ooeygui ooeygui added the more-information-needed Question asked label Nov 16, 2020
@zchenpds
Copy link
Contributor Author

@ooeygui Sure. I will do it later today.

@zchenpds zchenpds force-pushed the body_tracking_delay branch from d8633af to 4b0fe11 Compare November 17, 2020 08:51
@ooeygui ooeygui added enhancement New feature or request triage approved The Issue has been reviewed and approved for development by the Azure Kinect ROS Driver Team and removed more-information-needed Question asked labels Dec 9, 2020
@ooeygui
Copy link
Member

ooeygui commented Dec 9, 2020

Sorry for the delay on this, we are working on validating it.

@zchenpds
Copy link
Contributor Author

@amelhassan Please let me know if anything needs to fixed in this PR. I will lose access to the k4a device after this week so hopefully I can amend it to your satisfaction.

@amelhassan
Copy link
Contributor

Yes I'll take a look today and let you know if any changes are needed.

@amelhassan amelhassan dismissed a stale review via da602d5 October 29, 2021 23:04
@amelhassan amelhassan merged commit 3912711 into microsoft:melodic Oct 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request triage approved The Issue has been reviewed and approved for development by the Azure Kinect ROS Driver Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Body tracking latency
3 participants