NIDAQ continuous and background acquisition
12 ビュー (過去 30 日間)
古いコメントを表示
Hello everybody,
I'm trying to acquire and store data from a CompactDAQ from National Instruments. The configuration of the device itselt and the visualization of the data is pretty straightforward, but I'm having trouble in store this data in a matrix that I will be able to access after the acquisition is over.
I'm connecting the device as follows:
s=daq.createSession('ni');
s.addAnalogInputChannel('cDAQ1Mod2', 0, 'voltage');
s.Rate=2000;
s.DurationInSeconds = 60;
to plot the data in real time I use the following lines of code:
lh = s.addlistener('DataAvailable', @plotData);
function plotData(src,event)
plot(event.TimeStamps, event.Data)
end
and then I start it in background (I need to acquire data in background in order to perform other tasks at the same time):
s.startBackground();
This seems to work fine for the real time problem, but I'm having trouble to define a callback function that will store all data acquired into a matrix or structure. I'm sure that this is a quite basic problem.
I want to thank you all in advance for your time.
4 件のコメント
Jacob Graham
2018 年 6 月 15 日
Hey Everyone,
Looks like I'm a few years late to the party. These solutions and comments have been very helpful, but I still have a problem. The while(~s.IsDone) loop doesn't appear to be working for me. When I run the code below, Matlab remains busy indefinitely while it's on the while(~s.IsDone) line. When I debug and step past it manually, the code that follows works. I'd like to use the command window while the session is running in the background which is why this is a problem.
Thanks for your help
J
function acquireData
clear global;
global data
s = daq.createSession('ni');
ch3 = addAnalogInputChannel(s,'cDAQ1Mod1', 'ai3', 'RTD');
ch3.RTDType = 'Pt3851';
ch3.RTDConfiguration = 'ThreeWire';
ch3.R0 = 100;
s.Rate = 100;
duration = input('Duration of Sampling Period?');
s.DurationInSeconds = duration;
time = (0.01:0.01:duration);
lh = s.addlistener('DataAvailable', @plotData);
startBackground(s);
while(~s.IsDone)
end
close(gcf);
plot(time,data);
delete(lh);
end
function plotData(src, event)
global tempData;
global data
if(isempty(tempData))
tempData = [];
end
plot(event.TimeStamps, event.Data)
tempData = [tempData;event.Data];
data = tempData;
end
Walter Roberson
2018 年 6 月 15 日
See the change suggested at https://www.mathworks.com/matlabcentral/answers/6258-nidaq-continuous-and-background-acquisition#answer_24925
Or put in a brief pause() in the while loop so that there is an opportunity for background code to run and events to be processed.
採用された回答
Chirag Gupta
2011 年 4 月 26 日
Hi Nuno,
There are number of ways to achieve this:
1) Use some global data
function acquireData
global data
s = daq.createSession('ni');
s.addAnalogInputChannel('cDAQ1Mod1',0,'voltage')
s.Rate = 2000
s.DurationInSeconds = 5
lh = s.addlistener('DataAvailable',@plotData);
s.startBackground();
% Do something
while(~s.IsDone)
end
close(gcf);
plot(data); % plot global data
function plotData(src,event)
persistent tempData;
global data
if(isempty(tempData))
tempData = [];
end
plot(event.TimeStamps, event.Data)
tempData = [tempData;event.Data];
data = tempData;
end
2) Use NotifyWhenDataAvailableExceeds property (This decides when 'DataAvailable' is called. So if you don't want a live plot then this is suitable. Just set this to s.Rate * s.DurationInSeconds.
and have a callback for DataAvailable as:
function collectData(src,event)
time = event.TimeStamps;
data = event.Data;
plot(time,data)
save log.mat data time
disp(‘Background Acquisition complete’);
end
This should save the data to a matfile.
3) Write Data to a file:
function acquireData
s = daq.createSession('ni');
s.addAnalogInputChannel('cDAQ1Mod1',0,'voltage')
s.Rate = 2000
s.DurationInSeconds = 5
fid = fopen('log.csv','w');
lh = s.addlistener('DataAvailable',@(src,event)saveData(fid,event));
s.startBackground();
while(~s.IsDone)
end
function saveData(fid,event)
time = event.TimeStamps;
data = event.Data;
plot(time,data)
fprintf(fid,'%f,%f\n',time,data)
end
5 件のコメント
Tim Helps
2018 年 3 月 8 日
Hi Chriag,
Thanks for these solutions!
However, when I used them I noticed one problem with your tempData variable - since it's persistent, the data will be there still after the acquireData function is completed. If the acquireData function is run again, the previous data will still be there and tempData (and thus data) will continue to grow.
I've fixed this in my code by making it a global rather than persistent variable, and using: clearvars -global at the start of the acquireData function.
If you have a better solution please let me know!
Many thanks, Tim
Alexandre Piccini
2018 年 9 月 26 日
Hey,
I come from the future just to thank you for this. Just what I was looking for :)
Greetings
その他の回答 (4 件)
Nuno Martins
2011 年 4 月 27 日
2 件のコメント
avinash pabbireddy
2013 年 11 月 11 日
編集済み: Walter Roberson
2018 年 9 月 26 日
function nidaq9234
global data
s = daq.createSession('ni');
s.addAnalogInputChannel('cDAQ2Mod1',0,'voltage')
s.addAnalogInputChannel('cDAQ2Mod1', 1, 'Voltage');
s.Rate = 2000
s.DurationInSeconds = 20
lh = s.addlistener('DataAvailable',@plotData);
s.startBackground();
% Do something
while(~s.IsDone)
end
close(gcf);
plot(data); % plot global data
function plotData(src,event)
persistent tempData;
global data
if(isempty(tempData))
tempData = [];
end
plot(event.TimeStamps, event.Data)
tempData = [tempData;event.Data];
data = tempData;
end
i'm getting error while running the code
Error: File: nidaq9234.m Line: 26 Column: 1
The function "plotData" was closed
with an 'end', but at least one other function definition was not.
To avoid confusion when using nested functions,
it is illegal to use both conventions in the same file.
Walter Roberson
2018 年 9 月 26 日
You need to add one more 'end' to this, either right after the plot(data) or at the very end. For your code structure, right after the plot(data) would make the most sense.
jenni
2013 年 2 月 18 日
Is it possible to add a timer function to this code? I want a function to be executed once in every 2 sec?
0 件のコメント
Csaba
2011 年 7 月 18 日
Hi Chirag Gupta!
Do you know how to get the same result with a NI DAQ-6052E PCI acquisition card? I tried your code, but since I could not get any CompactDAQ device detected:
>> d=daq.getDevices
d =
No data acquisition devices available.
Click here for a list of known vendors.
Click here for troubleshooting tips.
??? Error: File: F20110718_AIAO_test_v15.m Line: 7 Column: 1
Function definitions are not permitted in this context.
the following line did not worked:
s.addAnalogInputChannel('cDAQ1Mod1',0,'voltage')
Maybe my device is not meant to work with sessions. It would be a shame, because the event detection would be very useful for me.
Indeed, my goal is to acquire 4 different signals on 4 different analog input through the NI BNC-2110 and the NI DAQ-6052E PCI card, plot them "live" as well as output "live" 3 different voltages (each would be a given function of the input voltages) on 3 different analog output.
Thank you very much! Kind regards,
Csaba
1 件のコメント
Chirag Gupta
2011 年 7 月 18 日
As of MATLAB R2011a, only Compact DAQ devices are supported with the Session based architecture described above. You can try the R2011b prerelease (download from the MathWorks website) that supports other NI boards with the new architecture.
Otherwise, you can look at this demo:
"Continuous Acquisition Using Analog Input"
Dagmar Fraser
2011 年 10 月 18 日
OOh top work - been looking for something like this for literal years!
I had to change
% Do something
while(~s.IsDone)
end
to
s.IsDone % will report 0
s.wait() % rather than while
s.IsDone % will report 1
1 件のコメント
Christos
2012 年 3 月 28 日
hi! i would like to ask if it is possible to use the following code:
s.addAnalogInputChannel('Dev1','ctr0','EdgeCount'), in order to get the Edge count value from NI card usb 6221?
Thanks for your help
参考
カテゴリ
Help Center および File Exchange で National Instruments Frame Grabbers についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!