メインコンテンツ

rosrate

Execute loop at fixed frequency

Description

The rosrate object allows you to execute a loop at a fixed frequency. The rosrate object uses the global ROS node as a source for time information. Therefore, it can use the ROS simulation or wall clock time (see the IsSimulationTime property).

The performance of the rosrate object, and the ability to maintain the desiredRate value depend on the publishing of the clock information in ROS network. Because the rosrate object relies on the pause function, disabling pause will result in inaccurate execution.

If rosinit creates a ROS master in MATLAB®, the global node uses wall clock time.

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

rate = rosrate(desiredRate) creates a Rate object, which enables you to execute a loop at a fixed frequency, DesiredRate. This syntax sets the DesiredRate and DesiredPeriod properties of the Rate object. The time source is linked to the time source of the global ROS node, which requires you to connect MATLAB to a ROS network using rosinit.

example

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

example

Properties

expand all

This property is read-only.

Indicator if simulation or wall clock time is used, returned as true or false. The value of this property is same as that of the use_sim_time parameter with these behaviors:

  • false — The Rate object uses wall clock time to regulate the rate of loop execution.

  • true — The Rate object uses ROS simulation time published on the /clock topic to regulate the rate of loop execution.

To set the use_sim_time parameter, follow these steps:

ptree = rosparam;
set(ptree,'/use_sim_time',true)

This property is read-only.

Desired execution rate of loop, specified as a scalar in hertz. When using waitfor, the loop operates every DesiredRate seconds, unless the loop takes longer. It then begins the next loop based on the specified OverRunAction.

This property is set during the object creation.

This property is read-only.

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

This property is set during the object creation.

This property is read-only.

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

This property is read-only.

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. After the first call, LastPeriod equals TotalElapsedTime.

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

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

  • 'slip' — immediately executes the loop again

Workflow of drop and slip overrun handling methods.

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

Object Functions

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

Examples

collapse all

Initialize the ROS master and the global node.

rosinit
Launching ROS Core...
Done in 0.37969 seconds.
Initializing ROS master on http://172.23.109.169:51436.
Initializing global node /matlab_global_node_58036 with NodeURI http://dcc3881203glnxa64:46449/ and MasterURI http://localhost:51436.

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.001244
Iteration: 2 - Time Elapsed: 1.004246
Iteration: 3 - Time Elapsed: 2.000570
Iteration: 4 - Time Elapsed: 3.000877
Iteration: 5 - Time Elapsed: 4.001722
Iteration: 6 - Time Elapsed: 5.000442
Iteration: 7 - Time Elapsed: 6.000569
Iteration: 8 - Time Elapsed: 7.000105
Iteration: 9 - Time Elapsed: 8.001079
Iteration: 10 - Time Elapsed: 9.000282

Shut down the ROS network.

rosshutdown
Shutting down global node /matlab_global_node_58036 with NodeURI http://dcc3881203glnxa64:46449/ and MasterURI http://localhost:51436.
Shutting down ROS master on http://172.23.109.169:51436.

Initialize the ROS master and node.

rosinit
Launching ROS Core...
Status before launching ros core :0
result before launching ros core: Microsoft.Management.Serv     2068 Services                   0    119,228 K
Microsoft.SharePoint.exe     23100 Console                    1    342,272 K
WindowsTerminal.exe          17292 Console                    1     78,248 K

Creating Python virtual environment for ros1.Done.
Adding required Python packages to virtual environment.Done.
Copying include folders.Done.
Copying libraries.Done.
.....Done in 5.9479 seconds.
* Inside getProcessPID function *
Process Name: rosmaster  - rosmaster  --core -p 57502 -w 3
Status before getting PID :0
Result before getting PID :Microsoft.Management.Serv     2068 Services                   0    119,236 K
Microsoft.SharePoint.exe     23100 Console                    1    342,024 K
WindowsTerminal.exe          17292 Console                    1     88,832 K
rosmaster.exe                 1012 Console                    1      5,052 K
python.exe                    8776 Console                    1      4,780 K
python.exe                   23180 Console                    1     30,428 K

Result: 
WindowsTerminal.exe          17292 Console                    1     88,832 K

Status: 0
PID obtained: 17292
Status after getting PID :0
Result after getting PID :Microsoft.Management.Serv     2068 Services                   0    119,268 K
Microsoft.SharePoint.exe     23100 Console                    1    342,024 K
WindowsTerminal.exe          17292 Console                    1     88,832 K
rosmaster.exe                 1012 Console                    1      5,052 K
python.exe                    8776 Console                    1      4,780 K
python.exe                   23180 Console                    1     30,428 K

* Exiting getProcessPID function *
Status after launching ros core :0
result after launching ros core: Microsoft.Management.Serv     2068 Services                   0    119,268 K
Microsoft.SharePoint.exe     23100 Console                    1    342,024 K
WindowsTerminal.exe          17292 Console                    1     88,832 K
rosmaster.exe                 1012 Console                    1      5,052 K
python.exe                    8776 Console                    1      4,780 K
python.exe                   23180 Console                    1     30,428 K

Status before deleting node :0
Result before deleting node :Microsoft.Management.Serv     2068 Services                   0    119,236 K
Microsoft.SharePoint.exe     23100 Console                    1    342,448 K
WindowsTerminal.exe          17292 Console                    1     88,832 K
rosmaster.exe                 1012 Console                    1      5,052 K
python.exe                    8776 Console                    1      4,780 K
python.exe                   23180 Console                    1     30,520 K
libmwros1server.exe           8844 Console                    1     29,412 K

Status after deleting node :0
Result after deleting node :Microsoft.Management.Serv     2068 Services                   0    119,272 K
Microsoft.SharePoint.exe     23100 Console                    1    342,448 K
WindowsTerminal.exe          17292 Console                    1     88,832 K
rosmaster.exe                 1012 Console                    1      5,052 K
python.exe                    8776 Console                    1      4,780 K
python.exe                   23180 Console                    1     30,480 K

Initializing ROS master on http://172.31.78.36:57502.
Initializing global node /matlab_global_node_18052 with NodeURI http://vdi-ah2wsp-001:51749/ and MasterURI http://localhost:57502.
node = ros.Node('/testTime');
Using Master URI http://localhost:57502 from the global node to connect to the ROS master.

Create a ros.Rate object 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 calling waitfor.

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

Shut down ROS node.

rosshutdown
Shutting down global node /matlab_global_node_18052 with NodeURI http://vdi-ah2wsp-001:51749/ and MasterURI http://localhost:57502.
Status before deleting node :0
Result before deleting node :Microsoft.Management.Serv     2068 Services                   0    119,444 K
Microsoft.SharePoint.exe     23100 Console                    1    342,448 K
WindowsTerminal.exe          17292 Console                    1     88,832 K
rosmaster.exe                 1012 Console                    1      5,052 K
python.exe                    8776 Console                    1      4,780 K
python.exe                   23180 Console                    1     30,560 K
libmwros1server.exe           4164 Console                    1     30,000 K
libmwros1server.exe          10304 Console                    1     29,868 K

Status after deleting node :0
Result after deleting node :Microsoft.Management.Serv     2068 Services                   0    119,412 K
Microsoft.SharePoint.exe     23100 Console                    1    342,448 K
WindowsTerminal.exe          17292 Console                    1     88,832 K
rosmaster.exe                 1012 Console                    1      5,052 K
python.exe                    8776 Console                    1      4,780 K
python.exe                   23180 Console                    1     30,520 K
libmwros1server.exe          10304 Console                    1     29,868 K

Shutting down ROS master on http://172.31.78.36:57502.
Status before deleting core :0
Result before deleting core :Microsoft.Management.Serv     2068 Services                   0    119,440 K
Microsoft.SharePoint.exe     23100 Console                    1    342,448 K
WindowsTerminal.exe          17292 Console                    1     88,832 K
rosmaster.exe                 1012 Console                    1      5,052 K
python.exe                    8776 Console                    1      4,780 K
python.exe                   23180 Console                    1     30,520 K
libmwros1server.exe          10304 Console                    1     29,868 K

* Inside killROSProcess function *
Status after Process Killed: 0
Result after Process Killed: SUCCESS: Sent termination signal to the process with PID 17292.

Status after deleting core :0
Result after deleting core :Microsoft.Management.Serv     2068 Services                   0    119,412 K
Microsoft.SharePoint.exe     23100 Console                    1    342,448 K
WindowsTerminal.exe          17292 Console                    1     79,472 K
libmwros1server.exe          10304 Console                    1     29,868 K

* Exiting killROSProcess function *

Extended Capabilities

expand all

Version History

Introduced in R2019b

See Also

(Robotics System Toolbox) |