Simultaneously acquire data from one channel and generate data in the other channel based on the acquired data in the same NI DAQ [MATLAB 2016b]
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I am using NI 'Dev1' to acquire data continuously from a rotary encoder and a photobeam sensor, so I create a session object and add three input channels. Here is my code:
% Create a data acquisition session object, s
s = daq.createSession('ni');
s.Rate = 5000; % number of scans per second
s.DurationInSeconds = 20; % duration of the session
% Add two input channels for rotary encoder
channelA = addAnalogInputChannel(s, 'Dev1', 'ai0', 'Voltage');
channelB = addAnalogInputChannel(s, 'Dev1', 'ai4', 'Voltage');
% Set channel mode
channelA.TerminalConfig = 'SingleEnded';
channelB.TerminalConfig = 'SingleEnded';
% Add one input channel for photobeam sensor
channelLight = addAnalogInputChannel(s, 'Dev1', 'ai1', 'Voltage');
channelLight.TerminalConfig = 'SingleEnded';
Now I wish to add another output channel to generate data to activate a water pump based on the input data in the previous step. For this output channel, I wish the output voltage to be constantly 0V. But when the voltage signal of channelLight exceeds a certain value, the output of channelWaterR will change to 5V for 500 ms. After 500 ms, the output goes back to 0V again.
% Add one output channel
channelWaterR = addAnalogOutputChannel(s, 'Dev1', 'ai5', 'Voltage');
channelWaterR.TerminalConfig = 'SingleEnded';
I read from the documentation that it is required to queue the output data before starting the session. However, I did not find a way to modify this output after the session start.
% Generate constant 0V output for the water reward channel
n = s.DurationInSeconds * s.Rate; % number of timestamps for the whole session
output = zeros(n, 1); % 0V output continuously
queueOutputData(s, output);
After this, I add a listener to plot a graph by using a self defined function, plotline, and update the graph every 2s.
Then, I start the data acquisition session.
% Add listener
lh = addlistener(s, 'DataAvailable', @(src, event) plotline(app, event.TimeStamps, event.Data));
% Update plot every 2s
s.NotifyWhenDataAvailableExceeds = round(s.Rate.*2);
% Start data acquisition
startBackground(s);
% Wait
wait(s);
Is there anyway that I can code inside my plotline function, so that when some criteria are met (for example, when the data from channelLight exceeds a certain value), then the voltage of the output channel changes?
0 Commenti
Risposte (0)
Vedere anche
Categorie
Scopri di più su Analog Data Acquisition in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!