Robotics with the Boe-Bot page 246
I believe the logic is incorrect on page 246 (Robotics with the Boe-Bot). The last ELSE is never executed. Shouldn’t the code be something more like this?
DO ' Main Routine.
FREQOUT 8, 1, 38500 ' Check IR detectors.
irDetectLeft = IN9
FREQOUT 2, 1, 38500
irDetectRight = IN0
' Decide navigation.
IF (irDetectLeft = 0) AND (irDetectRight = 0) THEN
pulseCount = 1 ' Both detected,
pulseLeft = 850 ' one pulse forward.
pulseRight = 650
ELSEIF (irDetectLeft = 1) AND (irDetectRight = 1) THEN ' Neither detected,
pulseCount = 15 ' back up and try again.
pulseLeft = 650
pulseRight = 850
ELSEIF (irDetectRight = 1) THEN ' Right not detected,
pulseCount = 10 ' 10 pulses left.
pulseLeft = 650
pulseRight = 650
ELSE ' Left not detected,
pulseCount = 10 ' 10 pulses right.
pulseLeft = 850
pulseRight = 850
ENDIF
FOR loopCount = 1 TO pulseCount ' Send pulseCount pulses
PULSOUT 13,pulseLeft
PULSOUT 12,pulseRight
PAUSE 20
NEXT
LOOP
Original code attached.
DO ' Main Routine.
FREQOUT 8, 1, 38500 ' Check IR detectors.
irDetectLeft = IN9
FREQOUT 2, 1, 38500
irDetectRight = IN0
' Decide navigation.
IF (irDetectLeft = 0) AND (irDetectRight = 0) THEN
pulseCount = 1 ' Both detected,
pulseLeft = 850 ' one pulse forward.
pulseRight = 650
ELSEIF (irDetectLeft = 1) AND (irDetectRight = 1) THEN ' Neither detected,
pulseCount = 15 ' back up and try again.
pulseLeft = 650
pulseRight = 850
ELSEIF (irDetectRight = 1) THEN ' Right not detected,
pulseCount = 10 ' 10 pulses left.
pulseLeft = 650
pulseRight = 650
ELSE ' Left not detected,
pulseCount = 10 ' 10 pulses right.
pulseLeft = 850
pulseRight = 850
ENDIF
FOR loopCount = 1 TO pulseCount ' Send pulseCount pulses
PULSOUT 13,pulseLeft
PULSOUT 12,pulseRight
PAUSE 20
NEXT
LOOP
Original code attached.
Comments
' Decide navigation.
IF (irDetectLeft = 0) AND (irDetectRight = 0) THEN
pulseCount = 1 ' Both detected,
pulseLeft = 850 ' one pulse forward.
pulseRight = 650
DEBUG HOME, "Both detected."
HIGH 10 'Turn left LED on
HIGH 1 'Turn right LED on
ELSEIF (irDetectRight = 1) THEN ' Right not detected,
pulseCount = 10 ' 10 pulses left.
pulseLeft = 650
pulseRight = 650
DEBUG HOME, "Right not detected."
HIGH 10 'Turn left LED on
LOW 1 'Turn right LED off
ELSEIF (irDetectLeft = 1) THEN ' Left not detected,
pulseCount = 10 ' 10 pulses right.
pulseLeft = 850
pulseRight = 850
DEBUG HOME, "Left not detected."
HIGH 1 'Turn right LED on
LOW 10 'Turn left LED off
ELSE ' Neither detected,
pulseCount = 15 ' back up and try again.
pulseLeft = 650
pulseRight = 850
DEBUG HOME, "Neither detected."
LOW 10 'Turn left LED off
LOW 1 'Turn right LED off
ENDIF
I would be inclined to say that your modified program is correct. However, as Andy Lindsay wrote the program, he may have some insight that I do not. He's out of the office this week, but I'll be sure to get you an answer when he returns!
Thanks for hanging in there!
Cheers,
Jessica
It helps if you put your code between code blocks.
Use
[ CODE] your code here [ /CODE]
But without the spaces inside the square brackets.
Just so others can see your code easier, I've wrapped it in code tags.
I think the original code is below.
I didn't get the comments spaced correctly. Oh well.
Yes, you are correct, the last ELSE would never be executed in the original code. Good catch.
Duane
You are correct. The AvoidTableEdge.bs2 program in the book cannot possibly make it to the ELSE block because the (irDetectRight = 1) condition will be true for two cases: (1) Right not detected and (2) Neither detected. Since IF...ELSEIF...ELSE blocks execute code for the first true condition and then jump to ENDIF, the "Neither detected" condition gets handled by the code for "Right not detected". Your adjusted AvoidTableEdgeCorrected.bs2 code also looks good. We'll use it in the errata and future revisions if that's okay with you.
I heartily second Duane's "Good catch" comment. The error has been there since the 2.0 revision in 2004. It probably went unreported for so long because the Boe-Bot still successfully avoids the table edge, and the fact that it never seemed to back up and try again is easily (though incorrectly) chalked up to the fact that it's sampling at 40 to 50 times per second. At that rate, detecting the drop-off with both sensors during the same sample is kind of a rare occurrence. It'll typically be one or the other.
Although it might take multiple tries to get both detectors to see the drop-off during the same sample by sending the Boe-Bot straight at the table edge, you can verify that the ELSE condition gets executed by just lifting the Boe-Bot up well above the table. With your updated code, that'll make both detectors see the drop-off and the Boe-Bot will try to back up repeatedly. With the code in the book, it'll just keep turning left.
Thanks for posting this bug report and updated code. Nice work.
Regards, Andy
I'm glad I could help. I basically just rearranged the lines of code. Of course you can use it.
The Boe-Bot and your book are awesome.
Thanks,
Mark