How to stop timer function at a specified time?
2 ビュー (過去 30 日間)
古いコメントを表示
Hello..
I have used a timer function to capture image files continuously at a fixed rate automatically, at a specified time. I used startat function to do this and it's working, however, my problem is that, I can't stop it at a specified time I want. Instead I use pause. However, once I paused the program, the whole program halts and it never execute the next functions.
Can anyone help me?
Here's some part of my code:
dstart={'07:30' '07:32' '07:34' '12:00' '13:30' '15:00' '18:00' '19:30'};
dfinish={'07:31' '07:33' '07:35' '13:25' '07:31' '16:25' '09:01' '20:55'};
%here i used the captureimage function to capture image using imaq toolbox..
t1=timer('TimerFcn', 'captureimage', 'ExecutionMode', 'FixedRate');
tp1=timer('TimerFcn', 'pause'); %I can't stop the timer fcn that's why i use pause
%the capture starts exactly at 07:30AM continuously with 7second interval
startat(t1, dstart(1,1));
%here, the program pauses when it is exactly 07:31AM
startat(tp1, dfinish(1,1));
How can i stop the timer function without using pause, because instead of pausing the timer function, the whole program pauses.
I will greatly appreciate your help.
Thank you.
0 件のコメント
採用された回答
Brett Shoelson
2011 年 2 月 4 日
Kent, Why not build into the callback a comparison of the current time (help NOW) with the desired stop time? Then, if current time >= desiredStopTime, then issue a STOP command. Cheers, Brett
その他の回答 (2 件)
Jan
2011 年 2 月 4 日
% Create times as DATENUM vector:
TimerData.start = floor(now) + datenum( ...
{'07:30' '07:32' '07:34' '12:00' '13:30' '15:00' '18:00' '19:30'});
TimerData.finish = floor(now) + datenum( ...
{'07:31' '07:33' '07:35' '13:25' '07:31' '16:25' '09:01' '20:55'});
TimerData.index = 1;
T = timer('TimerFcn', @myTimerFcn, ...
'ExecutionMode', 'FixedRate', 'Period', 7.0, ...
'UserData', TimerData);
startat(T, TimerData.start(1));
% ------ Timer function:
function myTimerFcn(TimerH, EventData)
TimerData = get(TimerH, 'UserData');
% Stop time reached?
if now >= TimerData.finish(TimerData.index)
stop(TimerH);
TimerData.index = TimerData.index + 1;
% Last time reached?
if TimerData.index > length(TimderData.start)
delete(TimerH); % Cleanup
else % Restart the timer at the next start time:
set(TimerH, 'UserData', TimerData); % EDITED2
startat(TimerH, TimerData.start(TimerData.index));
end
return; % EDITED
end
% Perform the action - insert your code here:
capturimage
% ------ End: Timer function
Good luck!
4 件のコメント
Jan
2011 年 2 月 4 日
@Kent: StartFcn and StopFcn are executed, when the timer is started or stopped, but they do not trigger the starting or stopping.
Yes, you need to start the timer by STARTAT. I insert it in the code.
I've inserted an RETURN in the code to improve it. Please try it again.
Kent Cabarle
2011 年 2 月 5 日
12 件のコメント
Walter Roberson
2011 年 2 月 7 日
If that is the error message you are getting, then you have a typo in your code, 'TimderData' instead of 'TimerData'
参考
カテゴリ
Help Center および File Exchange で Environment and Settings についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!