Execute loop at fixed frequency
robotics.ros.Rate object uses the
robotics.Rate superclass to inherit most of its properties and methods.
The main difference is that
robotics.ros.Rate uses the ROS node as a
source for time information. Therefore, it can use the ROS simulation or wall clock time
rosinit creates a ROS master in
MATLAB®, the global node uses wall clock time.
The performance of the
ros.Rate object and the ability to maintain
DesiredRate value depends on the publishing of the clock
information in ROS.
The scheduling resolution of your operating system and the level of other system activity can affect rate execution accuracy. As a result, accurate rate timing is limited to 100 Hz for execution of MATLAB code. To improve performance and execution speeds, use code generation.
rate = rosrate(desiredRate)
rate = robotics.ros.Rate(node,desiredRate)
rate = rosrate(
robotics.ros.Rate object, which enables you to
execute a loop at a fixed frequency,
time source is linked to the time source of the global ROS node, which
requires you to connect MATLAB to a ROS network using
rate = robotics.ros.Rate(
Rate object that operates loops at a fixed rate
based on the time source linked to the specified ROS node,
DesiredRate— Desired execution rate
Desired execution rate of loop, specified as a scalar in Hz. When using
waitfor, the loop operates every
DesiredRate seconds, unless the loop takes longer. It
then begins the next loop based on the specified
DesiredPeriod— Desired time period between executions
Desired time period between executions, specified as a scalar in seconds.
This property is equal to the inverse of
TotalElapsedTime— Elapsed time since construction or reset
Elapsed time since construction or reset, specified as a scalar in seconds.
OverrunAction— Method for handling overruns
Method for handling overruns, specified as one of these character vectors:
'drop' — waits until the next time
interval equal to a multiple of
'slip' — immediately executes the
Each code section calls
waitfor at the end of execution.
IsSimulationTime— Indicator if simulation or wall clock time is used
Indicator if simulation or wall clock time is used, returned as
Rate object is using the
ROS simulation time to regulate the rate of loop execution.
Initialize the ROS master and node.
Initializing ROS master on http://bat800610glnxa64:41803/. Initializing global node /matlab_global_node_19087 with NodeURI http://bat800610glnxa64:46069/
Create a rate object that runs at 1 Hz.
r = rosrate(1);
Start loop that prints iteration and time elapsed. Use
waitfor to pause the loop until the next time interval. Reset
r prior to the loop execution. Notice that each iteration executes at a 1-second interval.
reset(r) for i = 1:10 time = r.TotalElapsedTime; fprintf('Iteration: %d - Time Elapsed: %f\n',i,time) waitfor(r); end
Iteration: 1 - Time Elapsed: 0.010572 Iteration: 2 - Time Elapsed: 1.029639 Iteration: 3 - Time Elapsed: 2.015977 Iteration: 4 - Time Elapsed: 3.006077 Iteration: 5 - Time Elapsed: 4.011035 Iteration: 6 - Time Elapsed: 5.019578 Iteration: 7 - Time Elapsed: 6.004195 Iteration: 8 - Time Elapsed: 7.008239 Iteration: 9 - Time Elapsed: 8.005538 Iteration: 10 - Time Elapsed: 9.014444
Shut down the ROS network.
Shutting down global node /matlab_global_node_19087 with NodeURI http://bat800610glnxa64:46069/ Shutting down ROS master on http://bat800610glnxa64:41803/.
Initialize the ROS master and node.
Initializing ROS master on http://bat584004glnxa64:35003/. Initializing global node /matlab_global_node_88779 with NodeURI http://bat584004glnxa64:44751/
node = robotics.ros.Node('/testTime');
Using Master URI http://localhost:35003 from the global node to connect to the ROS master.
ros.Rate object running at 20 Hz.
r = robotics.ros.Rate(node,20);
Reset the object to restart the timer and run the loop for 30 iterations. Insert code you want to run in the loop before calling
reset(r) for i = 1:30 % User code goes here. waitfor(r); end
Shutdown ROS node.
Shutting down global node /matlab_global_node_88779 with NodeURI http://bat584004glnxa64:44751/ Shutting down ROS master on http://bat584004glnxa64:35003/.