Main Content

ros2time

Access ROS 2 time functionality

Description

time = ros2time returns a builtin_interfaces/Time ROS 2 message structure, time, with seconds and nanoseconds set to 0.

time = ros2time(totalSecs) initializes the time values for seconds and nanoseconds based on the specified time, in seconds, totalSecs.

time = ros2time(secs,nsecs) initializes the time values for seconds and nanoseconds individually. The function automatically wraps large values of nsecs, and adds the remainder to the seconds value of the message, secs.

example

time = ros2time(node,"now") returns the current ROS 2 time time within the specified ros2node object node. If the use_sim_time ROS 2 parameter is set to true, then ros2time returns the simulation time published on the clock topic. Otherwise, the function returns the system time of your machine. If you do not specify an output argument, the function prints the current time (in seconds) to the screen.

You can use ros2time to timestamp messages or to measure time in the ROS 2 network.

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

time = ros2time(node,"now","system") returns the system time of your machine, even if ROS publishes simulation time on the clock topic. If you do not specify an output argument, the function prints the system time (in seconds) to the screen.

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), January 1 1970, not counting leap seconds.

Examples

collapse all

Create a ROS 2 node.

node = ros2node("/my_node");

Get current ROS 2 time based on the source used by the ROS 2 node.

t = ros2time(node,"now")
t = struct with fields:
    MessageType: 'builtin_interfaces/Time'
            sec: 1669473179
        nanosec: 675274780

Create a stamped ROS 2 point message. Specify the header.stamp property with the current system time.

point = ros2message("geometry_msgs/PointStamped");
point.header.stamp = t;
point.point.x = 5;

Convert ROS 2 Time to the specified MATLAB format, datetime.

time = datetime(t.sec + 10^-9*int32(t.nanosec),'ConvertFrom','posixtime')
time = datetime
   26-Nov-2022 14:33:00

Input Arguments

collapse all

Total time, specified as a floating-point scalar. The integer portion sets the sec field, and the remainder sets the nanosec field the time message time.

Whole seconds, specified as a positive integer.

Note

The maximum and minimum values for secs are 0 and 4294967294.

Nanoseconds, specified as a positive integer. If this value is greater than or equal to 109, then the function wraps the value and adds the remainder to the value of secs.

ROS 2 node on the network, specified as a ros2node object.

Output Arguments

collapse all

ROS 2 time, returned as a builtin_interfaces/Time message structure.

Indicator whether time is simulation time, returned as a logical scalar.

Extended Capabilities

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

Version History

Introduced in R2022b