Execute loop at fixed frequency
ros2rate object allows you to execute a loop at a fixed
frequency. It uses the ROS 2 node as a source for time information. Therefore, it can use the
ROS 2 simulation time or wall clock time (see the
The performance of the
ros2rate object, and the ability to maintain the
DesiredRate value depend on the publishing of the clock information in
ROS 2 network. Because the ros2rate object relies on the
pause will result in inaccurate execution.
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 when executing MATLAB® code. To improve performance and execution speeds, use code generation.
rate = ros2rate(
rate, that enables
you to execute a loop at a fixed frequency,
desiredRate. The object
uses the time source of the specified ROS 2 node object,
IsSimulationTime — Loop execution uses simulation or wall clock time
Loop execution uses simulation or wall clock time, specified as
0 (false). If
ros2rate object uses the ROS simulation time to regulate the rate
of loop execution.
DesiredRate — Desired execution rate
Desired execution rate of the loop, specified as a scalar in Hz. When using
loop operates every
DesiredRate seconds, unless the loop takes longer
than that to execute. It then begins the next loop based on the specified
DesiredPeriod — Desired time period between executions
Desired time period between executions, specified as a positive 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 positive scalar in seconds.
OverrunAction — Method for handling overruns
'slip' (default) |
Method for handling overruns, specified as one of these character vectors:
'drop'— Executes the next iteration of the loop at the next time step equal to a multiple of
'slip'— Immediately executes the next iteration of the loop.
Each code section calls
waitfor at the
end of execution.
|Pause code execution to achieve desired execution rate|
|Statistics of past execution periods|
Run Loop at Fixed Rate Using
Create a ROS 2 node.
node = ros2node("/myNode");
Create a publisher to publish a standard integer message.
pub = ros2publisher(node,"/my_int","std_msgs/Int64");
ros2rate object that runs at 2 Hz.
r = ros2rate(node,2);
Start loop that prints the current 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.008025 Iteration: 2 - Time Elapsed: 0.507448 Iteration: 3 - Time Elapsed: 1.004842 Iteration: 4 - Time Elapsed: 1.503426 Iteration: 5 - Time Elapsed: 2.004145 Iteration: 6 - Time Elapsed: 2.510637 Iteration: 7 - Time Elapsed: 3.000392 Iteration: 8 - Time Elapsed: 3.500345 Iteration: 9 - Time Elapsed: 4.000225 Iteration: 10 - Time Elapsed: 4.500291
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
statisticsobject function is not supported.
'drop'is not supported.
Introduced in R2022b