Shop OBEX P1 Docs P2 Docs Learn Events
Boe-Bot Ping more often — Parallax Forums

Boe-Bot Ping more often

mikevmikev Posts: 23
edited 2012-03-20 20:07 in Robotics
Hello, new guy here as always. I recieved my boe bot with ping sensor and bracket recently and all is going well. The boe bot roams the house well enough. My question is how to pulse the ping sensor while the boe bot is in motion. I have changed the counter in the code so the sensor will ping more often yet its only once per forward cycle...(if that makes sense..) I have also shortened the forward pulse so that it stops more often to scan. On forward pulses over say 75 the boe bot will run into the wall and will not stop until the cylce is complete and then scan. I understand the stamp only does one command at a time, just really fast. Id like to make it like the parallax videos where you can see the ping sensor constantly pinging without so called stoping... Also I would like to have it stop before it hits the wall. I have googled and searched the forums to no avail. I apologize ahead of time if I have missed something. Thank you for any responses.
' =========================================================================
'
'
'   {$STAMP BS2}
'   {$PBASIC 2.5}

' -----[ I/O Definitions ]-------------------------------------------------
Piezo           PIN     4               ' Piezo Speaker
RightServo      PIN     12              ' Right Servo
LeftServo       PIN     13              ' Left Servo
PingServo       PIN     14              ' PING))) Servo
Ping            PIN     15              ' PING))) Sensor

' -----[ Variables ]-------------------------------------------------------
pulseCount      VAR     Byte            ' Used For Measuring Turns
distance        VAR     Word            ' Current Distance Of Object
oldDistance     VAR     Word            ' Old Distance Value
counter         VAR     Word            ' PING))) Cycle Counter
task            VAR     Nib             ' Current Task

' -----[ Initialization ]--------------------------------------------------
FREQOUT Piezo, 2000, 3000               ' Signal Program Start/Reset

' -----[ Program Code ]----------------------------------------------------
Main:
DO
  counter = counter + 1                 ' Increment Passive Counter
  IF counter > 1 THEN                  ' Wait For 1 Servo Pulses
    GOSUB Ping_Out                      ' Activate PING)))
  ENDIF
  IF (distance > 30) THEN               ' Is Object Farther Than 30 cm?
    GOSUB Forward_Pulse                 ' If Yes Go Forward
  ELSE
    GOSUB Ping_Around                   ' Otherwise Scan For Clear Path
  ENDIF
LOOP

' -----[ Subroutines ]-----------------------------------------------------
Forward_Pulse:                          ' Send A Single Forward Pulse
 FOR pulseCount = 1 TO 75
  PULSOUT PingServo, 765                ' Ping Servo Forward Pulse Value
  PULSOUT LeftServo, 850                ' Left Servo Forward Pulse Value
  PULSOUT RightServo, 650               ' Right Servo Forward Pulse Value
  PAUSE 20                               ' Refresh Delay
 NEXT
 RETURN
' *************************************************************************
' * USE THE APPROPRIATE PULSOUT VALUES TO MAKE YOUR BOE-BOT TURN LEFT 90  *
' * DEGREES.  USE THE SAME VALUE AS ABOVE FOR THE PING))) BRACKET SERVO.  *
' *************************************************************************
Turn_Left:                              ' Left Turn, About 45 Degrees
  FOR pulseCount = 0 TO 7               ' Number Of Pulses To Turn
    PULSOUT PingServo, 765              ' Ping Servo Forward Pulse Value
    PULSOUT LeftServo, 650              ' Left Servo Left Pulse Value
    PULSOUT RightServo, 650             ' Right Servo Left Pulse Value
    PAUSE 20                            ' Refresh Delay
  NEXT
  RETURN
' *************************************************************************
' * USE THE APPROPRIATE PULSOUT VALUES TO MAKE YOUR BOE-BOT TURN RIGHT 90 *
' * DEGREES.  USE THE SAME VALUE AS ABOVE FOR THE PING))) BRACKET SERVO.  *
' *************************************************************************
Turn_Right:                             ' Right Turn, About 45 Degrees
  FOR pulseCount = 0 TO 7               ' Number Of Pulses To Turn
    PULSOUT PingServo, 765              ' Ping Servo Forward Pulse Value
    PULSOUT LeftServo, 850              ' Left Servo Right Pulse Value
    PULSOUT RightServo, 850             ' Right Servo Right Pulse Value
    PAUSE 20                            ' Refresh Delay
  NEXT
  RETURN
' *************************************************************************
' * USE THE APPROPRIATE PULSOUT VALUES TO MAKE YOUR BOE-BOT MOVE BACKWARD *
' * WHILE THE PING))) IS FACING FORWARD.                                  *
' *************************************************************************
Back_Up:                                ' Back Up
  FOR pulseCount = 0 TO 40              ' Number Of Pulses To Backup
    PULSOUT PingServo, 765              ' Ping Servo Forward Pulse Value
    PULSOUT LeftServo, 650              ' Left Servo Backup Pulse Value
    PULSOUT RightServo, 850             ' Right Servo Backup Pulse Value
    PAUSE 20                            ' Refresh Delay
  NEXT
  RETURN
Ping_Out:                               ' PING)))
  counter = 0                           ' Reset Passive Delay Counter
  LOW Ping                              ' Force PING))) Line Low
  PULSOUT Ping, 5                       ' Activate PING))) Pulse
  PULSIN  Ping, 1, distance             ' Receive Return Pulse
  distance = distance ** 2257           ' Calculate Distance
  RETURN
Ping_Around:                            ' Start 180 Degree Pan-Scan
  counter = 0                           ' Reset Passive Delay Counter
  oldDistance = 30                      ' Current Old Distance Values
  task = 0                              ' Current Task Priority
' *************************************************************************
' * USE THE APPROPRIATE PULSOUT VALUE TO MAKE YOUR PING)))                *
' * TURN 90 DEGREES LEFT.                                                 *
' *************************************************************************
  FOR pulseCount = 0 TO 20              ' Number Of Pulses To Spin
    LOW Ping                            ' Force PING))) Line Low
    PULSOUT PingServo, 1260             ' Ping Servo 90 Left Pulse Value
    PULSOUT Ping, 5                     ' Activate PING)))
    PULSIN  Ping, 1, distance           ' Receive Distance Value
    PAUSE 20                            ' Refresh Delay
  NEXT
  distance = distance ** 2257           ' Calculate Distance In cm
  IF distance > oldDistance THEN        ' Is distance > Last Clear Path
    oldDistance = distance              ' Update oldDistance Value
    task = 1
  ENDIF
' *************************************************************************
' * USE THE APPROPRIATE PULSOUT VALUE TO MAKE YOUR PING)))                *
' * TURN 45 DEGREES LEFT.                                                 *
' *************************************************************************
  FOR pulseCount = 0 TO 20              ' Number Of Pulses To Spin
    LOW Ping                            ' Force PING))) Line Low
    PULSOUT PingServo, 850              ' Ping Servo 45 Left Pulse Value
    PULSOUT Ping, 5                     ' Activate PING)))
    PULSIN  Ping, 1, distance           ' Receive Distance Value
    PAUSE 20                            ' Refresh Delay
  NEXT
  distance = distance ** 2257           ' Calculate Distance In cm
  IF distance > oldDistance THEN        ' Is distance > Last Clear Path
    oldDistance = distance              ' Update oldDistance Value
    task = 2
  ENDIF
' *************************************************************************
' * USE THE APPROPRIATE PULSOUT VALUE TO MAKE YOUR PING)))                *
' * TURN 45 DEGREES RIGHT.                                                *
' *************************************************************************
  FOR pulseCount = 0 TO 20              ' Number Of Pulses To Spin
    LOW Ping                            ' Force PING))) Line Low
    PULSOUT PingServo, 400              ' Ping Servo 45 Right Pulse Value
    PULSOUT Ping, 5                     ' Activate PING)))
    PULSIN  Ping, 1, distance           ' Receive Distance Value
    PAUSE 20                            ' Refresh Delay
  NEXT
  distance = distance ** 2257           ' Calculate Distance In cm
  IF distance > oldDistance THEN        ' Is distance > Last Clear Path
    oldDistance = distance              ' Update oldDistance Value
    task = 3
  ENDIF
' *************************************************************************
' * USE THE APPROPRIATE PULSOUT VALUE TO MAKE YOUR PING)))                *
' * TURN 90 DEGREES RIGHT.                                                *
' *************************************************************************
  FOR pulseCount = 0 TO 20              ' Number Of Pulses To Spin
    LOW Ping                            ' Force PING))) Line Low
    PULSOUT PingServo, 300              ' Ping Servo 90 Right Pulse Value
    PULSOUT Ping, 5                     ' Activate PING)))
    PULSIN  Ping, 1, distance           ' Receive Distance Value
    PAUSE 20                            ' Refresh Delay
  NEXT
  distance = distance ** 2257           ' Calculate Distance In cm
  IF distance > oldDistance THEN        ' Is distance > Last Clear Path
    oldDistance = distance              ' Update oldDistance Value
    task = 4
  ENDIF
  ON task GOSUB Task0, Task1, Task2, Task3, Task4
  distance = 50                         ' Prevent Scan From Looping
  RETURN
Task0:                                  ' Forward Was Clearest Path
  GOSUB Turn_Right                      ' This Could Mean Narrow Path
  GOSUB Turn_Right                      ' So We'll Turn Around
  GOSUB Turn_Right                      ' You Can Change The Behavior
  GOSUB Turn_Right                      ' Of Any Of The Tasks
  RETURN
Task1:                                  ' 90 Degrees Left Was Clearest
  GOSUB Turn_Left
  GOSUB Turn_Left
  RETURN
Task2:                                  ' 45 Degrees Left Was Clearest
  GOSUB Turn_Left
  RETURN
Task3:                                  ' 45 Degrees Right Was Clearest
  GOSUB Turn_Right
  RETURN
Task4:                                  ' 90 Degrees Right Was Clearest
  GOSUB Turn_Right
  GOSUB Turn_Right
  RETURN

Comments

  • photomankcphotomankc Posts: 943
    edited 2012-03-20 06:51
    If you can instead of a simple delay in the drive functions you could instead do a PING subroutine between servo refresh cycle. Now, how do you make the cycle predictable and regular given the varible delay the PING introduces.... not sure. There is a reason I love programming the Propeller and have not used my BS since I put it away 3 years ago. Maybe someone with more BS prowess has a good way to do that. The PING has a maximum duration of just under 20ms by the documentation so it should work but as you get closer to an object your servo refresh will get faster. Not sure if there is a maximum refresh rate.

    The other way I can see is to not simply react to close objects but instead actually store the value you get from your PING in front of you and then use that value to decide how far to drive forward conservatively before you stop and look around again. This way you don't have to run the PING between servo refreshes instead you use it to measure out your movement before-hand. Then you stop look around and if the forward distance is less than your threshold you scan for a new direction to move.
  • mikevmikev Posts: 23
    edited 2012-03-20 11:31
    Ok. I called Parallax tech support and found.... I ordered a Servo Pal. Im gonna try to free up the BS2 (because it only does one commanded at a time, but we knew that). While the servo pal is mashing the wheels the BS2 is gonna ping the sensor. I will post how it goes. Also if a moderator see this, or anyone with power, can you change the title from unsolved to solved? I tried and could not get it. Thanks!
  • Matt GillilandMatt Gilliland Posts: 1,406
    edited 2012-03-20 20:07
    Hi mikev- Welcome to the Forums.
    Glad you got an answer you're looking for :thumb:
    -Matt
Sign In or Register to comment.