Help... Ping Parallax + atom28

EDIT<<

After looking closely with your code, it appears that you never set your pin # 15 as the proper state (Input/Output). There are also other discrepancies in the code example you provided verses the Parallax example code. I have provided the code example you should try in the following post below.

Example of your problem in the routine:

Your example shows “LOW ping” meaning:

You set P15 to low which is like turning it off like a light switch. You needed to set P15 to either an input or output pin which is what they were talking about with 1-0-1. 1= output 0=input.

Try this code and see what happens. I changed the pin 15 line to comply with the Basic Atom syntax. I don’t have a basic atom so I could not test it. The sub routine was very different than the code example you provided. So you have nothing to loose, try this code and let me know what happens:

[code]’ =========================================================================

’ File… Ping_Demo.BS2
’ Purpose… Demo Code for Parallax Ping Sonar Sensor
’ Author… Jon Williams – Parallax, Inc.
’ E-mail… [email protected]
’ Started…
’ Updated… 08 JUN 2005



’ =========================================================================

’ ----- Program Description ]---------------------------------------------

’ This program demonstrates the use of the Parallax Ping Sonar sensor and
’ converting the raw measurement to English (inches) and Metric (cm) units.

’ Sonar Math:

’ At sea level sound travels through air at 1130 feet per second. This
’ equates to 1 inch in 73.746 uS, or 1 cm in 29.034 uS).

’ Since the Ping sensor measures the time required for the sound wave to
’ travel from the sensor and back. The result – after conversion to
’ microseconds for the BASIC Stamp module in use – is divided by two to
’ remove the return portion of the echo pulse. The final raw result is
’ the duration from the front of the sensor to the target in microseconds.

’ ----- Revision History ]------------------------------------------------

’ ----- I/O Definitions ]-------------------------------------------------

Ping CON P15

’ ----- Constants ]-------------------------------------------------------

Trigger CON 13
Scale CON $0CD ’ raw x 0.80 = uS

RawToIn CON 889 ’ 1 / 73.746 (with **)
RawToCm CON 2257 ’ 1 / 29.034 (with **)

IsHigh CON 1 ’ for PULSOUT
IsLow CON 0

’ ----- Variables ]-------------------------------------------------------

rawDist VAR Word ’ raw measurement
inches VAR Word
cm VAR Word

’ ----- EEPROM Data ]-----------------------------------------------------

’ ----- Initialization ]--------------------------------------------------

Reset:
DEBUG CLS, ’ setup report screen
"Parallax Ping Sonar ", CR,
“=====================”, CR,
CR,
"Time (uS)… ", CR,
"Inches… ", CR,
"Centimeters… "

’ ----- Program Code ]----------------------------------------------------

Main:
DO
GOSUB Get_Sonar ’ get sensor value
inches = rawDist ** RawToIn ’ convert to inches
cm = rawDist ** RawToCm ’ convert to centimeters

DEBUG CRSRXY, 15, 3,                        ' update report screen
      DEC rawDist, CLREOL,
      CRSRXY, 15, 4,
      DEC inches, CLREOL,
      CRSRXY, 15, 5,
      DEC cm, CLREOL

PAUSE 100

LOOP
END

’ ----- 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:
Ping = IsLow ’ make trigger 0-1-0
PULSOUT Ping, Trigger ’ activate sensor
PULSIN Ping, IsHigh, rawDist ’ measure echo pulse
rawDist = rawDist */ Scale ’ convert to uS
rawDist = rawDist / 2 ’ remove return trip
RETURN

Not quite right.

Ping = IsLow

should be

Low Ping ;Set pin low before Pulsout so we geta 0-1-0 pulse

I looked in the Atom manual and it describes that setting a pins state should be a value of 1 or 0.

Here is a copy paste of that from the atom manual:

Pulsin
PULSIN pin, state, {TimeoutLabel,TimeoutMultiple,} Var
Measure the width of a pulse.
Pin is a variable or constant that specifies the I/O pin to use.
This pin will be placed into input mode during pulse
measurement and left in that state after the instruction finishes.
State is a variable or constant (0 or 1) that specifies whether the
pulse to be measured begins with a 0-to-1 transition (1) or a
1-to-0 transition (0).

Try to change the scale constant to $044.

Ping = IsLow, in BS2
Low Ping, in basicAtom
if I use Ping = IsLow, I’ll get an error

In Pulsin, Pin, in this case, it already sets to Low
right before the pulsout

Explanations from the article:
Initially, the trigger pin is made an output and a short pulse(5-10 uS) is used to trigger the ping (we’ll use PULSOUT to generate the trigger). The ping module delays the trigger to the sonic transmitter element for 500uS. This allows the Basic Stamp to load the next instruction (PULSIN) and e ready for the return echo.

The code starts by making the output bit or the trigger pin 0 (in my code pin 15) and the reason for this is that pulsout makes the trigger pin an output, toggles its state, delays, and then toggles that pin back to the original state. Since the Ping module is looking for a 0-1-0 pulse to trigger the measurement, presetting the pin to 0 makes this happen.

After the trigger is sent, PULSIN is used to measure the width of the echo pulse. As I stated earlier, the 500uS delay in the Ping allows pulsin to get loaded and ready. There is no danger of PULSIN timing out, as even the BS2P (fastest Basic Stamp Module) won’t time out for about 49 ms.

Units return by Pulsin:
BS2, BS2e 2.00ms
BS2sx, BS2p 0.88ms
BS2pe 1.88ms

how about Units return by Pulsin for basicatom28?

Trigger = 10uS / units return by pulsin
Scale should be units return by pulsin * 256 since */ works like multiplication but in units of 1/256

all other calculation can be read from the article
parallax.com/dl/docs/prod/ac … SeeYou.pdf

I’m not good in english but I’m pretty sure I’m in the right path
Please correct me if I’m wrong

I’m confused why with the Atom is LOW Ping, and the BS2 is Ping = IsLow.

In the atom manual it shows setting the pins state the same way the BS2 is set.

Anyway, in the BS2 Subroutine example P15 is set low and then set high later on on the sub. In choco’s example:

LOW Ping ’ make trigger 0-1-0 <----------- Set P15 to LOW
PULSOUT Ping, Trigger ’ activate sensor
PULSIN Ping, 1, rawDist ’ measure echo pulse <— Sets P15 to output state like BS2?

I’m confused between the Atom syntax and the BS2 syntax.

There has got to be someone out there that has used a PING sensor with the atom 28. A code example of just using the PING sensor with the atom 28 would be a great way to see what’s going on.

I wouild like to learn along with choco on this.

I don’t think the scale is the problem.
coz scale is used to get the real rawDist
anyway, just tried it. Still no luck.

Same here.
First I thought Ping = IsLow can be used in atom then I found out I have to use “LOW Ping” to set Ping to low.

For your information, I’m a newbie in atom. Today is my second day using atom :slight_smile:
I’m building a biped robot (similar to open pino) and need help with everything.
but at first I want to get the ping to work.
so anyone please… please help me or share your code if you have one
I need an Atom guru to help me with the interface

The reason you must use “Low Ping” is because you have defined Ping as a constant(ie Pin number).

Ping CON P15

The low command takes a pin number and makes the pin specified low.

Ping = IsLow doesn’t make sense because you can’t make change a constant without recompiling. You will get an error. To set a pin low or high by setting it to 0 or 1 you must use the Pins variable.

Ping var OUT15

Ping = 0

OUT15 is the name of pin 15s variable(it holds the current state of pin 15).

This is actually no different from the BS2 except the BS2 doesn’t pre define pin constants. it only uses numbers(unless you define your own pin constants). The BS2 also has pin variables.

Nathan

To clear this up to send a 0-1-0 pulse with the pulsout command you need to set the pin to be used low and then use the pulsout command. To generate a 1-0-1 pulse you must first set the pin HIGH and then pulsout.

There are two general ways to set a pins state. Either use the low or high commands(which take a pin number as the argument) or use the pins variable name and set the pin state to 0 or 1 to set it low or high. not that you must also make the pin an output(either using the OUTPUT command or by clearing the pins direction variable) if you are setting the state using the pins variable. The High and Low commands do this automatically.

Nathan

Thanks Acid! that makes perfect sense! I did not think of it that way where you define the pin as a constant and then try to change it using the = symbol. I learned something new today.

As for choco’s problem, he has the issue of not setting P15 high again in the sub, right?

What he needs is:
HIGH Ping <---- added line of code
then:
PULSIN Ping, 1, rawdist

SN96: which one?
This one?
Get_Sonar:
low Ping ’ make trigger 0-1-0
PULSOUT Ping, Trigger ’ activate sensor
HIGH Ping
PULSIN Ping, 1, rawDist ’ measure echo pulse
rawDist = rawDist */ Scale ’ convert to uS
rawDist = rawDist / 2 ’ remove return trip
RETURN

or this?
Get_Sonar:
HIGH Ping ’ make trigger 0-1-0
PULSOUT Ping, Trigger ’ activate sensor
PULSIN Ping, 1, rawDist ’ measure echo pulse
rawDist = rawDist */ Scale ’ convert to uS
rawDist = rawDist / 2 ’ remove return trip
RETURN

It’s working now
both gave me the same output and I don’t understand why must high
I thought I have to make the pulse 0-1-0 (low ping), not 1-0-1 (high ping)
sorry I’m not good with it

Here is the output: Approximately 20 cm
Parallax Ping Sonar

Time (uS)… 21
Inches… 0
Centimeters… 0

from the bs2 example time should be around 500+ uS
I know the problem should be the trigger and scale value

If only I could get the correct value for Trigger and Scale :frowning:

Your problem is that the stamp is slower than the atom so you need to recalculate those values.

The first example is the one I would try -----^

Let me know what happens. If you can get some kind of reading, the next step would be to figure out the scale.

PK:
I’ve been recalculating but still no luck.
Atom is way faster than Basic Stamp.
I’ll keep trying though
May be I’ve made some mistake.

Just now I email [email protected] ask for the magic numbers
I hope they can reply my email ASAP

SN96:
It’s working.
Ok here is the output after I put on comment on these two lines
'rawDist = rawDist */ Scale ’ convert to uS
'rawDist = rawDist / 2 ’ remove return trip

Output: approximately 20 cm distance

Parallax Ping Sonar

Time (uS)… 1170
Inches… 0
Centimeters… 0

Now I have the ‘Time’.
How about the inches and centimeters?
Shouldn’t the inches or centimeters show some value else than 0. :question:

Well you have a time of 1120us this is no good because you need to get this under 500us. The ping sensor will wait 500us before it echos and does the timing. The ping sensor has safe guards that wont allow an echo if the timing is past 500us. Since the atom is faster than the stamp you dont need that whole 500us since this specifically made so that the stamp can switch to a pulsin in mode. So this could be why your getting 0’s for readings. Can you post your current program.

choco,

Glad to hear it is working!

This is where I co no longe help since I have poor math skills :laughing:

Hopfully PK can take you to success.

Sorry for the slow reply.
My internet goes up and down all the time

PK:
A time of 1120 uS?
Are you talking about the output time (1170 uS)?

if so, in parallax code, time is 587 uS for 20 cm
** operator make me nuts

for 1170 uS, It should return 15 inches and I already did the calculation
May be I got it wrong? :confused:

Here is the latest code:
For the Scale, I chose a number and now the time returned is close enough with the one expected

’ ----- I/O Definitions ]-------------------------------------------------

Ping CON P15

’ ----- Constants ]-------------------------------------------------------

Trigger CON 10
Scale CON $FF40
RawToIn CON 889 ’ 1 / 73.746 (with **)
RawToCm CON 2257 ’ 1 / 29.034 (with **)

’ ----- Variables ]-------------------------------------------------------

rawDist VAR Word ’ raw measurement
inches VAR Word
cm VAR Word

’ ----- Program Code ]----------------------------------------------------
Main:
GOSUB Get_Sonar ’ get sensor value
inches = rawDist ** RawToIn ’ convert to inches
cm = rawDist ** RawToCm ’ convert to centimeters’’
GOSUB Distance
PAUSE 100
goto Main

’ ----- 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.

Distance:
DEBUG [0, "Parallax Ping Sonar "]
DEBUG [13, “=====================”]
DEBUG [13, "Time (uS)… ", dec rawDist]
DEBUG [13, "Inches… ", dec inches]
DEBUG [13, "Centimeters… ", dec cm]
RETURN

Get_Sonar:
HIGH Ping ’ make trigger 0-1-0
PULSOUT Ping, Trigger ’ activate sensor
PULSIN Ping, 1, rawDist ’ measure echo pulse
rawDist = rawDist */ Scale ’ convert to uS
rawDist = rawDist / 2 ’ remove return trip
RETURN

So do I :laughing:

damn the memsic need the unit too :frowning:

Yea sorry about that last post. My mistake.

Well looking at your code your scale is way to big that number should be half the value of the bs2 pulsin. BS2 pulsin at about 2ms and the atom does it at 1ms. So 1 x 256 = 256 convert this to $100 in that ball park.

It is working its just your getting some bad data to do the math from the scale con. You could also rewrite this program using floating math since the atom has that feature and the stamp doesnt unless a co processor is used.

Paul

Thanks PK
I also just figure out that Atom pulsin only takes a half of BS2 when I did some coding for my memsic

Here is my latest code: (again)
’ ----- I/O Definitions ]-------------------------------------------------

Ping CON P15

’ ----- Constants ]-------------------------------------------------------

Trigger CON 10
Scale CON $100
RawToIn CON 73.746
RawToCm CON 29.034

’ ----- Variables ]-------------------------------------------------------

rawDist VAR Long ’ raw measurement
inches VAR Long
cm VAR Long

’ ----- Program Code ]----------------------------------------------------

Main:

GOSUB Get_Sonar 				' get sensor value
inches = float rawDist FDIV float RawToIn 	' convert to inches
cm = float rawDist FDIV float RawToCm 	' convert to centimeters''
GOSUB Display
PAUSE 100

GOTO Main

’ ----- 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.

Display:
DEBUG [0, "Parallax Ping Sonar "]
DEBUG [13, “=====================”]
DEBUG [13, "Time (uS)… ", dec rawDist]
DEBUG [13, "Inches… ", real inches]
DEBUG [13, "Centimeters… ", real cm]
RETURN

Get_Sonar:
HIGH Ping ’ make trigger 0-1-0
PULSOUT Ping, Trigger ’ activate sensor
PULSIN Ping, 1, rawDist ’ measure echo pulse
'rawDist = rawDist / Scale ’ convert to uS <<<=== not using this line.
rawDist = rawDist / 2 ’ remove return trip
RETURN

Here is the output I’m getting at: (20cm from an object as usual)
Parallax Ping Sonar

Time (uS)… 583
Inches… 7.905514
Centimeters… 20.079906

Do I still need that line of code? :confused: