Main Content

rostime

Access ROS time functionality

Description

A ROS Time message represents an instance of time in seconds and nanoseconds. This time can be based on your system time, the ROS simulation time, or an arbitrary time.

Creation

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.

Note

In a future release, ROS Toolbox will use message structures instead of objects for ROS messages.

To use message structures now, set the "DataFormat" name-value argument to "struct". For more information, see ROS Message Structures.

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. The time is a ROS Time object. If no output argument is given, the current time (in seconds) is printed to the screen.

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

[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.

example

time = rostime(___,"DataFormat","struct") uses message structures instead of objects with any of the arguments in previous syntaxes. For more information, see ROS Message Structures.

example

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 wrapped and the remainders are added to the value of Sec.

Message format, specified as "object" or "struct". You must set this property on creation using the name-value input. For more information, see ROS Message Structures.

Examples

collapse all

Connect to a ROS network.

rosinit
Launching ROS Core...
Done in 0.54488 seconds.
Initializing ROS master on http://172.20.219.203:56625.
Initializing global node /matlab_global_node_53647 with NodeURI http://dcc422351glnxa64:37703/ and MasterURI http://localhost:56625.

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

[t,issim] = rostime('now','DataFormat','struct')
t = struct with fields:
     Sec: 1725565120
    Nsec: 203401735

issim = logical
   0

Shutdown the ROS network.

rosshutdown
Shutting down global node /matlab_global_node_53647 with NodeURI http://dcc422351glnxa64:37703/ and MasterURI http://localhost:56625.
Shutting down ROS master on http://172.20.219.203:56625.

Initialize a ROS network.

rosinit
Launching ROS Core...
.Done in 1.2944 seconds.
Initializing ROS master on http://172.30.144.24:60746.
Initializing global node /matlab_global_node_88351 with NodeURI http://dcc796195glnxa64:40039/ and MasterURI http://localhost:60746.

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

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

Shut down the ROS network.

rosshutdown
Shutting down global node /matlab_global_node_88351 with NodeURI http://dcc796195glnxa64:40039/ and MasterURI http://localhost:60746.
Shutting down ROS master on http://172.30.144.24:60746.

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.

Set up ROS network and store the ROS time as a structure message.

rosinit
Launching ROS Core...
Done in 0.70162 seconds.
Initializing ROS master on http://172.20.222.15:50869.
Initializing global node /matlab_global_node_02687 with NodeURI http://dcc648422glnxa64:43959/ and MasterURI http://localhost:50869.
t = rostime('now','DataFormat','struct');

Convert ROS Time to double using the seconds function and set time to a specified MATLAB format, datetime.

time = datetime(t.Sec + 10^-9*t.Nsec,'ConvertFrom','posixtime')
time = datetime
   05-Sep-2024 19:38:28

Shut down ROS network.

rosshutdown
Shutting down global node /matlab_global_node_02687 with NodeURI http://dcc648422glnxa64:43959/ and MasterURI http://localhost:50869.
Shutting down ROS master on http://172.20.222.15:50869.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2019b

expand all