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'

 채택된 답변

Jon
Jon 2020년 2월 17일

0 개 추천

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

Happy PhD
Happy PhD 2020년 2월 17일
편집: Happy PhD 2020년 2월 17일
I should have only two timers, one to take a snapshot from the camera and one to plot the image intensity profile. I don't get how there can be more timers. I start and stop the timers with a button.
Jon
Jon 2020년 2월 17일
편집: Jon 2020년 2월 17일
Every time you execute your CreateImageTimer code you will create a new timer. You need to delete the timers, not just stop them when you are done. You can either delete the timer specifically using
delete(app.timerImage)
or if you just want to make sure you get rid of all of them
delete(timerfindall)
Actually I see you have a function that should do this.
CleanUpImageTimer(app)
Maybe it doesn't get called everytime to do the cleanup.
I don't think this is your main problem though, you need to find the bug in the function that the timer calls
Happy PhD
Happy PhD 2020년 2월 17일
Yes I had forgot to call the CleanUpImageTimer..
Question about timers, what happens if you have a while loop or any other executions in your code when using timers.
My main problem is that the timer for taking a snapshot and plotting the images of the camera on the graph is basically still when I am moving my translation stage and continues after the movement is completed as normal. Can't they be runned simultaneously?
That is, is it possible to have the camera feed on (with my timer) and active during the movement of the translation stage? Right now, the image rate seem to be still during this motion.
Jon
Jon 2020년 2월 17일
편집: Jon 2020년 2월 17일
I don't really know about the details of your situation, but in general, the timer functions are not truly multithreaded. So they still must fit into the single excecution thread that MATLAB is running. As long as there is enough spare time everything works fine, but if not something has to give. Assuming you are having a problem where the timer just doesn't have enough time to excecute, you can have some control of this using the 'BusyMode' property of the timer, the default is to drop th timer function if there isn't enough time.
I think you still also need to find what is causing the error that you noted earlier
Error while evaluating TimerFcn for timer 'timer-23'
Jon
Jon 2020년 2월 19일
Were you finally able to resolve your problem. Did this answer your question?
Happy PhD
Happy PhD 2020년 2월 19일
편집: Happy PhD 2020년 2월 19일
Well, I haven’t been able to find out why I’m getting error with timers. In reality I only have two timers. One for plotting the image and one to plot its image profile. I turn the timer on and off with one (same) button and when I turn off the timer I delete the timer with a cleanup function. So there shouldn’t be more than two at most.
Idk if stopping the timer and deleting the timer is enough?
Most of my error with timers says timer has already been deleted or stopped and it can probably not delete it stop anything that doesn’t exist.
Can’t you tag the two timer so you know which timer it is about?
I have the busy mode to the default mode (drop). I know that the time it takes to complete the function is longer than the period of the timer, but it’s not a set one value. It usually varies a little. Also I have fixedSpacing modes.
The timer seem to halt completely when I activated my translation stage but resumes afterwards.
Been playing with the timer all day,.. it’s showing very confusing result. When the camera (timer) is off to begin with, I turn on the timer , it seem to be active during my movement of translation stage. If my camera (timer) is on to begin with .. it freezes and does not take an new snapshot until the end of the movement. I find this very weird. Just taking a snapshot (without timer) works whenever.
Jon
Jon 2020년 2월 19일
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 2020년 2월 19일
Well, hope you can crack it, good luck with your research!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

제품

릴리스

R2019b

태그

질문:

2020년 2월 17일

댓글:

Jon
2020년 2월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by