Shop OBEX P1 Docs P2 Docs Learn Events
Navigating with Ultrasonic Distance Sensor — Parallax Forums

Navigating with Ultrasonic Distance Sensor

PPraPPra Posts: 10
edited 2014-04-06 13:22 in General Discussion
So for my physics project, my partner and I wanted to use an ultrasonic distance sensor to navigate a maze. The problem is, we can only read the distance between the robot and an obstacle, and the sensor plays no part in navigating the robot. How would I program it so that the robot could manuever based on its readings from the sensor? I'm completely new to programming, sorry. Is there a way to program the robot so it doesn't have to be attached to the CPU to display the distance readings? Do we have to buy the mounting bracket, or should the sensor work by itself, without having to rotate around? Thanks in advance!!

Comments

  • PPraPPra Posts: 10
    edited 2014-04-05 10:58
    I tried those two downloads, but it seems as if they are for infrared sensors, not ultrasonic distance sensors. The programming just makes the robot just rotate itself around.....it also doesn't display the distances, which we were able to do with just Detecting Distance with the Ultrasonic Ping Sensor file. Is there any way to combine programs,so that the robot can navigate through a maze (where it may have to go to straight for 7 seconds, and then detect an obstacle and be able to maneuver accordingly) and display the distances detected?
  • PublisonPublison Posts: 12,366
    edited 2014-04-05 11:05
    PPra wrote: »
    I tried those two downloads, but it seems as if they are for infrared sensors, not ultrasonic distance sensors. The programming just makes the robot just rotate itself around.....it also doesn't display the distances, which we were able to do with just Detecting Distance with the Ultrasonic Ping Sensor file. Is there any way to combine programs,so that the robot can navigate through a maze (where it may have to go to straight for 7 seconds, and then detect an obstacle and be able to maneuver accordingly) and display the distances detected?

    The first download Franklin linked to, incorporates both the IR and Ping Sensors. This program has been around for quite a while and is well tested.

    http://www.parallax.com/downloads/roaming-ping-advanced-basic-stamp-code

    If you are having problems with the program, you might want to include a picture of how you have it hooked up.
  • PPraPPra Posts: 10
    edited 2014-04-05 11:26
    [video]https://drive.google.com/file/d/0B7VyCta1uRRmWi1QeTV0SjdqQjE0LWNUY3JXQnVyLUpfTmpv/edit?usp=sharing[/video]

    here is a video of my maze and the robot...the robot needs to move forward but it will only rotate around..i am using the basic stamp code roaming with ping, and just pressed play and disconnected the robot after and this is what happens
  • PublisonPublison Posts: 12,366
    edited 2014-04-05 12:41
    PPra wrote: »
    [video]https://drive.google.com/file/d/0B7VyCta1uRRmWi1QeTV0SjdqQjE0LWNUY3JXQnVyLUpfTmpv/edit?usp=sharing[/video]

    here is a video of my maze and the robot...the robot needs to move forward but it will only rotate around..i am using the basic stamp code roaming with ping, and just pressed play and disconnected the robot after and this is what happens

    Try making the walls a little higher. You will have to rely on the IR sensor on the breadboard for walls that low. Is see you do not have IR sensors.
  • PPraPPra Posts: 10
    edited 2014-04-05 13:10
    I placed it in front of one of the higher walls, it just continues to rotate around. any way to make it move straight, and then be able to turn by itself?
  • FranklinFranklin Posts: 4,747
    edited 2014-04-05 14:01
    If you post large, clear photos of your connections and the actual code you loaded on the BS2 perhaps we can help. The actions in the video are not those of a properly functioning bot running roaming with ping.
  • PPraPPra Posts: 10
    edited 2014-04-05 16:40
    onlineexampleconnection.png
    photo (2).JPG


    Here is the photo of the connection as well as the connection we got from the internet we used as an example to set it up. I think our board is unusual, because most of them had the 3 black thingies encircling the white portion (at least in the pictures we looked online), but ours only had black thingies on the top and to the left, not on the bottom (sorry I do not know the correct terminology).
    287 x 305 - 62K
    960 x 720 - 290K
  • PPraPPra Posts: 10
    edited 2014-04-05 16:49
    [video]https://drive.google.com/file/d/0B7VyCta1uRRmRGg2VW80U1ZhVDZSVlotejllOXprRHRKVkJR/edit?usp=sharing[/video]

    Here is the video of the programming...it is basically exactly what I downloaded Roaming with Ping Basic, I believe.
  • PublisonPublison Posts: 12,366
    edited 2014-04-05 16:58
    PPra wrote: »
    [video]https://drive.google.com/file/d/0B7VyCta1uRRmRGg2VW80U1ZhVDZSVlotejllOXprRHRKVkJR/edit?usp=sharing[/video]

    Here is the video of the programming...it is basically exactly what I downloaded Roaming with Ping Basic, I believe.

    attachment.php?attachmentid=78421&d=1297987572


  • PPraPPra Posts: 10
    edited 2014-04-05 17:15
    [/B]' ========================================================================='
    '   File...... RoamingWithPING.bs2
    '   Purpose... Roam And Scan Using PING))) Mounting Bracket
    '   Author.... Parallax, Inc.
    '   E-mail.... support@parallax.com
    '   Started...
    '   Updated... 12-21-2005
    '
    '   {$STAMP BS2}
    '   {$PBASIC 2.5}
    '
    ' =========================================================================
    
    
    
    
    ' -----[ Program Description ]---------------------------------------------
    
    
    ' This programs demonstrates roaming with the Parallax PING))) Mounting
    ' Bracket, which allows you to pan-scan with your Parallax PING))) sensor.
    '
    ' This program assumes you have already set your BOE-Bot up for roaming
    ' with IR sensors.  You must also have the PING))) Mounting bracket
    ' installed as per the instructions, with the servo connected to P14 and
    ' the PING))) sensor connected to P15.
    '
    ' Due to the way the PING))) bracket mounts you may have to trim your IR
    ' detectors down so that they don't stick up in the way of the moving
    ' PING))) bracket as it rotates.  It's best to angle them off at 45
    ' degree angles so that the BOE-Bot doesn't run into anything while
    ' moving along a wall at a narrow angle.
    '
    ' The BOE-Bot will now roam around and when it senses an object, it will
    ' scan within a 180 degree field of view for the clearest path, which it
    ' will then navigate scanning for an object again.  The IR Sensors prevent
    ' hitting a wall when coming at it from a narrow angle where the PING)))
    ' might have trouble seeing it due to the lack of reflection of sound.
    '
    ' Calibration is important to this code working properly.  If you do not
    ' have the correct values for your servos then you may get strange results
    ' from your BOE-Bot.  The necessary calibration information is listed in
    ' each section of the code where it is required.
    
    
    ' -----[ Revision History ]------------------------------------------------
    
    
    ' This code is basically a modified version of the Roaming With IR code
    ' from the Robotics With The BOE-Bot Manual, written by Andy Lindsay.
    ' Much of the original code was left untouched so you can see how it was
    ' altered.
    
    
    
    
    ' -----[ 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 ]-------------------------------------------------------
    
    
    irDetectLeft    VAR     Bit             ' Variable For Left IR Input
    irDetectRight   VAR     Bit             ' Variable For Right IR Input
    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
    
    
      FREQOUT 8, 1, 38500                   ' Emit 38.5 kHz IR To Left
      irDetectLeft = IN9                    ' Store IR Detection Values
    
    
      FREQOUT 2, 1, 38500                   ' Emit 38.5 kHz IR To Right
      irDetectRight = IN0                   ' Store IR Detection Values
    
    
      IF (irDetectLeft = 0) AND (irDetectRight = 0) THEN
        GOSUB Ping_Around                   ' Object Detected via IR Forward
      ELSEIF (irDetectLeft = 0) THEN
        GOSUB Ping_Around                   ' Object Detected via IR Left
      ELSEIF (irDetectRight = 0) THEN
        GOSUB Ping_Around                   ' Object Detected via IR Right
      ENDIF
    
    
      counter = counter + 1                 ' Increment Passive Counter
    
    
      IF counter > 10 THEN                  ' Wait For 10 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 ]-----------------------------------------------------
    
    
    ' *************************************************************************
    ' * USE THE APPROPRIATE PULSOUT VALUES TO MAKE YOUR BOE-BOT MOVE FORWARD  *
    ' * WHILE THE PING))) IS FACING FORWARD.                                  *
    ' *************************************************************************
    
    
    Forward_Pulse:                          ' Send A Single Forward Pulse
      PULSOUT PingServo, 750                ' 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
      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, 750              ' 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, 750              ' 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, 750              ' 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, 1085             ' 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, 225              ' 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 
    [COLOR=#333333] [/COLOR][B]
    

  • FranklinFranklin Posts: 4,747
    edited 2014-04-05 21:03
    This program assumes you have already set your BOE-Bot up for roaming
    ' with IR sensors.
    Did you modify the code so it will work WITHOUT the IR sensors you don't have on your bot?
  • PPraPPra Posts: 10
    edited 2014-04-05 21:12
    Well, that was what I was saying, but Publison said it could work for IR and Ping. So I'm not sure. How would I work it without IR sensors?
  • FranklinFranklin Posts: 4,747
    edited 2014-04-06 05:06
    but Publison said it could work for IR and Ping. So I'm not sure. How would I work it without IR sensors?
    The first link in my previous post expects IR as Publison said. The second link will run without, try it making sure you have the ping on the right pins.
  • Hal AlbachHal Albach Posts: 747
    edited 2014-04-06 07:41
    The original posting states that you want to use an ultrasonic distance sensor to navigate a maze and the sensor plays no part in navigating the robot. ????? Are these your restrictions or those of an assignment?

    Without the IR sensors how are you going to keep the bot from trying to climb the low walls of the maze? Two servos will always run at slightly different speeds, no matter how well calibrated. Also, any slight irregularity of the rolling surface could alter the direction the bot is headed. How will the bot know it is travelling down the center of the corridor and not dragging itself along one side? The IR sensors are there to correct the path the bot is taking so that it does not eventually run up against a side wall. They will also help in deciding which way to turn when reaching a corner since the rigidly mounted ultrasonic sensor will only report distance to whatever obstruction is straight ahead. If you don't have the IR sensors you could also try wire whiskers to help detect the side walls.
  • PPraPPra Posts: 10
    edited 2014-04-06 09:26
    Hal Albach: No, I'm trying to make the sensor play a part in navigating the robot, but so far, I have not found a way to make the sensors do so. I'll make the walls higher if needed cuz I don't have the time to buy IR sensors, and am only planning to use the Ultrasonic Distance Sensors. I just need the robot to move straight, and then when it needs to turn, it will turn. Is that possible?
  • PublisonPublison Posts: 12,366
    edited 2014-04-06 09:35
    PPra wrote: »
    Well, that was what I was saying, but Publison said it could work for IR and Ping. So I'm not sure. How would I work it without IR sensors?

    I did correct my post #6 after I saw your video with no IR sensors.
  • PublisonPublison Posts: 12,366
    edited 2014-04-06 09:36
    As Hal has said, a PING is only going to see what is in front of it. It will not keep it centered. At that point, if you hit a wall you have to make a decision to turn left or right in software.
    I just need the robot to move straight, and then when it needs to turn, it will turn

    You need another set of sensors for that. There is really no way to get around that.
  • twm47099twm47099 Posts: 867
    edited 2014-04-06 13:18
    When I looked at your wiring diagram and the code that you posted, along with your statements that you are not using the IR sensors or the ping)) bracket and ping servo, I saw a few problems.

    1. The Code you posted requires IR sensors (I'm not sure what a input value a disconnected pin has, but you probably don't want that.) The second link to code in post 2 does not require the IR sensors, but does use the bracket & ping servo.

    2. The pin definitions in the code have the ping)) sensor on pin 15. Your wiring diagram has it on pin 0. And the ping servo (which you don't have) is on pin 14 in the code. Make sure that the pin definitions in your code agree with your actual wiring.

    3. Without the ping bracket and servo, the ping will only point straight ahead. So if the BOT approaches a wall diagonally, it will hit the wall and not be able to adjust. With the bracket and servo, the ping would sweep from side to side and you could use those distances and adjust the BOTs direction. You can do the same without the bracket by:
    traveling a short distance forward - stop - rotate the BOT - measure distance - use that value to adjust the bots motion - rotate the bot back to the desired direction - repeat.

    That is significantly more complex code in addition to the maze solving code and much slower travel.

    Hope this helps
    Tom
  • Hal AlbachHal Albach Posts: 747
    edited 2014-04-06 13:22
    "I just need the robot to move straight, and then when it needs to turn, it will turn. Is that possible?"

    Not with a single, fixed mounted Ping. In order for a bot to traverse a maze it has to know what is to its left and right as well as what is up ahead. When the bot approaches a corner, all it knows is that something is in front of it at xx inches. How is the bot going to determine which way to turn? All it "sees" is the wall in front of it. At least with a rotatable Ping you will have angular information along with distance to object. With IR sensors or whiskers the bot will also know where it is relative to the walls and correct its direction of travel to keep it near the center. Also, with the IR sensors or whiskers the bot will orient itself to the new direction of travel after making the turn.
Sign In or Register to comment.