How to continuously Generate a signal on the DAQ

조회 수: 5 (최근 30일)
Hadi Hajieghrary
Hadi Hajieghrary 2016년 12월 20일
답변: Hadi Hajieghrary 2016년 12월 20일
I am writing a software to communicate with NI DAQ using the session-based Structure. I have a NI DAQ from which I am getting input data successfully (using background operation). Now, I want to generate a signal using the same DAQ. Again, I am Queuing data to the output and running it in the background. it works up until I call another function. I believe running in background means that I SHOULD be able to run another function without interrupting the operation of the DAQ card!
More, I am doing all of these in a GUI, then basically it is essential to use callback functions. But it seems like as soon as the control of Matlab enters to another functions variable environment it forgets the DAQ session running in the background! Even defining that session and even the DAQ device object as global variables does not solve the problem. HOW CAN I SOLVE THIS!!!???
Here is the code (if you take the keyword "function" out of the first function (define it as a routine) and the code works. But as soon as you put back the keyword, or in other word define the first module as function the code stop working, without any error or exception or anything in that matter. It seems as I said when the control of matlab goes out of the function it forgets the DAQ too!):
%%
%%
function TestValveControl()
global DAQdevice
global DAQOutputSession
DAQdevice = daq.getDevices;
DAQOutputSession=daq.createSession('ni');
DAQOutputSession.stop()
DAQOutputSession.Rate=1000;
DAQOutputSession.NotifyWhenDataAvailableExceeds=100;
DAQOutputSession.IsContinuous=true;
addAnalogOutputChannel(DAQOutputSession,DAQdevice.ID, 'ao1', 'Voltage');
hlistener2=addlistener(DAQOutputSession, 'DataRequired', @(srv,event)RefreshOutputData(srv,event));
InhaleTime=1;
ExhaleTime=2;
inValveLevel=2;
exValveLevel=10;
OutputSignal=[inValveLevel*ones(InhaleTime*round(DAQOutputSession.Rate),1); exValveLevel*ones(ExhaleTime*round(DAQOutputSession.Rate),1)];
queueOutputData(DAQOutputSession,OutputSignal)
startBackground(DAQOutputSession)
%%
%%
function RefreshOutputData(srv,event)
InhaleTime=1;
ExhaleTime=2;
inValveLevel=2;
exValveLevel=10;
OutputSignal=[inValveLevel*ones(InhaleTime*round(srv.Rate),1);
exValveLevel*ones(ExhaleTime*round(srv.Rate),1)];
queueOutputData(srv,OutputSignal)

답변 (1개)

Hadi Hajieghrary
Hadi Hajieghrary 2016년 12월 20일
I found one not-quite-elegant solution. If you first define the DAQ device and DAQ session globally in the BASE WORKSPACE you can access it inside the function globally too. Then you just need to add this at the first of the function "evalin('base','global DAQdevice')" and "evalin('base','global DAQOutputSession')".

카테고리

Help CenterFile Exchange에서 Simultaneous and Synchronized Operations에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by