Main Content


Pause code execution to achieve desired execution rate



waitfor(rate) pauses execution until the code reaches the desired execution rate. The function accounts for the time that is spent executing code between waitfor calls.

numMisses = waitfor(rate) returns the number of iterations missed while executing code between calls.


collapse all

Create a rate object that runs at 1 Hz.

r = robotics.Rate(1);

Start a loop using the Rate object inside to control the loop execution. Reset the object prior to the loop execution to reset timer. Print the iteration and time elapsed.

for i = 1:10
	time = r.TotalElapsedTime;
	fprintf('Iteration: %d - Time Elapsed: %f\n',i,time)
Iteration: 1 - Time Elapsed: 0.003931
Iteration: 2 - Time Elapsed: 1.002562
Iteration: 3 - Time Elapsed: 2.003178
Iteration: 4 - Time Elapsed: 3.001374
Iteration: 5 - Time Elapsed: 4.003042
Iteration: 6 - Time Elapsed: 5.002214
Iteration: 7 - Time Elapsed: 6.000803
Iteration: 8 - Time Elapsed: 7.001572
Iteration: 9 - Time Elapsed: 8.001542
Iteration: 10 - Time Elapsed: 9.000947

Each iteration executes at a 1-second interval.

Input Arguments

collapse all

Rate object, specified as a handle. This object contains the information for the desired rate and other information about the execution. See rateControl (Robotics System Toolbox) for more information.

Output Arguments

collapse all

Number of missed task executions, returned as a scalar. waitfor returns the number of times the task was missed in the Rate object based on the LastPeriod time. For example, if the desired rate is 1 Hz and the last period was 3.2 seconds, numMisses returns 3.

See Also

| | (Robotics System Toolbox)


Introduced in R2019b