How to prescribe an analog output when condition is met in analog input using background data acquisition?
9 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I'm following the example about how to acquire continuous and background data using NI devices (see code below). As in the example, I would like to detect a unique event in the analog input data (e.g., when voltage is greater than 1 V). However, instead of stopping the acquisition when the condition is met, I would like to continue data acquisiton and prescribe an analog output voltage (say, using NI 9263 on cDAQ1Mod2). If the condition is no longer met, I would like to set the analog output voltage to zero. How would I go about achieving this?
Thank you!
%% Acquire Continuous and Background Data Using NI Devices
%
% This example shows how to acquire analog input data using non-blocking
% commands. This allows you to continue working in the MATLAB command window
% during the acquisition. This is called *background acquisition*.
% Use *foreground acquisition* to cause MATLAB to wait for the
% entire acquisition to complete before you can execute your next command.
%
% Copyright 2010-2019 The MathWorks, Inc.
%% Create and Configure the DataAcquisition Object
% Use |daq| to create a DataAcquisition object and |addinput| to add an
% input channel to it. This example uses an NI
% 9205 module in National Instruments(R) CompactDAQ Chassis NI cDAQ-9178.
% This is module 1 in the chassis.
dq = daq("ni");
addinput(dq, "cDAQ1Mod1", "ai0", "Voltage");
dq.Rate = 2000;
%% Plot Live Data as It Is Acquired
% During a background acquisition, the DataAcquisition can handle acquired
% data in a specified way using the |ScansAvailableFcn| property.
dq.ScansAvailableFcn = @(src,evt) plotDataAvailable(src, evt);
%% Set ScansAvailableFcnCount
% By default, the ScansAvailableFcn is called 10 times per second. Modify
% the |ScansAvailableFcnCount| property to decrease the call frequency.
% The ScansAvailableFcn will be called when the number of points
% accumulated exceeds this value. Set the ScansAvailableFcnCount to the
% rate, which results in one call to ScansAvailableFcn per second.
dq.ScansAvailableFcnCount = 2000;
%% Start the Background Acquisition
% Use |start| to start the background acquisition.
start(dq, "Duration", seconds(5))
%%
% There are no other calculations to perform and the acquisition is set to
% run for the entire five seconds. Use |pause| in a loop to monitor the
% number of scans acquired for the duration of the acquisition.
while dq.Running
pause(0.5)
fprintf("While loop: Scans acquired = %d\n", dq.NumScansAcquired)
end
fprintf("Acquisition stopped with %d scans acquired\n", dq.NumScansAcquired);
%% Capture a Unique Event in Incoming Data
% Acquire continuously until a specific condition is met. In this example,
% acquire until the signal equals or exceeds 1 V.
%%
dq.ScansAvailableFcn = @(src,evt) stopWhenEqualsOrExceedsOneV(src, evt);
%%
% Configure the DataAcquisition to acquire continuously. The listener detects the
% 1V event and calls |stop|.
start(dq, "continuous");
%%
% Use |pause| in a loop to monitor the number of scans acquired for the
% duration of the acquisition. Note that the status string displayed by the
% |ScansAvailableFcn| may appear before the last status string displayed by
% the while loop.
while dq.Running
pause(0.5)
fprintf("While loop: Scans acquired = %d\n", dq.NumScansAcquired)
end
fprintf("Acquisition has terminated with %d scans acquired\n", dq.NumScansAcquired);
dq.ScansAvailableFcn = [];
%%
function plotDataAvailable(src, ~)
[data, timestamps, ~] = read(src, src.ScansAvailableFcnCount, "OutputFormat", "Matrix");
plot(timestamps, data);
end
function stopWhenEqualsOrExceedsOneV(src, ~)
[data, timestamps, ~] = read(src, src.ScansAvailableFcnCount, "OutputFormat", "Matrix");
if any(data >= 1.0)
disp('Detected voltage exceeds 1V: stopping acquisition')
% stop continuous acquisitions explicitly
src.stop()
plot(timestamps, data)
else
disp('Continuing to acquire data')
end
end
0 commentaires
Réponses (1)
arushi
le 29 Juil 2024 à 9:23
Hi Lesley,
I understand from your query that you want to continue the data acquisition and prescribe an output voltage based on a condition.
To achieve this, firstly add an analog output channel. You can do this using the ‘addoutput’ function.
addoutput(dq, "cDAQ1Mod2", "ao0", "Voltage");
You can learn more about it from the following documentation:
Next, you need to modify the ‘stopWhenEqualOrExceedsOneV’ function as below:
function stopWhenEqualsOrExceedsOneV(src, ~)
[data, timestamps, ~] = read(src, src.ScansAvailableFcnCount, "OutputFormat", "Matrix");
if any(data >= 1.0)
disp('Detected voltage exceeds 1V: setting analog output voltage')
outputVoltage = voltageValueIfConditionIsMet; % Set to a desired voltage level
outputSingleScan(src, outputVoltage); % Set analog output voltage
plot(timestamps, data)
else
disp('Voltage is below 1V: setting analog output voltage to zero')
outputSingleScan(src, 0); % Set analog output voltage to zero
disp('Continuing to acquire data')
end
end
Instead of stopping the acquisition explicitly using ‘src.stop()’, set the output voltage to a value based on the condition. You can use ‘outputSingleScan’ to write your data to the output channel. You can learn more about it here:
Hope it helps.
0 commentaires
Voir également
Catégories
En savoir plus sur Analog Data Acquisition dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!