Create a ROS duration object
returns a default
ROS duration object. The properties for seconds and nanoseconds are set to
dur = rosduration
In a future release, ROS Toolbox will use message structures instead of objects for ROS messages.
To use message structures now, set the
"struct". For more information, see ROS Message Structures.
Work with ROS Duration Objects
Duration objects, perform addition and subtraction, and compare duration objects. You can also add duration objects to ROS
Time objects to get another
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
dur1 > dur2
ans = logical 1
Initialize a ROS network.
Launching ROS Core... Done in 0.35836 seconds. Initializing ROS master on http://172.29.217.11:55798. Initializing global node /matlab_global_node_92965 with NodeURI http://dcc083455glnxa64:38993/ and MasterURI http://localhost:55798.
Add a duration to a ROS
time = rostime('now','system')
time = ROS Time with properties: Sec: 1.6619e+09 Nsec: 338297943
timeFuture = time + dur3
timeFuture = ROS Time with properties: Sec: 1.6619e+09 Nsec: 840297943
Shut down the ROS network.
Shutting down global node /matlab_global_node_92965 with NodeURI http://dcc083455glnxa64:38993/ and MasterURI http://localhost:55798. Shutting down ROS master on http://172.29.217.11:55798.
totalSecs — Total time
0 (default) | scalar
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
secs — Whole seconds
0 (default) | integer
Whole seconds, specified as an integer. This value is directly
set to the
Sec property of the
The maximum and minimum values for
nsecs — Nanoseconds
0 (default) | positive integer
Nanoseconds, specified as a positive integer. This value is
directly set to the
NSec property of the
unless it is greater than or equal to 109.
The value is then wrapped and the remainders are added to the value
dur — Duration
Duration object | structure
Duration, returned as a ROS
Duration object or message structure with
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Version HistoryIntroduced in R2019b
R2021a: ROS Message Structures
You can now create messages as structures with fields matching the message object properties. Using structures typically improves performance of creating, updating, and using ROS messages, but message fields are no longer validated when set. Message types and corresponding field values from the structures are validated when sent across the network.
To use ROS messages as structures, use the
argument when creating your publishers, subscribers, or other ROS objects. Any messages
generated from these objects will utilize structures.
pub = rospublisher("/scan","sensor_msgs/LaserScan","DataFormat","struct") msg = rosmessage(pub)
You can also create messages as structures directly, but make sure to specify the data
"struct" for the publisher, subscriber, or other ROS objects as
well. ROS objects still use message objects by default.
msg = rosmessage("/scan","sensor_msgs/LaserScan","DataFormat","struct") ... pub = rospublisher("/scan","sensor_msgs/LaserScan","DataFormat","struct")
In a future release, ROS messages will use structures by default and ROS message objects will be removed.
For more information, see Improve Performance of ROS Using Message Structures.