Shop OBEX P1 Docs P2 Docs Learn Events
A more efficient way to improve code — Parallax Forums

A more efficient way to improve code

alleykatalleykat Posts: 9
edited 2007-02-20 20:41 in BASIC Stamp
I am currently using·an 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 micro
switches·than what I am doing ?


What I want the robot to do is :
Move forward·until the·ir detectors sense an object, move left or right( depending on ir sensors),
then continue forward. If the microswitces come in contact with an object move right or left(depending on·microswitches),then·continue forward.
·
' {$STAMP BS2}
' {$PBASIC 2.5}

irDetectLeft VAR Bit······················ ; define left detector
irDetectRight VAR Bit····················· ;·define right detector
pulseLeft VAR Word······················· ; define· left servo
pulseRight VAR Word······················ ;·define right servo
I VAR Byte····································; define I
pulseCount VAR Byte····················· ; define pulseCount


'
[noparse][[/noparse] Main Routine ]

DO··········································· ; Start loop
GOSUB IR_TEST·························· ; Test IR detectors

FOR I = 1 TO 30························· ;·Added because robot jerked back and forth when both detectors saw an
GOSUB Pulsout_Move····················· object. The I value controls how fast or slow the program loops.
NEXT

GOSUB TEST_BUMP····················· ;· Test microswitches
LOOP·········································; Continue loop
·
IR_TEST:
irDetectLeft = IN2··················································· ;·left detector to input 2
irDetectRight = IN0················································· ; right detector to input 3
IF (irDetectLeft = 1) AND (irDetectRight = 1) THEN······· ; Test left and right detectors
GOSUB back_up····················································· ;·Move back
GOSUB left_turn················································· ··· ; Move left
GOSUB left_turn
···········
ELSEIF (irDetectLeft = 1) THEN································· ; Test left detector
pulseLeft = 850······················································
pulseRight = 850

ELSEIF (irDetectRight = 1) THEN································ ; Test right detector
pulseLeft = 650
pulseRight = 650

ELSE
pulseLeft = 850·······················································; Move forward
pulseRight = 650
ENDIF
RETURN

PULSOUT_MOVE:···················································· ; This is used to apply the pulse
PULSOUT 12,pulseLeft
PULSOUT 13,pulseRight
PAUSE 15
RETURN
TEST_BUMP:
IF (IN6 = 0) THEN·····················; Test left microswitch
GOSUB Back_Up·······················; Move back
GOSUB Turn_Right··················· ; Move right

ELSEIF (IN7 = 0) THEN············· ; Test right microswitch
GOSUB Back_Up······················ ; Move back
GOSUB Turn_Left···················· ; Move left

ELSE
GOSUB Forward_Pulse············· ; Move forward
ENDIF
RETURN


Subroutines
·Forward_Pulse:
PULSOUT 12,850
PULSOUT 13,650
PAUSE 20
NEXT
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

<!-- Edit -->

Comments

  • Steph LindsaySteph Lindsay Posts: 767
    edited 2007-02-20 20:41
    Hi AlleyKat,

    If you want to read about some efficient coding techniques for integrating feedback from several sensors on a rolling robot, check out Applied Robotics with the SumoBot (download free pdf and code samples here[noparse]:)[/noparse]

    http://www.parallax.com/detail.asp?product_id=27403

    The expamples are for a SumoBot using 6 sensors, but the concepts still apply.· Chapter 3 focuses on sensor management, and Chapter 4 on navigation, using sensor flags to choose navigation states. The trick is to use a byte variable as a sensor register in which you·store the state of each sensor in one bit, then making navigation decisions based on the value of the byte.· This allows the state of all the sensors to be checked and considered in combination each time before a motion command is selected and executed.·

    -Stephanie Lindsay

    Editor, Parallax Inc.
Sign In or Register to comment.