help with the ping sonar sensor
lobo
Posts: 100
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·········
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
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.
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.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
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
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.
If someone will, thanks.
Also, I attached the .bs2 file.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
God Bless,
Jason
Post Edited (jason_lakewhitney) : 3/16/2007 4:01:23 PM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
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
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.
To be more precise on when i would like to use 10 sensors is like within 4 days.
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
Jason
' {$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
' {$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
In one word, no. Underwater sonar is much more expensive, and usually operates at different frequencies than airborne sonar.
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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
Since this thread is specifically regarding the PING))) you should post questions about underwater sonar in the Sandbox Forum.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support