How can I control the repeat analog output on MATLAB?
1 view (last 30 days)
Show older comments
What I would like to do is to repeat analog output, submit ultrasonic through a sensor par 5 seconds and it continues runs for 60 seconds.
However, these error messages have appeared and I have no idea to fix them.
Error Message
error: daq.Session/releaseInternal(line 4162)
This command can not be used while the Session is running.
error: daq.Session/startBackground(line 1044)
obj.releaseInternal();
error: repeatAnalogoutput(line 45)
startBackground(s);
Code(repeat_analogoutput.m)
tx = daq.createSession('ni');
s = daq.createSession('ni');
s.Rate = 400000;
ultraFreq = 40000;
numCycle =8
addAnalogOutputChannel(tx, 'Dev1', 'ao0', 'Voltage');
th=addlistener(tx, 'DataRequired', @queueMoreData);
addAnalogInputChannel(s,'Dev1', 'ai0', 'Voltage');
ch = addAnalogInputChannel(s, 'Dev1', 'ai1', 'Voltage');
timeToRepeat = 60
for n = 1 : timeToRepeat
h = addlistener(s, 'DataAvailable', @plotData);
s.DurationInSeconds(1);
queueOutputData(tx, y');
startBackground(s);
tx.startForeground();
pause(5)
n = n + 1;
end
function plotData(src, event)
t1 = event.TimeStamps(:,1);
s1 = event.Data(:,1);
s2 = event.Data(:,2);
subplot(2,1,1)
plot(t1,s1)
ylim([-10.0 10.0]);
title('s_1')
subplot(2,1,2)
plot(t1,s2)
ylim([-10.0 10.0]);
title('s_2')
xlabel('Time (s)')
end
function queueMoreData(src, event)
queueOutputData(tx, y');
end
0 Comments
Answers (1)
Walter Roberson
on 6 May 2019
Do not add the listener or set the duration or start background or startForeground in the loop. The background and foreground will keep running until the listeners are deleted. When data is ready, plotData will be called. When data is needed for output, queueMoreData will be called.
You are not doing anything with the input except plotting it. Do you have a need to switch to a new figure or new line after 5 seconds? If you just want to reset the plot after 5 seconds, then have your callback check the current time and determine if is more than 5 seconds since you last plotted and make the appropriate change if so.
I do not understand why you are not using the animatedline based code that I posted for you before. Continually calling subplot and plot is comparatively expensive to updating the plot, so the version I posted is better able to keep up with higher speed plotting.
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!