acquire continuous and background data using ni devices -凯发k8网页登录
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.
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® 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);
while loop: scans acquired = 1000
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 = [];
while loop: scans acquired = 1000 detected voltage exceeds 1v: stopping acquisition while loop: scans acquired = 2000 acquisition has terminated with 2000 scans acquired
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
while loop: scans acquired = 2200 while loop: scans acquired = 3200 while loop: scans acquired = 4200 while loop: scans acquired = 5200 while loop: scans acquired = 6200 while loop: scans acquired = 7200 while loop: scans acquired = 8200 while loop: scans acquired = 9200 while loop: scans acquired = 10000 acquisition stopped with 10000 scans acquired