Generate Continuous and Background Signals Using NI Devices
This example shows how to generate analog output data using non-blocking commands. This allows you to continue working in the MATLAB® command window during the generation. This is called background generation. Use foreground generation to cause MATLAB to wait for the entire data generation to complete before you can execute your next command.
Create DataAcquisition Object and Add Analog Output Channels
Use the daq
function to create a DataAcquisition
object and the addoutput
function to add output channels. Set the data scan rate to 10000 samples per second. This example uses an NI 9263 module in National Instruments® CompactDAQ Chassis NI cDAQ-9178. This is module 2 in the chassis.
dq = daq("ni"); dq.Rate = 10000; addoutput(dq, "cDAQ1Mod2", 0:2, "Voltage");
Create Synchronized Signals
Generate output signals by creating a pattern of data that is repeatedly written to the output device. The data for each channel is column based and the output signals are synchronized to a common clock.
Create 3 waveforms:
data0
: 1 cycle of a sine wavedata1
: 1 cycle of a sine wave with a 45 degree phase lagdata2
: 10 cycles of a sine wave
data0 = sin(linspace(0, 2*pi, 1001))'; data1 = sin(linspace(0, 2*pi, 1001) + pi/4)'; data2 = sin(linspace(0, 2*pi*10, 1001))';
The above waveform contains sin(0) and sin(2*pi). To repeat the waveform coherently, omit the final point.
data0(end) = []; data1(end) = []; data2(end) = [];
At a generation rate of 10000 scans per second, you can expect to observe data0
and data1
as 10 Hz sine waves and data2
as a 100 Hz sine wave.
subplot(3,1,1) plot(data0) title('data0') grid on subplot(3,1,2) plot(data1) title('data1') grid on; subplot(3,1,3) plot(data2) title('data2') grid on;
Queue Output Data and Start Background Generation
To start continuous background generation immediately, preload
half a second of data. This example preloads 5000 scans of data because the device scan rate is defined as 10000 scans per second. Use start
to initiate the generation and return control to the command line immediately, allowing you to do other operations in MATLAB while the generation is running in the background.
preload(dq,repmat([data0, data1, data2], 5, 1));
start(dq, "repeatoutput")
Generate Output Data Dynamically Using MATLAB Functions
To dynamically generate the output data using a MATLAB function, assign the function to the ScansRequiredFcn
of the DataAcquisition
object. The code below is functionally equivalent to "repeatoutput"
.
dq.ScansRequiredFcn = @(src,evt) write(src, repmat([data0, data1, data2], 5, 1));
preload(dq, zeros(dq.Rate/2, 3));
start(dq, "continuous")
Monitor Data Generation
Use pause
in a loop to monitor the number of scans output by the hardware for the duration of the generation.
t = tic; while toc(t) < 1.0 pause(0.1) fprintf("Scans output by hardware = %d\n", dq.NumScansOutputByHardware) end fprintf("Generation has terminated with %d scans output by hardware\n", dq.NumScansOutputByHardware);
Scans output by hardware = 1109 Scans output by hardware = 2089 Scans output by hardware = 3100 Scans output by hardware = 4095 Scans output by hardware = 5093 Scans output by hardware = 6094 Scans output by hardware = 7082 Scans output by hardware = 8082 Scans output by hardware = 9088 Scans output by hardware = 10099 Generation has terminated with 10099 scans output by hardware
Stop Continuous Background Generation
Background generation runs simultaneously with other operations in MATLAB. Explicitly call stop
to end the background generation.
stop(dq)