Ask Your Question
5

ROS 2 time handling

asked 2018-04-09 00:43:52 -0500

KenYN gravatar image

In ROS 1 my node has code like this in a subscribed message callback:

ros::Time now = ros::Time::now();
ros::Duration frameSkip = ros::Duration(m_frameSyncMs/1000.0);

// Proceed if time interval has elapsed or if clock has gone backwards
if (m_previousTimeStamp + frameSkip > now)
// etc...

Basically, when m_frameSyncMs is 100 ms with the topic being a camera feed at 30 fps, just pick out every third frame or so. This works quite happily.

Now, for ROS 2 there no longer is a Time::now(), and looking at various web pages there might be a node clock, or there might be an rcl_get_time_point_now() but shouldn't that also take the result of rcl_get_default_ros_time()?

What is the best way to do this? Furthermore, where is the best place to learn about the latest ROS 2 developments; GitHub and Discord, for instance seem a bit too light on information, and reading the source might work, but the next beta may very well invalidate everything. Finally is there a ros2bag or equivalent for recording and playing back runs?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
4

answered 2018-04-09 00:56:58 -0500

William gravatar image

updated 2018-04-09 00:57:22 -0500

If you want the equivalent "ROS Time" as in ROS 1, e.g. might return wall clock time or simulated time (see https://wiki.ros.org/Clock#Using_Simu... ), then you need to call now() on a Node, see:

http://docs.ros2.org/ardent/api/rclcp...

The reason you need to get "now" from a node in ROS 2 is because unlike ROS 1, there's no global node singleton and the ROS time clock requires things like parameters and topics to implement sim time. So in ROS 2 you need to have a node to use ROS time.

If you only ever care about real time (no simulated time), then I suggest either using a custom made rclcpp::Clock, which defaults to "System time":

http://docs.ros2.org/ardent/api/rclcp...

Or give it an argument of RCL_STEADY_TIME to get monotonically increasing time.

You can also just use std::chrono::system_clock or std::chrono::steady_clock if you're not using ROS time:

http://en.cppreference.com/w/cpp/chro...

http://en.cppreference.com/w/cpp/chro...

edit flag offensive delete link more

Comments

Thanks for the quick reply! That's solved my immediate problem.

KenYN gravatar imageKenYN ( 2018-04-09 01:11:29 -0500 )edit

Your Answer

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

Add Answer

Question Tools

2 followers

Stats

Asked: 2018-04-09 00:43:52 -0500

Seen: 2,009 times

Last updated: Apr 09 '18