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.

example

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.

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.

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

Add a duration to a ROS Time object.

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

     Sec: 1.5803e+09
    Nsec: 66000000

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

     Sec: 1.5803e+09
    Nsec: 568000000

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 with Sec and Nsec properties.

Introduced in R2019b