acquire data from two devices at different rates -凯发k8网页登录
this example shows how to acquire data from two different daq devices running at different sampling rates. the example uses two national instruments compactdaq analog input modules (9201 and 9211) that have different acquisition rate limits. the 9211 module is used for temperature measurements and acquires at a slower rate (10 hz) than the 9201 module, which is used to measure voltage (100 hz). since all channels in a data acquisition object must acquire at the same rate, to acquire from two modules at multiple rates you need to use two data acquisition objects. to make both daq devices start simultaneously, you can use a hardware digital triggering configuration.
hardware setup
compactdaq chassis ni cdaq 9178 ('cdaq1')
ni cdaq 9211 module with thermocouple measurement type ('cdaq1mod1')
ni cdaq 9201 module with voltage measurement type ('cdaq1mod2')
thermocouple probe (type k)
analog voltage signal generated by a function generator instrument
configure data acquisition objects and channels
create two data acquisition objects, each with one analog input channel from a 9211 module or 9201 module. the data acquisition objects acquire data at rates of 10 hz and 100 hz, respectively.
% specify a common acquisition duration for both devices, in seconds daqduration = seconds(3); % create and configure dataacquisition object and channels for cdaq 9211 module d1 = daq('ni'); addinput(d1, 'cdaq1mod1', 'ai0', 'thermocouple'); d1.channels(1).thermocoupletype = 'k'; d1.rate = 10;
warning: the rate property was reduced to 14.2857 due to changes in the channel configuration.
% create and configure dataacquisition object and channels for cdaq 9201 module d2 = daq('ni'); addinput(d2, 'cdaq1mod2', 'ai0', 'voltage'); d2.rate = 100;
configure trigger connections
to synchronize the acquisition start you can use hardware triggering and a source/destination approach. one of the data acquisition objects (source) is started manually and triggers the acquisition start of the other data acquisition object (destination).
note: if you have a compactdaq chassis model (such as ni 9174) which does not have pfi triggering terminals, you can use an additional digital i/o module (such as ni 9402) to provide the pfi terminals for the trigger connections.
% configure the source data acquisition object to export a triggering % signal on the pfi0 terminal of cdaq1 chassis addtrigger(d1, 'digital', 'starttrigger', 'cdaq1/pfi0', 'external'); % configure the destination data acquisition object to start acquisition when an % external triggering signal is received at pfi0 terminal of cdaq1 chassis addtrigger(d2, 'digital', 'starttrigger', 'external', 'cdaq1/pfi0');
start acquisition and wait until complete
the destination data acquisition object must start first and be ready for an external trigger before the source data acquisition object starts.
start(d2, 'duration', daqduration) while ~d2.waitingfordigitaltrigger pause(0.1) end start(d1, 'duration', daqduration) % wait until data acquisition is complete while d1.running || d2.running pause(1) end
background operation has started. background operation will stop after 3 s. to read acquired scans, use read. background operation has started. background operation will stop after 3 s. to read acquired scans, use read.
save data as timetable
for each data acquisition object, the acquired measurement data and timestamps were stored in memory. read all acquired data from memory in the default timetable format.
data1 = read(d1, 'all'); data2 = read(d2, 'all');
plot acquired data
because the acquired data from the two devices have different scales and units, create a chart with two y-axes.
figure yyaxis left plot(data1.time, data1.variables, '-x') ylabel('temperature (deg. c)') ylim([0 50]) yyaxis right plot(data2.time, data2.variables, '-o') ylabel('voltage (v)') xlabel('time (s)')
clean up
clear the data acquisition objects to disconnect from hardware.
clear d1 d2