No announcement yet.

SHARP SENSOR linearization curve help

  • Filter
  • Time
  • Show
Clear All
new posts

  • SHARP SENSOR linearization curve help

    Hi , I want to use a Sharp GP2Y0A02YK distance sensor with PIC16F88 using ADC , and after read the datashet i see that the math use floating point numbers.

    Someone can guide me to solve this math in PBP3.

    Example 1

    A curve fit formula is used to approximate distance as a function of voltage:

    D= A + B X / 1 + C X + D X( SQR 2)

    D = Distance (cm)
    V = Voltage (V)
    A = 0.008 271
    B = 939.6
    C = -3.398
    D = 17.339

    Example 2

    Distance in (mm) = 1420000 / (output voltage (mV) - 1091

  • #2
    According to the professionals that frequent Microchip's PIC Forum, even the newer PIC18F devices are not well equipped to work with floating point math (let alone decades old PIC16F variants). There they suggest moving to a dsPIC33 chip to take advantage of the more robust math engines. I'm not sure if you will be able to make this work with FP. You could round off to work with fixed point math at the cost of accuracy, or you could look into more advanced processors. PBP does not work with the 16-bit PICs (which is what the dsPIC33's are), so you would have to either use ASM or C. You seem to have quite a challenge there.
    We can crack this cotton PIC'n thang!


    • #3
      mpgmike , Thank you very much for your answer, I will try to find a solution that works


      • #4
        the second example can be done easily enough by preloading the pbp registers for a 32 bit divide, there is nothing wrong with integer math, scaled correctly its more accurate than fp and faster too

        Scaler VAR WORD[2] ; 32-bit (Dword)  
        Result var word
        PutMulResult?D macro Din
        MOVE?BB Din, R2
        MOVE?BB Din + 1 , R2 + 1
        MOVE?BB Din + 2, R0
        MOVE?BB Din + 3, R0 + 1
        Scaler[0]=43744 ; 1420000 low word
        Scaler[1]=21 ; 1420000 high word
        @ PutMulResult?D _Scaler
        Result = DIV32 512 ;eg for 512mV
        Result = Result - 1091
        hserout [ 13,10,"dist = ",dec Result]​
        dist = 1682

        According to the professionals that frequent Microchip's PIC Forum, even the newer PIC18F devices are not well equipped to work with floating point math (let alone decades old PIC16F variants).
        what ??

        8 bit pic chips do 32b fp math as well as any other 8 bit chips you just need to remember and work with the limits of ieee754 fp accuracy


        • #5
          Dear Richard , I try to learn with your piece of code and I will try to implement it to write a PBP3 code for use this kind of sensor, then will share it here so others can learn how do it. Again thank you very much for your help and support.​

          Heere the link of Sharp Sensor GP2Y0A02YK with Visual Basic code :

          " "
          Last edited by Alejandro; 10-02-2022, 09:05 PM.


          • #6
            that link is a dud , the formula you have posted so far is very poorly formatted and is impossible to be certain how to implement it.
            neither (1420000/vresult )-1091 or 1420000/(vresult-1091) yields results anything like the data sheet, with vresult in millivolts.
            where are these formula coming from.
            example 1 i cannot interpret at all in any algebraic sense
            Attached Files


            • #7

              Dear Richard , My post is what I find about Sharp Sensor ,

              I continue search and found a example in PBP and the Linearization formula is different but work with integer math. " "

              One more time , Thanks a lot for you support . : )

              THE RESULTS

              For a microcontroller using 10-bit A2D measurements, such as the USBStem, the sensor output reading will range from 0 to 1023 ((2^10)-1). As an example, interpolation yields the following formula for a GP2Y0A21sensor:

              Equation 6: Linearized GP2Y0A21 range in integer math

              Comparing equation 5 and 6, m'=6787, b'=3 and k=4. For equation 6, V must be greater than 3 in order to avoid a divide-by-zero error. With 10-bit integers, voltage measurements from a GP2D12are typically above 80 when any object is in range. If nothing is in front of the sensor, the readings can drop to 0.

              This approach can also work for other Sharp sensors. After plugging in "eyeballed" calibration data for a GP2D120and adjusting the k offset to get a straight line, the following formula can be derived for a GP2D120:

              Equation 7: Linearized GP2D120 range in integer math

              Due to the use of integer math, the range units output from these equations will not be exactly centimeters but these functions will provide a range measurement that is more user-friendly than just using the raw A2D reading. Linearization of Data

              Approximations used in this linearization method work well for controllers which use integer math and will work even better for controllers that have floating point capabilities. Successful implementation depends on the quality of the calibration data and a good choice of the k constant to linearize the response curve for a given sensor model.

              GP2D120 sensor example
              ' by Tom Igoe

              ' Reads a changing voltage from a GP2D120 IR ranging sensor
              ' on analog input 0 and sends the result out in ASCII-encoded
              ' decimal numbers.

              ' Distance ranging formula comes from Acroname,
              ' I had to change the final constant to match my sensor's readings.

              ' Created 6 Oct. 2005
              ' Updated 25 Oct. 2005

              ' Define ADCIN parameters
              DEFINE ADC_BITS 10 ' Set number of bits in result
              DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
              DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS

              ' pins:
              tx var portc.6 ' serial TX
              rx var portc.7 ' serial RX
              resetLED var portb.7 ' blinking LED
              ' analog in is on porta.0

              ' constant to set the baud rate:
              inv9600 con 16468

              ' define variables:
              adcVar var word
              distance var word

              ' subroutine variables:
              i var byte

              TRISA = %11111111 ' Set PORTA to all input
              ADCON1 = %10000010 ' Set PORTA analog and right justify result

              gosub blink

              ' read analog input:
              adcin 0, adcVar
              ' Calculate linear slope of reading (thanks, Acroname!):
              distance = (2914 / (adcVar + 5)) - 1
              ' send the distance out:
              serout2 tx, inv9600, ["Distance = ", 9, distance, 10, 13]
              ' wait 10ms for ADC to reset before next reading:
              pause 10
              goto main

              ' Blink the reset LED:
              for i=0 to 3
              high resetLED
              pause 200
              low resetLED
              pause 200


              • #8
                plugging voltages into none of those formulas gives any results that match the data sheet range graph for a GP2Y0A02YK
                they are for different devices with different range capabilities