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

Revision history [back]

It depends on how you want to use ros::Time.

If you want the current live system time, there are two ways to do this. If you want a distributed, simulated time (like from Gazebo or rosbag play --clock), you can't do this without a ros system as it comes in over the /clock topic.

The first way is to use ros::Time, but call ros::Time::init(). You'll see the following error message if you try to use it without it:

Cannot use ros::Time::now() before the first NodeHandle has been created or ros::start() has been called.  If this is a standalone app or test that just uses ros::Time and does not communicate over ROS, you may also call ros::Time::init()

Just call ros::Time::init() and now ros::Time::now() should be available.

The other option is if you never, ever want to use simulated or recorded time, then you can use ros::WallTime. This is the same API as ros::Time, but always geared to the current time of the system on which ros is running.

Example:

ros::WallTime t = ros::WallTime::now();

Wiki page on WallTime:

http://wiki.ros.org/roscpp/Overview/Time#Wall_Time