Last class was a joy able class. We have fixed our whiskers onto our Boe-Bots and we even checked if they work by making them perform a series of tests. Today we will field test the whiskers. Since we are going to test them on the ground, we wouldn’t be able to tell if it works because we wouldn’t have the DEBUG Terminal. So, this can be done with a pair of LED circuits and a program that turns the LEDs on and off based on the whisker inputs.
So we will use the same program we have used before with tiny changes in it. We will insert these two IF…THEN statements between the PAUSE 50 and LOOP
commands.
IF (IN7 = 0) THEN
HIGH 1
ELSE
LOW 1
ENDIF
IF (IN5 = 0) THEN
HIGH 10
ELSE
LOW 10
ENDIF
These are called IF…THEN statements. These statements are used to make decisions in PBASIC. The first of the two IF…THEN statements sets P1 high, which turns the LED on when the whisker connected to
P7 is pressed (IN7 = 0). The ELSE portion of the statement makes P1 go low, which turns the LED off when the whisker is not pressed. The second IF…THEN statement does the same thing for the whisker connected to P5 and the LED connected to P10.
Now that we know how this program works we just have to test it and see. I ran the program and I pressed the whiskers gently onto the 3-pin headers and they worked, both of the red LEDs had light up. Next we will type down a program that we will make our Boe-Bot navigate with the help of the whiskers. Whenever the LEDs light up that means the Boe-Bot has bumped into an object. 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. BASIC has a command called an IF…..THEN statement that makes decisions.
IF (condition) THEN…{ELSEIF (condition)}…{ELSE}…ENDIF
You can place a code block of one or more commands between the keywords. This means if you want it to roll forward, just write down the command between the keywords and when you run the program it would perform the specified maneuver. This program makes decision based on the whiskers input.
These are
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
After that just run the program and test it. I kept obstacles on the ground with books as walls. I pressed the reset button and it began rolling forward, then as soon as the tip of the whisker touched the book it went on to perform the next maneuver in the program. It was so cool to watch it bump into every obstacle that comes ahead. It looked like a blind driver was driving the Boe-Bot.