rostime

Access ROS time functionality

Description

A ROS Time object representing and instance of time in seconds and nanoseconds. This time can be based off your system time, the ROS simulation time, or an arbitrary time.

Creation

Syntax

time = rostime(totalSecs)
time = rostime(secs,nsecs)
time = rostime("now")
[time,issimtime] = rostime("now")
time = rostime("now","system")

Description

time = rostime(totalSecs) initializes the time values for seconds and nanoseconds based on totalSecs, which represents the time in seconds as a floating-point number.

example

time = rostime(secs,nsecs) initializes the time values for seconds and nanoseconds individually. Both inputs must be integers. Large values for nsecs are wrapped automatically with the remainder added to secs.

time = rostime("now") returns the current ROS time. If the use_sim_time ROS parameter is set to true, the rostime returns the simulation time published on the clock topic. Otherwise, the function returns the system time of your machine. time is a ROS Time object. If no output argument is given, the current time (in seconds) is printed to the screen.

rostime can be used to timestamp messages or to measure time in the ROS network.

example

[time,issimtime] = rostime("now") also returns a Boolean that indicates if time is in simulation time (true) or system time (false).

example

time = rostime("now","system") always returns the system time of your machine, even if ROS publishes simulation time on the clock topic. If no output argument is given, the system time (in seconds) is printed to the screen.

The system time in ROS follows the Unix or POSIX time standard. POSIX time is defined as the time that has elapsed since 00:00:00 Coordinated Universal Time (UTC), 1 January 1970, not counting leap seconds.

Properties

expand all

Total time, specified as a floating-point scalar. The integer portion is set to the Sec property with the remainder applied to Nsec property of the Time object.

Whole seconds, specified as a positive integer.

Note

The maximum and minimum values for secs are [0, 4294967294].

Nanoseconds, specified as a positive integer. It this value is greater than or equal to 109, then the value is then wrapped and the remainders are added to the value of Sec.

Examples

collapse all

Connect to a ROS network.

rosinit
Initializing ROS master on http://AH-SRADFORD:11311/.
Initializing global node /matlab_global_node_00466 with NodeURI http://AH-SRADFORD:64390/

Get current ROS Time. You can also check whether is it system time by getting the issim output.

[t,issim] = rostime('now')
t = 

  ROS Time with properties:

     Sec: 1.4734e+09
    Nsec: 408000000


issim =

  logical

   0

Create a stamped ROS message. Specify the Header.Stamp property with the current system time.

point = rosmessage('geometry_msgs/PointStamped');
point.Header.Stamp = rostime('now','system');

This example shows how to convert current ROS time into a MATLAB® standard time. The ROS Time object is first converted to a double in seconds, then to the specified MATLAB time.

% Sets up ROS network and stores ROS time
rosinit
Initializing ROS master on http://bat800616glnxa64:44257/.
Initializing global node /matlab_global_node_09694 with NodeURI http://bat800616glnxa64:36541/
t = rostime('now');

% Converts ROS time to a double in seconds
secondtime = double(t.Sec)+double(t.Nsec)*10^-9;

% Sets time to a specified MATLAB format
time = datetime(secondtime, 'ConvertFrom','posixtime')
time = datetime
   13-Apr-2019 04:42:55

% Shuts down ROS network
rosshutdown
Shutting down global node /matlab_global_node_09694 with NodeURI http://bat800616glnxa64:36541/
Shutting down ROS master on http://bat800616glnxa64:44257/.

Use the seconds function to get the total seconds of a Time object from its Secs and Nsecs properties.

Create a Time object.

time = rostime(1,860000000)
time = 
  ROS Time with properties:

     Sec: 1
    Nsec: 860000000

Get the total seconds from the time object.

secs = seconds(time)
secs = 1.8600

Introduced in R2015a