How to change analog output according to acquired analog input data from a NI DAQ

3 visualizaciones (últimos 30 días)
Hello everyone, i'm a freshman of matlab. I just wrote a Script to accquire data from my DAQ device and to generate analog signal by the DAQ device. Here is my Script.
>>>>>>>>>>>>>>>>>>>>>>>>>>>> MY SCRIPT<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
clear all;
dq = daq("ni");
dq.Rate = 1000;
addinput(dq, "SimDev1", "ai0", "Voltage");
addoutput(dq, "Dev1", "ao0", "Voltage");
i=1;
while(1)
data(i,1) = read(dq, "OutputFormat", "Matrix");
if i >= 5000
return
end
output (data(i) >=2.0) = 5;
output (data(i) < 2.0) = -5;
%if data(i) >= 2.0
% output = linspace(1,1,1000)';
%else
% output = linspace(5,5,1000)';
%end
subplot(2,1,1)
plot(data);
ylabel("Voltage (V)");
title("Acquired Data");
xlim([0 5000]);
%ylim([-10 10])
subplot(2,1,2)
%newcolors = [0.8500 0.3250 0.0980];
%colororder(newcolors)
plot(i, output,'Marker','.','MarkerEdgeColor','r' );
%hold on
ylabel("Voltage (V)")
title("Output Data");
xlim([0 5000]);
ylim([-10 10]);
pause(0);
i=i+1;
end
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Finish<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Actually I want it to plot acquired data in Real Time and it seems work out fine.
Then I want it to generate analog signal according to data acquired.
For exp : When input > 2V , output = 5V. When input < 2V ,output = -5V. Sth like this.
But to make the output happen, i have to use "readwrite". Here is my trouble.
It seems that readwrite can only put at the behind of "output". But if there's no "read", Data wont be read and " output (data(i) >=2.0) = 5; " won't work.
I tried to use "read" then use "write" also failed.
Anybody can help me? Appreciate a lot.
  2 comentarios
Nubia Mendes
Nubia Mendes el 15 de Mzo. de 2022
Hey @Li Zhe did you end up finding a solution? I'm having the same issue.
Li Zhe
Li Zhe el 16 de Mzo. de 2022
Editada: Li Zhe el 16 de Mzo. de 2022
@Nubia Mendes Hi Mr.Mendes,
I find out that windows doesn’t support real time control with the script in the software. If you want to write or plot something according to required data.
There’s one way that you have to use a loop program by using while or for.
But you can only specify how many times you want it to loop for you, in other way, how many data you want, not a specific time ( 10seconds, 2 hours etc.)
If you want to do in a real time process, then you have to find a real time controller or a customizable scope. Some controller supports programs written by simulink or lab view. My situation is using Speedgoat and S-Box2. Maybe there’s more affordable controllers that you can find.
I hope this could help you.

Iniciar sesión para comentar.

Respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by