Question
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 bump switch addDigitalChannel(s,'myDAQ1','Port0/Line1','InputOnly');
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 bump switch addDigitalChannel(s,'myDAQ1','Port0/Line1','InputOnly'); %right bump switch addDigitalChannel(s,'myDAQ1','Port0/Line2','InputOnly'); %back bump switch
addAnalogOutputChannel(s,'myDAQ1','ao0','Voltage'); %servo addDigitalChannel(s,'myDAQ1','Port0/Line4','OutputOnly'); %left motor addDigitalChannel(s,'myDAQ1','Port0/Line5','OutputOnly'); %left motor addDigitalChannel(s,'myDAQ1','Port0/Line7','OutputOnly'); %right motor addDigitalChannel(s,'myDAQ1','Port0/Line6','OutputOnly'); %right motor addDigitalChannel(s,'myDAQ1','Port0/Line3','OutputOnly'); %light 0; % by default, 1000 scans persecond
addAnalogInputChannel(s,'myDAQ1','ai0','Voltage'); %line transistor addAnalogInputChannel(s,'myDAQ1','ai1','Voltage'); %puck transistor
%shortcuts meaning i can reference 'fwd' instead of typing '[1 0]' 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 open position
%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 which direction to turn around it to prevent an infinite loop outputSingleScan(s, [claw bck fwd light]); %turn clockwise else outputSingleScan(s, [claw fwd bck light]); %turn anticlockwise 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 the transister 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 line and have a puck pause(0.5) %keeps driving a bit outputSingleScan(s, [open bck bck light]);%reverse and open claw 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 line and dont have a puck outputSingleScan(s, [claw fwd bck light]); %turn 90 pause(1) end end
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started