How to close a DAQ session with triggers remaining
12 次查看(过去 30 天)
显示 更早的评论
Hi,
I'm using the DAQ toolbox to send a pattern to a motor whenever an external trigger arrives. For now, I can successfully drive the motor upon trigger arrival. However, since I do not know the exact number of triggers during the recording session (dependent on animal), I set the d.NumDigitalTriggersPerRun to 200 to ensure the motor moves every time theres a trigger.
However, this introduce a problem as I can't manually close the DAQ session, I've tried following without success:
- type stop(d) in command window
- ctrl-c
- Force re-run by clicking run above (matlab will always crash)
My code can be simplified as follows, thanks in advance!
clear; close all
d = daq('ni')
d.NumDigitalTriggersPerRun = 200;
% lines to addinput
% lines to addoutput
addtrigger(d,"Digital","StartTrigger","External",'Galvo/PFI0'); % add trigger
% lines to generate motor pattern
% run session
stop(d); % stop session if required
flush(d);
preload(d,pattern);
start(d);
2 个评论
Walter Roberson
2023-8-15
You "clear" so we know that there are no variables remaining in memory -- that in particular there is no d before the assignment you show.
You then have a structure assignment, and pass the structure to addtrigger(). But addtrigger() does not accept struct: it expects DataAcquisition objects.
采纳的回答
Ninad
2023-8-28
编辑:Ninad
2023-8-31
The script may not immediately stop when using ‘stop(d)’ or ‘Ctrl+C’ due to the asynchronous nature of DAQ operations. In an asynchronous session, data acquisition tasks run independently of the main program flow, allowing concurrent execution. MATLAB's DAQ operations default to asynchronous mode, enabling multitasking during acquisition. This behaviour is advantageous as it allows monitoring progress, processing acquired data, and performing calculations simultaneously, enhancing efficiency.
You can use a onCleanup method to detect the ‘Ctrl+C’ command.
cleanupObj = onCleanup(cleanupFun) creates an object that, when destroyed, executes the function cleanupFun. MATLAB® implicitly clears all local variables at the termination of a function, whether by normal completion, or a forced exit, such as an error, or Ctrl+C.
PF following link:
3 个评论
Ninad
2023-8-31
Thanks for pointing out Walter. OnCleanup is a function provided for the similar purpose as needed by Shun.
Walter Roberson
2023-8-31
Except that Shun is working with a script, not a function. Shun would have to deliberately wrap code into a function to take advantage of onCleanup . Which would probably be acceptable to do.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Acquisition Toolbox Supported Hardware 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!