Page 182 ·
Robotics with the Boe-Bot
GOSUB Turn_Right
ELSEIF (IN7 = 0) THEN
HIGH 1
GOSUB Back_Up
GOSUB Turn_Left
ELSE
LOW 10
LOW 1
GOSUB Forward_Pulse
ENDIF
√
Modify the
IF…THEN
statement in RoamingWithWhiskers.bs2 to make the Boe-
Bot broadcast its maneuver using the LED indicators.
ACTIVITY #4: ARTIFICIAL INTELLIGENCE AND DECIDING WHEN
YOU’RE STUCK
You may have noticed that the Boe-Bot gets stuck in corners. As the Boe-Bot enters the
corner, its whisker touches the wall on the left, so it turns right. When the Boe-Bot
moves forward again, its right whisker bumps the wall on the right, so it turns left. Then
it turns and bumps the left wall again, and the right wall again, and so on, until somebody
rescues it from its predicament.
Programming to Escape Corners
RoamingWithWhiskers.bs2 can be modified to detect this problem and act upon it. The
trick is to count the number of times that alternate whiskers are contacted. One important
thing about this trick is that the program has to remember what state each whisker was in
during the previous contact. It has to compare that to the whisker states of the current
contact. If they are opposite, then add one to the counter. If the counter goes over a
threshold that you (the programmer) have determined, then, it’s time to do a U-turn and
reset that alternate whisker counter.
This next program also relies on the fact that you can “nest”
IF…THEN
statements. In
other words, the program checks for one condition, and if that condition is true, it checks
for another condition within the first condition. Here is a pseudo code example of how
it can be used.
IF condition1 THEN
Commands for condition1
IF condition2 THEN