Shop OBEX P1 Docs P2 Docs Learn Events
help with the ping sonar sensor — Parallax Forums

help with the ping sonar sensor

lobolobo Posts: 100
edited 2007-03-20 14:13 in BASIC Stamp
I'm trying to get both of my ping sensors to work at the same time but i'm having no success. I already got one to work with my 4 hb-25 and 4 motors, but i dont know how to modify the program to use 2·ping sensors together to control the 4 motors.

I want to add more ping sensors (10 total)but first i have to figure out how to program more than one ping sensor. Like i said i already did this with one sensor using the· Ping_Demo.BS2 (Demo Code for Parallax PING))) Sonar Sensor)and it work.·I thought if i·just duplicated everything it would work but it didn't. I really want to get this program right because i'm having some freinds over and i want·to inspire them to get into robotics.·So any help will be much appreciated.

Thanks.

P.s. here is the program i modified but didn't work:
···

····· ' Purpose.... Demo Code for Parallax PING))) Sonar Sensor
····· ' Author..... Parallax, Inc.
····· ' E-mail..... support@parallax.com
····· ' Started....
····· ' Updated.... 08 JUN 2005
····· '
····· ' {$STAMP BS2}
····· ' {$PBASIC 2.5}
······ '
······ '
······ '
[noparse][[/noparse] I/O Definitions ]
······ Pinga··········· PIN···· 11
·······pingb··········· PIN···· 10
······ aHB25··········· PIN···· 13
······ bHB25··········· PIN···· 12
······ cHB25··········· PIN···· 14
······ dHB25··········· PIN···· 15
······· '
[noparse][[/noparse] Constants ]
······ #SELECT $STAMP
······ #CASE BS2, BS2E
······ Trigger CON 5 ' trigger pulse = 10 uS
······ Scale CON $200 ' raw x 2.00 = uS
······ #CASE BS2SX, BS2P, BS2PX
······ Trigger CON 13
······ Scale CON $0CD ' raw x 0.80 = uS
······ #CASE BS2PE
······ Trigger CON 5
······ Scale CON $1E1 ' raw x 1.88 = uS
······ #ENDSELECT
······ RawToIn CON 889 ' 1 / 73.746 (with **)
······ RawToCm CON 2257 ' 1 / 29.034 (with **)
······ IsHigh CON 1 ' for PULSOUT
······ IsLow CON 0
······ '
[noparse][[/noparse] Variables ]
······ rawDista··· VAR Word ' raw measurement
······ rawDistb··· VAR Word
······ inchesa···· VAR Word
······ inchesb···· VAR Word
······ cma········ VAR Word
······ cmb········ VAR Word
·······'
[noparse][[/noparse] Initialization ]
······ Reset:
·······DEBUG CLS,
······ "Parallax PING))) Sonar", CR, ' setup report screen
······ "======================", CR,
······ CR,
······ "Time (uS)..... ", CR,
······ "Inchesa........ ", CR,
······ "inchesb ........", CR,
······ "Centimeters... "
······ '
[noparse][[/noparse] Program Code ]
······ Main:
······ DO
······ GOSUB Get_Sonar ' get sensor value
·······inchesa = rawDista ** RawToIn ' convert to inches
······ inchesb = rawDistb ** RawToIn ' convert to inches
······ cma = rawDista ** RawToCm ' convert to centimeters
······ cmb = rawDistb ** RawToCm
······ DEBUG CRSRXY, 11, 3, ' update report screen
······ DEC rawDista, CLREOL,
······ DEC rawDistb, CLREOL
······ cRSRXY, 11, 4,
······ CRSRXY, 10, 4,
······ DEC inchesa, CLREOL,
······ DEC inchesb, CLREOL,
······ CRSRXY, 11, 5,
······ CRSRXY, 10, 5,
······ DEC cma, CLREOL
······ DEC cmb, CLREOL
······ PAUSE 100
······ IF (inchesa < 19) AND (inchesb < 19)THEN
······ PULSOUT aHB25,· 650 'rotate left
······ PULSOUT bHB25,· 650
······ PULSOUT cHB25,· 650
······ PULSOUT dHB25,· 650
······ ELSEIF (inchesa > 19) AND (inchesb > 19)THEN
······ PULSOUT aHB25,850· 'go foward
······ PULSOUT bHB25,650
······ PULSOUT cHB25,650
······ PULSOUT dHB25,850
······ PAUSE 20
······ ENDIF
······ LOOP
······ END
······ '
[noparse][[/noparse] Subroutines ]
······ ' This subroutine triggers the PING))) sonar sensor and measures
······ ' the echo pulse. The raw value from the sensor is converted to
······ ' microseconds based on the Stamp module in use. This value is
······ ' divided by two to remove the return trip -- the result value is
······ ' the distance from the sensor to the target in microseconds.
······ Get_Sonar:
······ Pinga = IsLow ' make trigger 0-1-0
······ pingb = IsLow
······ PULSOUT Pinga, Trigger ' activate sensor
······ PULSOUT pingb, Trigger
······ PULSIN· Pinga, IsHigh, rawDist ' measure echo pulse
······ PULSIN· Pingb, IsHigh, rawDist
······ rawDista = rawDist */ Scale ' convert to uS
······ rawDistb = rawDist */ Scale
······ rawDista = rawDist / 2 ' remove return trip
······ rawDistb = rawDist / 2
······ RETURN·········

Comments

  • FranklinFranklin Posts: 4,747
    edited 2007-03-15 19:47
    OK, first problem, you can't get data from the pings at the same time as you are trying. In the get sonar subroutine you need to do one ping, get the results and then do the other.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-03-15 19:56
    Correct. This is because of the way the Ping works. You do a "PULSOUT" to activate the ping. The ping sends a pulse, and times the time between when it's sent and when it's recieved. Meanwhile, you do a "PULSIN" to get the result. When the ping recieves its pulse, it returns a pulse to the BS2 indicating how long the delay was.

    If you do TWO 'PULSOUTS', you don't know which PULSOUT is being recieved by the first PING sensor, so the distance will probably be wrong. In addition, recieving that ping will delay your receving the second ping pulse. Which would also be wrong if you COULD recieve it.

    SO, the only way out of this is to do the PULSOUT on the first ping, do the PULSIN on the first ping. THEN, do the Pulsout on the second ping, PULSIN on the second ping. I don't think there's any way with the physics involved to do it any other way.

    Ah, you COULD call 'Get_Sonar', and each time you call it get data from a different PING sensor. That's if you don't want the "Get_Sonar" call to take too long. 10 Ping Sensors might take a while to get all the data. But however you do it, the PULSOUT PingPin, PULSIN PingPin MUST be done on the same sensor before going on to the next sensor.
  • lobolobo Posts: 100
    edited 2007-03-15 22:57
    so i would name it Get_Sonar1: for the first ping sensor and Get_Sonar2: for the second sensor?

    the reason i thought this was possible is because i have seen alot of robots with many ping sensors connected to a microcontroller. So i thought i give it a try.
  • FranklinFranklin Posts: 4,747
    edited 2007-03-16 03:14
    Oh, it will work. You just need to do them one at a time.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-03-16 13:37
    For instance, see the attached. The attached lets you set a "PingNum" variable to the Ping device you want to read. It then sets the "RawDist" to the value read from this sensor. You can then store the RawDist wherever you want, then read the next sensor.
  • jason_lakewhitneyjason_lakewhitney Posts: 19
    edited 2007-03-16 14:00
    first create a array(number of ping sensors)
    also for var. like raw, ping, inches and so on to the number of ping sensors that you have

    when you call GoSub Get_Sensors:
    have a for loop something like:

    FOR i = 1 to NumofSensors
    PULSOUT Ping(i), Trigger
    PULSIN Ping(i), IsHigh, rawDist
    Next

    This way you can add and remove sensors and the only code change is would be to a constant like NumofSensors that you use to create all of your arrays

    BTW:
    That may not be the correct syntax for PBasic For/Next.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    God Bless,
    Jason
  • allanlane5allanlane5 Posts: 3,815
    edited 2007-03-16 14:36
    Jason, that's a good idea. The only problem with it is that he has 10 'ping' sensors, and only 26 bytes of memory to take data with.

    So, let's see. A 10-location array of Nibbles takes 5 bytes. A 10 location array of words takes 20 bytes. Ok, he's got ONE byte left -- 1/2 byte if 'I' is a nibble. So it would work, but kind of uses ALL his resources up.

    Just a thought.
  • jason_lakewhitneyjason_lakewhitney Posts: 19
    edited 2007-03-16 15:43
    Allan, I took some time and re-wrote the program look over it. I am new to Basic Stamps but not new to programming. I work as a software and gis developer for a few years now. So I have been pretty excited to try to learn something new. There may be some syntax that is incorrect, it tokenized successfully but I am at work and can not test it also I do not any ping sensors yet so I really can't test.
    If someone will, thanks.

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    #SELECT $STAMP
    #CASE BS2, BS2E
      Trigger CON 5 ' trigger pulse = 10 uS
      Scale CON $200 ' raw x 2.00 = uS
    #CASE BS2SX, BS2P, BS2PX
      Trigger CON 13
      Scale CON $0CD ' raw x 0.80 = uS
    #CASE BS2PE
      Trigger CON 5
      Scale CON $1E1 ' raw x 1.88 = uS
    #ENDSELECT
    
    NumPings  CON 10                      'Nib      4  bits
    PingNum    VAR Nib                     'Nib      4  bits
    Ping           VAR Nib(NumPings)    'Nib*10   40 bits
    Dist           VAR Word                  'Word     16 bits
                                                     'Total    64 bits = 8  bytes - check the math to make sure that correct
                                                     'Memory  208 bits = 26 bytes - leaves 18 bytes
    
    RESET:
      GOSUB Set_Screen
      GOSUB Set_Pins
    
    MAIN:
      FOR PingNum = 0 TO NumPings       'PBASIC ARRAYS ARE 0(zero) BASED
        GOSUB Get_Sonar
      NEXT
      GOTO MAIN
    
    Get_Sonar:
      DEBUG DEC Ping(PingNum), TAB
      Ping(PingNum) = 0
    
      PULSOUT Ping(PingNum), Trigger
      PULSIN  Ping(PingNum), 1, Dist
      Dist = Dist */ Scale
      Dist = Dist  / 2
    
      DEBUG DEC Dist, TAB
      Dist  = Dist ** 889
      DEBUG DEC Dist, TAB
      Dist  = Dist ** 2257
      DEBUG DEC Dist, CR   '<---- CR is TAB in the file, that is a mistake
    RETURN
    
    Set_Screen:
      DEBUG CLS
      DEBUG "Ping Number", TAB, "RAW", TAB, "INCHES", TAB, "CENTIMETERS", CR
      DEBUG "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~", CR
    RETURN
    
    Set_Pins:
      ' Ping sensor to Pins
      Ping(0)  = 0
      Ping(1)  = 1
      Ping(2)  = 3
      Ping(3)  = 4
      Ping(4)  = 6
      Ping(5)  = 7
      Ping(6)  = 8
      Ping(7)  = 9
      Ping(8)  = 10
      Ping(9)  = 11
      Ping(10) = 12
    RETURN
    
    



    Also, I attached the .bs2 file.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    God Bless,
    Jason

    Post Edited (jason_lakewhitney) : 3/16/2007 4:01:23 PM GMT
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-03-16 16:29
    Variable space for the pin assignments could be eliminated altogether if sequential pins were used. In that case a constant could define the highest pin and the pins could be referenced indexed to the start pin. Take care. Of course, without storing all the readings not much could be done with the data unless it was processed in real time as received per PING))). Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • lobolobo Posts: 100
    edited 2007-03-16 18:52
    wow!
  • jason_lakewhitneyjason_lakewhitney Posts: 19
    edited 2007-03-16 19:02
    Lobo,
    Does that look like that might help? I am not saying it will work but if it does let me know. It should I took the program you had posted and just re-did it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    God Bless,
    Jason
  • lobolobo Posts: 100
    edited 2007-03-16 21:44
    yes it does look like it might work. i havent tried it yet though. I just got back from the store. I'am going to try it know. i will let you know if it does or doesnt.

    Oh, just so that ya know i'm currently using only two sonars connected to pin 10 and pin 11. I want to in the future use up to 10 sonars.

    Ok thanks. i will try the program now.
  • lobolobo Posts: 100
    edited 2007-03-16 21:47
    P.s
    To be more precise on when i would like to use 10 sensors is like within 4 days.
  • jason_lakewhitneyjason_lakewhitney Posts: 19
    edited 2007-03-19 13:03
    lobo,
    Okay, lets set it up for two Ping Sensors.

    1) Change NumPings to 2
    2) In 'Set_Pins' comment out each line except for 'Ping(0) and Ping(1)'
    3) Now set 'Ping(0) = 10' and 'Ping(1) = 11'
    4) Save, Download and Run

    That should do it, when you add more Pings do it the same way you just did.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    God Bless,
    Jason
  • lobolobo Posts: 100
    edited 2007-03-19 20:52
    i did the following recommended modification but i still couldn't get 2 ultrasonic sensors to work. I know it's something i am not doing or for got to do for sure!!

    Jason
  • lobolobo Posts: 100
    edited 2007-03-19 20:53
    here is the program i modified for 2 ultrasonic sensors:


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

    #SELECT $STAMP
    #CASE BS2, BS2E
    Trigger CON 5 ' trigger pulse = 10 uS
    Scale CON $200 ' raw x 2.00 = uS
    #CASE BS2SX, BS2P, BS2PX
    Trigger CON 13
    Scale CON $0CD ' raw x 0.80 = uS
    #CASE BS2PE
    Trigger CON 5
    Scale CON $1E1 ' raw x 1.88 = uS
    #ENDSELECT
    NumPings CON 2 'Nib 4 bits is this where i'am suppouse to change the number of pings to 2?
    'NumPings CON 10 'Nib 4 bits
    PingNum VAR Nib 'Nib 4 bits
    Ping VAR Nib(NumPings) 'Nib*10 40 bits
    Dist VAR Word 'Word 16 bits
    'Total 64 bits = 8 bytes - check the math to make sure that correct
    'Memory 208 bits = 26 bytes - leaves 18 bytes

    RESET:
    GOSUB Set_Screen
    GOSUB Set_Pins

    MAIN:
    FOR PingNum = 0 TO NumPings 'PBASIC ARRAYS ARE 0(zero) BASED
    GOSUB Get_Sonar
    NEXT
    GOTO MAIN

    Get_Sonar:
    DEBUG DEC Ping(PingNum), TAB
    Ping(PingNum) = 0

    PULSOUT Ping(PingNum), Trigger
    PULSIN Ping(PingNum), 1, Dist
    Dist = Dist */ Scale
    Dist = Dist / 2

    DEBUG DEC Dist, TAB
    Dist = Dist ** 889
    DEBUG DEC Dist, TAB
    Dist = Dist ** 2257
    DEBUG DEC Dist, TAB
    RETURN

    Set_Screen:
    DEBUG CLS
    DEBUG "Ping Number", TAB, "RAW", TAB, "INCHES", TAB, "CENTIMETERS", CR
    DEBUG "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~", CR
    RETURN

    Set_Pins:
    ' Ping sensor to Pins
    Ping(0) = 10 'is this where i'am suppouse to set ping 0=10 and ping 1 = 11 ?
    Ping(1) = 11
    'Ping(2) CON 3
    'Ping(3) CON 4
    'Ping(4) CON 6
    'Ping(5) CON 7
    'Ping(6) CON 8
    'Ping(7) CON 9
    'Ping(8) CON 10
    'Ping(9) CON 11
    'Ping(10) CON 12
    RETURN
  • jason_lakewhitneyjason_lakewhitney Posts: 19
    edited 2007-03-19 21:42
    Made some changes I took Chris's approach and remove the need to keep the 'Ping()' varible.



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

    #SELECT $STAMP
    #CASE BS2, BS2E
    Trigger CON 5 ' trigger pulse = 10 uS
    Scale CON $200 ' raw x 2.00 = uS
    #CASE BS2SX, BS2P, BS2PX
    Trigger CON 13
    Scale CON $0CD ' raw x 0.80 = uS
    #CASE BS2PE
    Trigger CON 5
    Scale CON $1E1 ' raw x 1.88 = uS
    #ENDSELECT

    NumPings CON 2 'Nib 4 bits
    PingNum VAR Nib 'Nib 4 bits
    'Ping VAR Nib(NumPings) 'Nib*10 40 bits
    Dist VAR Word 'Word 16 bits
    'Total 64 bits = 8 bytes - check the math to make sure that correct
    'Memory 208 bits = 26 bytes - leaves 18 bytes

    RESET:
    GOSUB Set_Screen

    MAIN:
    FOR PingNum = 0 TO NumPings - 1 'PBASIC ARRAYS ARE 0(zero) BASED
    GOSUB Get_Sonar
    NEXT
    GOTO MAIN

    Get_Sonar:
    DEBUG DEC PingNum, TAB
    'Ping(PingNum) = 0

    PULSOUT PingNum, Trigger 'Ping(PingNum), Trigger
    PULSIN PingNum, 1, Dist 'Ping(PingNum), 1, Dist
    Dist = Dist */ Scale
    Dist = Dist / 2

    DEBUG DEC Dist, TAB
    Dist = Dist ** 889
    DEBUG DEC Dist, TAB
    Dist = Dist ** 2257
    DEBUG DEC Dist, TAB
    RETURN

    Set_Screen:
    DEBUG CLS
    DEBUG "Ping Number", TAB, "RAW", TAB, "INCHES", TAB, "CENTIMETERS", CR
    DEBUG "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~", CR
    RETURN

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    God Bless,
    Jason
  • toitoi Posts: 7
    edited 2007-03-20 04:06
    can I used this sonar for underwater application?
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2007-03-20 06:46
    toi -

    In one word, no. Underwater sonar is much more expensive, and usually operates at different frequencies than airborne sonar.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • toitoi Posts: 7
    edited 2007-03-20 08:11
    any suggestion where can i find the sonar that can operate underwater? at what frequencies it operates?
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2007-03-20 09:13
    Toi -

    If you're looking for something to "hack", marine fish finders or depth finders·are a good place to start. The specific unit you find will dictate the frequency of operation.

    Regards,

    Bruce Bates
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-03-20 14:13
    Toi,

    Since this thread is specifically regarding the PING))) you should post questions about underwater sonar in the Sandbox Forum.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
Sign In or Register to comment.