How to handle timer

10 views (last 30 days)
Happy PhD
Happy PhD on 17 Feb 2020
Commented: Jon on 19 Feb 2020
I have a program in app designer, for which I have a camera that is showing live feed with a timer. See example below,..
% timer for plotting camera image
function CreateImageTimer(app)
app.timerImage = timer;
app.timerImage.StartDelay=1;
app.timerImage.ExecutionMode='fixedSpacing'; % 'fixedDelay'; %
app.timerImage.Period = 0.001;
app.timerImage.TimerFcn = @(~, ~)PlotImage(app);
app.timerImage.StopFcn = @(~, ~)CleanUpImageTimer(app);
end % end function
function CleanUpImageTimer(app)
delete(app.timerImage)
end % end fucntion
function PlotImage(app)
%tic
if(~isempty(app))
if (isempty(app))
delete(imaqfind);
end % end if
if (app.ButtonCamera.Text=='Stop Camera')
%disp('frame')
%clear('app.frame')
%axesHandlesToChildObjects = findobj(app.Video, 'Type', 'image');
% if ~isempty(axesHandlesToChildObjects)
% delete(axesHandlesToChildObjects);
% end
[app.frame, app.timeStamps]=snapshot(app.VidObj);
app.frame = app.frame; %*(2^16/2^12);
imagesc(app.Video, app.frame);
drawnow
% (more code...)
end
end
I also have a screip to control a translation stage using NET.addAssembly, which I call to move to a certain position.
My issue is that in a scan of different position (or any movement of the translation stage) the camera is still (not taking images), that is the timer seem to not be functioning simultaneously as the translation stage is moving.
Any idea why and how I can have the camera to continuously to take pictures during movement of the trsanslation stage or at least pasue the timer until the stage has reached its end position?
Is this possible?
Get a lot of errors like
Error while evaluating TimerFcn for timer 'timer-23'

Accepted Answer

Jon
Jon on 17 Feb 2020
You probably have some error in the code, PlotImage, that the timer is calling. Debugging problems in timer functions is a little tricky because the error message doesn't give much detail about where the error occurs.
I have found it helps to wrap the entire contents of the function that the timer calls in a try-catch block.
Also you can try just calling the PlotImage function with a separate test script, or command line call, not using the timer to see what errors come up.
Also it looks like you have a lot of timers, the error is for timer-23, are you sure you are stopping and deleting your old timers when you are done with them?
  8 Comments
Jon
Jon on 19 Feb 2020
I don't know enough about the details of your application to help you too much more, but I think that the underlying problems is that as I understand it MATLAB is fundamentally single threaded (it can only do one thing at a time). Even though the timers may at first look seem like they are somehow running independently what ever they are doing must fit into the one sequence of code execution steps that MATLAB is processing. So if a chunk of code, perhaps like the one that runs your translation stage, starts running, the timer may not interrupt it. It just skips doing the timer function with the default 'BusyMode'. I couldn't find any good documentation on this, but I have the impression that mex functions and other compiled functions will run to completion and not allow the timer to jump in.
Jon
Jon on 19 Feb 2020
Well, hope you can crack it, good luck with your research!

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Tags

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!