The problem is that on this platform,notifications more frequent than 20 times prt second may not be achievable.So please tell me how to modify code. Actually what I want to do is to input and output data one by one in real-time control.THANKS

4 views (last 30 days)
if true
% code
global time;
global DoF;
global dt; dt = 0.001;
global AVCstates;
function [FitVal]=Controller1test3(kx, kdx)
d = daq.getDevices;
d(1)
s = daq.createSession('ni');
addAnalogInputChannel(s,'Dev1', 13, 'Voltage');
s.Rate =8000;
s.DurationInSeconds = 0.001;
s;
devices = daq.getDevices;
devices(1)
s2 = daq.createSession('ni');
addAnalogOutputChannel(s2,'Dev1',0,'Voltage');
s2.IsContinuous=true;
s2.Rate =20000;
outputSingleValue = 1;
outputSingleScan(s2,outputSingleValue);
T=0.01;
n=round(T/dt);
if ne(size(AVCstates,2),3)
ddx=0; dx=0; x=0;
ddx=AVCstates(3);
dx=AVCstates(2);
x=AVCstates(1);
end
Accn=zeros(1,n); Vel=zeros(1,n); Disp=zeros(1,n);
time=0;
for i=1:n
time=time+dt;
cputime
xOld=x;
dxOld=dx;
fAVC=-(kx*x+kdx*dx);
outputSignal =fAVC;
s2.NotifyWhenScansQueuedBelow=1;
lh2=addlistener(s2,'DataRequired',...
@(src,event) src.queueOutputData(outputSignal'));
queueOutputData(s2,outputSignal');
startBackground(s2);
data = inputSingleScan(s);
ddx=data;
dx=dxOld+ddx*dt ;%velocity v=v'+at
x=xOld+dx*dt ;% displacement s=s'+vt
Accn(i)=ddx;
Vel(i)=dx;
Disp(i)=x;
end
figure(2)
plot(Accn);
xlabel('Time(Secs)');
ylabel('Voltage');
displayEndOfDemoMessage(mfilename)
figure(3)
plot(Vel);
xlabel('Time(Secs)');
ylabel('Velocity');
figure(4)
plot(Disp);
xlabel('Time(Secs)');
ylabel('Displacement');
figure(5); cla;
hL=plot(Disp); axis([0 n -0.002 0.002]);
set(gcf);
grid 'on';
FitVal=CalcFitness(Disp);
AVCstates(3)=ddx;
AVCstates(2)=dx;
AVCstates(1)=x;
end
end
if true
% code
function FitVal=CalcFitness(Vect)
Vect=Vect';
NoPoints=size(Vect,1);
FitVal=max(Vect(round(NoPoints*0.5):NoPoints),[],1);
end
end
if true
% code
displacement=1;
velocity=2;
fitness=Controller1test3(displacement,velocity)
end

Answers (0)

Categories

Find more on Data Acquisition Toolbox Supported Hardware 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!