Filter löschen
Filter löschen

How can I control the repeat analog output on MATLAB?

3 Ansichten (letzte 30 Tage)
horizon
horizon am 4 Mai 2019
Kommentiert: horizon am 6 Mai 2019
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

Antworten (1)

Walter Roberson
Walter Roberson am 6 Mai 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.
  1 Kommentar
horizon
horizon am 6 Mai 2019
Thank you for your answer.
I didn't understand the way to use animatedline() and how much efficient it is.
I've changed the plotData() like below.
function plotData(src, event)
t1 = event.TimeStamps(:,1);
s1 = event.Data(:,1);
s2 = event.Data(:,2);
ax1 = subplot(2,1,1);
L1 = animatedline(ax1);
ylim([-5.0 5.0]);
title(ax1, 's_1')
xlabel(ax1, 'Time (s)')
ax2 = subplot(2,1,2);
L2 = animatedline(ax2);
ylim([-5.0 5.0]);
title(ax2, 's_2')
xlabel(ax2, 'Time (s)')
end
I don't need to switch to a new figure or new line after 5 seconds. Is there any example or
MATLAB Document to check the current time and repeat the submission using callback?
I haven't understood callback on MATLAB yet.

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by