Shop OBEX P1 Docs P2 Docs Learn Events
PING sensor array and HB-25 motor controller — Parallax Forums

PING sensor array and HB-25 motor controller

sebogawasebogawa Posts: 1
edited 2008-12-15 06:01 in BASIC Stamp
'there are five ping sensors in a row in the front of the robot each with its assigned position , and etc
'the motor controller i used were two HB-25 motor controllers each individually wired to its own motor.
'the robot has ample external power and all the motors respond to the example code but i cant get the select case
'to work for the different variables. i would greatly appretiate any help. Thank you very much. [noparse]:)[/noparse]

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

timeFarRight VAR Word
timeRight VAR Word
timeMid VAR Word
timeLeft VAR Word
timeFarLeft VAR Word
speed VAR Byte
distFarLeft VAR Byte
distLeft VAR Byte
distRight VAR Byte
distFarRight VAR Byte
distMid VAR Byte
index VAR Word

leftWheel PIN 9
rightWheel PIN 8
farLeft PIN 0
farRight PIN 12
left PIN 15
right  PIN 13
mid PIN 14

DO : LOOP UNTIL leftWheel = 1                ' Wait For HB-25 Power Up
LOW leftWheel                                ' Make I/O Pin Output/Low
PAUSE 5                                 ' Wait For HB-25 To Initialize
PULSOUT leftWheel, 750                       ' Stop Motor 1

DO : LOOP UNTIL rightWheel = 1                ' Wait For HB-25 Power Up
LOW rightWheel                                ' Make I/O Pin Output/Low
PAUSE 5                                 ' Wait For HB-25 To Initialize
PULSOUT rightWheel, 750


DO
GOSUB distance
LOOP

distance:
PULSOUT farLeft, 5
PULSIN farLeft, 1, timeFarLeft
timeFarLeft = timeFarLeft ** 2251
distFarLeft = timeFarLeft

PULSOUT left, 5
PULSIN left, 1, timeLeft
timeLeft = timeLeft ** 2251
distLeft = timeLeft

PULSOUT mid, 5
PULSIN mid, 1, timeMid
timeMid = timeMid ** 2251
distMid = timeMid

PULSOUT right, 5
PULSIN right, 1, timeRight
timeRight = timeRight ** 2251
distRight = timeRight

PULSOUT farRight, 5
PULSIN farRight, 1, timeFarRight
timeFarRight = timeFarRight ** 2251
distFarRight = timeFarRight

DEBUG HOME, "Left wheel: ", DEC4 distFarLeft, " cm",
CR, "Body Left: ", DEC4 distLeft, " cm",
CR, "Center Distance: ", DEC4 distMid, " cm",
CR, "Body Right: ", DEC4 distRight, " cm",
CR, "Right wheel: ", DEC4 distFarRight, " cm"
RETURN


brickWall:
FOR index = 0 TO 250
PULSOUT leftWheel, 750 + index
PAUSE 1
PULSOUT rightWheel, 750 - index
NEXT
PAUSE 2000
IF (distFarLeft>12 AND distLeft AND distMid>12 AND distRight>12 AND distFarRight) THEN
GOSUB brickWall
ELSE
RETURN
ENDIF
obstLeft:
PAUSE 100   'ill figure this one out later i just need response to distance first
RETURN

obstRight:
PAUSE 100  'same as above
RETURN




and this is the code for the HB-25 motor contorller example:

' =========================================================================
'   File...... HB-25 Motor Test.bs2
'   Purpose... Tests One Or Two HB-25's Connected To P15
'   Author.... Parallax, Inc.
'   E-mail.... support@parallax.com
'   Updated... 01-18-2006
'
'   {$STAMP BS2}
'   {$PBASIC 2.5}

' -----[noparse][[/noparse] Program Description ]---------------------------------------------

' This program tests the HB-25 by waiting for it to power up, then pulsing
' the output to ramp the motors up in opposite directions, wait 3 seconds
' then ramp them back down to a stopped position.  While the code is
' written for two HB-25/motors you can use it with just one by commenting
' out or removing the lines for the second motor, commented below.  If you
' have two HB-25/motors connected, remember to remove the jumper block from
' the second HB-25.


' -----[noparse][[/noparse] I/O Definitions ]-------------------------------------------------

HB25            PIN     9              ' I/O Pin For HB-25


' -----[noparse][[/noparse] Variables ]-------------------------------------------------------

index           VAR     Word            ' Counter For Ramping


' -----[noparse][[/noparse] Initialization ]--------------------------------------------------

DO : LOOP UNTIL HB25 = 1                ' Wait For HB-25 Power Up
LOW HB25                                ' Make I/O Pin Output/Low
PAUSE 5                                 ' Wait For HB-25 To Initialize
PULSOUT HB25, 750                       ' Stop Motor 1
PAUSE 1                                 ' 1 mS Delay
PULSOUT HB25, 750                       ' Stop Motor 2 (If Connected)
                                        ' The Above 2 Lines May Be Removed
                                        ' If You Are Using Single Mode
' -----[noparse][[/noparse] Program Code ]----------------------------------------------------

Main:
  PAUSE 20                              ' Wait 20 mS Before Ramping
  FOR index = 0 TO 250                  ' Ramp Up To Full Speed
    PULSOUT HB25, 750 + index           ' Motor 1 Forward
    PAUSE 1                             ' 1 mS Delay For Motor 2 Pulse
    PULSOUT HB25, 750 - index           ' Motor 2 Reverse
    PAUSE 20                            ' 20 mS Smoothing Delay
  NEXT
  PAUSE 3000                            ' Wait 3 Seconds
  FOR index = 250 TO 0                  ' Ramp Back Down
    PULSOUT HB25, 750 + index           ' Motor 1 Forward Slowing
    PAUSE 1                             ' 1 mS Delay For Motor 2
    PULSOUT HB25, 750 - index           ' Motor 2 Reverse Slowing
    PAUSE 20                            ' 20 mS Smoothing Delay
  NEXT
  STOP                                  ' Use STOP To Prevent State Change

Comments

  • SRLMSRLM Posts: 5,045
    edited 2008-12-15 06:01
    I'm not quite sure from your comments of what you are looking for, but I did notice that you never call the brickwall function. You may want to add that in your main program loop. Also, you are trying to use recursion in the brick wall function, and that probably won't work the way that you want it. The BS2 has a maximum number of nested GOSUBs, and it's fairly limited when trying to do recursion (8 GOSUBS maybe? I don't remember). Anyway, bad things can happen (read unexpected) if you have too many.
Sign In or Register to comment.