ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Getting the rclcpp::Duration in microseconds

asked 2022-04-21 15:56:36 -0500

Hello everyone!

I am trying to calculate the time it takes for a single message from one Node to get to the other Node.

The way I calculate the difference is by substituting the timestamp of the received message from the present time of the node (rclcpp::Node::now()) and store it as rclcpp::Duration. Next, I convert the rclcpp::Duration to std::chrono::duration of microseconds, so that I can store it later in uint64_t. There is a method to achieve this as described here.

However, I seem to not understand, will this conversion convert only the nanoseconds? (assuming the seconds() represent amount of seconds since epoch, and nanoseconds() represent nanoseconds from seconds). Should I then separately convert seconds and nanoseconds, convert them to microseconds and then sum up?

Or, the to_chrono() converts both seconds and nanoseconds (ie total time since epoch) in nanoseconds?

The code is below:

void inputCallback(const digital_twin_msgs::msg::SupplyInput::SharedPtr msg)
      if(msg->seq_id == p_input_sub->next_id) {
        p_input_sub->recv_counter += 1;
        rclcpp::Duration diff = rclcpp::Node::now() - msg->stamp;
        auto num_of_us = diff.to_chrono<std::chrono::duration<uint64_t, std::micro>>(); // <- is this total time since epoch in microseconds?
        uint64_t var = num_of_us.count();
        p_input_sub->lost_count += 1;
      p_input_sub->next_id = msg->seq_id + 1;

Thank you in advance!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2022-04-21 17:03:19 -0500

aprotyas gravatar image

Yes, what you're getting is the time in microseconds. May I suggest one QoL change to that line:

auto num_of_us = diff.to_chrono<std::chrono::microseconds>();
edit flag offensive delete link more



To answer the rest of the question. No, the nanoseconds function won't only convert the nanoseconds. nanoseconds returns the full time period represented by the rclcpp::Duration object in nanoseconds, as does seconds in seconds. There's usually a lot of accuracy lost with the latter, so I'd suggest sticking to the former, or better, to to_chrono.

aprotyas gravatar image aprotyas  ( 2022-04-21 17:06:04 -0500 )edit

Great! Thank you for the QoL change and for explanation, that is exactly what I need :) . I wish I had the experience to know these kinds of little details regarding std::chrono types. It indeed looks much clearer.

sejego gravatar image sejego  ( 2022-04-22 01:24:26 -0500 )edit

No worries. Slight tangent, but if you want to learn about std::chronospecifically, this video will take you a long way:

aprotyas gravatar image aprotyas  ( 2022-04-22 13:23:49 -0500 )edit

Question Tools

1 follower


Asked: 2022-04-21 15:56:36 -0500

Seen: 2,757 times

Last updated: Apr 21 '22