Improper use of putdata

1 view (last 30 days)
Csaba
Csaba on 13 Jul 2011
Hello,
I try to control an industrial process through Matlab, NI DAQ-6052E and NI BNC-2110. That's why I need to output a continuous voltage, which is varying in time.
My problem is that using the putdata function, I get only a single value as output result (as you can see it bellow in the answer I get).
Any idea ?
Thank you in advance, Csaba
Here is the code :
%%Start NI DAQ-6052E
disp(' ');
disp('START');
disp(' ');
clear all;
close all;
%%Define ai
disp('Init ai: 100Hz, 10s');
ai = analoginput('nidaq', 'Dev1');
addchannel(ai, 0);
ai.SampleRate = 100;
t_in = 10;
ai.SamplesPerTrigger = t_in*ai.SampleRate;
ai.TriggerType = 'Immediate';
ai.Timeout = t_in+10 ;
%%Define AO
disp('Init AO: as ai');
AO = analogoutput('nidaq','Dev1');
addchannel(AO, 0);
set(AO,'SampleRate',ai.SampleRate);
set(AO,'TriggerType','Immediate');
t_out = t_in;
f_out = get(AO,'SampleRate');
n_out = f_out*t_out;
%%Reading input values
disp('Start acq');
start(ai);
wait(ai,t_in+1);
[value_in,t] = getdata(ai);
%%Generating output data
disp('Calc data out');
n_out = size(value_in,1);
data_out = zeros(n_out,1);
for i=1:1:(t_out*f_out)
t(i,1)=i/(f_out);
data_out(i,1) = 5-value_in(i,1);
end
*%%Outputting data
disp('Putdata');
putdata(AO,data_out);
s_out=get(AO,'SamplesOutput');
wait(AO,t_out+1);
start(AO);
s_out=get(AO,'SamplesOutput');
%%Verifying output
disp('# value read : '); disp(n_out);
disp('# data out : '); disp(s_out);
delta = n_out - s_out;
disp('DELTA : '); disp(delta);*
%%Display graph
disp('Graph');
fig = figure('Name', 'Test AI & AO on NI DAQ-6052E ND Matlab', 'NumberTitle','off');
subplot(2,1,1);
graph_in = plot(t,value_in);
set(graph_in,'Color','red','LineWidth',2)
title('Values read on ai')
axis([0 10 0 10])
set(gca,'XTick',0:1:10)
set(gca,'XTickLabel','0|1|2|3|4|5|6|7|8|9|10')
set(gca,'YTick',[0 1 2 3 4 5 6 7 8 9 10])
set(gca,'YTickLabel','0|1|2|3|4|5|6|7|8|9|10')
xlabel('Time [s]')
ylabel('Voltage [V]')
subplot(2,1,2);
graph_out = plot(t,data_out);
set(graph_out,'Color','black','LineWidth',2)
title('Data sent on AO')
axis([0 10 0 10])
set(gca,'XTick',0:1:10)
set(gca,'XTickLabel','0|1|2|3|4|5|6|7|8|9|10')
set(gca,'YTick',[0 1 2 3 4 5 6 7 8 9 10])
set(gca,'YTickLabel','0|1|2|3|4|5|6|7|8|9|10')
xlabel('Time [s]')
ylabel('Voltage [V]')
%%Stop NI DAQ-6052E
disp('Clear AO');
delete(AO);
clear AO;
disp('END.');
disp('*********');
disp(' ');
And here is the answer :
START
Init ai: 100Hz, 10s
Init AO: as ai
Start acq
Calc data out
Putdata
*# value read :
1000
# data out :
1*
DELTA :
999
Graph
Clear AO
END.
*********

Accepted Answer

Chirag Gupta
Chirag Gupta on 13 Jul 2011
Put the wait command after start(AO) in your code.
putdata queues the data in the engine and start(AO) would start the output.
putdata(....)
start(AO);
wait(AO,....)
...
  1 Comment
Chirag Gupta
Chirag Gupta on 13 Jul 2011
Also you should use SamplesAvailable property to check for the number of samples available for output.
So:
putdata(...)
samp_out_avail = get(AO,'SamplesAvailable');
start(AO);
while (AO.Running)
s_out = get(AO,'SamplesOutput')
pause(0.5)
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!