Menu

(Solved) : Please Explain Code Line Line D Daqgetdevices Finds Daq S Daqcreatesession Ni Makes Sessio Q43938468 . . .

Can you please explain the code line by line

d =daq.getDevices; %finds the daq
s =daq.createSession (‘ni’);%makes a session
s.Rate=100
addDigitalChannel(s,’myDAQ1′,’Port0/Line0′,’InputOnly’); %left bumpswitch
addDigitalChannel(s,’myDAQ1′,’Port0/Line1′,’InputOnly’); %rightbump switch
addDigitalChannel(s,’myDAQ1′,’Port0/Line2′,’InputOnly’); %back bumpswitch

addAnalogOutputChannel(s,’myDAQ1′,’ao0′,’Voltage’); %servo
addDigitalChannel(s,’myDAQ1′,’Port0/Line4′,’OutputOnly’); %leftmotor
addDigitalChannel(s,’myDAQ1′,’Port0/Line5′,’OutputOnly’); %leftmotor
addDigitalChannel(s,’myDAQ1′,’Port0/Line7′,’OutputOnly’); %rightmotor
addDigitalChannel(s,’myDAQ1′,’Port0/Line6′,’OutputOnly’); %rightmotor
addDigitalChannel(s,’myDAQ1′,’Port0/Line3′,’OutputOnly’);%light
0; % by default, 1000 scans persecond

addAnalogInputChannel(s,’myDAQ1′,’ai0′,’Voltage’); %linetransistor
addAnalogInputChannel(s,’myDAQ1′,’ai1′,’Voltage’); %pucktransistor

%shortcuts meaning i can reference ‘fwd’ instead of typing ‘[10]’ etc.
fwd = [0 1];
bck = [1 0];
stop = [0 0];
open = 3;
closed = 0;
light = 0;
claw = open; %set the claw variable to open
outputSingleScan(s, [claw stop stop light]); %move the claw to openposition

%making a counter for each switch
CounterRight=1;
CounterLeft=1;
CounterBoth=1;

while (1)%creates an infinite loop
Inputs= inputSingleScan(s);%read daq inputs
%making more variables for ease of use
LS=Inputs(1,1);
RS=Inputs(1,2);
BS=Inputs(1,3);
LineT=Inputs(1,4);
PuckT=Inputs(1,5);
  
%switches
if RS==1 %if right switch is bumped
CounterRight=CounterRight+1; %add one to the counter
outputSingleScan(s, [claw bck bck light]); %drive backwards
pause(0.5); %let it drive backwards for half a second
if rem(CounterRight,3)==0 %read counter value to determine whichdirection to turn around it to prevent an infinite loop
outputSingleScan(s, [claw bck fwd light]); %turn clockwise
else
outputSingleScan(s, [claw fwd bck light]); %turnanticlockwise
end
pause(0.6); %let it turn for a bit
  
elseif LS==1 %if light switch is bumped
CounterLeft=CounterLeft+1;%add one to the counter
outputSingleScan(s, [claw bck bck light]);%drive backwards
pause(0.5);%let it drive backwards for half a second
if rem(CounterLeft,3)==0 %read value
outputSingleScan(s, [claw bck fwd light]);%turn clockwise
else
outputSingleScan(s, [claw fwd bck light]);%turn anticlockwise
end
pause(0.4);%let it turn for a bit
  
elseif LS==1 && RS==1 %if both switches are bumped
CounterBoth=CounterBoth+1;
outputSingleScan(s, [claw bck bck light]);
pause(1);
if rem(CounterBoth,3)==0
outputSingleScan(s, [claw bck fwd light]);
else
outputSingleScan(s, [claw fwd bck light]);
end
pause(0.7);
  
else LS==0 && RS==0; %if no switches are activated
outputSingleScan(s, [claw fwd fwd light]); %drive forawrd
end
  
%transistors
%   
if PuckT> 2.3 && PuckT<3.5 %if the value of thetransister is between 2.3 and 3.5 then
claw=closed; %set claw variable to closed
light=1
outputSingleScan(s, [claw fwd fwd light]);%close the claw
  
elseif PuckT>0.5&&PuckT<1 %if detect blue puck
outputSingleScan(s, [claw bck bck light]); %drive backwards
pause(0.4);
outputSingleScan(s, [claw bck fwd light]);%turn around
pause(0.7);

end
  
if LineT<=0.4 && claw == closed %if detect black lineand have a puck
pause(0.5) %keeps driving a bit
outputSingleScan(s, [open bck bck light]);%reverse and openclaw
pause(0.5)
claw = open;%set variable to open
light=0
outputSingleScan(s, [claw fwd bck light]); %turn 180
pause(1)
  
elseif LineT<=0.4 && claw == open %if detect black lineand dont have a puck
outputSingleScan(s, [claw fwd bck light]); %turn 90
pause(1)
end
end

Expert Answer


Answer to Can you please explain the code line by line d =daq.getDevices; %finds the daq s =daq.createSession (‘ni’);%makes a sess…

OR