Stopping and restarting a timer within its own callback - why does it then run the callback twice at a time, but only with fixedSpacing?

11 ビュー (過去 30 日間)
I want to start and stop a timer within the timer callback function*. I do this with code like the following script:
global timerRunCount
timerRunCount = 0;
% Try changing "fixedDelay" to "fixedSpacing".
timerObj = timer(ExecutionMode="fixedDelay", Period=1, StartDelay=1, TimerFcn=@timerFunction);
timerObj.start;
function timerFunction(tmr, ~)
global timerRunCount
timerRunCount = timerRunCount + 1;
if isnan(tmr.InstantPeriod)
disp("Iteration: " + timerRunCount + "; interval: N/A")
else
disp("Iteration: " + timerRunCount + "; interval: " + tmr.InstantPeriod)
end
switch timerRunCount
case 5
tmr.stop
tmr.start
case 10
tmr.stop
tmr.start
case 15
tmr.stop
tmr.delete
end
end
Result:
% Iteration: 1; interval: N/A
% Iteration: 2; interval: 1.001
% Iteration: 3; interval: 1.001
% Iteration: 4; interval: 1.001
% Iteration: 5; interval: 1.001
% Iteration: 6; interval: N/A
% Iteration: 7; interval: 1.002
% Iteration: 8; interval: 1.001
% Iteration: 9; interval: 1.001
% Iteration: 10; interval: 1.001
% Iteration: 11; interval: N/A
% Iteration: 12; interval: 1.001
% Iteration: 13; interval: 1.001
% Iteration: 14; interval: 1
% Iteration: 15; interval: 1.001
We see that it happily runs timerFunction() about every second. (I know that global variables are poor practice; it's just to keep this example simple).
Now see what happens when we change ExecutionMode to "fixedSpacing":
% Iteration: 1; interval: N/A
% Iteration: 2; interval: 1.046
% Iteration: 3; interval: 1.002
% Iteration: 4; interval: 1.002
% Iteration: 5; interval: 1.003
% Iteration: 6; interval: N/A
% Iteration: 7; interval: 0.004
% Iteration: 8; interval: 1
% Iteration: 9; interval: 0.003
% Iteration: 10; interval: 0.999
% Iteration: 11; interval: N/A
% Iteration: 12; interval: 0.001
% Iteration: 13; interval: 1.001
% Iteration: 14; interval: 0.002
% Iteration: 15; interval: 1.001
The first five iterations work as expected; but after we first start and stop the timer, it runs timerFunction() twice every time the timer triggers. We see this from the intervals alternating between ~1s and a few ms. In the third section, I have sometimes seen the timerFunction() run three times for every timer trigger (depending on how long the second stage runs for).
Can anyone please explain why a change to ExecutionMode causes this issue? By my understanding of the Matlab help, this should only change whether or not the duration of timerFunction() is included in the timer period. I don't actually need this issue fixed (the exact period of my timer isn't important), but I want to understand any strange subtleties of the timer object before basing my application on it.
* The reason is that I want to repeatedly run a function with a timer (checking a shared memory location for changes) and when it detects changes, switch to a different polling function, and when *that* detects changes, switch to yet another different polling function, etc. You have to stop a timer before you can change the period, and then restart it.
  3 件のコメント
raym
raym 2023 年 2 月 18 日
I tried fixedSpacing and fixedDelay,the problem still exist.
raym
raym 2023 年 2 月 18 日
https://www.mathworks.com/matlabcentral/answers/1914920-stop-and-re-start-timer-inside-timerfcn-cause-pseudo-multiple-parallel-duplicated-timer-in-fact-onl

サインインしてコメントする。

回答 (1 件)

Sugandhi
Sugandhi 2023 年 2 月 15 日
Hi David,
I work at MathWorks, and the relevant people are aware of the issue.
Regards,
Sugandhi.
  2 件のコメント
David Szwer
David Szwer 2023 年 2 月 15 日
That's good to know, thank you.
(I won't "Accept" this answer, until I hear about a workaround or a fix, but it's still very helpful).
raym
raym 2023 年 2 月 18 日
  1. I eucountered the same problem today:
  2. https://www.mathworks.com/matlabcentral/answers/1914920-stop-and-re-start-timer-inside-timerfcn-cause-pseudo-multiple-parallel-duplicated-timer-in-fact-onl

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by