MATLAB Answers

James Potter

Why is timer object firing at an incorrect frame rate?

Asked by James Potter
on 16 Jan 2013

I am using a timer object to try to enforce a frame rate of 25 Hz (0.04 seconds per period) for a few minutes. When I use tic/toc to see how long each 25-frame "second" takes, it is not one second. The time-per-second depends on what I have running in the TimerFcn. I have seen a string of 1.1-second "seconds", and other times there are strings of 0.92-second "seconds".

I am confused because when I use the "AveragePeriod" method, the timer always thinks it has perfect timing, and has an average period of 0.04 seconds. Even if the actual periods seem to be 10% off. Does the timer use a completely different system than the tic/toc functions?

I am running it in 'FixedRate' mode, so it should run the TimerFcn every 0.04 seconds. I have tried it with both 'drop' and 'queue' options for the BusyAction.


How long does the timerfcn take to run?

Please show the code for construction of the timer object.

Here's how I'm defining the timer:

trialTimer = timer('ExecutionMode', 'FixedRate', 'Period', 0.04, 'TimerFcn', @RunOneFrame, 'TasksToExecute', 115*25, 'StartDelay', 0);

As an experiment, I made RunOneFrame contain almost nothing. In the Profile Viewer, timer.wait took up over 95% of the self time, so excessive computation time wasn't an issue. Using get(trialTimer, 'AveragePeriod) gave answers of 0.04 seconds, but the time measured with tic/toc (output every second) showed that 0.94 seconds was passing for each 25-frame set, instead of 1 second.

The computer is running MATLAB 2012a, with Windows XP operating system. It has 1 GB of RAM (it's a little old).

Again, the main thing that confuses me is the difference between what the timer thinks its average period is, and what the actual average period is. I don't know what would cause these to be different.


No products are associated with this question.

0 Answers

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!