Chapter 7: Navigating with Infrared Headlights

Chapter 7: Navigating with Infrared Headlights · Page 235
Chapter 7: Navigating with Infrared Headlights
Today's hottest products seem to have one thing in common: wireless communication.
Personal organizers beam data into desktop computers, and wireless remotes let us
channel surf. Many remote controls and PDA’s use signals in the infrared frequency
range to communicate, below the visible light spectrum. With a few inexpensive and
widely available parts, the BASIC Stamp can also receive and transmit infrared light
signals.
USING INFRARED HEADLIGHTS TO SEE THE ROAD
Detecting objects without whiskers doesn’t require anything as sophisticated as machine
vision. Some robots use RADAR or SONAR (sometimes called SODAR when used in
air instead of water). An even simpler system is to use infrared light to illuminate the
robot’s path and determine when the light reflects off an object. Thanks to the
proliferation of infrared (IR) remote controls, IR illuminators and detectors are readily
available and inexpensive.
Infrared: Infra means below, so Infra-red is light (or electromagnetic radiation) that has
lower frequency, or longer wavelength than red light. Table 7-1 shows the wavelengths for
common colors along with the infrared spectrum. Our IR LED and detector work at 980 nm
(nanometers) which is considered near infrared. Night-vision goggles and IR temperature
sensing use far infrared wavelengths of 2000-10,000 nm, depending on the application.
Table 7-1 shows the wavelengths for common colors along with the infrared spectrum.
Table 7-1: Colors and Approximate Wavelengths
Color
Wavelength
Color
Violet
400
Red
Blue
470
Near infrared
Green
565
Infrared
Yellow
590
Far infrared
Orange
630
Wavelength
780
800-1000
1000-2000
2000-10,000
Page 236 · Robotics with the Boe-Bot
Infrared Headlights
The infrared object detection system we’ll build on the Boe-Bot is like a car’s headlights
in several respects. When the light from a car’s headlights reflects off obstacles, your
eyes detect the obstacles and your brain processes them and makes your body guide the
car accordingly. The Boe-Bot uses infrared LEDs for headlights as shown in Figure 7-1.
They emit infrared, and in some cases, the infrared reflects off objects and bounces back
in the direction of the Boe-Bot. The eyes of the Boe-Bot are the infrared detectors. The
infrared detectors send signals indicating whether or not they detect infrared reflected off
an object. The brain of the Boe-Bot, the BASIC Stamp, makes decisions and operates the
servo motors based on this sensor input.
Figure 7-1
Object Detection
with IR Headlights
The IR detectors have built-in optical filters that allow very little light except the 980 nm
infrared that we want to detect with its internal photodiode sensor. The infrared detector
also has an electronic filter that only allows signals around 38.5 kHz to pass through. In
other words, the detector is only looking for infrared that’s flashing on and off 38,500
times per second. This prevents IR interference from common sources such as sunlight
and indoor lighting. Sunlight is DC interference (0 Hz), and indoor lighting tends to flash
on and off at either 100 or 120 Hz, depending on the main power source in the region.
Since 120 Hz is outside the electronic filter’s 38.5 kHz band pass frequency, it is
completely ignored by the IR detectors.
Chapter 7: Navigating with Infrared Headlights · Page 237
Some fluorescent lights do generate signals that can be detected by the IR detectors.
These lights can cause problems for your Boe-Bot’s infrared headlights. One of the things
you will do in this chapter is develop an infrared interference “sniffer” that you can use to test
the fluorescent lights near your Boe-Bot courses.
ACTIVITY #1: BUILDING AND TESTING THE IR PAIRS
In this activity, you will build and test the infrared transmitter/detector pairs.
Parts List:
(2)
(2)
(2)
(2)
Infrared detectors
IR LEDs (clear case)
IR LED shield assemblies
Resistors - 220 Ω
(red-red-brown)
(2) Resistors – 1 kΩ
(brown-black-red)
1
2
3
+
Figure 7-2
New Parts
Used in this
Chapter
1
2
3
Longer lead
IR detector
(top)
+
-
IR LED
(middle)
Flattened
edge
IR LED
shield
assembly
(bottom)
Building the IR Headlights
√
√
√
Insert the infrared LED into the shield assembly as shown in Figure 7-3.
Make sure the LED snaps into the larger part of the housing.
Snap the smaller part of the housing over the LED case and onto the larger part.
IR LED will snap in.
+
-
Figure 7-3
Snapping the IR
LED into the
Shield Assembly
Page 238 · Robotics with the Boe-Bot
One IR pair (IR LED and detector) is mounted on each corner of the breadboard. Figure
7-4 shows the IR headlights circuit as a schematic and Figure 7-5 shows the circuit as a
wiring diagram.
√
√
Disconnect power from your board and servos.
Build the circuit shown by the schematic in Figure 7-4, using the wiring diagram
for your board in Figure 7-5 as a reference for parts placement.
Vdd
P2
1 kΩ
IR
LED
P9
220 Ω
Vss
Vss
Vdd
P8
Figure 7-4
Left and Right IR
Pairs
1 kΩ
IR
LED
P0
220 Ω
Vss
Vss
Left IR Pair
Right IR Pair
Watch your IR LED anodes and cathodes!
Remember that the anode lead is the longer lead on an IR LED by convention, but that you
need to check the LED’s plastic case to make sure. The cathode lead is the one near the
flat spot on the case. In Figure 7-5, the anode lead of each IR LED connects to a 1 kΩ
resistor. The cathode lead plugs into the same breadboard row as an IR detector’s center
pin, and that row connects to Vss with a jumper wire.
Chapter 7: Navigating with Infrared Headlights · Page 239
To Servos
To Servos
15 14 Vdd 13 12
X4
Vdd
X5
Vin
Vdd
Vss
anode
leads
+
Board of Education
Rev C
© 2000-2003
Vin
Vss
X3
X3
P15
P14
P13
P12
P11
P10
P9
P8
P7
P6
P5
P4
P3
P2
P1
P0
X2
Figure 7-5
Wiring
Diagrams for
Infrared
Emitter and
Receiver
Circuits
(916) 624-8333
Rev B
www.parallax.com
www.stampsinclass.com
Red
Black
P15
P14
P13
P12
P11
P10
P9
P8
P7
P6
P5
P4
P3
P2
P1
P0
X2
anode
leads
+
Board of
Education
(left) and
HomeWork
Board (right).
HomeWork Board
Testing the IR Pairs Using the FREQOUT Trick
The FREQOUT command was designed mainly to synthesize audio tones. The actual range
of the FREQOUT command is 1 to 32768 Hz. One interesting phenomenon of digitally
synthesized tones is that they contain signals called harmonics. A harmonic is a higher
frequency tone that’s mixed in with the tone you want to hear. These tones are outside
human abilities to detect sound, which tend to range from 20 Hz to 20 kHz. The
harmonics generated by the FREQOUT command start at 32769 Hz and go upward. You
can directly control these harmonics using Freq1 arguments above 32768. In this
activity, you will use the command FREQOUT 8, 1, 38500 to send a 38.5 kHz harmonic
that lasts 1 ms to P8. The infrared LED circuit connected to P8 will broadcast this
harmonic. If the infrared light is reflected back to the Boe-Bot by an object in its path,
the infrared detector will send the BASIC Stamp a signal to let it know that the reflected
infrared light was detected.
Page 240 · Robotics with the Boe-Bot
FREQOUT Command - Fundamentals and Harmonics
The fundamental frequency is the value of the Freq1 argument when it’s at or below
32768. For example, when you use the command FREQOUT 4, 2000, 3000, the
fundamental frequency is 3000 Hz. That's the intended sound, but there is also a harmonic
sound that accompanies it. This harmonic is a much higher frequency that the human ear
can detect, in the neighborhood of 62.5 kHz. Here's how to calculate the harmonic
frequency given the fundamental and visa versa.
Whenever you use the FREQOUT command to send a tone in this range, it contains that
hidden (harmonic) tone as well. The equation for the harmonic is:
harmonic frequency = 65536 – Freq1,
Freq1 <= 32678
Whenever you use the FREQOUT command with a Freq1 argument above 32768 to send
a harmonic, it contains a fundamental tone. The equation for the fundamental is:
fundamental frequency = 65536 – Freq1,
Freq1 > 32768
The key to making each IR LED/detector pair work is to send 1 ms of 38.5 kHz FREQOUT
harmonic, and then, immediately store the IR detector’s output in a variable. Here is an
example that sends the 38.5 kHz signal to the IR LED connected to P8, then stores the IR
detector’s output, which is connected to P9, in a bit variable named irDetectLeft.
FREQOUT 8, 1, 38500
irDetectLeft = IN9
The IR detector’s output state when it sees no IR signal is high. When the IR detector
sees the 38500 Hz harmonic reflected by an object, its output is low. The IR detector’s
output only stays low for a fraction of a millisecond after the FREQOUT command is done
sending the harmonic, so it’s essential to store the IR detector’s output in a variable
immediately after sending the FREQOUT command. The value stored by the variable can
then be displayed in the Debug Terminal or used for navigation decisions by the Boe-Bot.
Example Program: TestLeftIrPair.bs2
√
√
Reconnect power to your board.
Enter, save, and run TestLeftIrPair.bs2.
' Robotics with the Boe-Bot - TestLeftIrPair.bs2
' Test IR object detection circuits, IR LED connected to P8 and detector
' connected to P9.
' {$STAMP BS2}
Chapter 7: Navigating with Infrared Headlights · Page 241
' {$PBASIC 2.5}
irDetectLeft
VAR
Bit
DO
FREQOUT 8, 1, 38500
irDetectLeft = IN9
DEBUG HOME, "irDetectLeft = ", BIN1 irDetectLeft
PAUSE 100
LOOP
√
√
√
√
√
Leave the Boe-Bot connected to the serial cable, because you will be using the
Debug Terminal to test your IR pair.
Place an object, such as your hand or a sheet of paper, about an inch from the left
IR pair, in the manner shown in Figure 7-1 on page 236.
Verify that when you place an object in front of the IR pair the Debug Terminal
displays a 0, and when you remove the object from in front of the IR pair, it
displays a 1.
If the Debug Terminal displays the expected values for object not detected (1)
and object detected (0), move on to the Your Turn section following the example
program.
If the Debug Terminal does not display the expected values, try the steps in the
Trouble-Shooting box.
Trouble-Shooting
If the Debug Terminal does not display the expected values, check for circuit and program
entry errors.
If you are always getting 0, even when an object is not placed in front of the Boe-Bot, there
may be a nearby object that is reflecting the infrared. The surface of the table in front of the
Boe-Bot is a common culprit. Move the Boe-Bot so that the IR LED and detector cannot
possibly be reflecting off any nearby object.
If the reading is 1 most of the time when there is no object in front of the Boe-Bot, but
flickers to 0 occasionally, it may mean you have interference from a nearby fluorescent light.
Turn off any nearby fluorescent lights and repeat your tests.
Your Turn
√
Save TestLeftIrPair.bs2 as TestRightIrPair.bs2.
Page 242 · Robotics with the Boe-Bot
√
√
√
√
√
Change the DEBUG statement, title and comments to refer to the right IR pair.
Change the variable name from irDetectLeft to irDetectRight. You will
need to do this in four places in the program.
Change the FREQOUT command’s Pin argument from 8 to 2.
Change the input register monitored by the irDetectRight variable from IN9
to IN0.
Repeat the testing steps in this activity for the right IR pair; with the IR LED
circuit connected to P2 and the detector connected to P0.
ACTIVITY #2: FIELD TESTING FOR OBJECT DETECTION AND
INFRARED INTERFERENCE
In this activity, you will build and test indicator LEDs that will tell you if an object is
detected without the help of the Debug Terminal. This is handy if you are not near a PC
or laptop, and you need to trouble-shoot your IR detector circuits. You will also write a
program to “sniff” for infrared interference from fluorescent lights. Some fluorescent
lights send signals that resemble the signal sent by your infrared LEDs. The device
inside a fluorescent light fixture that controls voltage for the lamp is called the ballast.
Some ballasts operate in the same frequency range of your IR detector, 38.5 kHz, which
in turn causes the lamp to emit a signal at this frequency. When you integrate IR object
detection with navigation, this interference can cause some bizarre Boe-Bot behavior!
Rebuilding the LED Indicator Circuits
These are the same LED indicator circuits that you used with the whiskers.
Parts List:
(2) Red LEDs
(2) Resistors – 220 Ω (red-red-brown)
√
√
Disconnect power from your board and servos.
Build the circuit shown in Figure 7-6 using Figure 7-7 as a reference.
Chapter 7: Navigating with Infrared Headlights · Page 243
P1
P10
220 Ω
220 Ω
Figure 7-6
Left and Right
Indicator LEDs
Red
LED
Red
LED
Vss
Left IR Pair
Right IR Pair
Vss
To Servos
To Servos
15 14 Vdd 13 12
Anode
leads
Anode
leads
Red
Black
X4
Vdd
X5
Vin
+
Board of Education
Rev C
Vdd
Vin
Vss
X3
Vss
X3
P15
P14
P13
P12
P11
P10
P9
P8
P7
P6
P5
P4
P3
P2
P1
P0
X2
(916) 624-8333
Rev B
www.parallax.com
www.stampsinclass.com
P15
P14
P13
P12
P11
P10
P9
P8
P7
P6
P5
P4
P3
P2
P1
P0
X2
+
Figure 7-7
Wiring
Diagrams for
Red LED
Indicators
with IR Object
Detection
Circuits
Board of
Education
(left) and
HomeWork
Board (right).
HomeWork Board
© 2000-2003
Testing the System
There are quite a few components involved in this system, and this increases the
likelihood of a wiring error. That’s why it’s important to have a test program that shows
you what the infrared detectors are sensing. You can use this program to verify that all
the circuits are working before unplugging the Boe-Bot from its serial cable and testing
other objects.
Example Program – TestIrPairsAndIndicators.bs2
√
√
Reconnect power to your board.
Enter, save, and run TestIrPairsAndIndicators.bs2.
Page 244 · Robotics with the Boe-Bot
√
√
√
Verify that the speaker makes a clear, audible tone while the Debug Terminal
displays “Testing piezospeaker…”.
Use the Debug Terminal to verify that the BASIC Stamp still receives a zero
from each IR detector when an object is placed in front of it.
Verify that the LED next to each detector emits light when the detector detects
an object. If one or both of the LEDs appear not to work, check your wiring and
your program.
' Robotics with the Boe-Bot - TestIrPairsAndIndicators.bs2
' Test IR object detection circuits.
' {$STAMP BS2}
' {$PBASIC 2.5}
' Stamp directive.
' PBASIC directive.
' -----[ Variables ]---------------------------------------------------------irDetectLeft
irDetectRight
VAR
VAR
Bit
Bit
' -----[ Initialization ]----------------------------------------------------DEBUG "Testing piezospeaker..."
FREQOUT 4, 2000, 3000
DEBUG CLS,
"IR DETECTORS", CR,
"Left
Right", CR,
"----- -----"
' -----[ Main Routine ]------------------------------------------------------DO
FREQOUT 8, 1, 38500
irDetectLeft = IN9
FREQOUT 2, 1, 38500
irDetectRight = IN0
IF (irDetectLeft = 0) THEN
HIGH 10
ELSE
LOW 10
ENDIF
IF (irDetectRight = 0) THEN
HIGH 1
ELSE
Chapter 7: Navigating with Infrared Headlights · Page 245
LOW 1
ENDIF
DEBUG CRSRXY, 2, 3, BIN1 irDetectLeft,
CRSRXY, 9, 3, BIN1 irDetectRight
PAUSE 100
LOOP
Your Turn – Remote Testing and Range Testing
You can now use your LED detectors to take your Boe-Bot and test your IR detectors on
objects that might not otherwise be in reach of your computer’s serial cable.
√
√
Unplug your Boe-Bot from the serial cable, and take your Boe-Bot to a variety
of objects and test the range of the IR detectors.
Try the detection range of different colored objects. What color is detected at the
furthest range? What color is detected at the closest range?
Sniffing for IR Interference
If you happened to notice that your Boe-Bot let you know it detected something even
though nothing was in range, it may mean that a nearby light is generating some IR light
at a frequency close to 38.5 kHz. If you try to have a Boe-Bot contest or demonstration
under one of these lights, your infrared systems might end up performing very poorly.
The last thing anybody wants is to have their robot not perform as advertised during a
public demonstration, so make sure to check any prospective demo area with this IR
interference “sniffer” program before-hand.
The concept behind this program is simple, don’t transmit any IR through the IR LEDs,
just monitor to see if any IR is detected. If IR is detected, sound the alarm using the
piezospeaker.
You can use a handheld remote for just about any piece of equipment to generate IR
interference. TVs, VCRs, CD/DVD players, and projectors all use the same IR detectors
you have on your Boe-Bot right now. Likewise, the remotes you use to control these
devices all use the same kind of IR LED that's on your Boe-Bot to transmit messages to the
IR detector in your TV, VCR, CD/DVD player, etc.
Page 246 · Robotics with the Boe-Bot
Example Program – IrInterferenceSniffer.bs2
√
√
Enter, save, and run IrInterferenceSniffer.bs2.
Test to make sure the Boe-Bot sounds the alarm when it detects IR interference.
You can do this with a separate Boe-Bot that’s running
TestIrPairsAndIndicators.bs2. If you don’t have a second Boe-Bot, just use a
handheld remote for a TV, VCR, CD/DVD player, or projector. Simply point
the remote at the Boe-Bot and press a button. If the Boe-Bot responds by
sounding the alarm, you know your IR interference sniffer is working.
' Robotics with the Boe-Bot – IrInterferenceSniffer.bs2
' Test fluorescent lights, infrared remotes, and other sources
' of 38.5 kHz IR interference.
' {$STAMP BS2}
' {$PBASIC 2.5}
counter
' Stamp directive.
' PBASIC directive.
VAR
Nib
DEBUG "IR interference not detected, yet...", CR
DO
IF (IN0 = 0) OR (IN9 = 0) THEN
DEBUG "IR Interference detected!!!", CR
FOR counter = 1 TO 5
HIGH 1
HIGH 10
FREQOUT 4, 50, 4000
LOW 1
LOW 10
PAUSE 20
NEXT
ENDIF
LOOP
Your Turn – Testing for Fluorescent Lights that Interfere
√
Disconnect your Boe-Bot from its serial cable, and point it at any fluorescent
light near where you plan to operate it. Especially if you get frequent alarms,
turn off that fluorescent light before trying to use IR object detection under it.
Always use this IrInterferenceSniffer.bs2 to make sure that any area where you are
using the Boe-Bot is free of infrared interference.
Chapter 7: Navigating with Infrared Headlights · Page 247
ACTIVITY #3: INFRARED DETECTION RANGE ADJUSTMENTS
You may have noticed that brighter car headlights (or a brighter flashlight) can be used to
see objects that are further away when it’s dark. By making the Boe-Bot’s infrared LED
headlights brighter, you can also increase its detection range. By resisting electric current
less, a smaller resistor allows more current to flow through an LED. More current
through an LED is what causes it to glow more brightly. In this activity, you will
examine the effect of different resistance values with both the red and infrared LEDs.
Parts List:
You will need some extra parts for this activity.
(2)
(2)
(2)
(2)
Resistors – 470 Ω (yellow-violet-brown)
Resistors – 220 Ω (red-red-brown)
Resistors – 2 kΩ (red-black-red)
Resistors – 4.7 kΩ (yellow-violet-red)
Series Resistance and LED Brightness
First, let’s use one of the red LEDs to “see” the difference that a resistor makes in how
brightly an LED glows. All we need to test the LED is a program that sends a high signal
to the LED.
Example Program – P1LedHigh.bs2
√
√
Enter, save and run P1LedHigh.bs2.
Run the program and verify that the LED in the circuit connected to P1 emits
light.
' Robotics with the Boe-Bot - P1LedHigh.bs2
' Set P1 high to test for LED brightness testing with each of
' these resistor values in turn: 220 ohm , 470 ohm, 1 k ohm.
' {$STAMP BS2}
' {$PBASIC 2.5}
DEBUG "Program Running!"
HIGH 1
STOP
Page 248 · Robotics with the Boe-Bot
The command STOP is used here rather than END, since END would put the BASIC Stamp
into low power mode.
Your Turn – Testing LED Brightness
Remember to disconnect power before you make changes to a circuit. Remember
also that the same program will run again when you reconnect power, so you can pick up
right where you left off with each test.
√
√
√
√
√
√
√
Note how brightly the LED in the circuit connected to P1 is glowing with the
220 Ω resistor.
Replace the 220 Ω resistor connected to P1 and the right LED’s cathode with a
470 Ω resistor.
Note now how brightly the LED glows.
Repeat for a 2 kΩ resistor.
Repeat once more with a 4.7 kΩ resistor.
Replace the 4.7 kΩ resistor with the 220 Ω resistor before moving on to the next
portion of this activity.
Explain in your own words the relationship between LED brightness and series
resistance.
Series Resistance and IR Detection Range
We now know that less series resistance will make an LED glow more brightly. A
reasonable hypothesis would be that brighter IR LEDs can make it possible to detect
objects that are further away.
√
√
Open and run TestIrPairsAndIndicators.bs2 (from page 244).
Verify that both detectors are working properly.
Your Turn – Testing IR LED Range
√
√
√
With a ruler, measure the furthest distance from the IR LED that a sheet of paper
facing the IR LED can be detected, with the 1 kΩ resistors in place, and record
your data in Table 7-2.
Replace the 1 kΩ resistors that connect P2 and P8 to the IR LED anodes with 4.7
kΩ resistors.
Determine the furthest distance at which the same sheet of paper is detected, and
record your data.
Chapter 7: Navigating with Infrared Headlights · Page 249
√
√
√
Repeat with 2 kΩ resistors.
Repeat with 470 Ω resistors.
Repeat with 220 Ω resistors.
Table 7-2: Detection Distance vs. Resistance
IR LED Series
Resistance, (Ω)
Maximum Detection Distance,
Circle one: ( in / cm )
4700
2000
1000
470
220
√
√
Before moving on to the next activity, restore your IR pairs to their original
configuration (with 1 kΩ resistors in series with each IR LED).
Also, before moving on, make sure to test this last change with
TestIrPairsAndIndicators.bs2 to verify that both IR LED/detector pairs are
working properly.
ACTIVITY #4: OBJECT DETECTION AND AVOIDANCE
An interesting thing about the IR detectors is that their outputs are just like the whiskers.
When no object is detected, the output is high; when an object is detected, the output is
low. In this activity, RoamingWithWhiskers.bs2 from page 178 is modified so that it
works with the IR detectors.
Converting the Whiskers Program for IR Object Detection/Avoidance
This next example program started as RoamingWithWhiskers.bs2. Aside from adjusting
the name and description, two bit variables were added to store the states of the IR
detectors.
irDetectLeft VAR
irDetectRight VAR
Bit
Bit
A routine was also added to read the IR pairs.
FREQOUT 8, 1, 38500
Page 250 · Robotics with the Boe-Bot
irDetectLeft = IN9
The IF…THEN statements were modified so that they look at the variables that store the IR
pair detections instead of the whisker inputs.
IF (irDetectLeft = 0) AND (irDetectRight = 0) THEN
GOSUB Back_Up
GOSUB Turn_Left
GOSUB Turn_Left
ELSEIF (irDetectLeft = 0) THEN
GOSUB Back_Up
GOSUB Turn_Right
ELSEIF (irDetectRight = 0) THEN
GOSUB Back_Up
GOSUB Turn_Left
ELSE
GOSUB Forward_Pulse
ENDIF
Example Program – RoamingWithIr.bs2
√
√
√
√
√
Open RoamingWithWhiskers.bs2
Modify it so that it matches the program below.
Reconnect power to your board and servos.
Save and run it.
Verify that, aside from the fact that there’s no contact required, it behaves like
RoamingWithWhiskers.bs2.
' -----[ Title ]-------------------------------------------------------------' Robotics with the Boe-Bot - RoamingWithIr.bs2
' Adapt RoamingWithWhiskers.bs2 for use with IR pairs.
' {$STAMP BS2}
' {$PBASIC 2.5}
' Stamp directive.
' PBASIC directive.
DEBUG "Program Running!"
' -----[ Variables ]---------------------------------------------------------irDetectLeft VAR
irDetectRight VAR
pulseCount
VAR
Bit
Bit
Byte
' -----[ Initialization ]-----------------------------------------------------
Chapter 7: Navigating with Infrared Headlights · Page 251
FREQOUT 4, 2000, 3000
' Signal program start/reset.
' -----[ Main Routine ]------------------------------------------------------DO
FREQOUT 8, 1, 38500
irDetectLeft = IN9
' Store IR detection values in
' bit variables.
FREQOUT 2, 1, 38500
irDetectRight = IN0
IF (irDetectLeft = 0) AND (irDetectRight = 0) THEN
GOSUB Back_Up
' Both IR pairs detect obstacle
GOSUB Turn_Left
' Back up & U-turn (left twice)
GOSUB Turn_Left
ELSEIF (irDetectLeft = 0) THEN
' Left IR pair detects
GOSUB Back_Up
' Back up & turn right
GOSUB Turn_Right
ELSEIF (irDetectRight = 0) THEN
' Right IR pair detects
GOSUB Back_Up
' Back up & turn left
GOSUB Turn_Left
ELSE
' Both IR pairs 1, no detects
GOSUB Forward_Pulse
' Apply a forward pulse
ENDIF
' and check again
LOOP
' -----[ Subroutines ]-------------------------------------------------------Forward_Pulse:
PULSOUT 13,850
PULSOUT 12,650
PAUSE 20
RETURN
' Send a single forward pulse.
Turn_Left:
FOR pulseCount = 0 TO 20
PULSOUT 13, 650
PULSOUT 12, 650
PAUSE 20
NEXT
RETURN
' Left turn, about 90-degrees.
Turn_Right:
FOR pulseCount = 0 TO 20
PULSOUT 13, 850
PULSOUT 12, 850
PAUSE 20
NEXT
RETURN
' Right turn, about 90-degrees.
Page 252 · Robotics with the Boe-Bot
Back_Up:
FOR pulseCount = 0 TO 40
PULSOUT 13, 650
PULSOUT 12, 850
PAUSE 20
NEXT
RETURN
' Back up.
Your Turn
√
Modify RoamingWithIr.bs2 so that the IR pairs are checked in a subroutine.
ACTIVITY #5: HIGH PERFORMANCE IR NAVIGATION
The style of pre-programmed maneuvers that were used in the previous activity were fine
for whiskers, but are unnecessarily slow when using the IR LEDs and detectors. You can
greatly improve the Boe-Bot’s roaming performance by checking for obstacles before
delivering each set of pulses to the servos. The program can use the sensor inputs to
select the best maneuver for each moment of navigation. That way, the Boe-Bot never
turns further than it has to, and it can neatly find its way around obstacles and
successfully navigate more complex courses.
Sampling Between Every Pulse to Avoid Collisions
The great thing about detecting an obstacle before bumping into it is that it gives the BoeBot some room to navigate around it. The Boe-Bot can apply a pulse to turn away from
an object, check again and if the object is still there, apply another pulse to avoid it. The
Boe-Bot can keep applying pulses and checking, until it steers clear of the obstacle.
Then, it can resume forward pulses. After experimenting with this next example
program, you’ll likely agree that it’s a much better way for the Boe-Bot to roam.
Example Program – FastIrRoaming.bs2
√
Enter, save, and run FastIrRoaming.bs2.
' Robotics with the Boe-Bot - FastIrRoaming.bs2
' Higher performance IR object detection assisted navigation
' {$STAMP BS2}
' {$PBASIC 2.5}
DEBUG "Program Running!"
irDetectLeft
VAR
Bit
' Variable Declarations
Chapter 7: Navigating with Infrared Headlights · Page 253
irDetectRight
pulseLeft
pulseRight
VAR
VAR
VAR
Bit
Word
Word
FREQOUT 4, 2000, 3000
' Signal program start/reset.
DO
' Main Routine
FREQOUT 8, 1, 38500
irDetectLeft = IN9
FREQOUT 2, 1, 38500
irDetectRight = IN0
' Check IR Detectors
' Decide how to navigate.
IF (irDetectLeft = 0) AND (irDetectRight = 0) THEN
pulseLeft = 650
pulseRight = 850
ELSEIF (irDetectLeft = 0) THEN
pulseLeft = 850
pulseRight = 850
ELSEIF (irDetectRight = 0) THEN
pulseLeft = 650
pulseRight = 650
ELSE
pulseLeft = 850
pulseRight = 650
ENDIF
PULSOUT 13,pulseLeft
PULSOUT 12,pulseRight
PAUSE 15
' Apply the pulse.
LOOP
' Repeat main routine
How FastIrRoaming.bs2 Works
This program takes a slightly different approach to applying pulses. Aside from the two
bits used to store the IR detector outputs, it uses two word variables to set the pulse
durations delivered by the PULSOUT command.
irDetectLeft
irDetectRight
pulseLeft
pulseRight
VAR
VAR
VAR
VAR
Bit
Bit
Word
Word
Inside the DO…LOOP, the FREQOUT commands are used to send a 38.5 kHz IR signal to
each IR LED. Immediately after the 1 ms burst of IR is sent, a bit variable stores the
output state of the IR detector. This is necessary, because if you wait any longer than a
Page 254 · Robotics with the Boe-Bot
command’s worth of time, the IR detector will return to the not detected (1 state),
regardless of whether or not it detected an object.
FREQOUT 8, 1, 38500
irDetectLeft = IN9
FREQOUT 2, 1, 38500
irDetectRight = IN0
In the IF…THEN statements, instead of delivering pulses or calling navigation routines,
this program sets variable values that will be used in PULSOUT commands’ Duration
arguments.
IF (irDetectLeft = 0) AND (irDetectRight = 0) THEN
pulseLeft = 650
pulseRight = 850
ELSEIF (irDetectLeft = 0) THEN
pulseLeft = 850
pulseRight = 850
ELSEIF (irDetectRight = 0) THEN
pulseLeft = 650
pulseRight = 650
ELSE
pulseLeft = 850
pulseRight = 650
ENDIF
Before the DO…LOOP repeats, the last thing to do is to deliver pulses to the servos. Notice
that the PAUSE command is no longer 20. Instead, it’s 15 since roughly 5 ms is taken
checking the IR LEDs.
PULSOUT 13,pulseLeft
PULSOUT 12,pulseRight
PAUSE 15
' Apply the pulse.
Your Turn
√
√
√
Save FastIrRoaming.bs2 as FastIrRoamingYourTurn.bs2.
Use the LEDs to broadcast that the Boe-Bot has detected an object.
Try modifying the values that pulseLeft and pulseRight are set to so that the
Boe-Bot does everything at half speed.
Chapter 7: Navigating with Infrared Headlights · Page 255
ACTIVITY #6: THE DROP-OFF DETECTOR
Up until now, the Boe-Bot has mainly been programmed to take evasive maneuvers when
an object is detected. There are also applications where the Boe-Bot must take evasive
action when an object is not detected. For example, if the Boe-Bot is roaming on a table,
its IR detectors might be looking down at the table surface as shown in Figure 7-8. The
program should make it continue forward so long as both IR detectors can “see” the
surface of the table. In other words, the Boe-Bot can continue forward so long as the
table top it’s navigating on is detected.
√
√
Disconnect power from your board and servos.
Point your IR pairs downward and outward as shown in Figure 7-8.
To Servos
15 14 Vdd 13 12
Red
Black
X4
Vdd
X5
Vin
Vss
Figure 7-8
IR Pairs
Directed
Downwards to
Scan for a
Drop-Off
X3
P15
P14
P13
P12
P11
P10
P9
P8
P7
P6
P5
P4
P3
P2
P1
P0
X2
+
Board of Education
Rev C
© 2000-2003
Top View
Side View
Recommended Materials:
(1) Roll of black vinyl electrical tape – ¾″ (19 mm) wide.
(1) Sheet of white poster board – 22 x 28 in (56 x 71 cm).
Simulating a Drop-Off with Electrical Tape
A sheet of white poster board with a border made of electrical tape makes for a handy
way to simulate the drop-off presented by a table edge, with much less risk to your BoeBot.
Page 256 · Robotics with the Boe-Bot
√
√
√
√
√
Build a course similar to the electrical tape delimited course shown in Figure 79. Use at least three strips of electrical tape, edge to edge with no paper visible
between the strips.
Replace your 1 kΩ resistors with 2 kΩ resistors (red-black-red) to connect P2 to
its IR LED and P8 to its IR LED. We want the Boe-Bot to be nearsighted for
this activity.
Reconnect power to your board.
Run the program IrInterferenceSniffer.bs2 (page 246) to make sure that nearby
fluorescent lighting will not interfere with your Boe-Bot’s IR detectors.
Use the TestIrPairsAndIndicators.bs2 (page 244) to make sure that the Boe-Bot
detects the poster board but does not detect the electrical tape.
If the Boe-Bot still "sees" the electrical tape too clearly, here are a few remedies:
√
√
√
Try adjusting the IR detectors and LEDs downward at various angles.
√
Adjust the FREQOUT command with different Freq1 arguments. Here are some
arguments that will make the Boe-Bot more nearsighted: 38250, 39500, 40500
Try a different brand of vinyl electrical tape.
Try replacing the 2 kΩ resistors with 4.7 kΩ (yellow-violet-red) resistors to make the
Boe-Bot more nearsighted.
If you are using older IR LEDs, the Boe-Bot might actually be having problems with being
too nearsighted. Here are some remedies that will increase the Boe-Bot's sensitivity to
objects and make it more far sighted:
√
Try 1 kΩ (brown-black-red) or 470 Ω (yellow-violet-brown) or even 220 Ω (red-redbrown) resistors in series with the IR LEDs instead of 2 kΩ.
Chapter 7: Navigating with Infrared Headlights · Page 257
22” (56 cm)
22” (56 cm)
Figure 7-9
Electrical Tape
Outline
Simulates
Tabletop Edge
If you try a tabletop after success with the electrical tape course:
√
Remember to follow the same steps you followed before running the Boe-Bot in the
electrical tape delimited course!
Make sure to be the spotter for your Boe-Bot. Be ready as your Boe-Bot roams the tabletop:
√
Always be ready to pick your Boe-Bot up from above as it approaches the edge of the
table it’s navigating. If the Boe-Bot tries to drive off the edge, pick it up before it takes
the plunge. Otherwise, your Boe-Bot might become a Not-Bot!
√
Your Boe-Bot may detect you if you are standing in its line of sight. Its current program
has no way to differentiate you from the table below it, so it might try to continue
forward and off the edge of the table. So, stay out of its detector’s line of sight as you
spot.
Programming for Drop-Off Detection
For the most part, programming your Boe-Bot to navigate around a table top without
going over the edge is a matter of adjusting the IF...THEN statements from
FastIrNavigation.bs2. The main adjustment is that the servos should be directed to make
the Boe-Bot roll forward when irDetectLeft and irDetectRight are both 0,
indicating that an object (the table’s surface) has been detected. The Boe-Bot also has to
turn away from a detector that indicates it has not detected an object. For example, if
irDetectLeft is 1, the Boe-Bot had better turn right.
Page 258 · Robotics with the Boe-Bot
A second feature of a program for turning away from drop-offs is adjustable distance.
You may want your Boe-Bot to only take one pulse forward between checking the
detectors, but as soon as a drop-off is detected, you may want your Boe-Bot to take
several pulses worth of turn before checking the detectors again.
Just because you are taking multiple pulses in an evasive maneuver, it doesn’t mean you
have to return to whiskers-style navigation. Instead, you can add a pulseCount variable
that you can use to set to the number of pulses to deliver for a maneuver. The PULSOUT
command can be placed inside a FOR…NEXT loop that executes FOR 1 TO pulseCount
pulses. For one pulse forward, pulseCount can be 1, for ten pulses left, pulseCount
can be set to 10, and so on.
Example Program – AvoidTableEdge.bs2
√
√
√
√
√
Open FastIrNavigation.bs2 and save it as AvoidTableEdge.bs2.
Modify the program so that it matches the example program. This will involve
adding variables, modifying the IF…THEN statements, and nesting the PULSOUT
commands inside a FOR…NEXT loop. Be careful to make sure that all the
pulseLeft and pulseRight variable values inside the IF…THEN statement are
properly adjusted.
Their values are different from the ones in
FastIrNavigation.bs2 because the rules of the course are different.
Reconnect your board and servos.
Test the program on your electrical tape delimited course.
If you decide to try a tabletop, remember to follow the testing and spotting tips
discussed earlier.
' Robotics with the Boe-Bot - AvoidTableEdge.bs2
' IR detects object edge and navigates to avoid drop-off.
' {$STAMP BS2}
' {$PBASIC 2.5}
DEBUG "Program Running!"
irDetectLeft
irDetectRight
pulseLeft
pulseRight
loopCount
pulseCount
VAR
VAR
VAR
VAR
VAR
VAR
Bit
Bit
Word
Word
Byte
Byte
' Variable declarations.
Chapter 7: Navigating with Infrared Headlights · Page 259
FREQOUT 4, 2000, 3000
' Signal program start/reset.
DO
' Main Routine.
FREQOUT 8, 1, 38500
irDetectLeft = IN9
FREQOUT 2, 1, 38500
irDetectRight = IN0
' Check IR detectors.
' Decide navigation.
IF (irDetectLeft = 0) AND (irDetectRight = 0) THEN
pulseCount = 1
' Both detected,
pulseLeft = 850
' one pulse forward.
pulseRight = 650
ELSEIF (irDetectRight = 1) THEN
' Right not detected,
pulseCount = 10
' 10 pulses left.
pulseLeft = 650
pulseRight = 650
ELSEIF (irDetectLeft = 1) THEN
' Left not detected,
pulseCount = 10
' 10 pulses right.
pulseLeft = 850
pulseRight = 850
ELSE
' Neither detected,
pulseCount = 15
' back up and try again.
pulseLeft = 650
pulseRight = 850
ENDIF
FOR loopCount = 1 TO pulseCount
PULSOUT 13,pulseLeft
PULSOUT 12,pulseRight
PAUSE 20
NEXT
' Send pulseCount pulses
LOOP
How AvoidTableEdge.bs2 Works
Since this program is a modified version of FastIrRoaming.bs2, only changes to the program
are discussed here.
A FOR…NEXT loop is added to the program to control how many pulses are delivered each
time through the main (DO…LOOP) routine. Two variables are added, loopCount
functions as an index for a FOR…NEXT loop and pulseCount is used as the EndValue
argument.
loopCount
VAR
Byte
Page 260 · Robotics with the Boe-Bot
pulseCount
VAR
Byte
The IF…THEN statements now set the value of pulseCount as well as the values of
pulseRight and pulseLeft. If both detectors can see the table, take one cautious pulse
forward.
IF (irDetectLeft = 0) AND (irDetectRight = 0) THEN
pulseCount = 1
pulseLeft = 850
pulseRight = 650
Else, if the right IR detector does not see the tabletop, rotate left 10 pulses.
ELSEIF (irDetectRight = 1) THEN
pulseCount = 10
pulseLeft = 650
pulseRight = 650
Else, if the left IR detector does not see the tabletop, rotate right 10 pulses.
ELSEIF (irDetectLeft = 1) THEN
pulseCount = 10
pulseLeft = 850
pulseRight = 850
Else, if neither detector can see the table top, back up 15 pulses and try again, hoping that
one of the detectors will see the drop-off before the other.
ELSE
pulseCount = 15
pulseLeft = 650
pulseRight = 850
ENDIF
Now that the value of pulseCount, pulseLeft, and pulseRight are set, this FOR…NEXT
loop delivers the specified number of pulses for the maneuver determined by the
pulseLeft and pulseRight variable.
FOR loopCount = 1 TO pulseCount
PULSOUT 13,pulseLeft
PULSOUT 12,pulseRight
PAUSE 20
Chapter 7: Navigating with Infrared Headlights · Page 261
NEXT
Your Turn
You can experiment with setting different pulseLeft, pulseRight, and pulseCount
values inside the IF…THEN statement. For example, if the Boe-Bot doesn’t turn as far, it
may actually track the edge of the electrical tape delimited course. Pivoting backward
instead of rotating in place may also lead to some interesting behaviors.
√
√
Modify AvoidTableEdge.bs2 so that it follows the edge of the electrical tape
delimited course by adjusting the pulseCount values so that the Boe-Bot
doesn’t turn too far away from the edge.
Experiment with pivoting as a way to make the Boe-Bot roam inside the
perimeter instead of following the edge.
Page 262 · Robotics with the Boe-Bot
SUMMARY
This chapter covered a unique technique for infrared object detection that uses the
infrared LED found in common handheld remotes, and the infrared detector found in
TVs, CD/DVD players, and other appliances that are controlled by these remotes. By
shining infrared into the Boe-Bot’s path and looking for its reflection, object detection
can be accomplished without physically contacting the object. Infrared LED circuits are
used to send a 38.5 kHz signal with the help of a property of the FREQOUT command
called a harmonic, which is inherent to digitally synthesized signals.
An infrared detection indicator program was introduced for remote (not connected to the
PC) testing of the IR LED/detector pairs. An infrared interference sniffer program was
also introduced to help detect interference that can be generated by some fluorescent light
fixtures. Since the signals sent by the IR detectors are so similar to the signals sent by the
whiskers, RoamingWithWhiskers.bs2 was adapted to the infrared detectors. A program
that checks the IR detectors between each servo pulse was introduced to demonstrate a
higher performance way of roaming without colliding into objects. This program was
then modified to avoid the edge of an electrical tape delimited area. Since electrical tape
absorbs infrared, framing a large sheet of construction paper emulates the drop-off that is
seen at a table edge without the danger to the actual Boe-Bot.
Questions
1. What is the frequency of the harmonic sent by FREQOUT 2, 1, 38500? What is
the value of the fundamental frequency sent by that command? How long are
these signals sent for? What I/O pin does the IR LED circuit have to be
connected to in order to broadcast this signal?
2. What command has to immediately follow the FREQOUT command in order to
accurately determine whether or not an object has been detected?
3. What does it mean if the IR detector sends a low signal? What does it mean
when the detector sends a high signal?
4. What happens if you change the value of a resistor in series with a red LED?
What happens if you change the value of a resistor in series with an infrared
LED?
Chapter 7: Navigating with Infrared Headlights · Page 263
Exercises
1. Modify a line of code in IrInterferenceSniffer.bs2 so that it only monitors one of
the IR LED/detector pairs.
2. Explain the function of pulseCount in AvoidTableEdge.bs2. How does this
relate to your answer to Exercise 3?
Projects
1. Design a Boe-Bot application that sits still until you wave your and in front of it,
then it starts roaming.
2. Design a Boe-Bot application that slowly rotates in place until it detects an
object. As soon as it detects an object, it locks onto and chases the object. This
is a classic SumoBot behavior.
3. Design a Boe-Bot application that roams, but if it detects infrared interference, it
sounds the alarm briefly, then continues roaming. This alarm should be different
from the low battery alarm.