Main Content

rosduration

Create a ROS duration object

Description

dur = rosduration returns a default ROS duration object. The properties for seconds and nanoseconds are set to 0.

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.

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

example

dur = rosduration(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.

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

Examples

collapse all

Create ROS Duration objects, perform addition and subtraction, and compare duration objects. You can also add duration objects to ROS Time objects to get another Time object.

Create a duration using seconds and nanoseconds.

dur1 = rosduration(100,2000000)
dur1 = 
  ROS Duration with properties:

     Sec: 100
    Nsec: 2000000

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

dur2 = rosduration(20.5)
dur2 = 
  ROS Duration with properties:

     Sec: 20
    Nsec: 500000000

Add the two durations together to get a single duration.

dur3 = dur1 + dur2
dur3 = 
  ROS Duration with properties:

     Sec: 120
    Nsec: 502000000

Subtract durations and get a negative duration. You can initialize durations with negative values as well.

dur4 = dur2 - dur1
dur4 = 
  ROS Duration with properties:

     Sec: -80
    Nsec: 498000000

dur5 = rosduration(-1,2000000)
dur5 = 
  ROS Duration with properties:

     Sec: -1
    Nsec: 2000000

Compare durations.

dur1 > dur2
ans = logical
   1

Initialize a ROS network.

rosinit
Launching ROS Core...
Done in 0.8288 seconds.
Initializing ROS master on http://172.20.225.136:50101.
Initializing global node /matlab_global_node_34590 with NodeURI http://dcc882528glnxa64:33231/ and MasterURI http://localhost:50101.

Add a duration to a ROS Time object.

time = rostime('now','system')
time = 
  ROS Time with properties:

     Sec: 1.7215e+09
    Nsec: 516223608

timeFuture = time + dur3
timeFuture = 
  ROS Time with properties:

     Sec: 1.7215e+09
    Nsec: 18223608

Shut down the ROS network.

rosshutdown
Shutting down global node /matlab_global_node_34590 with NodeURI http://dcc882528glnxa64:33231/ and MasterURI http://localhost:50101.
Shutting down ROS master on http://172.20.225.136:50101.

Input Arguments

collapse all

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

Whole seconds, specified as an integer. This value is directly set to the Sec property of the Duration object.

Note

The maximum and minimum values for secs are [-2147483648, 2147483647].

Nanoseconds, specified as a positive integer. This value is directly set to the NSec property of the Duration object unless it is greater than or equal to 109. The value is then wrapped and the remainders are added to the value of secs.

Output Arguments

collapse all

Duration, returned as a ROS Duration object or message structure with fields Sec and NSec

Extended Capabilities

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

Version History

Introduced in R2019b

expand all