rosserial overview: NodeHandles and Initialization | Messages | Publishers and Subscribers | Time | Logging | Limitations | Protocol | Parameters

Time and Duration

ROS has builtin time and duration primitive types, which rosserial provides as the ros::Time and ros::Duration classes, respectively. A Time is a specific moment (e.g. "today at 5pm") whereas a Duration is a period of time (e.g. "5 hours"). Durations can be negative.

Times and durations have identical representations:

unsigned long secs
unsigned long nsecs

Getting the Current Time

In order to get the current ROS time, you must be connected to ROS. Then, you can use your NodeHandle object, nh, to query the current time. nh.now()

  • Get the current time as a ros::Time instance:

Toggle line numbers
   1 ros::Time begin = nh.now();

Creating Time and Duration Instances

You can create a Time or Duration to a specific value as well, either using a two-integer constructor:

Toggle line numbers
   1 ros::Time a_little_after_the_beginning(0, 10000);
   2 ros::Duration five_seconds(5, 0);

Converting Time and Duration Instances

Time and Duration objects can also be turned into floating point seconds:

Toggle line numbers
   1 double secs = nh.now().toSec();
   2 
   3 ros::Duration d(1, 0);
   4 secs = d.toSec();

Wiki: rosserial/Overview/Time (last edited 2011-10-31 20:44:01 by MichaelFerguson)