Main Content

ros2duration

Create a ROS 2 duration message

Since R2022b

Description

dur = ros2duration returns a builtin_interfaces/Duration ROS 2 message structure, dur, with seconds and nanoseconds set to 0.

dur = ros2duration(totalSecs) initializes the duration values for seconds and nanoseconds based on the specified time, in seconds, totalSecs

example

dur = ros2duration(secs,nsecs) initializes the duration 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.

Examples

collapse all

Create a ROS 2 duration message using seconds and nanoseconds.

dur1 = ros2duration(100,2000000)
dur1 = struct with fields:
    MessageType: 'builtin_interfaces/Duration'
            sec: 100
        nanosec: 2000000

Create a ROS 2 duration message using a floating-point value. This sets the seconds using the integer portion and nanoseconds with the remainder.

dur2 = ros2duration(20.5)
dur2 = struct with fields:
    MessageType: 'builtin_interfaces/Duration'
            sec: 20
        nanosec: 500000000

Add a ROS 2 duration message to a ROS 2 time message.

node = ros2node("/test");
t1 = ros2time(node,"now");
t2 = ros2time(t1.sec+dur1.sec,t1.nanosec+dur1.nanosec)
t2 = struct with fields:
    MessageType: 'builtin_interfaces/Time'
            sec: 1725565430
        nanosec: 568473195

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 of the duration message dur.

Whole seconds, specified as an integer. This value directly sets to the sec field of the duration message dur.

Note

The maximum and minimum values for secs are 2147483648 and 2147483647, respectively.

Nanoseconds, specified as a positive integer. This value directly sets the nanoSec field of the duration message dur, unless it is greater than or equal to 109. If so, the function wraps the value and adds the remainder to the value of secs.

Output Arguments

collapse all

ROS 2 duration, returned as a builtin_interfaces/Duration message structure.

Extended Capabilities

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

Version History

Introduced in R2022b