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;
         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.

Views: 795

Reply to This

Replies to This Discussion

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.

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


From memory, when I last looked, the raw value from the maxbotix is in cm. 

I have three books here that do not agree, they offer three different formulas to convert analog raw to distance measurements. But I have not found that any of them work.

And if I read the code correctly when rushing through another "homework" assignment f... of the good will of our helpful members here, it seems to be the same conclusion the author of our libraries came to...


However, I use the XL rather than the LV modules, and I also read in some spec sheet somewhere that they might perform differently... which are you using?


If you read far enough in the thread above, it will show you a simple example of code for how to read the analog, and discusses some of my concerns about various algorithms. One of those takes the raw value by 1.24. However, in physical tests of the XL, I do not see that it works properly. Another is far more complex.... from memory, it tries to convert the raw value to mV, I think it was 5000 / 1024 * value, then you convert from mV to inches by mV/9.8 which yields cm, then multiply by 2.54. I've even seen examples where the author then convert all the values to inches, then back to cm, using integers (similar to what is done in the PW and analog examples on the Arduino hardware examples page!) which of course means that you never have any better resolution than an inch, even though the sensor is very capable of giving you cm's. 


Anyway, lets start with your hardware, what product line do you have? XL or LV?

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...


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.

The LV specific files were removed on 4 sept 2011 because 


AP_RangeFinder - removed LV version because we use XL which covers both types of sonars


You should use AP_RangeFinder_MaxsonarXL.h to adjust your sonar behavior, if that is what you want to do.


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

same code. 


the raw value is taken as cm. If that is not right, then edit the following function in 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


For example, if the current value is 23, but you measure 46 cm, and also if you try 500 cm, but the sensor reports 250 cm, then you have it easy, because you can see it is linear. So you might change it to:


int convert_raw_to_distance(int _raw_value) { 

        int _real_cm;

        _real_cm = _raw_value * 2;

        return _real_cm; 


So if it is not generating a correct value, then build a table:


Real Distance Sensor Distance

22 15 // do not use less than ~21 cm real distance, the sensor cannot detect less

35 28

45 32

55 43


745 532

755 601

765 640  // 765 is about max range for this sensor


The try to find a formula that matches for all values. Post your table and formula here please.

You should take five or ten readings for each distance, then find the mod or median and use that value as you will get some noise.





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

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



class AP_RangeFinder_MaxsonarXL : public RangeFinder
 // public:
    //AP_GPS_MTK(Stream *s);
    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
    }   // read value from analog port and return distance in cm

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

I am working with Riley.

Using the same sonar unit as Farooq we attained these measurements for our sensor:

Actual distance Reported Distance Ratio

34cm 47cm 1.38

62cm 88cm 1.41

94cm 134cm 1.42

117cm 164cm 1.40

(excuse my abuse of significant figures)

From an eye test linear regression, 1.4 seems to be the scaling factor. We made the change in the code to exactly the code that Farooq posted and we are getting accurate readings now. 


Flight test pending. 


I didn't mention this at the time, but I would focus even more on the 20-30cm range to get a more accurate reading there, then test that out to 700cm or so. I'm not sure if you will find 1.4 as ideal. There are a range of other formulas which might be more accurate, but I suppose you only need it just so accurate...


As I said in the other thread on this, I wanted to do more testing, especially in the 20-30 cm range, but haven't found the time. So many wonderful things to work on. But I hope this discussion was helpful...

Reply to Discussion



Season Two of the Trust Time Trial (T3) Contest 
A list of all T3 contests is here. The current round, the Vertical Horizontal one, is here

© 2020   Created by Chris Anderson.   Powered by

Badges  |  Report an Issue  |  Terms of Service