Hel programing Devantech SRF04 Ultrasonic Range Finder to measures distances to
hvilla01
Posts: 19
Hi all,
I try the example that is at http://www.acroname.com/robotics/info/examples/srf04-1/srf04-1.html
' {$STAMP BS2}
' {$PBASIC 2.5}
'
'
' Devantech SRF04 Example
'
wDist VAR Word
INIT CON 0
ECHO CON 1
' CONVERSION FACTORS:
'
' The PULSIN command returns the round-trip echo time in 2us units
' which is equivalent to the one-way trip time in 1us units.
'
' distance = (echo time) / (conversion factor)
'
' use 74 for inches (73.746us per 1 in)
' use 29 for centimeters (29.033us per 1 cm)
'
convfac CON 29 ' use inches
'
main:
······· GOSUB sr_sonar
······· DEBUG DEC wDist, CR
······· PAUSE 200
······· GOTO main
sr_sonar:
······· PULSOUT INIT,5 ' 10us init pulse
······· PULSIN ECHO,1,wDist ' measure echo time
······· wDist=wDist/convfac ' convert to inches
······· PAUSE 10
······· RETURN
sr_sonar_2:
······· PULSOUT INIT,5 ' 10us init pulse
······· OUTPUT INIT ' dummy command (delay)
······· RCTIME ECHO,1,wDist ' measure echo time
······· wDist=wDist/convfac ' convert to inches
······· PAUSE 10
······· RETURN
END
And the only thing I receive is a repited 0 on the debug screen, what I want to do is to move two servos forward until the SFR04 detects object
aprox at 4 or 5 cm, any help will be really apriciated.
Humberto
·
I try the example that is at http://www.acroname.com/robotics/info/examples/srf04-1/srf04-1.html
' {$STAMP BS2}
' {$PBASIC 2.5}
'
'
' Devantech SRF04 Example
'
wDist VAR Word
INIT CON 0
ECHO CON 1
' CONVERSION FACTORS:
'
' The PULSIN command returns the round-trip echo time in 2us units
' which is equivalent to the one-way trip time in 1us units.
'
' distance = (echo time) / (conversion factor)
'
' use 74 for inches (73.746us per 1 in)
' use 29 for centimeters (29.033us per 1 cm)
'
convfac CON 29 ' use inches
'
main:
······· GOSUB sr_sonar
······· DEBUG DEC wDist, CR
······· PAUSE 200
······· GOTO main
sr_sonar:
······· PULSOUT INIT,5 ' 10us init pulse
······· PULSIN ECHO,1,wDist ' measure echo time
······· wDist=wDist/convfac ' convert to inches
······· PAUSE 10
······· RETURN
sr_sonar_2:
······· PULSOUT INIT,5 ' 10us init pulse
······· OUTPUT INIT ' dummy command (delay)
······· RCTIME ECHO,1,wDist ' measure echo time
······· wDist=wDist/convfac ' convert to inches
······· PAUSE 10
······· RETURN
END
And the only thing I receive is a repited 0 on the debug screen, what I want to do is to move two servos forward until the SFR04 detects object
aprox at 4 or 5 cm, any help will be really apriciated.
Humberto
·
Comments
Jeff T.
Thanks for your promp response, I tried what you tolb but it shows an error (Undifined label), I am jusr srating using BS2.
Here is my code with tha cange you suggest
' {$STAMP BS2}
' {$PBASIC 2.5}
'
'
' Devantech SRF04 Example
'
wDist VAR Word
INIT CON 0
ECHO CON 1
' CONVERSION FACTORS:
'
' The PULSIN command returns the round-trip echo time in 2us units
' which is equivalent to the one-way trip time in 1us units.
'
' distance = (echo time) / (conversion factor)
'
' use 74 for inches (73.746us per 1 in)
' use 29 for centimeters (29.033us per 1 cm)
'
convfac CON 29 ' use inches
'
main:
GOSUB sr_sonar:
DEBUG DEC wDist, CR
PAUSE 200
GOTO main
GOSUB sr_sonar_2:
PULSOUT INIT,5 ' 10us init pulse
PULSIN ECHO,1,wDist ' measure echo time
wDist=wDist/convfac ' convert to inches
PAUSE 10
RETURN
sr_sonar_2:
PULSOUT INIT,5 ' 10us init pulse
OUTPUT INIT ' dummy command (delay)
RCTIME ECHO,1,wDist ' measure echo time
wDist=wDist/convfac ' convert to inches
PAUSE 10
RETURN
END
All I need is the sensor tell me whan is about 3 to five cm so I can stop the servos.
Thanks a lo I hope there is not to much trouble, any help will be realy apriciated.
Humberto
where it says:
main:
GOSUB sr_sonar
change to:
main:
GOSUB sr_sonar_2
Jeff T.
0
0
And thats all, I connect my SRF04 like this
GRD VSS
UNUSED
INIT P0
EDCHO P1
V+ VDD
This is the program with your changes
' {$STAMP BS2}
' {$PBASIC 2.5}
'
'
' Devantech SRF04 Example
'
wDist VAR Word
INIT CON 0
ECHO CON 1
' CONVERSION FACTORS:
'
' The PULSIN command returns the round-trip echo time in 2us units
' which is equivalent to the one-way trip time in 1us units.
'
' distance = (echo time) / (conversion factor)
'
' use 74 for inches (73.746us per 1 in)
' use 29 for centimeters (29.033us per 1 cm)
'
convfac CON 74 ' use inches
'
main:
GOSUB sr_sonar_2:
DEBUG DEC wDist, CR
PAUSE 200
GOTO main
GOSUB sr_sonar_2:
PULSOUT INIT,5 ' 10us init pulse
PULSIN ECHO,1,wDist ' measure echo time
wDist=wDist/convfac ' convert to inches
PAUSE 10
RETURN
sr_sonar_2:
PULSOUT INIT,5 ' 10us init pulse
OUTPUT INIT ' dummy command (delay)
RCTIME ECHO,1,wDist ' measure echo time
wDist=wDist/convfac ' convert to inches
PAUSE 10
RETURN
END
Regards
good luck
Jeff T.
GOSUB mytag
mytag:
RETURN
I tried the program that came at http://www.acroname.com/robotics/parts/R93-SRF04p.pdf and it works great, I hope it not to much trouble but I dont know where to put on the program that if thr SRF04 reads and object then stop go back turn and the go forward.
Regards
Humberto
What I need is when the SRF04 reads an object at 4 cm to stop the servos and go back few cms and then tern and go forward.
Regards
Humberto
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
I still can make the program to work properly, if any one can help it will be great,
This is the program
' =========================================================================
'
' File....... SRF04_Demo.BS2
' Purpose.... Devantech SRF04 Ultrasonic Range Finder
' Author..... Parallax, Inc. (Copyright 2003 - All Rights Reserved)
' E-mail..... support@parallax.com
' Started.... 06 MAR 2002
' Updated.... 01 OCT 2003
'
' {$STAMP BS2}
' {$PBASIC 2.5}
'
' =========================================================================
'
[noparse][[/noparse] Program Description ]
'
' This program uses the Devantech SRF04 to measure the distance between the
' unit and a target. Display is raw value, centimeters, and inches.
'
[noparse][[/noparse] Revision History ]
'
' 01 OCT 2003 : Updated for PBASIC 2.5 and for any BASIC Stamp module
'
[noparse][[/noparse] I/O Definitions ]
'
Trigger PIN 0
Echo PIN 1
'
[noparse][[/noparse] Constants ]
#SELECT $STAMP
#CASE BS2, BS2E
Trig10 CON 5 ' trigger pulse = 10 uS
ToCm CON 30 ' conversion factor to cm
#CASE BS2SX, BS2P
Trig10 CON 13
ToCm CON 78
#CASE BS2PE
Trig10 CON 5
ToCm CON 31
#ENDSELECT
'
[noparse][[/noparse] Variables ]
samples VAR Nib ' loop counter
pWidth VAR Word ' pulse width from sensor
rawDist VAR Word ' filtered measurment
cm VAR Word ' centimeters
inches VAR Word
pulsecount VAR Byte
'
[noparse][[/noparse] EEPROM Data ]
'
[noparse][[/noparse] Initialization ]
Setup:
LOW Trigger
DEBUG CLS,
"Devantech SRF04 Demo", CR,
"
", CR,
"Raw........... ", CR,
"Centimeters... ", CR,
"Inches........ "
'
[noparse][[/noparse] Program Code ]
Main:
DO
GOSUB Forward_Pulse
GOSUB Get_Sonar ' take sonar reading
DEBUG CRSRXY, 15, 2, DEC rawDist, CLREOL
cm = rawDist / ToCm ' convert to centimeters
DEBUG CRSRXY, 15, 3, DEC cm, CLREOL
IF cm < 100 THEN
GOSUB Back_Up
GOSUB Turn_Left
ELSE
RETURN
ENDIF
inches = cm */ $03EF ' x 3.937 (to 0.1 inches)
DEBUG CRSRXY, 15, 4,
DEC inches / 10, ".", DEC1 inches,
CLREOL
PAUSE 10 ' delay between readings
LOOP
END
Forward_Pulse:
FOR pulseCount = 0 TO 50
PULSOUT 13, 1000
PULSOUT 12, 650
PAUSE 15
NEXT
RETURN
Back_Up:
FOR pulseCount = 0 TO 50
PULSOUT 13, 650
PULSOUT 12, 850
PAUSE 15
NEXT
RETURN
Turn_Left:
FOR pulseCount = 0 TO 50
PULSOUT 13, 650
PULSOUT 12, 650
PAUSE 15
NEXT
RETURN
Turn_Right:
FOR pulseCount = 0 TO 50
PULSOUT 13, 850
PULSOUT 12, 850
PAUSE 15
NEXT
RETURN
'
[noparse][[/noparse] Subroutines ]
Get_Sonar:
rawDist = 0 ' clear measurement
FOR samples = 1 TO 5 ' take five samples
PULSOUT Trigger, Trig10 ' 10 uS trigger pulse
#SELECT $STamp
#CASE BS2, BS2E
RCTIME Echo, 1, pWidth ' measure pulse
#CASE #ELSE
PULSIN Echo, 1, pWidth ' measure pulse
#ENDSELECT
rawDist = rawDist + (pWidth / 5) ' simple digital filter
PAUSE 10 ' minimum period between
NEXT
RETURN