Home > Aaron's Interests > Boe-Bot Code

Aaron Klapheck's Website

   Home | CSUS | Classes | Experience | Projects | Site Map | Websites   
site search by freefind

Boe-Bot Source

The following code was written by me and is organized by chapter (according to the book Robotics with the Boe-Bot) and name.


Chapter 1

 

Hello Boe-Bot

' Robotics with the Boe-Bot - HelloBoeBot.bs2
' BASIC Stamp sends a text message to your PC/laptop.

' {$STAMP BS2}
' {$PBASIC 2.5}

DEBUG "Hello, this is a message from your Boe-Bot."

END

Back to top of page

 

Hello Boe-Bot your decimal

' Robotics with the Boe-Bot - HelloBoeBotYourDec.bs2
' BASIC Stamp displays a number.

' {$STAMP BS2}
' {$PBASIC 2.5}

DEBUG "Hello, this is a message from your Boe-Bot. "
x VAR Byte
x=65
DEBUG DEC x 'show decimal value of x

END

Back to top of page


Chapter 2

 

Project #2 (located near the end of the chapter)

' Robotics with the Boe-Bot - Chapter2Project2.bs2
' This takes the servos through three seconds of each of the four different
' combinations of rotation. First, both servos should rotate CCW,
' then they should both rotate CW. Then, the P12 servo should rotate CW as the P13
' servo rotates CCW, and finally, the P12 servo should rotate CCW while the P13
' servo rotates CW

' {$STAMP BS2}
' {$PBASIC 2.5}

counter VAR Byte

DEBUG "Program Running!"

FOR counter = 1 TO 120 ' 120 = 3s/((1.7 + 1.7 + 20 + 1.6)0.001s)
PULSOUT 13, 850
PULSOUT 12, 850
PAUSE 20
NEXT

FOR counter = 1 TO 124 ' 124 = 3s/((1.3 + 1.3 + 20 + 1.6)0.001s)
PULSOUT 13, 650
PULSOUT 12, 650
PAUSE 20
NEXT

FOR counter = 1 TO 122 ' 122 = 3s/((1.7 + 1.3 + 20 + 1.6)0.001s)
PULSOUT 13, 850
PULSOUT 12, 650
PAUSE 20
NEXT

FOR counter = 1 TO 122 ' 122 = 3s/((1.3 + 1.7 + 20 + 1.6)0.001s)
PULSOUT 13, 650
PULSOUT 12, 850
PAUSE 20
NEXT

END

Back to top of page

 

If Then Else Blinking Light

' Robotics with the Boe-Bot - IfThenElseLight.bs2
' Test If Then Else command involving a light.

' {$STAMP BS2}
' {$PBASIC 2.5}

#DEFINE test = 2

DEBUG "The LED connected to Pin 13 is blinking!"

#IF test = 1 #THEN
HIGH 13
PAUSE 500
LOW 13
PAUSE 500
#ELSE
HIGH 12
PAUSE 500
LOW 12
PAUSE 500
#ENDIF

END

Back to top of page

 

Hello Once Every Second

' Robotics with the Boe-Bot - HelloOnce EverySecond.bs2
' Display a message once every second.

' {$STAMP BS2}
' {$PBASIC 2.5}

define_variables:
counter VAR Byte

Setup:
FREQOUT 4, 2000, 3000

Main:
FOR counter = 1 TO 5
DEBUG "Hello!", CR
PAUSE 1000
NEXT
END

Back to top of page


Chapter 3

 

Right Servo Test

' Robotics with the Boe-Bot - RightServoTest.bs2
' Right servo turns clockwise three seconds, stops 1 second,
' then turns CCW for three seconds

' {$STAMP BS2}
' {$PBASIC 2.5}

counter VAR Byte

DEBUG "Program Running!"

FREQOUT 4, 2000, 3000

FOR counter = 1 TO 122 ' CW just under 3s
PULSOUT 12, 650
PAUSE 20
NEXT

FOR counter = 1 TO 122 ' Stop 1s
PULSOUT 12, 750
PAUSE 20
NEXT

FOR counter = 1 TO 122 ' CCW 3s
PULSOUT 12, 850
PAUSE 20
NEXT

FREQOUT 4, 2000, 3500

END

Back to top of page

 

Test Servo Speed

' Robotics with the Boe-Bot - TestServoSpeed.bs2
' Move forward, left, right, then backward for testing and tuning.

' {$STAMP BS2}
' {$PBASIC 2.5}

define_variables:
counter VAR Word
pulseWidth VAR Word
pulseWidthComp VAR Word

Setup:
FREQOUT 4, 2000, 3000 ' Signal program start/reset.

Main:
DO

DEBUG "Enter pulse width: "

DEBUGIN DEC pulseWidth

pulseWidthComp = 1500 - pulseWidth

FOR counter = 1 TO 244
PULSOUT 12, pulseWidth
PULSOUT 13, pulseWidthComp
PAUSE 20
NEXT
LOOP

END

Back to top of page

 

Start Reset Indicator

' Robotics with the Boe-Bot - StartResetIndcator.bs2
' Test the piezospeaker circuit.

' {$STAMP BS2}
' {$PBASIC 2.5}

counter VAR Word

DEBUG CLS, "Beep!!!"
FREQOUT 4, 2000, 3000 ' pin (constant), duration (ms), Freq1 (Hz) {, Freq2}.

FOR counter = 1 TO 10
DEBUG CR, "waiting for reset..."
PAUSE 500
NEXT

END

Back to top of page


Chapter 4

 

EEPROM Navigation Half Square with Ramping

' Robotics with the Boe-Bot - EepromNavigationHalfSquareWithRamping.bs2
' Store lists of word values that dictate.

' Path looks like:  |_    , going from south to north.
'                           _|
'                          |


' {$STAMP BS2} ' Stamp directive.
' {$PBASIC 2.5} ' PBASIC directive.

DEBUG "Program Running!"

' -----[ Variables ]-------------------------------------------------------------------

' Variable for main.
counter VAR Word
pulseCount VAR Word ' Stores number of pulses.
addressOffset VAR Byte ' Stores EEPROM address.
instruction VAR Byte ' Stores EEPROM instruction.
pulseRight VAR Word ' Stores servo pulse widths.
pulseLeft VAR Word ' Variable for RampUp/RampDown.
pulseCountRamp VAR Word ' Varialbe for I/O pins.
rightWheel PIN 12
leftWheel PIN 13
piezospeaker PIN 4

' -----[EEPROM Data ]------------------------------------------------------------------

Pulses_Count DATA Word 60, Word 21, Word 60, Word 21, Word 60,
Word 21, Word 60, Word 21, Word 60, Word 0
Pulses_Left DATA Word 850, Word 850, Word 850, Word 650, Word 850,
Word 650, Word 850, Word 850, Word 850
Pulses_Right DATA Word 650, Word 850, Word 650, Word 650, Word 650,
Word 650, Word 650, Word 850, Word 650

' -----[ Initialization ]--------------------------------------------------------------

FREQOUT piezospeaker, 2000, 3000 ' Signal program start/reset.

' -----[ Main Routine ]----------------------------------------------------------------

DO

READ Pulses_Count + addressOffset, Word pulseCount
READ Pulses_Left + addressOffset, Word pulseLeft
READ Pulses_Right + addressOffset, Word pulseRight

addressOffset = addressOffset + 2

IF (pulseCount = 60) THEN
GOSUB RampUp
ENDIF

FOR counter = 1 TO pulseCount
PULSOUT leftWheel, pulseLeft
PULSOUT rightWheel, pulseRight
PAUSE 20
NEXT

IF (pulseCount = 60) THEN
GOSUB RampDown
ENDIF

LOOP UNTIL (pulseCount = 0)

END

RampUp:
FOR pulseCountRamp = 1 TO 100
PULSOUT leftWheel, 750 + pulseCountRamp
PULSOUT rightWheel, 752 - pulseCountRamp
PAUSE 20
NEXT
RETURN

RampDown:
FOR pulseCountRamp = 100 TO 1
PULSOUT leftWheel, 750 + pulseCountRamp
PULSOUT rightWheel, 752 - pulseCountRamp
PAUSE 20
NEXT
RETURN

Back to top of page

 

Forward Left Right Backward Ramping and Subroutines

' Robotics with the Boe-Bot - ForwardLeftRightBackwardRampingAndSubroutines.bs2
' Move forward, left, right, then backward using subroutines.

' {$STAMP BS2}
' {$PBASIC 2.5}

counter VAR Word
PulseCount VAR Word

FREQOUT 4, 2000, 3000 ' Signal program start/reset.

' -----[ Main ]------------------------------------------------------------------------

DEBUG "Program Running!"

GOSUB Forward
GOSUB Turn_right
GOSUB Turn_left
GOSUB Back

END

Forward:

' Ramp up forward.

FOR PulseCount = 1 TO 100 ' Loop ramps up for 100 pulses.
PULSOUT 13, 750 + PulseCount ' Pulse = 1.5ms + pulseCount
PULSOUT 12, 750 - PulseCount ' Pulse = 1.5ms - PulseCount
PAUSE 20
NEXT

' Continue forward for 64 pulses.

FOR counter = 1 TO 30
PULSOUT 13, 850
PULSOUT 12, 650
PAUSE 20
NEXT

' Ramp down from going forward to a full stop.

FOR PulseCount = 100 TO 1 ' Loop ramps up for 100 pulses.
PULSOUT 13, 750 + PulseCount ' Pulse = 1.5ms + pulseCount
PULSOUT 12, 750 - PulseCount ' Pulse = 1.5ms - PulseCount
PAUSE 20
NEXT

RETURN

Turn_right:

' Ramp up right rotate

FOR PulseCount = 1 TO 26
PULSOUT 13, 750 + PulseCount
PULSOUT 12, 750 + PulseCount
PAUSE 20
NEXT

' Ramp down right rotate

FOR PulseCount = 26 TO 1
PULSOUT 13, 750 + PulseCount
PULSOUT 12, 750 + PulseCount
PAUSE 20
NEXT

RETURN

Turn_left:

' ramp up left rotate

FOR PulseCount = 1 TO 26
PULSOUT 13, 750 - PulseCount
PULSOUT 12, 750 - PulseCount
PAUSE 20
NEXT

' Ramp up down left rotate

FOR PulseCount = 26 TO 1
PULSOUT 13, 750 - PulseCount
PULSOUT 12, 750 - PulseCount
PAUSE 20
NEXT

RETURN

Back:

'Ramp up back word.

FOR PulseCount = 1 TO 100 ' Loop ramps up for 100 pulses.
PULSOUT 13, 750 - PulseCount
PULSOUT 12, 750 + PulseCount
PAUSE 20
NEXT

'Continue back word for 64 pulses.

FOR counter = 1 TO 30
PULSOUT 13, 650
PULSOUT 12, 850
PAUSE 20
NEXT

'Ramp down from going back word to a full stop.

FOR PulseCount = 100 TO 1 ' Loop ramps up for 100 pulses.
PULSOUT 13, 750 - PulseCount
PULSOUT 12, 750 + PulseCount
PAUSE 20
NEXT

RETURN

Back to top of page

 

Movements With Variables and One Subroutine

' Robotics with the Boe-Bot - MovementsWithVariablesAndOneSubroutine.bs2
' Make a navigation routine that accepts parameters.

' {$STAMP BS2}
' {$PBASIC 2.5}

DEBUG "Program Running!"

counter VAR Word
pulseLeft VAR Word
pulseRight VAR Word
pulseCount VAR Byte

FREQOUT 4, 2000, 3000 ' Signal program start/reset.

' Forward

pulseLeft = 850: pulseRight = 650: pulseCount = 64: GOSUB Navigate

' Left turn

pulseLeft = 650: pulseRight = 650: pulseCount = 24: GOSUB Navigate

' Right turn

pulseLeft = 850: pulseRight = 850: pulseCount = 24: GOSUB Navigate

' Left turn

pulseLeft = 650: pulseRight = 850: pulseCount = 64: GOSUB Navigate

END

Navigate:
FOR counter = 1 TO pulseCount
PULSOUT 13, pulseLeft
PULSOUT 12, pulseRight
PAUSE 20
NEXT
PAUSE 200
RETURN

Back to top of page


Chapter 5

 

Escaping Corners

' -----[ Title ]-----------------------------------------------------------------------
' Robotics with the Boe-Bot - EscapingCorners.bs2
' Boe-Bot navigates out of corners by detecting alternating whisker presses.
' Used rechargeable 1.25V batteries.

' {$STAMP BS2} ' Stamp directive.
' {$PBASIC 2.5} ' PBASIC directive.

DEBUG "Program Running!"

' -----[ Variables ]-------------------------------------------------------------------

' Pin variables.
LedRight PIN 1
LedLeft PIN 10
WheelRight PIN 12
WheelLeft PIN 13
WhiskerRight PIN 7
WhiskerLeft PIN 5
Piezospeaker PIN 4

' Loop variables.
PulseCount VAR Byte ' FOR...NEXT loop counter.

' Leaving corners
counter VAR Nib
old7 VAR Bit
old5 VAR Bit

' -----[ Initialization ]--------------------------------------------------------------

FREQOUT Piezospeaker, 2000, 3000 ' Signal program start/reset.
Counter = 1
old7 = 0 ' Make up old variables
old5 = 1

' -----[ Escaping Corners ]------------------------------------------------------------

DO
IF (IN7 <> IN5) THEN ' One of either whiskers is pressed.
IF (old7 <> IN7) AND (old5 <> IN5) THEN ' Different Whiskers pressed form last time.
counter = counter + 1
old7 = IN7 ' Record Whisker press for next comparison.
old5 = IN5
IF (counter > 4) THEN ' More than 3 consecutive whisker presses
counter = 1 ' then bot makes a U-turn.
GOSUB Turn_Around
ENDIF ' End if counter is > 4
ELSE ' Whisker presses don't alternate,
counter = 1 ' so rest counter.
ENDIF
ENDIF ' No whiskers pressed

' -----[ Same as RoamingWithWhiskersWhithLeds.bs2 ]------------------------------------

IF (IN5 = 0 ) AND (IN7 = 0) THEN ' Both whiskers detect obstacle
HIGH LedRight
HIGH LedLeft
GOSUB Turn_Around ' Back up and U-turn (left twice).
ELSEIF (IN5 = 0) THEN ' Left whisker contacts
HIGH LedLeft
GOSUB Back_Up ' Back up and turn right
GOSUB Turn_Right
ELSEIF (IN7 = 0) THEN ' Right whisker contacts
HIGH LedRight
GOSUB Back_Up ' Back up and turn left
GOSUB Turn_Left
ELSE
LOW LedRight
LOW LedLeft
GOSUB Forward_Pulse ' No whiskers contact, go forward
ENDIF
LOOP ' Check again

' -----[ Subroutines ]-----------------------------------------------------------------

Turn_Around:

GOSUB Back_Up

FREQOUT Piezospeaker, 2000, 1000 ' Angerey noise.

GOSUB Turn_Left
GOSUB Turn_Left

RETURN

Forward_Pulse: ' Send a single forward pulse.
PULSOUT WheelLeft, 850
PULSOUT WheelRight, 682
PAUSE 20
RETURN

Turn_Left: ' Left turn, about 90-degrees.
FOR PulseCount = 0 TO 22
PULSOUT WheelLeft, 650
PULSOUT WheelRight, 650
PAUSE 20
NEXT
RETURN

Turn_Right: ' Right turn, about 90-degrees.
FOR PulseCount = 0 TO 21
PULSOUT WheelLeft, 850
PULSOUT WheelRight, 850
PAUSE 20
NEXT
RETURN

Back_Up: ' Back up.
FOR PulseCount = 0 TO 40
PULSOUT WheelLeft, 650
PULSOUT WheelRight, 816
PAUSE 20
NEXT
RETURN

Back to top of page

 

Roaming with Whiskers

' -----[ Title ]-----------------------------------------------------------------------
' Robotics with the Boe-Bot - RoamingWithWhiskers.bs2
' Use whiskers to detect objects and navigate around them.
' Used rechargeable 1.25V batteries.

' {$STAMP BS2} ' Stamp directive.
' {$PBASIC 2.5} ' PBASIC directive.

DEBUG "Program Running!"

' -----[ Variables ]-------------------------------------------------------------------

' Pin variables.
LedRight PIN 1
LedLeft PIN 10
WheelRight PIN 12
WheelLeft PIN 13
WhiskerRight PIN 7
WhiskerLeft PIN 5
Piezospeaker PIN 4

' Loop variables.
PulseCount VAR Byte ' FOR...NEXT loop counter.

' -----[ Initialization ]--------------------------------------------------------------

FREQOUT Piezospeaker, 2000, 3000 ' Signal program start/reset.

' -----[ Main Routine ]----------------------------------------------------------------

DO
IF (IN5 = 0 ) AND (IN7 = 0) THEN ' Both whiskers detect obstacle
GOSUB Back_Up ' Back up and U-turn (left twice).
GOSUB Turn_Left
GOSUB Turn_Left
ELSEIF (IN5 = 0) THEN ' Left whisker contacts
GOSUB Back_Up ' Back up and turn right
GOSUB Turn_Right
ELSEIF (IN7 = 0) THEN ' Right whisker contacts
GOSUB Back_Up ' Back up and turn left
GOSUB Turn_Left
ELSE
GOSUB Forward_Pulse ' No whiskers contact, go forward
ENDIF
LOOP ' Check again

' -----[ Subroutines ]-----------------------------------------------------------------

Forward_Pulse: ' Send a single forward pulse.
PULSOUT WheelLeft, 850
PULSOUT WheelRight, 682
PAUSE 20
RETURN

Turn_Left: ' Left turn, about 90-degrees.
FOR PulseCount = 0 TO 22
PULSOUT WheelLeft, 650
PULSOUT WheelRight, 650
PAUSE 20
NEXT
RETURN

Turn_Right: ' Right turn, about 90-degrees.
FOR PulseCount = 0 TO 21
PULSOUT WheelLeft, 850
PULSOUT WheelRight, 850
PAUSE 20
NEXT
RETURN

Back_Up: ' Back up.
FOR PulseCount = 0 TO 40
PULSOUT WheelLeft, 650
PULSOUT WheelRight, 816
PAUSE 20
NEXT
RETURN

Back to top of page

 

Roaming with Whiskers with LED's

' -----[ Title ]-----------------------------------------------------------------------
' Robotics with the Boe-Bot - RoamingWithWhiskersWithLeds.bs2
' Use whiskers to detect objects and navigate around them.
' Used rechargeable 1.25V batteries.

' {$STAMP BS2} ' Stamp directive.
' {$PBASIC 2.5} ' PBASIC directive.

DEBUG "Program Running!"

' -----[ Variables ]-------------------------------------------------------------------

' Pin variables.
LedRight PIN 1
LedLeft PIN 10
WheelRight PIN 12
WheelLeft PIN 13
WhiskerRight PIN 7
WhiskerLeft PIN 5
Piezospeaker PIN 4

' Loop variables.
PulseCount VAR Byte ' FOR...NEXT loop counter.

' -----[ Initialization ]--------------------------------------------------------------

FREQOUT Piezospeaker, 2000, 3000 ' Signal program start/reset.

' -----[ Main Routine ]----------------------------------------------------------------

DO
IF (IN5 = 0 ) AND (IN7 = 0) THEN ' Both whiskers detect obstacle
HIGH LedRight
HIGH LedLeft
GOSUB Back_Up ' Back up and U-turn (left twice).
GOSUB Turn_Left
GOSUB Turn_Left
ELSEIF (IN5 = 0) THEN ' Left whisker contacts
HIGH LedLeft
GOSUB Back_Up ' Back up and turn right
GOSUB Turn_Right
ELSEIF (IN7 = 0) THEN ' Right whisker contacts
HIGH LedRight
GOSUB Back_Up ' Back up and turn left
GOSUB Turn_Left
ELSE
LOW LedRight
LOW LedLeft
GOSUB Forward_Pulse ' No whiskers contact, go forward
ENDIF
LOOP ' Check again

' -----[ Subroutines ]-----------------------------------------------------------------

Forward_Pulse: ' Send a single forward pulse.
PULSOUT WheelLeft, 850
PULSOUT WheelRight, 682
PAUSE 20
RETURN

Turn_Left: ' Left turn, about 90-degrees.
FOR PulseCount = 0 TO 22
PULSOUT WheelLeft, 650
PULSOUT WheelRight, 650
PAUSE 20
NEXT
RETURN

Turn_Right: ' Right turn, about 90-degrees.
FOR PulseCount = 0 TO 21
PULSOUT WheelLeft, 850
PULSOUT WheelRight, 850
PAUSE 20
NEXT
RETURN

Back_Up: ' Back up.
FOR PulseCount = 0 TO 40
PULSOUT WheelLeft, 650
PULSOUT WheelRight, 816
PAUSE 20
NEXT
RETURN

Back to top of page

 


Chapter 6

 

Flashlight Controlled Beo-Bot

' -----[ Title ]-----------------------------------------------------------------------
' Robotics with the Boe-Bot - FlashlightControlledBeoBot.bs2
' Boe-Bot follows flashlight beam focused in front of it..
' Used rechargeable 1.25V batteries.

' {$STAMP BS2} ' Stamp directive.
' {$PBASIC 2.5} ' PBASIC directive.

DEBUG "program Running!"

' -----[ Constants ]-------------------------------------------------------------------

LeftAmbient CON 74
RightAmbient CON 54
LeftBright CON 46
RightBright CON 33

LeftThreshold CON LeftBright + LeftAmbient / 2 * 7 / 8
RightThreshold CON RightBright + RightAmbient / 2 * 7 / 8

' -----[ Variables ]-------------------------------------------------------------------

' Pin variables.
WheelRight PIN 12
WheelLeft PIN 13
PRRight PIN 3
PRLeft PIN 6
Piezospeaker PIN 4
' Declare variables for storing measured RC times of photoresistors.
TimeLeft VAR Word
TimeRight VAR Word

' -----[ Initialization ]--------------------------------------------------------------

FREQOUT Piezospeaker, 2000, 3000 ' Signal program start/reset.

' -----[ Main Routine ]----------------------------------------------------------------

DO
GOSUB Test_Photoresistors
GOSUB Navigate
LOOP

' -----[ Subroutine - Test_Photoresistors ]--------------------------------------------

Test_Photoresistors:

HIGH PRLeft ' Left RC time measurement.
PAUSE 2
RCTIME PRLeft, 1, TimeLeft

HIGH PRRight ' Right RC time measurement.
PAUSE 2
RCTIME PRRight, 1, TimeRight

RETURN

' -----[ Subroutine - Navigate ]-------------------------------------------------------

Navigate:

IF (TimeLeft < LeftThreshold) AND (TimeRight < RightThreshold) THEN
PULSOUT WheelLeft, 850
PULSOUT WheelRight, 682
ELSEIF (TimeLeft < LeftThreshold) THEN ' Left detects shadow, pivot left
PULSOUT WheelLeft, 700
PULSOUT WheelRight, 700
ELSEIF (TimeRight < RightThreshold) THEN ' Right detects shadow, pivot right
PULSOUT WheelLeft, 800
PULSOUT WheelRight, 800
ENDIF

PAUSE 20

RETURN

Back to top of page

 

Shadow Guided Boe-Bot

' -----[ Title ]-----------------------------------------------------------------------
' Robotics with the Boe-Bot - ShadowGuidedBoeBot.bs2
' Boe-Bot detects shadows caste by your hand and tries to follow them.
' Used rechargeable 1.25V batteries.

' {$STAMP BS2} ' Stamp directive.
' {$PBASIC 2.5} ' PBASIC directive.

DEBUG "Program Running!"

' -----[ Variables ]-------------------------------------------------------------------

' Pin variables.
WheelRight PIN 12
WheelLeft PIN 13
PRRight PIN 3
PRLeft PIN 6
Piezospeaker PIN 4

' -----[ Initialization ]--------------------------------------------------------------

FREQOUT Piezospeaker, 2000, 3000 ' Signal program start/reset.

' -----[ Main Routine ]----------------------------------------------------------------

DO
IF (IN6 = 0 ) AND (IN3 = 0) THEN ' Both detect shadows
PULSOUT WheelLeft, 850
PULSOUT WheelRight, 682
ELSEIF (IN6 = 0) THEN ' Left detects shadow, pivot left
PULSOUT WheelLeft, 750
PULSOUT WheelRight, 650
ELSEIF (IN3 = 0) THEN ' Right detects shadow, pivot right
PULSOUT WheelLeft, 850
PULSOUT WheelRight, 750
ENDIF

PAUSE 20

LOOP ' Check again

END

Back to top of page

 

Roaming Toward the Light

' -----[ Title ]-----------------------------------------------------------------------
' Robotics with the Boe-Bot - RoamingTorwardTheLight.bs2
' Boe-Bot roams and turns away from dark areas in favor of brighter areas.
' Used rechargeable 1.25V batteries.

' {$STAMP BS2} ' Stamp directive.
' {$PBASIC 2.5} ' PBASIC directive.

DEBUG "program Running!"

' -----[ Variables ]-------------------------------------------------------------------

' Pin variables.
WheelRight PIN 12
WheelLeft PIN 13
PRRight PIN 3
PRLeft PIN 6
Piezospeaker PIN 4
' Declare variables for storing measured RC times of photoresistors.
TimeLeft VAR Word
TimeRight VAR Word
' Variables for calculation.
average VAR Word
difference VAR average

' -----[ Initialization ]--------------------------------------------------------------

FREQOUT Piezospeaker, 2000, 3000 ' Signal program start/reset.

' -----[ Main Routine ]----------------------------------------------------------------

DO
GOSUB Test_Photoresistors
GOSUB Average_And_Difference
GOSUB Navigate
LOOP

' -----[ Subroutine - Test_Photoresistors ]--------------------------------------------

Test_Photoresistors:

HIGH PRLeft ' Left RC time measurement.
PAUSE 2
RCTIME PRLeft, 1, TimeLeft

HIGH PRRight ' Right RC time measurement.
PAUSE 2
RCTIME PRRight, 1, TimeRight

RETURN

' -----[ Subroutine - Average_And_Differenc ]------------------------------------------

Average_And_Difference:

average = TimeRight + TimeLeft / 2
difference = average / 6

RETURN

' -----[ Subroutine - Navigate ]-------------------------------------------------------

Navigate:

IF (TimeLeft > TimeRight + difference) THEN
PULSOUT WheelLeft, 850
PULSOUT WheelRight, 850
ELSEIF (TimeRight > TimeLeft + difference) THEN
PULSOUT WheelLeft, 650
PULSOUT WheelRight, 650
ELSE
PULSOUT WheelLeft, 850
PULSOUT WheelRight, 682
ENDIF

PAUSE 20

RETURN

END

Back to top of page


Chapter 7

 

Avoiding Table Edges

' -----[ Title ]-----------------------------------------------------------------------
'Robotics with the Boe-Bot - AvoidingTableEdges.bs2
'IR detects object edge and navigates to avoid drop-off.
'
' -----[ Notes ]-----------------------------------------------------------------------
'
'Used rechargeable 1.25V batteries.

' -----[ Program ]---------------------------------------------------------------------

'{$STAMP BS2} 'Stamp directive.
'{$PBASIC 2.5} 'PBASIC directive.

DEBUG "Program Running!"

' -----[ Variables/Constants/Pins ]----------------------------------------------------

IrDetectorLeft VAR Bit
IrDetectorRight VAR Bit
PulseLeft VAR Word
PulseRight VAR Word
LoopCount VAR Byte
PulseCount VAR Byte

FreqDetectable CON 38500

Piezospeaker PIN 4 'Speaker
WheelRight PIN 12 'Wheels left/right.
WheelLeft PIN 13
RedLedRight PIN 1 'Red LED's
RedLedLeft PIN 10
IrDetectLeft PIN 9 'Left emitter/detector pair
IrLedLeft PIN 8
IrDetectRight PIN 0 'Right emitter/detector pair
IrLedRight PIN 2

' -----[ Initialization ]--------------------------------------------------------------

FREQOUT Piezospeaker, 2000, 3000 'Signal program start/reset.

' -----[ Main Routine ]----------------------------------------------------------------

DO 'Main loop

FREQOUT IrLedLeft, 1, FreqDetectable 'Store IR detection values.
IrDetectorLeft = IN9

FREQOUT IrLedRight, 1, FreqDetectable
IrDetectorRight = IN0

IF (IrDetectorLeft = 0 ) AND (IrDetectorRight = 0) THEN
'Both detected, go forward.
PulseCount = 1
' HIGH RedLedRight
' HIGH RedLedLeft
PulseLeft = 850
PulseRight = 684
ELSEIF (IrDetectorLeft = 0) THEN 'Right not detected, go left.
PulseCount = 10
' HIGH RedLedLeft
PulseLeft = 650
PulseRight = 650
ELSEIF (IrDetectorRight = 0) THEN 'Left not detected, go right.
PulseCount = 10
' HIGH RedLedRight
PulseLeft = 850
PulseRight = 850
ELSE 'Nither detected, back up
PulseCount = 15
' LOW RedLedRight
' LOW RedLedLeft
PulseLeft = 684
PulseRight = 850
ENDIF '(IrDetectorLeft = 0 ) AND (IrDetectorRight = 0)

FOR
PULSOUT WheelLeft, PulseLeft
PULSOUT WheelRight, PulseRight
PAUSE 15
NEXT

LOOP 'Main loop

END

Back to top of page

 

Fast IR Roaming and Pulsing and Escape Corners

' -----[ Title ]-----------------------------------------------------------------------
'Robotics with the Boe-Bot - FastIrRoamingAndPulsingAndEscapeCorners.bs2
'Modification of FastIrRoaming (added pulse command).
'
' -----[ Notes ]-----------------------------------------------------------------------
'
'Used rechargeable 1.25V batteries.
'
'The program change from FastIrRoamingAndPulsing.bs2 to the one now shown and is a
'significant improvement. The second problem is now solved.
'
'1st. The Boe-Bot has flipped over in the past. It gets beside a wall
'(angled into it) and a wheel will drive up the side of a wall.
'
'2nd. The Boe-Bot cannot get out of corners.
'
'3rd. It keeps resetting itself. It keeps doing this because there is not enough
'energy to power the Red LED's, servos, emitters, and detectors. Solved this
'problem by turning off the Red LED's.

' -----[ Program ]---------------------------------------------------------------------

'{$STAMP BS2} 'Stamp directive.
'{$PBASIC 2.5} 'PBASIC directive.

DEBUG "Program Running!"

' -----[ Variables/Constants/Pins ]----------------------------------------------------

IrDetectorLeft VAR Bit
IrDetectorRight VAR Bit
PulseLeft VAR Word
PulseRight VAR Word
LoopCount VAR Byte
PulseCount VAR Byte

FreqDetectable CON 38500

Piezospeaker PIN 4 'Speaker
WheelRight PIN 12 'Wheels left/right.
WheelLeft PIN 13
RedLedRight PIN 1 'Red LED's
RedLedLeft PIN 10
IrDetectLeft PIN 9 'Left emitter/detector pair
IrLedLeft PIN 8
IrDetectRight PIN 0 'Right emitter/detector pair
IrLedRight PIN 2

' Leaving corners
CounterRight VAR Nib
CounterLeft VAR Nib

' -----[ Initialization ]--------------------------------------------------------------

FREQOUT Piezospeaker, 2000, 3000 'Signal program start/reset.
CounterLeft = 1
CounterRight = 1

' -----[ Main Routine ]----------------------------------------------------------------

DO 'Main loop

'-----[ Added to FastIrRoamingAndPulsing.bs2 to get out of corners ]-------------------


IF (CounterLeft > 4) AND (CounterRight > 4) THEN
GOSUB Turn_Around
CounterLeft = 1
CounterRight = 1
ENDIF '(CounterLeft > 4) AND (CounterRight > 4)

'-----[ Same as FastIrRoamingAndPulsing.bs2 ]------------------------------------------

FREQOUT IrLedLeft, 1, FreqDetectable 'Store IR detection values.
IrDetectorLeft = IN9

FREQOUT IrLedRight, 1, FreqDetectable
IrDetectorRight = IN0

IF (IrDetectorLeft = 0 ) AND (IrDetectorRight = 0) THEN
'Both LEDs detected.
' HIGH RedLedRight
' HIGH RedLedLeft
PulseCount = 15
PulseLeft = 650 'Back up.
PulseRight = 816
ELSEIF (IrDetectorLeft = 0) THEN 'Left LED detected.
' HIGH RedLedLeft
CounterLeft = CounterLeft + 1
PulseCount = 10
PulseLeft = 850 'Turn Right.
PulseRight = 850
ELSEIF (IrDetectorRight = 0) THEN 'Right LED detected.
' HIGH RedLedRight
CounterRight = counterRight + 1
PulseCount = 10
PulseLeft = 650 'Turn Left.
PulseRight = 650
ELSE 'Go forward.
' LOW RedLedRight
' LOW RedLedLeft
PulseCount = 1
PulseLeft = 850 'No IR detected, go forward.
PulseRight = 684
ENDIF '(IrDetectorLeft = 0 ) AND (IrDetectorRight = 0)

FOR LoopCount = 1 TO PulseCount
PULSOUT WheelLeft, PulseLeft
PULSOUT WheelRight, PulseRight
PAUSE 15
NEXT 'LoopCount = 1 TO PulseCount

LOOP 'Main loop

'------[ Subroutines ]-----------------------------------------------------------------

Turn_Around:

GOSUB Back_Up

FREQOUT Piezospeaker, 2000, 1000 ' Angerey noise.

GOSUB Turn_Left
GOSUB Turn_Left

RETURN 'IF (counter > 4)

Back_Up: ' Back up.
FOR PulseCount = 0 TO 40
PULSOUT WheelLeft, 650
PULSOUT WheelRight, 816
PAUSE 20
NEXT
RETURN 'Turn_Around:

Turn_Left: ' Left turn, about 90-degrees.
FOR PulseCount = 0 TO 22
PULSOUT WheelLeft, 650
PULSOUT WheelRight, 650
PAUSE 20
NEXT
RETURN 'Turn_Around:

Back to top of page

 

Roaming with IR and Red LED's

' -----[ Title ]-----------------------------------------------------------------------
' Robotics with the Boe-Bot - RoamingWithIrAndRedLeds.bs2

'------[ Notes ]-----------------------------------------------------------------------

'Adapt ReamingWithWhiskers.bs2 for use with IR pairs.

'Used rechargeable 1.25V batteries.

'This is the best roaming program using IR detectors/emitters in chapter 7.

'------[ Program ]----------------------------------------------------------------------

' {$STAMP BS2} ' Stamp directive.
' {$PBASIC 2.5} ' PBASIC directive.

DEBUG "Program Running!"

' -----[ Variables/Constants/Pins ]----------------------------------------------------

IrDetectorLeft VAR Bit
IrDetectorRight VAR Bit
PulseCount VAR Byte

FreqDetectable CON 38500

Piezospeaker PIN 4 ' Speaker
WheelRight PIN 12 ' Wheels left/right.
WheelLeft PIN 13
RedLedRight PIN 1 ' Red LED's
RedLedLeft PIN 10
IrDetectLeft PIN 9 ' Left emitter/detector pair
IrLedLeft PIN 8
IrDetectRight PIN 0 ' Right emitter/detector pair
IrLedRight PIN 2

' -----[ Initialization ]--------------------------------------------------------------

FREQOUT Piezospeaker, 2000, 3000 ' Signal program start/reset.

' -----[ Main Routine ]----------------------------------------------------------------

DO

FREQOUT IrLedLeft, 1, FreqDetectable ' Store IR detection values.
IrDetectorLeft = IN9

FREQOUT IrLedRight, 1, FreqDetectable
IrDetectorRight = IN0

IF (IrDetectorLeft = 0 ) AND (IrDetectorRight = 0) THEN
' Both LED detected.
HIGH RedLedRight 'Not enough power to run lights
HIGH RedLedLeft 'and servos and emitters/detectors.
PAUSE 1000
LOW RedLedRight
LOW RedLedLeft
GOSUB Back_Up ' Back up and U-turn (left twice).
GOSUB Turn_Left
GOSUB Turn_Left
ELSEIF (IrDetectorLeft = 0) THEN ' Left LED detected.
HIGH RedLedLeft
GOSUB Back_Up ' Back up and turn right.
GOSUB Turn_Right
ELSEIF (IrDetectorRight = 0) THEN ' Right LED detected.
HIGH RedLedRight
GOSUB Back_Up ' Back up and turn left.
GOSUB Turn_Left
ELSE
LOW RedLedRight
LOW RedLedLeft
GOSUB Forward_Pulse ' No IR detected, go forward.
ENDIF
LOOP ' Check again.

' -----[ Subroutines ]-----------------------------------------------------------------

Forward_Pulse: ' Send a single forward pulse.
PULSOUT WheelLeft, 850
PULSOUT WheelRight, 682
PAUSE 20
RETURN

Turn_Left: ' Left turn, about 90-degrees.
FOR PulseCount = 0 TO 22
PULSOUT WheelLeft, 650
PULSOUT WheelRight, 650
PAUSE 20
NEXT
RETURN

Turn_Right: ' Right turn, about 90-degrees.
FOR PulseCount = 0 TO 21
PULSOUT WheelLeft, 850
PULSOUT WheelRight, 850
PAUSE 20
NEXT
RETURN

Back_Up: ' Back up.
FOR PulseCount = 0 TO 40
PULSOUT WheelLeft, 650
PULSOUT WheelRight, 816
PAUSE 20
NEXT
RETURN

Back to top of page


Chapter 8

 

Display Both Distances with LED's

'------[ Title ]-----------------------------------------------------------------------
'Robotics with the Boe-Bot - DisplayBothDistancesWithLeds.bs2
'Test IR detector distance responses of both emitter/detector pairs to
'frequency sweep.
'
'------[ Notes ]-----------------------------------------------------------------------
'
'Used rechargeable 1.25V batteries.

'------[ Program ]---------------------------------------------------------------------

'{$STAMP BS2} 'Stamp directive.
'{$PBASIC 2.5} 'PBASIC directive.

'------[ Variables/Constants/Pins ]----------------------------------------------------

FreqSelect VAR Nib
IrFrequency VAR Word
IrDetectorLeft VAR Bit
IrDetectorRight VAR Bit
DistanceLeft VAR Nib
DistanceRight VAR Nib

Piezospeaker PIN 4 'Speaker
WheelRight PIN 12 'Wheels right/left.
WheelLeft PIN 13
RedLedRight PIN 1 'Red LED's
RedLedLeft PIN 10
IrDetectLeft PIN 9 'Left emitter/detector pair
IrLedLeft PIN 8
IrDetectRight PIN 0 'Right emitter/detector pair
IrLedRight PIN 2

'------[ Initialization ]--------------------------------------------------------------

DEBUG CLS,
"IR Object Zone", CR,
"Left Right", CR,
"----- -----"

'------[ Main Routine ]----------------------------------------------------------------

DO

GOSUB Get_Distances
GOSUB Display_Distances

LOOP

'------[ Subroutine - Get_Distances ]--------------------------------------------------

Get_Distances:

DistanceLeft = 0
DistanceRight = 0

FOR FreqSelect = 0 TO 4

LOOKUP FreqSelect, [37500,38250,39500,40500,41500], IrFrequency

FREQOUT IrLedLeft, 1, IrFrequency
IrDetectorLeft = IN9
DistanceLeft = DistanceLeft + IrDetectorLeft

FREQOUT IrLedRight, 1, IrFrequency
IrDetectorRight = IN0
DistanceRight = DistanceRight + IrDetectorRight

IF (IrDetectorRight = 0) THEN
HIGH RedLedRight
ELSEIF (IrDetectorLeft = 0) THEN
HIGH RedLedLeft
ENDIF

PAUSE 100

LOW RedLedRight
LOW RedLedLeft

NEXT 'FreqSelect = 0 TO 4

RETURN

'------[ Subroutine - Display_Distances ]----------------------------------------------

Display_Distances:

DEBUG CRSRXY, 2, 3, DEC1 DistanceLeft,
CRSRXY, 9, 3, DEC1 DistanceRight

RETURN

Back to top of page

 

Test Left Frequency Sweep with LED's

'------[ Title ]-----------------------------------------------------------------------
'Robotics with the Boe-Bot - TestLeftFrequencySweepWithLeds.bs2
'Test IR detector distance responses to frequency sweep.
'
'------[ Notes ]-----------------------------------------------------------------------
'
'Used rechargeable 1.25V batteries.

'------[ Program ]---------------------------------------------------------------------

'{$STAMP BS2} 'Stamp directive.
'{$PBASIC 2.5} 'PBASIC directive.

'------[ Variables/Constants/Pins ]----------------------------------------------------

FreqSelect VAR Nib
IrFrequency VAR Word
IrDetect VAR Bit
Distance VAR Nib

Piezospeaker PIN 4 'Speaker
WheelRight PIN 12 'Wheels right/left.
WheelLeft PIN 13
RedLedRight PIN 1 'Red LED's
RedLedLeft PIN 10
IrDetectLeft PIN 9 'Left emitter/detector pair
IrLedLeft PIN 8
IrDetectRight PIN 0 'Right emitter/detector pair
IrLedRight PIN 2

'------[ Initialization ]--------------------------------------------------------------

DEBUG CLS,
" Object", CR,
"Frequency Detected", CR,
"--------- --------"

'------[ Main Routine ]----------------------------------------------------------------

DO 'Main loop

Distance = 0

FOR FreqSelect = 0 TO 4

LOOKUP FreqSelect, [37500,38250,39500,40500,41500], IrFrequency
FREQOUT IrLedLeft, 1, IrFrequency
IrDetect = IN9
Distance = Distance + IrDetect

DEBUG CRSRXY, 4, (FreqSelect + 3), DEC5 IrFrequency,
CRSRXY, 11, (FreqSelect + 3)

IF (IrDetect = 0) THEN
DEBUG "Yes"
HIGH RedLedRight
HIGH RedLedLeft
ELSE
DEBUG "No "
ENDIF

PAUSE 100

LOW RedLedRight
LOW RedLedLeft

NEXT 'FreqSelect = 0 TO 4

DEBUG CR,
"--------- --------", CR,
"Zone ", DEC1 Distance

LOOP 'Main loop

Back to top of page

 

Following Boe-Bot with LED's

'------[ Title ]-----------------------------------------------------------------------
'Robotics with the Boe-Bot - FollowingBoeBotWithLeds.bs2
'Boe-Bot adjusts its position to keep objects it detects in zone (SetPoint).
'
'------[ Notes ]-----------------------------------------------------------------------
'
'Used rechargeable 1.25V batteries.
'
'Variables/Constants/Pins section:
'1. Kpl and Kpr: Constants go from 15 to 50, the lower the value the slower
' the Boe-Bot moves and turns.
'2. SetPoint: 1 or 2 are the best values because its closer to the Boe_Bot, my
' Boe-Bot's detecting range is on the far side, so having a small number makes
' it easier to navigate.
'3. DistanceLeft and DistanceRight store the detected "zone" (see page 271).
'
'Caution the Boe-Bot will restart if it goes from full speed forward to full speed
'backwards because it requires too much energy and the safety features will simply
'kill the power to the Boe-Bot to avoid damage. This can be avoided by simply
'commenting out the IF..THEN statement in the Subroutine Get_Ir_Distances section.

'------[ Program ]---------------------------------------------------------------------

'{$STAMP BS2} 'Stamp directive.
'{$PBASIC 2.5} 'PBASIC directive.

'------[ Variables/Constants/Pins ]----------------------------------------------------

Kpl CON -25
Kpr CON 25
SetPoint CON 1
CenterPulse CON 750

FreqSelect VAR Nib 'For...Next variable
IrFrequency VAR Word 'Stores frequency emitted
IrDetectorLeft VAR Bit 'Stores frequency's detected
IrDetectorRight VAR Bit
DistanceLeft VAR Nib 'Distance detection variables
DistanceRight VAR Nib
PulseRight VAR Word 'Wheel pulse varialbes
PulseLeft VAR Word

Piezospeaker PIN 4 'Speaker
WheelRight PIN 12 'Wheels right/left.
WheelLeft PIN 13
RedLedRight PIN 1 'Red LED's
RedLedLeft PIN 10
IrDetectLeft PIN 9 'Left emitter/detector pair
IrLedLeft PIN 8
IrDetectRight PIN 0 'Right emitter/detector pair
IrLedRight PIN 2

'------[ Initialization ]--------------------------------------------------------------

FREQOUT Piezospeaker, 2000, 3000

'------[ Main Routine ]----------------------------------------------------------------

DO 'Main do loop

GOSUB Get_Ir_Distances 'Get DistanceLeft and DistanceRight values.

'Calculate proportional output.
PulseLeft = (SetPoint - DistanceLeft) * Kpl + CenterPulse
PulseRight = (SetPoint - DistanceRight) * Kpr + CenterPulse

GOSUB Send_Pulse 'Boe-Bot moves according to PulseLeft and PulseRight values.

LOOP 'Main do loop

'------[ Subroutine - Get_Ir_Distances ]-----------------------------------------------

Get_Ir_Distances:

DistanceLeft = 0
DistanceRight = 0

FOR FreqSelect = 0 TO 4

LOOKUP FreqSelect, [37500,38250,39500,40500,41500], IrFrequency

FREQOUT IrLedLeft, 1, IrFrequency
IrDetectorLeft = IN9
DistanceLeft = DistanceLeft + IrDetectorLeft

FREQOUT IrLedRight, 1, IrFrequency
IrDetectorRight = IN0
DistanceRight = DistanceRight + IrDetectorRight

'Added code for LED's
LOW RedLedRight
LOW RedLedLeft

IF (IrDetectorRight = 0) AND (IrDetectorLeft = 0) THEN
HIGH RedLedRight
HIGH RedLedLeft
ELSEIF (IrDetectorRight = 0) THEN
HIGH RedLedRight
ELSEIF (IrDetectorLeft = 0) THEN
HIGH RedLedLeft
ENDIF '(IrDetectorRight = 0) AND (IrDetectorLeft = 0)
'/Added code for LED's

NEXT 'FreqSelect = 0 TO 4

RETURN 'GOSUB Get_Ir_Distances

'------[ Subroutine - Display_Distances ]----------------------------------------------

Send_Pulse:
PULSOUT WheelLeft, PulseLeft
PULSOUT wheelRight, PulseRight
PAUSE 5

RETURN 'GOSUB Send_Pulse

Back to top of page


Last updated: 05/25/2010
For More information, contact Aaron Klapheck at aaron.a.klapheck@gmail.com
http://www.aaronklapheck.com