MATLAB Answers

Sending Desired Signal to DAQ Device with Data Acquisition Toolbox

7 views (last 30 days)
Hakan Caldag
Hakan Caldag on 6 Mar 2018
Commented: Hakan Caldag on 10 Sep 2018
Hey everyone, I am working on controlling current sent to a pair of Helmholtz coils through Matlab with Data Acquisition Toolbox. Looking at related documentation, I was able to generate code that would queue desired signal to NI DAQ device and sustain it but what I want to do is the flexibility of changing the current I send to the device whenever desired. When the data queued with queueOutputData() is finished, the program seems to queue back whatever is in the listener. I actually want it to continue with whatever I queued until I change it and if I change it I want to continue with that. Do I have to update the listener and if so how can I do that? Also, I understand that when I queue a certain signal, I can't cancel it and replace with another signal before it's processed. Is there any way I can overcome it? Below I share the part of the code that generates desired signal, initiates a listener and queues some output data.
s=daq.createSession('ni');
out1=addAnalogOutputChannel(s,'Dev1',0,'Voltage');
s.Rate = 20000; %Device initialization
f=5;
w=2*pi*f;
out1_amp=0.8;
out2_amp=out1_amp;
out1_signal=out1_amp*sin(linspace(0,2*pi*f,s.Rate+1)');
out1_signal(end)=[]; %Signal generation
lh=addlistener(s,'DataRequired', @(src, event) src.queueOutputData([out1_signal]));
s.IsContinuous=true;
queueOutputData(s,repmat([out1_signal],5,1));
s.startBackground();

  6 Comments

Show 3 older comments
Dora Mahecic
Dora Mahecic on 7 Sep 2018
Hi, I am trying to do something similar. I managed to write a callback function which changes the data sent to the queue whenever the waveform properties are changed. However, it seems that the queue somehow remembers the previous data as well, since instead of switching from the first waveform to the second, then third (1 1 1 1 2 2 2 3 3 3 3 ), it starts iterating between the three (1 1 1 1 2 1 2 1 2 3 1 2 3 1 2 3 1 2 3). Why does the queue contain the old data too? Any suggestion on how to solve this would be appreciated, thanks
Hakan Caldag
Hakan Caldag on 10 Sep 2018
I was able to solve this problem somehow but I cannot guarantee that it works at all times and I am not sure which one of the things below made it work. And since I do not have access to the code right now, I may be missing a few things.
-Firstly, instead of using "addlistener" function, I use "listener" function to define my listener.
- I stop() and release() whenever I want to update my signal.
- After this, I remember redefining the listener based on the new signal but I am not sure whether I delete() the listener and then redefine with listener() function or overwrite it without deleting.
I hope these help.

Sign in to comment.

Answers (0)

Sign in to answer this question.