Shop OBEX P1 Docs P2 Docs Learn Events
Hel programing Devantech SRF04 Ultrasonic Range Finder to measures distances to — Parallax Forums

Hel programing Devantech SRF04 Ultrasonic Range Finder to measures distances to

hvilla01hvilla01 Posts: 19
edited 2006-09-19 02:35 in BASIC Stamp
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
·

Comments

  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2006-09-10 17:09
    Humberto, I think the first line of code in the main: routine should read ....GOSUB sr_sonar_2: . This routine uses RCTIME (not PULSIN) to measure the time taken for the pulse to echo back to your reciever, the next line divides that time by the conversion factor.

    Jeff T.
  • hvilla01hvilla01 Posts: 19
    edited 2006-09-10 17:25
    Hi Jeff,

    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
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2006-09-10 17:38
    Humberto, you altered the wrong line of code, undo what changes you made and make this change.

    where it says:

    main:

    GOSUB sr_sonar

    change to:

    main:

    GOSUB sr_sonar_2

    Jeff T.
  • hvilla01hvilla01 Posts: 19
    edited 2006-09-11 02:44
    Hi Jeff I did what you told me and the result is the same *in the devug screen unli shows 0
    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
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2006-09-11 17:55
    Humberto, sorry that didn't work out for you. Parallax have a very detailed explanation of how to implement the SRF04 this is one link to that information http://www.acroname.com/robotics/parts/R93-SRF04p.pdf

    good luck

    Jeff T.
  • doug.taylordoug.taylor Posts: 31
    edited 2006-09-12 12:32
    Just a quick observation. You have a colon after the tag name in your GOSUB statements. The colon only goes after the tag name when it is used as a tag.


    GOSUB mytag

    mytag:
    RETURN
  • hvilla01hvilla01 Posts: 19
    edited 2006-09-15 23:01
    Hi Doug,

    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
  • hvilla01hvilla01 Posts: 19
    edited 2006-09-16 17:22
    Hi,

    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
  • FranklinFranklin Posts: 4,747
    edited 2006-09-16 18:31
    The program should do this.. move forward, check distance, if distance < 4cm stop, backup, turn left(or right), go forward. Most of this code is in the pdf about the boe-bot and works fine. I used this code to have my boe-bot move until it sensed and object and then swivel the srf until it found the most open direction and then head that way.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • hvilla01hvilla01 Posts: 19
    edited 2006-09-17 05:11
    Thank's Stephen but this will work with the SRF04?
  • hvilla01hvilla01 Posts: 19
    edited 2006-09-19 02:35
    Hi,
    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
Sign In or Register to comment.