Main Content

ros2rate

Execute loop at fixed frequency

Description

The 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 IsSimulationTime property).

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 function, disabling pause will result in inaccurate execution.

Tip

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.

Creation

Description

example

rate = ros2rate(node,desiredRate) creates a ros2rate object, 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, node.

Properties

expand all

Loop execution uses simulation or wall clock time, specified as 1 (true) or 0 (false). If true, the ros2rate object uses the ROS simulation time to regulate the rate of loop execution.

Desired execution rate of the loop, specified as a scalar in Hz. When using waitfor, the loop operates every DesiredRate seconds, unless the loop takes longer than that to execute. It then begins the next loop based on the specified OverrunAction.

Desired time period between executions, specified as a positive scalar in seconds. This property is equal to the inverse of DesiredRate.

Elapsed time since construction or reset, specified as a positive scalar in seconds.

Elapsed time between last two calls to waitfor, specified as a scalar. By default, LastPeriod is set to NaN until waitfor is called for the first time.

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 DesiredPeriod.

  • 'slip' — Immediately executes the next iteration of the loop.

Comparison of drop and slip overrun handling methods.

Each code section calls waitfor at the end of execution.

Object Functions

waitforPause code execution to achieve desired execution rate
statisticsStatistics of past execution periods
resetReset ros2rate object

Examples

collapse all

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");

Create a 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.003668
Iteration: 2 - Time Elapsed: 0.503131
Iteration: 3 - Time Elapsed: 1.002043
Iteration: 4 - Time Elapsed: 1.501720
Iteration: 5 - Time Elapsed: 2.002890
Iteration: 6 - Time Elapsed: 2.504980
Iteration: 7 - Time Elapsed: 3.000443
Iteration: 8 - Time Elapsed: 3.500634
Iteration: 9 - Time Elapsed: 4.000355
Iteration: 10 - Time Elapsed: 4.500963

Extended Capabilities

Version History

Introduced in R2022b

See Also

|