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

Query on get_clock()->now()

asked 2020-07-03 10:26:14 -0600

kk2105 gravatar image

Hi,

I am trying to update the msg header stamp using the below method.

custom_msg.header.stamp = get_clock()->now();

Seems like this updates both sec and nanosec field in the stamp.

In the subscriber node, I need to use this timestamp to calculate the time difference. I have pasted the sample output below.

   header:
      stamp:
        sec: 1593787132
        nanosec: 472017003
      frame_id: custom_node
    ---
    header:
      stamp:
        sec: 1593787132
        nanosec: 672051846
      frame_id: custom_node
    ---
    header:
      stamp:
        sec: 1593787132
        nanosec: 872009918
      frame_id: custom_node
    ---
    header:
      stamp:
        sec: 1593787133
        nanosec: 71999048
      frame_id: custom_node
    ---
    header:
      stamp:
        sec: 1593787133
        nanosec: 271995190
      frame_id: custom_node
    ---
    header:
      stamp:
        sec: 1593787133
        nanosec: 471998850
      frame_id: custom_node
    ---
    header:
      stamp:
        sec: 1593787133
        nanosec: 671974068
      frame_id: custom_node
    ---
    header:
      stamp:
        sec: 1593787133
        nanosec: 871955846
      frame_id: custom_node
    ---

Looks like I can not use the time in sec as it not changing in every cycle. And in nanosec the time value decreases in some of the cycles resulting in negative difference.

Could anybody please explain how the nanosec is being updated. My understanding is that the value has to increase in each cycle.

Thank you, KK

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2020-07-10 13:04:22 -0600

sloretz gravatar image

Could anybody please explain how the nanosec is being updated. My understanding is that the value has to increase in each cycle.

The current time is the seconds + nanoseconds. Notice that when nanosec decreased it's because sec increased by 1.

To calculate the difference between two times you'll need to get the difference between the sum of those two. Thankfully there's already rclcpp::Time and rclcpp::Duration classes to handle that. Create two instances of rclcpp::Time for the start time and end time, then subtract the two to get an rclcpp::Duration containing the difference.

edit flag offensive delete link more

Comments

Thanks for the clear explanation. I was thinking both are not related to each other.

kk2105 gravatar image kk2105  ( 2020-07-10 23:28:21 -0600 )edit
0

answered 2022-01-19 14:02:24 -0600

Debanik Roy gravatar image
rclcpp::Time now = this->get_clock()->now();
rclcpp::Time when = this->get_clock()->now() - rclcpp::Duration(5, 0);
rclcpp::Time t_delta = now + rclcpp::Duration(1/10, 0);
edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2020-07-03 10:26:14 -0600

Seen: 5,853 times

Last updated: Jan 19 '22