Shop OBEX P1 Docs P2 Docs Learn Events
Need bumper and ir help ? — Parallax Forums

Need bumper and ir help ?

alleykatalleykat Posts: 9
edited 2007-02-01 19:14 in BASIC Stamp
I am currently using the parallax emitter/detector pair and two microswitches for collision avoidance and
collision detection·for my robot. I was wondering if threre is a more efficient way to test the IR·and bumper
sensors than what I am doing ? What I want the robot to do is


irdetectleft and irdetectright = 0 then, back and turn right
irdetectleft = 0 then, turn right
irdetectright = then, turn left

bumpswitch (IN6 =0), back and turn right
bumpswitch (IN7=0), back and turn left

If IR and micro-switches detect nothing, go forward

' {$STAMP BS2}
' {$PBASIC 2.5}
irDetectLeft VAR Bit
irDetectRight VAR Bit
pulseLeft VAR Word
pulseRight VAR Word
I VAR Byte
pulseCount VAR Byte
DO
GOSUB IR_TEST
FOR I = 1 TO 30
GOSUB Pulsout_Move
NEXT
GOSUB TEST_BUMP
LOOP
IR_TEST:
FREQOUT 3, 1, 38500
irDetectLeft = IN2
FREQOUT 1, 1, 38500
irDetectRight = IN0
IF (irDetectLeft = 0) AND (irDetectRight = 0) THEN
HIGH 4
HIGH 5
pulseLeft = 650
pulseRight = 850
ELSEIF (irDetectLeft = 0) THEN
HIGH 5
pulseLeft = 850
pulseRight = 850
ELSEIF (irDetectRight = 0) THEN
HIGH 4
pulseLeft = 650
pulseRight = 650
ELSE
LOW 4
LOW 5
pulseLeft = 850
pulseRight = 650
ENDIF
RETURN
PULSOUT_MOVE:
PULSOUT 12,pulseLeft
PULSOUT 13,pulseRight
PAUSE 15
RETURN
TEST_BUMP:
IF (IN6 = 0) THEN
GOSUB Back_Up
GOSUB Turn_Right
ELSEIF (IN7 = 0) THEN
GOSUB Back_Up
GOSUB Turn_Left
ELSE
GOSUB Forward_Pulse
ENDIF
RETURN
·Forward_Pulse:
PULSOUT 12,850
PULSOUT 13,650
PAUSE 20
RETURN
Turn_Left:
FOR pulseCount = 0 TO 20
PULSOUT 12, 650
PULSOUT 13, 650
PAUSE 20
NEXT
RETURN
Turn_Right:
FOR pulseCount = 0 TO 20
PULSOUT 12, 850
PULSOUT 13, 850
PAUSE 20
NEXT
RETURN
Back_Up:
FOR pulseCount = 0 TO 40
PULSOUT 12, 650
PULSOUT 13, 850
NEXT
RETURN

Post Edited (alleykat) : 1/30/2007 2:26:25 AM GMT

Comments

  • metron9metron9 Posts: 1,100
    edited 2007-02-01 19:14
    You should use more spaces in your code. Put spaces around IF and ENDIF blocks and around subroutines entry and return. Your gosub IR_TEST performs logic to move the robot, it should just do what the function suggests and read the IR state and return the state to the main program or global variables. Also comment your code blocks so you and others can see exactly what the code does without having to actually read the code. comments like 'MOVE FORWARD at the start of a block of code allows one to understand the code without reading the details of pulsout etc.

    After you do this, you and others will be able to see much more clearly the logic flow and perhaps tweek the code to make it more efficient. As it stands now it looks like a bowl of spaghetti and is perhaps the reason the post moved to page 2 of posts without an answer.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think Inside the box first and if that doesn't work..
    Re-arrange what's inside the box then...
    Think outside the BOX!
Sign In or Register to comment.