Sonar Calculation help required

Trying to correct the measurement what I am getting from 1260MB. When i measured I found out that the sonar was giving 36cm against a distance from ground of one meter. Guide me if this function is responsible for range calculation in AP_RangeFinder_SharpGP2Y.h

 

int convert_raw_to_distance(int _raw_value)
{
     if( _raw_value == 0 )
         return max_distance;
     else
         return 14500/_raw_value;

}

If it is so then what is 14500 thing how did it come. If I can find out this then I can apply the correction to get the real value.

 

While measuring this I am just using CLI and APM is not connected to any motor.

You need to be a member of diydrones to add comments!

Join diydrones

Email me when people reply –

Replies

  • Ok so here are the results and modifications that I did With the code given AP_RangeFinder_MaxsonarXL.h I tested using CLI test for sonar. Since MB1260 can measure upto 1068 cm so I used a wall to measure the distance horizontally rather than vertically. I used a measuring tape laid on the ground and held the MB1260 and my laptop in hand. Tried to maintain it vertically over the exact reading I read the distance from CLI and here are the results.

    Actual Distance Measured Distance Ratio
    30 23 1.304348
    40 30 1.333333
    50 39 1.282051
    100 75 1.333333
    150 111 1.351351
    200 145 1.37931
    250 194 1.28866
    350 263 1.330798
    500 377 1.32626
    750 560 1.339286
    800 699 1.144492

     

     

     

     

    So from here it seem that except the last value the rest of the line is pretty straight. So I did some math and used an average value of 1.33 as scaling factor.Measured the distance again found some more error and changed the scaling value to 1.4. So now finally
    100cm == 100 cm meaning the sonar correctly measures the 1 meter distance. Tried it on 500m it had an error of 5 cm. But this is pretty accurate with this rough method of measurement. Now I have changed the code to following in AP_RangeFinder_MaxsonarXL.h

     

    #define AP_RANGEFINDER_MAXSONARXL_MIN_DISTANCE 20
    #define AP_RANGEFINDER_MAXSONARXL_MAX_DISTANCE 1068

    class AP_RangeFinder_MaxsonarXL : public RangeFinder
    {
     // public:
        //AP_GPS_MTK(Stream *s);
      public:
        AP_RangeFinder_MaxsonarXL(AP_ADC *adc, ModeFilter *filter);

        int convert_raw_to_distance(int _raw_value)
        {
            if(_raw_value!= 20)
             return (_raw_value*1.4);     //Added scaling to compensate the error
             else
                 return(20);
        }   // read value from analog port and return distance in cm
    };
    #endif

     This code is works quite accurately in range of 30 to 200 cm then I encountered a few cm different.

  • Ok Thanks I need sometime to work it out and then I shall inform the results

  • Sorry my mistake I have MB1260 XL-MaxSonar-EZL0 High Performance Ultrasonic Range Finder purchased from DIY site.

  • Thanks for help and sorry i replied late since in this part of world we are 12 hours ahead of so I just woke up and saw you reply and as first thing in the day I am writing this

     

    I have MB 1260LV.

  • Moderator

    My sensors use this code:

     

    ## from AP_RangeFinder_MaxsonarXL.h

    ...

    int convert_raw_to_distance(int _raw_value) { return _raw_value; }   // read value from analog port and return distance in cm

     

    So, raw value IS cm

    But I'm not sure what hardware you have, or what code path is followed for you...

     

  • I think my C skills are not very good. However if you could please see where this code is lying then may be we can figure out the actual change required in the code to fix the sonar issue. I also tried the scaling thing but that is not accurate as well

     

  • Moderator

    Sorry, I haven't looked in a while, but is this code from AP_RangeFinder_SharpGP2Y.h even called when a Maxbotics sonar is used? From memory, I did not think so, but I could be mistaken.

This reply was deleted.

Activity