Main Content

rosrate

Execute loop at fixed frequency

Description

Therosrateobject uses therateControl(Robotics System Toolbox)superclass to inherit most of its properties and methods. The main difference is thatrateControluses the ROS node as a source for time information. Therefore, it can use the ROS simulation or wall clock time (see theIsSimulationTimeproperty).

Ifrosinitcreates a ROS master in MATLAB®,global node uses wall clock time.

The performance of therosrateobject and the ability to maintain theDesiredRatevalue depends on the publishing of the clock information in ROS.

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 for execution of MATLAB code. To improve performance and execution speeds, use code generation.

Creation

Description

example

rate= rosrate(desiredRate)creates aRateobject, which enables you to execute a loop at a fixed frequency,DesiredRate. The time source is linked to the time source of the global ROS node, which requires you to connect MATLAB to a ROS network usingrosinit.

example

rate= ros.Rate(node,desiredRate)creates aRateobject that operates loops at a fixed rate based on the time source linked to the specified ROS node,node.

Properties

expand all

Desired execution rate of loop, specified as a scalar in hertz. When usingwaitfor,loop operates everyDesiredRateseconds, unless the loop takes longer. It then begins the next loop based on the specifiedOverRunAction.

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

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

Elapsed time between last two calls towaitfor, specified as a scalar. By default,LastPeriodis set toNaNuntilwaitforis called for the first time. After the first call,LastPeriodequalsTotalElapsedTime.

Method for handling overruns, specified as one of these character vectors:

  • 'drop'— waits until the next time interval equal to a multiple ofDesiredPeriod

  • 'slip'— immediately executes the loop again

Each code section callswaitfor(Robotics System Toolbox)at the end of execution.

Indicator if simulation or wall clock time is used, returned astrueorfalse. Iftrue,Rateobject is using the ROS simulation time to regulate the rate of loop execution.

Object Functions

waitfor Pause code execution to achieve desired execution rate
statistics Statistics of past execution periods
reset ResetRateobject

Examples

collapse all

Initialize the ROS master and node.

rosinit
Launching ROS Core... ....Done in 4.1747 seconds. Initializing ROS master on http://192.168.88.1:58653. Initializing global node /matlab_global_node_60184 with NodeURI http://ah-avijayar:59309/

创建一个rate object that runs at 1 Hz.

r = rosrate(1);

Start loop that prints iteration and time elapsed. Usewaitforto pause the loop until the next time interval. Resetrprior to the loop execution. Notice that each iteration executes at a 1-second interval.

reset(r)fori = 1:10 time = r.TotalElapsedTime; fprintf('Iteration: %d - Time Elapsed: %f\n',i,time) waitfor(r);end
Iteration: 1 - Time Elapsed: 0.002974 Iteration: 2 - Time Elapsed: 1.000752 Iteration: 3 - Time Elapsed: 2.000610 Iteration: 4 - Time Elapsed: 3.000943 Iteration: 5 - Time Elapsed: 4.001317 Iteration: 6 - Time Elapsed: 5.000800 Iteration: 7 - Time Elapsed: 6.000602 Iteration: 8 - Time Elapsed: 7.000298 Iteration: 9 - Time Elapsed: 8.000352 Iteration: 10 - Time Elapsed: 9.000417

Shut down the ROS network.

rosshutdown
Shutting down global node /matlab_global_node_60184 with NodeURI http://ah-avijayar:59309/ Shutting down ROS master on http://192.168.88.1:58653.

Initialize the ROS master and node.

rosinit
Launching ROS Core... ....Done in 4.1733 seconds. Initializing ROS master on http://192.168.88.1:51279. Initializing global node /matlab_global_node_86106 with NodeURI http://ah-avijayar:50550/
node = ros.Node('/testTime');
Using Master URI http://localhost:51279 from the global node to connect to the ROS master.

创建一个ros.Rateobject running at 20 Hz.

r = 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 callingwaitfor.

reset(r)fori = 1:30% User code goes here.waitfor(r);end

Shut down ROS node.

rosshutdown
Shutting down global node /matlab_global_node_86106 with NodeURI http://ah-avijayar:50550/ Shutting down ROS master on http://192.168.88.1:51279.

Extended Capabilities

See Also

(Robotics System Toolbox)|

Introduced in R2019b