PDA

View Full Version : Nesting loops in Boe-Bot whisker navigation



Robostooge
01-23-2006, 08:36 AM
Greetings. We are new to Basic Stamp and the forum. We are going through the Boe-Bot Manual, and have a question about using whisker navigation. We used the code from the manual (Example Program: RoamingWithWhiskers.bs2, page 192 of the Acrobat version). The whisker navigation works great, and we decided to add flashing LED’S. (LED’S continually flash as the Boe- Bot moves)
·
We used the following code to flash LED’s
·
Do
High 1························· ‘LED ·1 on
High 10······················· ‘LED 2 on
Pause 500
Low 1························· ‘LED off
High 10······················· ‘LED off
Pause 500
Loop
·
This code works on it’s own, but we can’t seem to figure out where to put it without it messing up the main program or the subroutines.
·
In other words, we want the flashing LED subroutine to be continuously executed as the whisker decision trees for the navigation work independently.
·
We’ve tried it at every place in the program we can think of, and it messes up the subroutines.
·
Are we missing something basic or is this actually more complicated to do than we think?
·
PS we did not write out the code from the book to save space, but can if that will help get the question answered.

Any ideas?

Many thanks

Andy Lindsay (Parallax)
01-23-2006, 10:40 AM
Later in the book, example programs that send servo pulses from only once per main loop will make it easier to add this feature.·

Recall from Chapter 2, Activity #6 that servo pulses repeat about every 25 ms.· To delay for 0.5 seconds, this program's Flash_Leds subroutine·counts up to 20 servo pulses (20 x .025 s = 0.5 s), then changes the state of the LEDs.· The Flash_Leds subroutine is called from everywhere the program delivers a servo pulse.· More importantly, the program is doing it from inside the various FOR...NEXT loops.· If those Flash_Leds subroutine calls were outside the FOR...NEXT loops, there would be interruptions in the flashing every time a maneuver was executed.

BTW, another interesting modification is to light up the left LED while the Boe-Bot is avoiding an object on the left, the right LED while the Boe-Bot is avoiding an object on its right, etc.



' -----[ Title ]--------------------------------------------------------------
' Robotics with the Boe-Bot - RoamingWithWhiskers.bs2
' Boe-Bot uses whiskers to detect objects, and navigates around them.

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

DEBUG "Program Running!"

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

pulseCount VAR Byte ' FOR...NEXT loop counter.
counter VAR Byte

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

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

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

DO
IF (IN5 = 0) AND (IN7 = 0) THEN ' Both whiskers detect obstacle
GOSUB Back_Up ' Back up & U-turn (left twice)
GOSUB Turn_Left
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 ' and check again
LOOP

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

Forward_Pulse: ' Send a single forward pulse.
PULSOUT 13,850
PULSOUT 12,650
PAUSE 20
GOSUB Flash_Leds
RETURN

Turn_Left: ' Left turn, about 90-degrees.
FOR pulseCount = 0 TO 20
PULSOUT 13, 650
PULSOUT 12, 650
PAUSE 20
GOSUB Flash_Leds
NEXT
RETURN

Turn_Right:
FOR pulseCount = 0 TO 20 ' Right turn, about 90-degrees.
PULSOUT 13, 850
PULSOUT 12, 850
PAUSE 20
GOSUB Flash_Leds
NEXT
RETURN

Back_Up: ' Back up.
FOR pulseCount = 0 TO 40
PULSOUT 13, 650
PULSOUT 12, 850
PAUSE 20
GOSUB Flash_Leds
NEXT
RETURN

Flash_Leds:

counter = counter + 1
IF counter = 20 THEN
counter = 1
TOGGLE 1
TOGGLE 10
ENDIF

RETURN


Post Edited (Andy Lindsay (Parallax)) : 1/23/2006 4:05:00 AM GMT

Robostooge
01-24-2006, 05:14 AM
Thank you for your advice. I used the program you listed above and now my Boe-Bot is roaming around the room with flashing LED's!http://forums.parallax.com/images/smilies/smilewinkgrin.gif