Ask Your Question

runtime error from rclcpp::Time

asked 2018-09-11 01:28:36 -0500

Skyking gravatar image

I am getting the error terminate called after throwing an instance of 'std::runtime_error' what(): can't compare times with different time sources when I compare two rclcpp::Time object .

Below is the code


  rclcpp::Time last_published_(rclcpp::Time(std::numeric_limits<rcl_time_point_value_t>::min()))

  rclcpp::Time TIME_MIN = rclcpp::Time(std::numeric_limits<rcl_time_point_value_t>::min());

  rclcpp::Time header_stamp = msg->header.stamp;

  if (last_published_ != TIME_MIN  &&
        (((header_stamp - last_published_).nanoseconds()/1000*1000*1000) > 1.5 * (1.0 / expected_rate_)))



Any help would be appreciated.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2018-09-11 02:49:18 -0500

tfoote gravatar image

Time objects can have different clocks, see the optional argument: and with that clock there may be different time sources, such as simulated time or system time. It's only valid to compare times on the same epoch.

To read more see the design article:

edit flag offensive delete link more

answered 2018-09-11 02:24:50 -0500

This is rather strange, but there are a couple of things you could try.

Can you test the code with the if statement only containing last_publisher_ != TIME_MIN just to confirm that is causing the problem. Also you want to change .nanoseconds()/1000*1000*1000 to either .nanoseconds()/1000000000 or .nanoseconds()/(1000*1000*1000)

Are you deliberately creating a rclcpp::Time object as a copy of another Time object in the following line? The difference in the style of construction between TIME_MIN and last_published_ may be causing this problem (even though it probably shouldn't!).

rclcpp::Time last_published_(rclcpp::Time(std::numeric_limits<rcl_time_point_value_t>::min()));

This could be more succinctly written as:

rclcpp::Time last_published_(std::numeric_limits<rcl_time_point_value_t>::min());

If you construct TIME_MIN in the same way as below it may help:

rclcpp::Time TIME_MIN(std::numeric_limits<rcl_time_point_value_t>::min());

Hope this helps.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2018-09-11 01:28:36 -0500

Seen: 771 times

Last updated: Sep 11 '18