I use the long range analog sensors to sense the ground and assist smooth landing on my RC aircraft. The only thing to watch is that the SAME voltage output occurs at TWO ranges, one very close and the other the real range. I check the altimeter to decide which one to use. The formula below varies with each SHARP model. Set up a spreadsheet and experiment and you will quickly find a simple formula that is accurate enough for most needs.
The following was shamelessly lifted from the ACRONAME web site at
http://www.acroname.com/robotics/inf.../irlinear.html
I have bought Sharp sensors from Acroname before and they are fine to deal with - don't forget to buy the JST cable assemblies - they are very hard to work with otherwise.
The graphs in the Acroname article will not reproduce here - go to the source to see them.
The Linearizing Function
According to page 10 of the Sharp Device Specification for Distance Measuring Sensor Model No. GP2D120, a plot of the following interpolated equation is demonstrated:
V = 1 / ( R + 0.42 )
Where V is voltage and R is range, the equation produces a very straight line. The division operation acts as a linearizing function that turns the ungainly curve into a linear plot. This observation is the key to finding a simple approximation function for a Sharp IR range finders.
The constant in the linearization function depends on the sensor type and calibration data parameters. The value of 0.42, as shown above, works well for the GP2D120 and the calibration points in the Sharp document, but may not be suitable for other sensors. This constant will be represented as k since it may change. The first step in getting a good voltage-to-range function is to find a constant k that linearizes the data. The following plot shows how the GP2D12 response graph shown above can be linearized by defining the variable k = 4.0.
The next step is to find a straight line approximation that relates the voltage to the linearizing function. This involves finding suitable m and b constants for the familiar line equation:
y = m * x + b
In this case, y is equal to the linearized range. Substituting the linearizing function from above for y and substituting V for x yields:
1 / (R + k) = m * V + b
Rearranging the equation terms gives range as a function of voltage:
R = (1 / (m * V + b)) - k
This is a useful result for languages that support floating point math, but it can be rearranged further to get:
R = (m' / (V + b')) - k
where m' = 1/m and b' = b/m . This extra step produces an equation that works nicely with integer math as will be shown below.
The Constants
Getting the constants takes a bit of work. The first step is to get some calibration data. This can be obtained experimentally or "eyeballed" from the voltage-to-range curve on the spec sheet. Create a table of voltage vs. range for a set of range values. Then create a table of voltage in controller units vs. linearized range. Some experimentation may be required to find a k constant that produces a linear plot. Do a linear regression on that data to find m and b and you're done.
Contents
The Results
For a BrainStem GP 1.0 controller using 10-bit A2D measurements (integers ranging from 0 to 1023) successful interpolation yields the following formula for a GP2D12 sensor:
R = (6787 / (V - 3)) - 4
Where m'=6787, b'=3 and k=4. The user must check the input V to prevent divide-by-zero, checking that V is larger than b'. For this formula, V must be greater than 3. With 10-bit integers, voltage measurements from a GP2D12 are typically above 80 when something 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 GP2D120 and tweaking the k offset to get a straight line, the following formula can be derived for a GP2D120:
R = (2914 / (V + 5)) - 1
Because of the integer math, the units won't be exact centimeters but these functions will provide a range measurement that is more user-friendly than just using the raw A2D reading.
Contents
Summary
These approximations work well for controllers that use integer math. For controllers that have floating point capabilities, the techniques will work even better. Successful implementation depends on the quality of the calibration data and a good choice of the k constant that straightens out the curve.
HTH
BrianT
Bookmarks