How to reduce delay of QueueOutputData() during session
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Chris Carlson
am 1 Aug. 2017
Beantwortet: Walter Roberson
am 1 Aug. 2017
I am trying to queue some output data during a session. I am using s.startBackround() to collect data in real time and an event listener to queue some output data if a certain condition is met. The delay between when the queueOutputData() is called and the actual time when it outputs the data is about 40ms. I am trying to reduce this delay as much as possible. How can I do this? Is there a way to queue the output data before the function handle, and if it is in the function handle, immediately output this data?
Below is my code:
% initialize session
s = daq.createSession('ni');
% analog inputs
addAnalogInputChannel(s,'Dev1', 'ai1', 'Voltage');
addAnalogInputChannel(s,'Dev1', 'ai2', 'Voltage');
addAnalogInputChannel(s,'Dev1', 'ai3', 'Voltage');
% analog outputs
addAnalogOutputChannel(s,'Dev1', 'ao0', 'Voltage');
s.Rate = 80000;
% not sure how to not start background session without this
data0 = zeros(1,40001)';
data0(end) = [];
queueOutputData(s,repmat(data0, 5, 1));
s.IsContinuous = true;
% event listener where output data can be queued
lh = addlistener(s,'DataAvailable', @kick);
s.startBackground();
s.wait(1000);
stop(s);
handle function:
function kick(src, event)
% collects all data in single file
fp = fopen('DATA.bin', 'a');
X = event.Data;
T = event.TimeStamps;
fwrite(fp, [T, X]', 'double');
if (X(end,1) > 0 && X(end,1)<2.5)
% row of zeros to determine when queueOutputData() was called
fwrite(fp,[0 0 0 0]','double');
% create and queue output data
outputSingleValue1 = 9;
outputSignal = [outputSingleValue1*[0:0.01:1 1:-0.01:0] zeros(1,38000)];
outputSignal(end) = [];
queueOutputData(src,outputSignal');
end
fclose(fp);
end
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 1 Aug. 2017
To improve performance, do not fopen in a loop: fopen before you designate the callback, and pass the fid to the callback. Also, since your output signal is constant, pre-compute it.
outputSingleValue1 = 9;
outputSignal = [outputSingleValue1*[0:0.01:1 1:-0.01:0] zeros(1,38000)] .';
outputSignal(end) = [];
fp = fopen('DATA.bin', 'a');
lh = addlistener(s,'DataAvailable', @(src,event) kick(src, event, fp, outputSignal));
function kick(src, event, fp, outputSignal)
% collects all data in single file
X = event.Data;
T = event.TimeStamps;
fwrite(fp, [T, X]', 'double');
if (X(end,1) > 0 && X(end,1)<2.5)
% row of zeros to determine when queueOutputData() was called
fwrite(fp, [0 0 0 0]', 'double');
% queue the pre-built output data
queueOutputData(src,outputSignal);
end
end
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Analog Data Acquisition finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!