​ ​ ​ ​ Sharp IR Sensor Algorithms
Results 1 to 8 of 8

Thread: Sharp IR Sensor Algorithms

  1. #1
    Senior Member
    Join Date
    Jul 2011
    Location
    Los Angeles, CA
    Posts
    1,903

    Default Sharp IR Sensor Algorithms

    I like those Sharp IR distance sensors, even if their analog output is non-linear. I suspect most of us use these either with lookup tables or in non-precision applications where rough ranges are acceptable. But this morning I found some hardcore equations at https://www.upgradeindustries.com/pr...(GP2Y0A21YK0F) and https://www.upgradeindustries.com/pr...(GP2Y0A02YK0F)

    Depending on your ADC resolution and other factors, you might not need floating-point accuracy to 6-7 sig figs.

  2. #2
    Senior Member
    Join Date
    Feb 2010
    Location
    Don't Mess With My Texas!
    Posts
    2,133

    Default

    No time to work on it now. For what it's worth:

    range (cm) = 306.439 + voltage * (-512.611 + voltage * (382.268 + voltage * (-129.893 + voltage * 16.2537)))
    - Tex
    __________________________________________________ _______________________
    "Truth lies dormant in our future history." ― Tex Clodhopper LXV
    “Confidence is ignorance. If you're feeling cocky, it's because there's something you don't know.” ― Eoin Colfer, Artemis Fowl

  3. #3
    Senior Member
    Join Date
    Sep 2016
    Location
    United Kingdom
    Posts
    694

    Default

    v*16.2537-v*v+382.268*v-512.611*v+306.439 in picaxe left to right...or maybe not.
    By the time it's worked that out, the robot's crashed.

  4. #4
    New Member
    Join Date
    May 2008
    Location
    Oxford
    Posts
    4

    Default

    I have used these sensors and found that there is a simple formula that gives good results.
    Distance in cm = 1735/8bitADC - 6
    This was derived using the solver in MSExcel to give the best fit to the manufacturer's datasheet calibration.

    This calibration has an accuracy of +/- 1cm to the data in the graph of figure #2 published in the datasheet:

    www.pololu.com/file/0J85/gp2y0a21yk0f.pdf


    e.g.
    True_____ Measured _ 8bit_____ Calc _____ Error
    distance__ Voltage ___ adc ____ Distance __ Distance
    cm ______ Volts _____ # _____ cm ______ cm
    80, ______ 0.40, ____ 20, ____ 80, ______ 0
    70, ______ 0.45, ____ 23, ____ 69, ______ -1
    60, ______ 0.50, ____ 26, ____ 60, ______ 0
    50, ______ 0.60, ____ 31, ____ 49, ______ -1
    40, ______ 0.75, ____ 38, ____ 39, ______ -1
    30, ______ 0.90, ____ 46, ____ 31, ______ +1
    20, ______ 1.25, ____ 64, ____ 21, ______ +1
    15, ______ 1.60, ____ 82, ____ 15, ______ 0
    10, ______ 2.10, ___ 107, ____ 10, ______ 0
    05, ______ 3.20, ___ 163, ____ 04, ______ -1
    Last edited by MarkLondon; 26-07-2017 at 23:21.

  5. #5
    Senior Member
    Join Date
    Jul 2011
    Location
    Los Angeles, CA
    Posts
    1,903

    Default

    Mark, that's excellent work. Thank you for sharing that formula. Impressive accuacy!

  6. #6
    Senior Member
    Join Date
    Sep 2016
    Location
    United Kingdom
    Posts
    694

    Default

    I used a2dval=readad10(a2d)
    a2dval=6050/a2dval
    if a2dval>=2 then
    a2dval=a2dval-2
    end if
    if a2dval<10 then
    a2dval=10
    end if
    if a2dval>80 then
    a2dval=80
    end if
    ie 10 bit a-d. Sampling is slower than SRF04 type ultrasonic even allowing 10 ms for settling.
    It's easy to get accuracy but the device is too slow updating for a robot with it mounted on a scanning servo.

  7. #7
    Senior Member
    Join Date
    Jul 2011
    Location
    Los Angeles, CA
    Posts
    1,903

    Default

    Quote Originally Posted by stan74 View Post
    It's easy to get accuracy but the device is too slow updating for a robot with it mounted on a scanning servo.
    IR sensors play nicer with servos than ultrasonic sensors do. And of course, at under $4 (and most Picaxes having plenty of ADC channels), why not use several stationary Sharp sensors in an array? These guys went overkill on both ultrasonic and IR sensors:


  8. #8
    Senior Member
    Join Date
    Sep 2016
    Location
    United Kingdom
    Posts
    694

    Default

    1st Sharp I ordered never arrived so bought 1 from UK supplier. They're over £12 each. I can't remember if it's acquisition time is in the data sheet but it was missing object in front of it on a robot servo. moving your hand in front and monitoring range I can see it's laggy.
    As for the robot in your photo,that would miss a chair leg between the sensors,the beam is only 15 degrees. Also the sensors should be vertical as they don't detect straight ahead.
    The servo I use scans in steps of 15 and at the ends of scan,starts next scan from + or - 5 so it checks the angles it missed in the previous scan. Also 2 us sensors at 45 degrees on a servo was the best solution in that they look in different directions and the servo has less movement and can move in smaller steps. Waiting 10 mS for each sensor to settle is ok..I don't know about 5 us and 3 IR as in the photo though. Bit OTT

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •