Shop OBEX P1 Docs P2 Docs Learn Events
Sonar range changes on stationary object — Parallax Forums

Sonar range changes on stationary object

AImanAIman Posts: 531
edited 2007-01-19 15:01 in Robotics
I am running a code I picked up from Parallax and modified to fit two run two sonar. There are two problems I can't seem to get around.


First - Why do I get different readings on a stationary object?
The code is set to average 5 readings so with a stationary sonar and a stationary object there should be no changes.

Second - Why do I get false distance readings?
The sonar are off a lot - upto a couple feet. One is pointing at the ceiling and shows the ceiling·moving up and down. If I put my hand in front of either sonar its reading changes so much that somethings it says my hand is 1.5 inches away when I know its a lot more.

Here's the code:

'Parallax, INC. • Devantech SRF04 Ultrasonic Range Finder (#28015) • 10/2003 Page 4
' =========================================================================
'
' File....... SRF04_Demo.BS2
' Purpose.... Devantech SRF04 Ultrasonic Range Finder
' Author..... Parallax, Inc. (Copyright 2003 - All Rights Reserved)
' E-mail..... [url=mailto:support@parallax.com]support@parallax.com[/url]
' 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
Trigger2 PIN 2
Echo2 PIN 3
' -----[noparse][[/noparse] Constants ]-------------------------------------------------------
Trig10 CON 5 ' trigger pulse = 10 uS
ToCm CON 30 ' conversion factor to cm
' -----[noparse][[/noparse] Variables ]-------------------------------------------------------
samples VAR Nib ' loop counter
pWidth VAR Word ' pulse width from sensor
rawDist VAR Word ' filtered measurment
rawDist2 VAR Word ' filtered measurment
cm VAR Word ' centimeters
inches VAR Word
' -----[noparse][[/noparse] Initialization ]--------------------------------------------------
Setup:
LOW Trigger
DEBUG CLS,
"Devantech SRF04 Demo", CR,
"--------------------", CR,
"Raw........... ", CR,
"Centimeters... ", CR,
"Inches........ ", CR,
"Raw2........... ", CR,
"Centimeters... ", CR,
"Inches........ ", CR, HOME

' -----[noparse][[/noparse] Program Code ]----------------------------------------------------
Main:
DO
   GOSUB Get_Sonar ' take sonar reading
   cm = rawDist / ToCm ' convert to centimeters
   inches = cm */ $03EF ' x 3.937 (to 0.1 inches)
   DEBUG CRSRXY, 15, 4,
   DEC inches / 10, ".", DEC1 inches,  CR
   cm = rawDist2 / ToCm ' convert to centimeters
   inches = cm */ $03EF ' x 3.937 (to 0.1 inches)
   DEBUG CRSRXY, 15, 7,
   DEC inches / 10, ".", DEC1 inches,  CR, HOME
   PAUSE 250 ' delay between readings
LOOP
END
' -----[noparse][[/noparse] Subroutines ]-----------------------------------------------------
Get_Sonar:
rawDist = 0 ' clear measurements
rawDist2 = 0
 
FOR samples = 1 TO 5 ' take five samples
   PULSOUT Trigger, Trig10 ' 10 uS trigger pulse
   RCTIME Echo, 1, pWidth ' measure pulse
   rawDist = rawDist + (pWidth / 5) ' simple digital filter
   PAUSE 10 ' minimum period between
   PULSOUT Trigger2, Trig10 ' 10 uS trigger pulse
   RCTIME Echo2, 1, pWidth ' measure pulse
   rawDist2 = rawDist2 + (pWidth / 5) ' simple digital filter
   PAUSE 10 ' minimum period between
NEXT
RETURN

By the way, how does·$03EF translate to a decimal? I can't use a decimal no matter what I try.
«1

Comments

  • John R.John R. Posts: 1,376
    edited 2006-12-31 05:12
    Can't help with the sonar, but $03EF = 1007.

    If you have a windows based PC, you can easily convert with the "Calculator" program. Just go to the "View" menu, and put it in "Scientific" mode. Then you can enter a number in HEX and then change to DEC and visa-versa.

    Also, for a more thorough understanding: Digits 0-9 are the same as in Decimal, A-F = 10 - 15. Starting from the right most digit, the first digit is "ones", the second is "Sixteens", then "256s", followed by "4096s". Thusly:

    0 = 0 x 4096 = 0
    3 = 3 x 256 = 768
    E = 14 x 16 = 224
    F = 15 x 1 = 15

    And 0 + 768 + 224 + 15 = 1007

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    John R.

    8 + 8 = 10
  • PARPAR Posts: 285
    edited 2006-12-31 07:58
    AIman said...
    I am running a code I picked up from Parallax and modified to fit two run two sonar. There are two problems I can't seem to get around.


    First - Why do I get different readings on a stationary object?
    The code is set to average 5 readings so with a stationary sonar and a stationary object there should be no changes.

    Second - Why do I get false distance readings?
    The sonar are off a lot - upto a couple feet. One is pointing at the ceiling and shows the ceiling·moving up and down. If I put my hand in front of either sonar its reading changes so much that somethings it says my hand is 1.5 inches away when I know its a lot more.

    Here's the code:
    Why not find out (use DEBUG) if the two "pwidth" values are as variable as your various calculated values before proceeding further. If they are not (very) variable, that would suggest something wrong in the calculations sections. If they are very variable, that would suggest either a signal timing problem (Trig10 and/or PAUSE length) and/or a problem with the sonar device(s) / circuit.

    PAR
  • Steve JoblinSteve Joblin Posts: 784
    edited 2006-12-31 14:48
    AIman:· Fear not, you are not alone!!!!!

    I too suffer the same as you... at first I thought it was my wiring, so I buildt a totally new board... then I thought it was my sensor, so I bought a totally new one, but I am still where you are.... I even increased the cycle to calculate an average of 50 readings, but same result... the numbers just keep jumping all over... I saw your other posting where someone suggested adding a filter cap... I added a 100uF polarized electrolytic, but no real change... I solicited this group for other suggested values, but have not gotten a response (http://forums.parallax.com/showthread.php?p=623041)

    Even if we can't ever figure out why, at least it is comforting knowing that I am not alone with my problem....

    I am tempted to buy a PING))) and compare the two...
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2006-12-31 15:39
    The following is available at the Devantech (FAQ) --

    Q. What is the accuracy of the ranging?
    A.· We quote 3-4cm. Its normally better than this, however so many factors affect accuracy that we won't specify anything better than this. The speed of sound in air is approx. 346m/S at 24 degrees C. At 40KHz the wavelength is 8.65mm. The sonar's detect the echo by listening for the returning wavefronts. This echo has an attack/decay envelope, which means it builds up to a peak then fades away. Depending on which wavefront is the 1st to be strong enough to be detected, which could be the 1st, 2nd or even 3rd, the result can jitter by this much. Another effect which limits accuracy is a phasing effect where the echo is not coming from a point source. Take a wall for example, the ping will bounce off the wall and return to the sonar. The wall is large, however, and there will be reflections from a large area, with reflections from the outside being slightly behind the central reflection. It is the sum of all reflections which the sensor sees which can be either strengthened or weakened by phasing effects. If the echo is weakened then it may be the following wavefront which is detected - resulting in 8.65mm of jitter. It is possible to see changes of distance as small as mm but then get cm of jitter.


    Wow, jitter.
  • Steve JoblinSteve Joblin Posts: 784
    edited 2006-12-31 17:32
    Very interesting PJ! Thanks for the insights!
  • RickBRickB Posts: 395
    edited 2006-12-31 21:46
    Try running just 1 sonar at a time and see if the readings stabilize.

    Rick
  • lairdtlairdt Posts: 36
    edited 2006-12-31 22:26
    I've been working with a Ping))) unit, and I've not had any drift/jitter on it's readings. I've not run more than one at a time yet, but I don't expect that would chagne anything as long as they don't go active at the same time and clash with another unit's measurement.
  • ObliowObliow Posts: 4
    edited 2006-12-31 22:33
    Do the units send multiple pulses per range measurement ? If they do and one pulse train overlaps the other, unless each unit operates coherently they will detect echoes from transmitted pulses from the other unit. Such unsynchronized returns will give variable erroneous range measurements.
  • Steve JoblinSteve Joblin Posts: 784
    edited 2007-01-01 01:07
    Unfortunately, both of my attempts have been just using one sonar unit....·· simple set up of a BS2 and a SRF04....
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-01-01 02:18
    One thing that can cause inconsistent readings is the object you’re trying to bounce the signal from. ·People, for example don’t reflect a signal very good.· Neither does carpet or pets. ·Now if you have a wall or other solid surface then it should be good as long as the surface is roughly perpendicular to your position. ·Another thing is with multiple sensors you never want to trigger more than one at a time or you could be getting interference.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • Steve JoblinSteve Joblin Posts: 784
    edited 2007-01-01 03:56
    Chris: Thanks for the insights about objects that are not "good".... when testing, I have been using my hand.... I'll set up a test rig tomorrow with a thick peice of cardboard mounted in front of the sensors and see if that solves the problem.... btw, happy new year! Here is to more great Parallax products in '07 (cough, cough, penguin, cough cough)!!!!!
  • AImanAIman Posts: 531
    edited 2007-01-02 16:48
    Running two and three SRF04 that are pointing in different directions does not make a difference. The code listed is for two that are at 90 degrees to each other, and with three sonar they were on X, Y and Z axis. For the two currently running, one sonar faces the ceiling and one faces the kitchen wall. Using only one does stablize the readings some but not entirely. There are still variances off an stationary object of upto 18 inches.

    This set up takes 5 readings
    FOR samples = 1 TO 5 ' take five samples
       PULSOUT Trigger, Trig10 ' 10 uS trigger pulse
       RCTIME Echo, 1, pWidth ' measure pulse
       rawDist = rawDist + (pWidth / 5) ' simple digital filter
       PAUSE 10 ' minimum period between
       PULSOUT Trigger2, Trig10 ' 10 uS trigger pulse
       RCTIME Echo2, 1, pWidth ' measure pulse
       rawDist2 = rawDist2 + (pWidth / 5) ' simple digital filter
       PAUSE 10 ' minimum period between
    NEXT
    
    

    So yes, it does send multi pulses.

    However with a 10 ms pause and a perpendicular arrangment I do not understand why this is getting false readings. Perhaps the sonar need to trigger farther apart.

    If the SRF04 is the problem then I will have to go with QTI, IR or Ping))) provided one of them is more reliable. The intent is to mount IR directly below the Sonar so there is a second distance measurment and so that more surfaces can be detected.
  • PARPAR Posts: 285
    edited 2007-01-02 18:34
    AIman said...
    Running two and three SRF04 that are pointing in different directions does not make a difference. The code listed is for two that are at 90 degrees to each other, and with three sonar they were on X, Y and Z axis. For the two currently running, one sonar faces the ceiling and one faces the kitchen wall. Using only one does stablize the readings some but not entirely. There are still variances off an stationary object of upto 18 inches.

    This set up takes 5 readings
    FOR samples = 1 TO 5 ' take five samples
       PULSOUT Trigger, Trig10 ' 10 uS trigger pulse
       RCTIME Echo, 1, pWidth ' measure pulse
       rawDist = rawDist + (pWidth / 5) ' simple digital filter
       PAUSE 10 ' minimum period between
       PULSOUT Trigger2, Trig10 ' 10 uS trigger pulse
       RCTIME Echo2, 1, pWidth ' measure pulse
       rawDist2 = rawDist2 + (pWidth / 5) ' simple digital filter
       PAUSE 10 ' minimum period between
    NEXT
    
    

    So yes, it does send multi pulses.

    However with a 10 ms pause and a perpendicular arrangment I do not understand why this is getting false readings. Perhaps the sonar need to trigger farther apart.

    If the SRF04 is the problem then I will have to go with QTI, IR or Ping))) provided one of them is more reliable. The intent is to mount IR directly below the Sonar so there is a second distance measurment and so that more surfaces can be detected.
    So what happened when you ran the test using DEBUG to print each individual "pwidth" value (rather than only examining the five-sampled averages? What does the variation look like?

    PAR
  • lboucherlboucher Posts: 139
    edited 2007-01-02 21:14
    I had the same odd problem. Everything was working fine with my ping as I was building a somewhat elaborate autonomous wandering routine. My code got longer and longer and then in one particular part of the code the ping distances started to make no sense and things went flaky.

    The particular part of the code was when the Boe was driving forward the ping would sample to make sure nothing is in front. But the values I was getting back made no sense. And the if statement directly after the ping that used the distance value seemed to be messing up.

    If i place a short pause after the ping subroutine (which does include the conversion to cm) and the if statement that uses the final distance value everything works perfect.

    This is really odd because I call the same ping subroutine other places in the code and everything works fine. By all accounts the error should not happen but i tested it more than a few times with with the pause commented out and then with the pause, making sure to change nothing else and in a very open room so there was no chance of any odd reflections and this definitly was the solution to the problem.

    I am at work right now (GDRS) so I don't have the code in front of me but I will post it when I get home.

    See if this solves your problem
  • lboucherlboucher Posts: 139
    edited 2007-01-02 21:24
    Also why do you care about decimal places. The sensor isn't that accurate anyways. Just round to nearest integer

    For example if you want to multiply x by 1.4 just do

    y = (x * 14) / 10

    This eliminates the need to do things with HEX. Which honestly I don't care to take the time to work with because its much easier to mess up the math in HEX.

    In the code I am running I do no averaging at all, and all the ping measurements make sense, maybe an average would be a little more precise but I don't see the point, if something is far away, just drive closer and ping it again and get a much more accurate distance by being closer to the object. Then drive around using that distance.
  • edited 2007-01-02 21:44
    The book Smart Sensors and Applications book demonstrates a lot of PBASIC programming techniques for minimizing error propagation. Chapter 2 focuses on the Ping))) Ultrasonic Distance sensor, and it explains scaling from time to distance. It also explains how to compensate for time measurement variations that result from air temperature variations.

    Smart Sensors and Applications product page: http://www.parallax.com/detail.asp?product_id=28029

    Smart Sensors and Applications PDF (5.5 MB): http://www.parallax.com/dl/docs/prod/sic/SmartSensors-v1.0.pdf

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Andy Lindsay

    Education Department
    Parallax, Inc.

    Post Edited (Andy Lindsay (Parallax)) : 1/2/2007 9:48:49 PM GMT
  • AImanAIman Posts: 531
    edited 2007-01-03 00:29
    My programs run on debug 'mode' until I can get consistant readings that are accurate. I will run the program again and see what sort of data I get back and post it.
  • AImanAIman Posts: 531
    edited 2007-01-03 00:44
    OK, here are some of the distance variances from the two sonar that are running, taken in the order they appear on the debug screen.·Keep in mind that both are stationary shooting at stationary objects about the same distance away - right around 5 feet. This is from the code listed at the begging of this discussion.

    Sonar 1····································· Sonar 2

    376··········································· 771.7

    386.2·········································706

    435.8·········································53.10

    319··········································· 797.3

    383.1········································ 573.9

    388.2·········································623.8

    383.9········································ 599.8



    The longer the code runs the bigger the problem gets.

    As you can see these are not minor changes. Sonar 1 is the more stable but still changes from a high of 435 to a low of 319 - thats over a hunderd. Sonar 2 makes changes in excess of 700.

    I ran this a second time with a pause of 20 between the individual sonar pulses and got to following. (Not looping, pulses.·The loop has a pause of 10 before geting an echo from sonar 1 AFTER sonar 2 has triggered.)

    Sonar 1······························· Sonar 2

    421.2·································· 855.5

    416.1·································· 828.8

    421.2·································· 823.3

    410.6···································831.1

    420.1···································100.3

    60······································· 420.8

    451.8··································· 809.9

    Again Sonar 1 and 2 both show large differences. Sonar one changes by almost 400 and sonar 2 changes by over 700.

    These measurments are current as of this posting. I literally ran the program while on line to get accurate data to post.
  • Steve JoblinSteve Joblin Posts: 784
    edited 2007-01-03 01:20
    AIman: been playing around with mine and think I discovered that putting in a comand to create a very slight delay between the
    "PULSOUT Trigger, Trig10" and the "RCTIME Echo, 1, pWidth" gets much more reliable and stable readings!

    Try inserting "Output Trigger" between the two commands so it looks like this...

    PULSOUT Trigger, Trig10 ' 10 uS trigger pulse
    OUTPUT Trigger ' creates a very slight delay by having the Stamp process a "dummy" command
    RCTIME Echo, 1, pWidth ' measure pulse

    I also am allowing at least 100ms between each reading so the sound waves are not bouncing off each other... I suspect that I can reduce this a bit, but I seem to be on to something here!

    Can you give it a try and confirm that you are getting better results as well?
  • AImanAIman Posts: 531
    edited 2007-01-03 03:08
    Ran the code with the dummy command of OUTPUT Trigger and got these results

    Sonar 1···························· Sonar 2

    461··································· 701

    459····································99

    489··································· 766

    469····································651

    493··································· 99

    503··································· 3

    498····································813

    With the code showing a dummy command of OUTPUT Trigger and 100 ms pause I get these results



    Sonar 1······················· Sonar 2

    452.7···························814.6

    521.9·························· 147.1

    449.6···························764.6

    472.4·························· 836.6

    448·····························120.3

    457.4··························718.2



    Again large differences and as early this was done while on the post so the measurments and code changes will be accurate to the thread.



    I have also tried this code:



    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    '

    ' Devantech SRF04 Example
    '

    wDist· VAR· Word
    wDist2· VAR· Word
    INIT· CON· 0
    ECHO· CON· 1
    INIT2 CON 2
    ECHO2 CON 3


    ' 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


    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

    · PULSOUT INIT2,5··· ' 10us init pulse
    · OUTPUT INIT2··· ' dummy command (delay)
    · RCTIME ECHO2,1,wDist2· ' measure echo time
    · wDist2=wDist2/convfac· ' convert to inches
    · PAUSE 10

    · RETURN

    Which gave much better results of 47, 48, 45, 48, 44



    And this code


    ' {$STAMP BS2}
    ' Sonar check
    '''''variables'''''
    Dist VAR Word··· ' variable that will be used to store reading from sensor

    '''''notes'''''
    ' Pin 0 of BS2 wired to init (next to unused pin)
    ' Pin 1 of BS2 wired to echo (inbetween init and VDD)

    '''''Main routine'''''
    main:
    GOSUB checksensors
    DEBUG ? Dist
    GOTO main

    '''''sub routines'''''
    checksensors:
    PULSOUT 0,5······· 'send initial pulse
    OUTPUT 0··········· 'dummy delay
    RCTIME 1,1,Dist·· 'store value of received pulse in Dist
    RETURN

    Which returned raw distance as 3247, 3222, 3234, 3561, 2957, 3273

    This leads me to believe that the top code for this response is the most stable of the codes listed in this thread.

    The code needs to be debugged for both sensors, so I will post again in a moment.

    Post Edited (AIman) : 1/3/2007 3:21:29 AM GMT
  • AImanAIman Posts: 531
    edited 2007-01-03 03:17
    I modified one of the codes and ran it. The measurments returned more accurate results this time.

    Sonar 1····························· Sonar 2

    3256································· 3549

    3239··································3595

    3262································· 3568

    3252································· 3567

    3290································· 3557

    Ironic that code that was sent to me is more accurate then the web site that sells the product...

    Heres the code that gave the above results, so if anyone else needs code there are 4 different functional examples posted with this one being the most reliable so far.

    ' {$STAMP BS2}
    ' Sonar check
    '''''variables'''''
    Dist VAR Word    ' variable for reading sonar 1
    Dist2 VAR Word   ' variable for reading sonar 2
    
    '''''notes'''''
    ' Pin 0 of BS2 wired to init (next to unused pin)
    ' Pin 1 of BS2 wired to echo (inbetween init and VDD)
    ' Pin 2 of BS2 wired to init (next to unused pin)
    ' Pin 3 of BS2 wired to echo (inbetween init and VDD)
    '''''Main routine'''''
    main:
    GOSUB checksensors
    DEBUG ? Dist, CR
    DEBUG ? Dist2, HOME
    PAUSE 10000    ' really long pause to write down numbers
    GOTO main
    '''''sub routines'''''
    checksensors:
    PULSOUT 0,5        'send initial pulse
    OUTPUT 0            'dummy delay
    RCTIME 1,1,Dist   'store value of received pulse in Dist
    PULSOUT 2,5        'send initial pulse
    OUTPUT 2            'dummy delay
    RCTIME 3,1,Dist2   'store value of received pulse in Dist
    RETURN
    

    (replace this text with your code)
    
  • lboucherlboucher Posts: 139
    edited 2007-01-03 04:19
    Hum just realized your code is using the RCTIME function to receive the input pulse. My code is using the PULSEIN command. Anyone know if there is any difference between the two??? Also I have attached my autonomous wandering code. Its working well but Im very new to the boe bot and pbasic. If anyone has any pointers I would really appreciate comments. My emails is lboucher@vt.edu. Thanks all.
  • lboucherlboucher Posts: 139
    edited 2007-01-03 04:29
    Just though of one thing, have you tried switching the ping sensors, maybe its a hardware problem and you just have a bad ping. If nothing else testing it real quick would eliminate the possibility.

    Also for reference I just tested my ping with this code and my returns couldn't have been more stable. Up to about 100 cm 20 returns are almost always equal, and over 100 cm they may vary 1 or 2 cm, but thats really acceptable. Not sure how this compares to the actuall distance but it is dependable.


    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    RawDist VAR Word
    Ping PIN 15
    'Constants for Ping
    Scale CON $200
    Trigger CON 5
    RawToIn CON 889
    RawToCm CON 2257
    IsHigh CON 1
    IsLow CON 0
    counter VAR Byte
    DEBUG CLS
    FOR counter = 1 TO 20
    Ping = IsLow
    PULSOUT Ping, Trigger
    PULSIN Ping, IsHigh, RawDist
    RawDist = RawDist */Scale
    RawDist = (RawDist/2) ** RawToCm
    DEBUG DEC RawDist,CR
    NEXT
    END
  • AImanAIman Posts: 531
    edited 2007-01-03 14:26
    The last code I ran is the most stable and will vary but not as much. Typically the variance is about 2 or 3 inches, with random bursts of one or two returns being double the distance.

    I have a few other sonar I can swap out and will give that a shot.

    The other solution would be to do a couple of variables to hold a number and if the distance is greater then a set amount ignore the figure unless all the returns start getting that same distance. Probably something like +/- 5%.
  • John AbshierJohn Abshier Posts: 1,116
    edited 2007-01-03 15:01
    You might want to consider a median filter.· Attached is c code.
  • cotangcotang Posts: 5
    edited 2007-01-07 17:18
    I was wondering how you got so far on this? I'm also having the same issue and I tried your code and it not very stable on my rig. I'm measuring about 40cm away and it fluctuates between 32 - 38 cm and on occasionally 0 reading. I don't mind the fluctuation but the 0 will throw off my results.
  • Steve JoblinSteve Joblin Posts: 784
    edited 2007-01-07 19:30
    what code are you referring to? Using RCTime vs Pulsin? Adding the dummy "output' command? Putting in a large delay between readings to prevent echos of echos? All of these?

    What stamp are you using? are you using the SRF04 or Ping)))?
  • cotangcotang Posts: 5
    edited 2007-01-08 06:00
    Sorry, should have been a lot more specific. I'm using pulsin and PING))). I'm not using a stamp just a pic 16f84 and the compiler is proton+. I'm using about 750ms to 1 sec delay and it seems to be pretty stable +/- 5%, although I do get the occasional 0cm reading which is really wierd...... my code is as follows....

    Main:
    'Get Distance Data

    Low SRRS ' Makes Trigger 0-1-0, SRRS is signal pin of PING)))
    PulsOut SRRS, 5 ' Activate sensor
    PulsIn SRRS, 1, RawDis ' Measure echo pulse
    RawDis = RawDis * 2 ' Convert to uS
    RawDis = RawDis / 2 ' Remove return trip
    RawDis = RawDis / 29.034 ' Convert to cm


    Print $Fe, 1 'Clear LCD display
    Print $Fe, 2 'Set to Home position
    Print "Distance in cm" 'Print text to LCD
    Print $fe, $c0 'Set Cursor to second line
    Print Dec RawDis 'Print Value on second line
    DelayMS 1000 ' Pause for 1 sec
    goto Main

    Post Edited (cotang) : 1/8/2007 6:10:33 AM GMT
  • SailerManSailerMan Posts: 337
    edited 2007-01-08 14:57
    In your code can't you eliminate these two lines?

    RawDis = RawDis * 2 ' Convert to uS
    RawDis = RawDis / 2 ' Remove return trip
    They seem to cancel one another out. Or am I missing something.

    Regards,
    Eric
    ·
  • Steve JoblinSteve Joblin Posts: 784
    edited 2007-01-08 15:19
    Have you tried using the techniques described in this thread? That is, try using RCTime instead of Pulsin, try putting in a dummy command just after the pulsout... does that help?
Sign In or Register to comment.