Help... Ping Parallax + atom28

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:

an object at 20cm reading 20.08cm is very accurate. What line of code are you talking about that you have not added yet?

It seems to work, If the performance is consistant I would leave it as is.

Yea that line figures the time and then you use that line of code to determine the distances. If you pull that out I believe you would get 0’s for all the output data that would display on your screen.

Now to get even more accurate I would try $0FF, $0FE, $0FD, $0FC, $0FB, $0FA, $0F9, $0F8 etc. gust decreasing the constant by one.

Or compensate for the difference by subtracting. Since you know that at 20cm your getting 20.8 subtract .8 from all your results. Now this might not work depending if every reading you take is off by .8 otherwise there would some crazy math to figure out the equation.

I would mess with the scale con a little to see what happens it could be that $100 is as accurate as you can get.

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 <<<=== this line here
rawDist = rawDist / 2 ’ remove return trip
RETURN

the output is consistant but I still need to know why

PK:
I did pull out that line (See my last post for the output)

I did try to use $100
Here is the output:
Parallax Ping Sonar

Time (uS)… 2
Inches… 0.027120
Centimeters… 0.068885

even you decresing the constant by one you still get inaccurate output
unless… Scale = 1
Any number that divided by 1 will result that number itself

Wait, I think I know why.
I don’t have to use that line anymore.
that line is used to convert the return duration by pulsin from ms to uS
where in Atom return duration by pulsin is already in uS
Am I right?

parallax.com/dl/docs/prod/ac … SeeYou.pdf
Read page 2

Problem solved :wink:

Thanks a lot for the help guys

Hi.

Maybe this code will help you. Is the complete program. sensor subroutine contains the managemente of parallax Ping. Some comments are in spanish, but code is the important thing.

I hope it will help you.

P.D. I am having problem with a Parallax GPS module also. Seems is something common with parallax ATOM28 interations

Regards

Ricardo Alonso

[email protected]

'====================== SECCION DE DECLARACION DE LAS VARIABLES Y CONSTANTES =====================
lecturaA var word
lecturaB var word
VA var long
VB var long
heading var long
SP_RUMBO var long
flag var byte
gradiente var long
FLAG_ESCAPE VAR BYTE
FLAG_EVITAR VAR BYTE
FLAG_NAVEGAR VAR BYTE
T VAR WORD
M VAR LONG 'ALMACENA LA DIFERENCIA ENTRE LA LECTURA DE DISTANCIA DE
'EL SONAR Y LA DEL SONAR DERECHO
VAVG VAR LONG 'AVERAGE CALCULO DE VELOCIDADES DE AMBOS SONARES
AAVG VAR LONG 'AVERAGE CALCULO DE ACELERACIONES DE AMBOS SONARES
Medicion1 VAR WORD 'ALMACENA LA MEDICION DEL SENSOR IZQUIERDO EN US
Medicion2 VAR WORD 'ALMACENA LA MEDICION DEL SENSOR DERECHO EN US
Distance1 VAR LONG 'DISTANCIA DEL OBJETO AL SENSOR PING IZQUIERDO EN CM
Distance2 VAR LONG 'DISTANCIA DEL OBJETO AL SENSOR PING DERECHO EN CM
D1OLD VAR LONG
D2OLD VAR LONG
V1NEW VAR LONG
V2NEW VAR LONG
V1OLD VAR LONG
V2OLD VAR LONG
A1 VAR LONG
A2 VAR LONG
RUMBO VAR LONG
CONTADOR VAR BYTE
SIG1 CON P0 'TERMINAL DE E/S DEL SENSOR PING IZQUIERDO
SIG2 CON P1 'TERMINAL DE E/S DEL SENSOR PING DERECHO
mot_der CON P8
mot_izq CON P7
Conversion FCON 29.034 'CONSTANTE PARA LA MEDICION

rot_izq			var		sword
rot_der			var		sword
temporal		VAR		SWORD

CUENTA			VAR		BYTE


GOSUB SENSORES

SP_RUMBO = Heading

D1OLD = 300.00
D2OLD = 300.00
V1OLD = 0.00
V2OLD = 0.00
RUMBO = SP_RUMBO
CONTADOR = 0

'================================================================================================

CICLO:

GOSUB SENSORES

IF ( Distance1 < 10.0 ) OR ( Distance2 < 10.0 ) THEN
	FLAG_ESCAPE = 1
ELSE
	FLAG_ESCAPE = 0	
ENDIF

IF (((Distance1 >= 10.0) AND (Distance1 < = 40.0)) OR ((Distance2 >= 10.0) AND (Distance2 < = 40.0))) AND (FLAG_ESCAPE = 0) THEN
	FLAG_EVITAR = 1
ELSE
	FLAG_EVITAR = 0	
ENDIF

IF (FLAG_ESCAPE = 0) AND (FLAG_EVITAR = 0) THEN
	FLAG_NAVEGAR = 1
ELSE
	FLAG_NAVEGAR = 0	
ENDIF



IF FLAG_ESCAPE = 1 THEN GOSUB ESCAPE
IF FLAG_EVITAR = 1 THEN GOSUB EVITAR
IF FLAG_NAVEGAR = 1 THEN GOSUB NAVEGAR

IF CONTADOR >= 15 THEN 
	RUMBO = SP_RUMBO
	CONTADOR = 0	
ENDIF			

CONTADOR = CONTADOR + 1

GOTO CICLO

SENSORES:

’ ESTA PARTE DE LA SUBRUTINA SE DEDICA A LA LECTURA DE LOS DOS SONARES DEL ROBOT

’ PARA EL SENSOR IZQUIERDO SE COMIENZA LA MEDICION

LOW 	SIG1						'GARANTIZAR ESTADO INICIAL DEL PIN				
PULSOUT SIG1,5          	 		'EMITIR PULSO DE ACTIVACION
PULSIN  SIG1,1, Medicion1			'LEER SALIDA DEL SENSOR (ECO)
									'EL TIEMPO ALMACENADO EN Medicion FUE EL REQUERIDO POR LA ONDA ULTRA- 
									'SONICA PARA LLEGAR HASTA EL OBJETO Y REGRESARSE

’ SE HACE LA MEDICION PARA EL SENSOR DERECHO

LOW 	SIG2						'GARANTIZAR ESTADO INICIAL DEL PIN				
PULSOUT SIG2,5		           		'EMITIR PULSO DE ACTIVACION
PULSIN  SIG2,1, Medicion2 	    	'LEER SALIDA DEL SENSOR (ECO)
									'EL TIEMPO ALMACENADO EN Medicion FUE EL REQUERIDO POR LA ONDA ULTRA- 
									'SONICA PARA LLEGAR HASTA EL OBJETO Y REGRESARSE

Medicion1  = Medicion1 /2   		'TIEMPO DE VIAJE DE LA SEÑAL
Medicion2  = Medicion2 /2 
Distance1 = FLOAT(Medicion1) FDIV Conversion
Distance2 = FLOAT(Medicion2) FDIV Conversion
									'DISTANCIA GUARDA LA DISTANCIA ENTRE EL PING Y EL OBJETO EN CM 	

V1NEW = (D1OLD FSUB Distance1) FDIV 0.3
V2NEW = (D2OLD FSUB Distance2) FDIV 0.3

A1 = (V1NEW FSUB V1OLD) FDIV 0.3
A2 = (V2NEW FSUB V2OLD) FDIV 0.3

D1OLD = Distance1
D2OLD = Distance2
V1OLD = V1NEW
V2OLD = V2NEW

VAVG = 0.5 FMUL (V1NEW FADD V2NEW)
AAVG = 0.5 FMUL (A1 FADD A2)

’ ESTA PARTE DE LA SUBRUTINA SE DEDICA A LA LECTURA DEL COMPAS MAGNETICO 1655 DINSMORE

pause 1500		' ESPERE 1.5 SEGUNDOS PARA QUE SE ESTABILIZE LA BRUJULA

Adin AX0,2,AD_RON,lecturaA
Adin AX1,2,AD_RON,lecturaB
	
VA = ((5.00 fdiv 1024.0) fmul (float lecturaA))
VB = ((5.00 fdiv 1024.0) fmul (float lecturaB))
gradiente = (VA fsub VB) fadd 10.00

IF (VA >= 2.08) AND (VA <= 2.92) AND (gradiente <= 10.00) then
	heading = 45.0 fadd 107.14 fmul ( 2.92 fsub VA )
endif

IF (VA >= 2.08) AND (VA <= 2.92) AND (gradiente >= 10.0) then
	heading = 225.0 fadd 107.14 fmul ( VA fsub 2.08)
endif
	
IF (VB >= 2.08) AND (VB <= 2.92) AND (gradiente <= 10.0) then
	heading = 135.0 fadd 107.14 fmul ( 2.92 fsub VB )
endif
	
IF (VB >= 2.5) AND (VB <= 2.92) AND (gradiente >= 10.0) then
	heading = 107.14 fmul ( VB fsub 2.50)
endif

IF (VB >= 2.08) AND (VB < 2.50) AND (gradiente >= 10.0) then
	heading = 315.00 fadd 107.14 fmul ( VB fsub 2.08)
endif

’ serout s_out,i9600, REAL heading\2, " grados ",REAL Distance1, " ", REAL Distance2,13]

RETURN

'================================================================================================

EVITAR:

M = ((Distance2  FSUB  Distance1) fadd 360.00)

’ SEROUT S_OUT, I9600 , [real Distance1\2," “,real Distance2\2, " " , real M,” ", DEC T, 13]

GOSUB	AJUSTE

GOSUB 	SENSORES
RUMBO    = heading
CONTADOR = 0

IF (M < 364.00) AND (M > 356.00)    THEN GOSUB   AVANCE

RETURN

'=================================================================================================

NAVEGAR:

M = ((RUMBO FSUB heading) fadd 360.00) 

GOSUB	AJUSTE	

IF (M < 370.00) AND (M > 350.00)    THEN GOSUB   AVANCE

RETURN

'================================================================================================
AJUSTE:

temporal = int(M)

rot_izq = 3*(temporal - 360)
rot_der = 3*(temporal - 360)

’ serout s_out , i9600, [sdec rot_der, " ", sdec rot_izq,13]

servo mot_der,rot_der,1
servo mot_izq,rot_izq,1

RETURN

'=================================================================================================

AVANCE:

FOR CUENTA = 1 TO 5

	rot_der = 400
	rot_izq = -400
	
	servo mot_der,rot_der,1
	servo mot_izq,rot_izq,1
	
NEXT

RETURN	

'=================================================================================================

ESCAPE:

FOR CUENTA = 1 TO 50

servo mot_izq, 200,1
servo mot_der,-200,1

NEXT

FOR CUENTA = 1 TO 20

servo mot_izq, 200,1
servo mot_der, 200,1 

NEXT

GOSUB 	SENSORES
RUMBO    = heading
CONTADOR = 0

RETURN

Could some one please write a working subroutine that reads a ping sensor and returns some sort of useable value (in, cm). :slight_smile: All these clips of programs are getting confusing. Thanks.