How are events queued when using the Image Acquisition Toolbox callback functions?

4 views (last 30 days)
I am using the Image Acquisition Toolbox FramesAcquiredFcn and TimerFcn callback functions. It is possible that at the times these callback functions are scheduled to execute, MATLAB is busy in other code. I would like to know how MATLAB handles the queuing of these events.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
MATLAB might execute a late TimerFcn callback, but will not place subsequent TimerFcn events in the event queue. As a result, TimerFcn events are dropped if they fall behind.
However, MATLAB queues FramesAcquiredFcn events, and the callbacks for these events are guaranteed to execute, even if they are late. Because FramesAcquiredFcn events can be queued, it is possible that calls to GETDATA from the callback could return frames that were acquired after the callback was scheduled to run. To avoid this scenario, do not acquire more frames than FramesAcquiredFcnCount in your call to GETDATA.
For example, consider the following MATLAB script:
vid = videoinput('winvideo',1);
triggerconfig(vid, 'Immediate');
set(vid,'FramesPerTrigger',inf);
set(vid,'FramesAcquiredFcnCount',4)
set(vid,'FramesAcquiredFcn',@myimaqcallback);
Where @myimaqcallback consists of the following code:
function myimaqcallback(obj, event)
% Do not acquire more than FramesAcquiredFcnCount:
data = getdata(obj,obj.FramesAcquiredFcnCount);
% Processing begins here...
It is also possible to check the FramesAvailable property from within the callback to see how much data is actually available for GETDATA.
As with many programs running on a desktop operating system, it is not straightforward to give an exact formula on how to predict when a callback will be queued or executed, or whether it will be fairly in sync with the camera or framegrabber. It will depend on the frame rate, the device, and on OS scheduling on the PC.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!