how to speed up the loop..PLEASE HELP ME!

2 views (last 30 days)
danhua yu
danhua yu on 22 Jul 2015
Edited: Stephen23 on 22 Jul 2015
function [FitVal]=Controller1test6(kx, kdx)
s = daq.createSession('ni');
addAnalogInputChannel(s,'cDAQ2Mod1', 0, 'Accelerometer');
s.Rate =2000;
s.DurationInSeconds = 0.001;
s.Channels(1).Sensitivity = 0.0976;
s2 = daq.createSession('ni');
addAnalogOutputChannel(s2,'Dev1',0,'Voltage');
s2.Rate =2000;
s2.IsContinuous=true;
global time;
global DoF;
global dt; dt = 0.001;
global AVCstates;
T=0.01;
n=round(T/dt);
if ne(size(AVCstates,2),3)
ddx=0; dx=0; x=0;
else
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);
outputSingleScan =1*fAVC';
s2.NotifyWhenScansQueuedBelow=1;
lh2=addlistener(s2,'DataRequired',...
@(src,event) src.queueOutputData(outputSingleScan));
queueOutputData(s2,outputSingleScan);
startBackground(s2);
A(:,1) = s.startForeground;
ddx=A(1,1);
dx=dxOld+ddx*dt ;
x=xOld+dx*dt ;
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
function FitVal=CalcFitness(Vect)
Vect=Vect';
NoPoints=size(Vect,1);
FitVal=max(Vect(round(NoPoints*0.5):NoPoints),[],1);
end
  1 Comment
danhua yu
danhua yu on 22 Jul 2015
My cDAQ is NI9171. I am trying to use inputSingleScan command to input data one by one,but it shows that 'Warning: A channel that does not support on-demand operations was added to the session. On-demand operations using inputSingleScan and outputSingleScan will be disabled. Only clocked operations using startForeground and startBackground can be done. '

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!