Time Crisis! Why is my timer not executing the callback function at the period specified?

4 views (last 30 days)
Hi, I have a huge project which uses a timer to execute a task periodically. It was working fine until today. After hours of debugging, I found that the 'set'command, which I was using to set xdata and ydata for a plot, was causing a delay in my timer. I made a test file to confirm it was the set command. Below is the code.
So basically it is meant to update the plots every 100ms but it isn't for the first few iterations.
Somehow it stabilises to 100ms in this code, but in my other program it remains offset.
function timerTest() I=1 %start a plot h = plot(0,0,'r');
%Start timer
t1 = timer('Period', 0.1, 'TasksToExecute', 20, ...
'ExecutionMode', 'fixedRate');
t1.TimerFcn = {@my_callback1,I};
start(t1)
function my_callback1(a,b,c)
tic
disp(strcat('LSR',datestr(now,'MM:SS:FFF')));
set(h,'xdata',[1:1:100].*I,'ydata',[1:1:100].*I);
set(h,'xdata',[1:1:100].*I+1,'ydata',[1:1:100].*I+1);
set(h,'xdata',[1:1:100].*I+1,'ydata',[1:1:100].*I+1);
set(h,'xdata',[1:1:100].*I+1,'ydata',[1:1:100].*I+1);
set(h,'xdata',[1:1:100].*I+1,'ydata',[1:1:100].*I+1);
set(h,'xdata',[1:1:100].*I+1,'ydata',[1:1:100].*I+1);
I = I+1;
toc
end
end
Here is the output
if true
% LSR52:49:318 <<here
Elapsed time is 0.001267 seconds.
LSR52:49:468 <<here (50ms?)
Elapsed time is 0.001068 seconds.
LSR52:49:517 <<here
Elapsed time is 0.000840 seconds.
LSR52:49:618
Elapsed time is 0.001026 seconds.
LSR52:49:718
Elapsed time is 0.001022 seconds.
LSR52:49:818
Elapsed time is 0.000981 seconds.
LSR52:49:917
Elapsed time is 0.001049 seconds.
LSR52:50:088 <<here
Elapsed time is 0.001002 seconds.
LSR52:50:117
Elapsed time is 0.000844 seconds.
LSR52:50:218
Elapsed time is 0.001000 seconds.
LSR52:50:319
Elapsed time is 0.002937 seconds.
LSR52:50:418
Elapsed time is 0.001102 seconds.
LSR52:50:519
Elapsed time is 0.002921 seconds.
LSR52:50:618
Elapsed time is 0.001025 seconds.
LSR52:50:717
Elapsed time is 0.001014 seconds.
LSR52:50:819
Elapsed time is 0.002934 seconds.
LSR52:50:918
Elapsed time is 0.001065 seconds.
LSR52:51:018
Elapsed time is 0.001052 seconds.
LSR52:51:119
Elapsed time is 0.002973 seconds.
LSR52:51:217
Elapsed time is 0.000984 seconds.
end
So Even though the time taken to execute the callback function is very small, the timer runs the callback at a time greater or less than 100ms
anyone know why? Thanks for your help.

Answers (1)

Jos (10584)
Jos (10584) on 7 Apr 2014
Your callback function takes a lot of time to execute, including (many) redundant set operations and prints to the command window (toc). This will not work that properly, similar to this example:
t1 = timer('Period', 1, 'TasksToExecute', 20, ...
'ExecutionMode', 'fixedRate',
'TimerFcn','pause(1.1) ; toc') ;
tic
start(t1)
An option is to optimise the timer function!
  1 Comment
Chris
Chris on 7 Apr 2014
As I mentioned, this was merely a testing code. I put many redundant set commands to resemble my real code, which must call set several times.
More importantly, even though there are many set operations, the tic and toc tells me that it is only taking 10ms on average to perform ALL of the set operations, which is less than 100ms. So the time it takes to execute the callback is not exceeding the specified period.

Sign in to comment.

Categories

Find more on Programming Utilities in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!