How to reduce delay of QueueOutputData() during session
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Chris Carlson
le 1 Août 2017
Réponse apportée : Walter Roberson
le 1 Août 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 commentaires
Réponse acceptée
Walter Roberson
le 1 Août 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 commentaires
Plus de réponses (0)
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!