Page 178 · Robotics with the Boe-Bot
Programming the Boe-Bot to Navigate Based on Whisker Inputs
This next program makes the Boe-Bot go forward until it encounters an obstacle. In this
case, the Boe-Bot knows when it encounters an obstacle by bumping into it with one or
both of its whiskers. As soon as the obstacle is detected by the whiskers, the navigation
routines and subroutines developed in Chapter 4 will make the Boe-Bot back up and turn.
Then, the Boe-Bot resumes forward motion until it bumps into another obstacle.
In order to do that, the Boe-Bot needs to be programmed to make decisions. PBASIC has
a command called an
IF…THEN statement that makes decisions. The syntax for IF…THEN
statements is:
IF (condition) THEN…{ELSEIF (condition)}…{ELSE}…ENDIF
The “…” means you can place a code block (one or more commands) between the
keywords. The next example program makes decisions based on the whisker inputs, and
then calls subroutines to make the Boe-Bot take action. The subroutines are similar to the
ones you developed in Chapter 4. Here is how
IF…THEN is used.
IF (IN5 = 0) AND (IN7 = 0) THEN
GOSUB Back_Up ' Both whiskers detect obstacle,
GOSUB Turn_Left ' back up & U-turn (left twice)
GOSUB Turn_Left
ELSEIF (IN5 = 0) THEN ' Left whisker contacts
GOSUB Back_Up ' Back up & turn right
GOSUB Turn_Right
ELSEIF (IN7 = 0) THEN ' Right whisker contacts
GOSUB Back_Up ' Back up & turn left
GOSUB Turn_Left
ELSE ' Both whiskers 1, no contacts
GOSUB Forward_Pulse ' Apply a forward pulse &
ENDIF ' check again
Example Program: RoamingWithWhiskers.bs2
This program demonstrates one way of evaluating the whisker inputs and deciding which
navigation subroutine to call using
IF…THEN.
√ Reconnect power to your board and servos.
√ Enter, save, and run RoamingWithWhiskers.bs2.