How to close a DAQ session with triggers remaining

6 vues (au cours des 30 derniers jours)
Shun Li
Shun Li le 15 Août 2023
Commenté : Walter Roberson le 31 Août 2023
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:
  1. type stop(d) in command window
  2. ctrl-c
  3. 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 commentaires
Walter Roberson
Walter Roberson le 15 Août 2023
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.
Shun Li
Shun Li le 16 Août 2023
I dont have a struct assignment, I just didn't explicitly write out the line I add d (which I did now). The code works and runs normally. Its just I can't find a way to end it with triggers remaining

Connectez-vous pour commenter.

Réponse acceptée

Ninad
Ninad le 28 Août 2023
Modifié(e) : Ninad le 31 Août 2023
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 commentaires
Ninad
Ninad le 31 Août 2023
Thanks for pointing out Walter. OnCleanup is a function provided for the similar purpose as needed by Shun.
Walter Roberson
Walter Roberson le 31 Août 2023
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Hardware Discovery and Setup dans Help Center et File Exchange

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by