When comparing a couple of I2C (digital) sonars with the normal (analog) sonars I noticed the range reported by the analog sonar appeared to be wrong. A tape measure proved the digital sonars were spot on and the analog data was well off. Other users have reported similar problems (here and here).
Some work with a spread sheet, schematics and the sonar data sheets helps point to the causes of this.
First the sonar module analog output is in proportion to the Vcc input to the sonar - this is a particular problem when running on USB where your Vcc can be down around 4.8 volts - you can not assume an accurate 5 volts to the sensor. This means you can not just measure the analog voltage output and hope to calculate an accurate range.
Second different Maxbotics sonar models output different voltages per centimetre. Even within the XL model range there is a 200% difference in output voltage for the same range. eg (from data sheets):
LV - EZ0 MB1000:(Vcc/512) per inch
XL- EZ0 MB1200: (Vcc/1024) per cm.
XL- EZL0 MB1260: (Vcc/1024) per 2 cm.
Third the analog port on the APM / IMU shield uses the ADS7844 external Analog to digital converter. This device does a 12 bit conversion relative to a reference voltage. The 'Foxtrot' version schematic shows this reference is 3.3 Volts. The ADS7844 data sheets says in this configuration it will digitize a signal in the range 0V to 3.3V. This appears to mean we cannot reliably use the sonars to their maximum range as they will output greater than 3.3 volts at some point (any range over 5 meters for the MB1200 and over 7 metres for the 1260). I wonder if this is why some users report strange 'zoom climb' behaviour when in altitude hold - particularly in the 4 to 8 metres altitude range.
The Rangefinder library makes no allowance for either of the three points above. It assumes the numbers coming out of the ADC are directly proportional to centimetres, and this is not the case at all. If you have a MB 1200 it will read twice the range of a MB1260 if you have a MB1000 then you have to convert from inches to metric and allow for half the resolution. If your power supply voltage (Vcc) is not known then you can not accurately calculate your range. If your sonar signal goes over 3.3 volts you may not get the correct result from the ADC.
Below is a screen grab of my google spreadsheet that shows the difference in reported range and actual range and the ability to calculate an fairly good adjusted range by taking the points above into account. I haven't tried this at longer ranges yet so cannot test the 3.3 Volt limit theory.
Before we charge off and re-write the Rangefinder library I'd like to hear what people think of the theories outlined above.
(for those that are curious the digital (I2C) sonars use timing of pulses to indicate distance so no voltages are involved and so they avoid all of these issues)