How to stop timer function at a specified time?

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.

 Réponse acceptée

Brett Shoelson
Brett Shoelson le 4 Fév 2011

0 votes

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

Plus de réponses (2)

Jan
Jan le 4 Fév 2011
% 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 commentaires

Kent Cabarle
Kent Cabarle le 4 Fév 2011
thank you jan.. i'll see what i can do with that.. i also appreciate it, thanks to both of you..
Kent Cabarle
Kent Cabarle le 4 Fév 2011
Jan, will I still use startat function to execute the timer function?
Kent Cabarle
Kent Cabarle le 4 Fév 2011
Jan, I've tried it, but still, the capture does not stop at 07:31AM (where the capture starts at 07:30AM)..instead, the program keeps on capturing (from 7:30AM onwards).. What do you think is the problem?
Can StartFcn or StopFcn be useful?
Thanks.
Jan
Jan le 4 Fév 2011
@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.

Connectez-vous pour commenter.

Kent Cabarle
Kent Cabarle le 5 Fév 2011
%Actually, here is my main code..
function kxlsforum
% Assignation
[num dset]=xlsread('class.xls'); % 9x7 skedule
day = datestr(now, 'dddd'); % exact date
t0=timer('TimerFcn', 'kaptur1camtemplate', 'ExecutionMode', 'SingleShot');
dk={'1' '2' '3' '4' '5' '6' '7' '8'};
% Assign and check day and time
for n=2:7 % check Mon - Sat
k=dset(1,n); % disp Mon-Sat etc, compare with actual day
c = strcmp(k,day);
if c == 1
fdate=datestr(now, 'mmddyyyy');
mkdir(fdate);
disp('Today is '); disp(day);
disp('Capturing an image template..');
start(t0); % capture 1 image as template
disp('Image template captured')
for p=1:8
de(1,p)=isempty(dset{p+1,n}); % 0 - occupied
switch de(1,p) % 1 - vacant
case 0
mkdir(fdate, dk{1,p});
end
end
disp(de);
break % break FOR loop
end
end
f=find(~de); % find the 1st class time & last class time capture
mini=min(f);
maxi=max(f);
TimerData.start = floor(now) + datenum({'07:30' '07:34' '07:37' ... '12:00' '13:30' '15:00' '18:00' '19:30'});
TimerData.finish = floor(now) + datenum({'07:31' '07:35' '07:38'... '13:25' '07:31' '16:25' '09:01' '20:55'});
TimerData.index = 1;
T = timer('TimerFcn', @myTimerFcn, 'ExecutionMode','FixedRate',... 'Period', 7.0,'UserData', TimerData);
start(T) % i used this is execute the timer
Here is the timer function (separate file)
% ------ Timer function: function myTimerFcn(TimerH, EventData) TimerData = get(TimerH, 'UserData'); %Stop time reached?
TimerData.start = floor(now) + datenum({'07:30' '07:34' '07:37' '12:00' '13:30' '15:00' '18:00' '19:30'}); TimerData.finish = floor(now) + datenum({'07:31' '07:35' '07:38' '13:25' '07:31' '16:25' '09:01' '20:55'}); TimerData.index = 1;
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:
startat(TimerH, TimerData.start(TimerData.index));
end
return;
end
% Perform the action - insert your code here:
kaptur1
end
% ------ End: Timer function
In the main function, i used the start(T) to execute the timer, instead of startat.. (i used a separate function which is kaptur1camtemplate to capture a single image which will be the basis for image processing..[not included here])
and in the timer function, i just did what you advised me to do..(i used the kaptur1 function for image capturing here..) however, the capturing did not start capturing at 7.30AM, and still not stopping at its specified time, for for this instance 7.31AM..
Can't figure it out.. What do you think?
Thanks for the help..a lot..

12 commentaires

Jan
Jan le 5 Fév 2011
I'm confused. If the capturing does not start at 7:30, how do you find out, that it does not stop at 7:31?? If you start the timer by START, it starts immediately, not at 7:30.
I assume using the debugger would help to find out, what happens. Set some break points and go through the program line by line.
Kent Cabarle
Kent Cabarle le 5 Fév 2011
once i start the capture for example my time now is 7.28AM, it starts triggering in that time, and not with 7.30AM ..and it continuously capturing, without stopping at 07.31AM.. it means the capturing does not actually stops at the time we wanted to..
all i need is how to stop the timer T at a specific time specified..
thanks a lot for the concern..
Jan
Jan le 5 Fév 2011
What does kaptur1 do? Perhaps you start this function, it runs continuously and you expect it to be stopped from the outside? This will not work and is not connected to the timer at all. Then you have to modify this function, such that it stops the recording.
Kent Cabarle
Kent Cabarle le 6 Fév 2011
kaptur1 is the same as the captureimage from my 1st post.. i just changed it to be familiar with the reader easily..
kaptur1 is my separate function which captures a single image data (i.e which uses getsnapshot).. because of the 'FixedRate' in the code:
T = timer('TimerFcn', @myTimerFcn, 'ExecutionMode','FixedRate',... 'Period', 7.0,'UserData', TimerData);
..it continuously capturing on and on..
my only problem is how to stop it.. the very opposite of startat function, is exactly what i think i need..at the very first place, MATLAB can start a function at a specific time using the timer function, and i believe that there's also a way (like a stopat command) to do the same thing, except that it stops at specific time..
i tried the debugger and setting break points but they are only applicable for the looping, not particularly with the execution time especially using startat..
[I had a mistake by mentioning: kaptur1camtemplate to capture a single image which will be the basis for image processing..[not included here])]
because i thought it was not included to my post..
Jan
Jan le 6 Fév 2011
1. Please cleanup the layout of the code you've posted. It looks, like you redefine the Timer's UserData inside the callback?!
2. What does happen if this line is processed in my suggested solution, after the finish time has been reached: "now >= TimerData.finish(TimerData.index)" ??? You _can_ set a breakpoint in "stop(TimerH)"; Please do not state repeatedly, that the timer does not stop, but post, what happens instead in the "stop(TimerH)" line.
3. I've added the forgotten update of the Timer's UserData in my example code (see EDITED2 tag).
Kent Cabarle
Kent Cabarle le 7 Fév 2011
1. Aplogy. I already removed the Timer's UserData inside the callback.. my bad..
2. When i run the 2 functions you posted (the main and the myTimerFcn)
??? Error using ==> timer.startat at 123
StartDelay property cannot be set. StartDelay is limited to
values
less than 2.1474e+006.
Error in ==> simon at 12
startat(T, TimerData.start(1));
3. Yes, thanks, is used it, and there some kinda error which is in #2..
What's the probable cause of the error in #2 ?
Walter Roberson
Walter Roberson le 7 Fév 2011
When unspecified, the year defaults to the current year, month and day default to January 1, hour and minute default to 00:00, and second and millisecond default to 00.000.
Walter Roberson
Walter Roberson le 7 Fév 2011
Some of what I thought I wrote seems to have disappeared.
Your problem is in the floor(now) + datenum({'07:30'}) . floor(now) is calculating the serial date number of the beginning of today. datenum({'07:30'}) applies the defaults I quoted above, Jan 1 of the current year, so you are getting the serial datenum of Jan 1 2011 07:30 and adding that to the datenum of the start of the day.
The solution is fairly simple:
floor(now) + datenum([0 0 0 7 30 0; 0 0 0 7 34 0; 0 0 0 7 37 0])
and so on.
Jan
Jan le 7 Fév 2011
@Walter: You are correct. I tried it under Matlab 6.5: DATENUM('07:31') => 0.3125, Matlab 2009a: DATENUM('07:30') => 7.3341e+5.
Even "floor(now)" will fail, if you run the program ad midnight. The most stable method would be to use a full time specifier as DATENUM('07-Feb-2011 07:31:00').
Kent Cabarle
Kent Cabarle le 7 Fév 2011
sir walter, i found this error when i tested what you told me..
??? Error while evaluating TimerFcn for timer 'timer-5'
Undefined variable "TimderData" or class "TimderData.start".
the capture started at exactly 7:30:00AM, after 7:30:59AM, the capture stops and the error went out..what do you think?
sir jan simon, thank you for the help.. sir walter, thanks also..
Walter Roberson
Walter Roberson le 7 Fév 2011
If that is the error message you are getting, then you have a typo in your code, 'TimderData' instead of 'TimerData'
Kent Cabarle
Kent Cabarle le 7 Fév 2011
@Walt. It worked. It starts capturing at 7.30AM and stops at 7.31AM. Then starts capturing again at 7.32AM, and so on.. Great. Thank you..
@Jan: Thanks also. Thanks for the kindness and wit. Thanks to both of you. Appreciate it. ^^

Connectez-vous pour commenter.

Catégories

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by