RelatedProducts Sharp GP2D120XJ00F IR Package Sharp GP2D12J0000F IR Package Sharp GP2Y0A02YK0F IR Package

Contents

Introduction

This article describes one way to convert the non-linear voltage output of a Sharp analog infrared range finder into a usable distance measurement.  The technique may be applied to controllers that only support integer math.

The Sharp family of infrared range finders are very popular for robotics and automatic distance measurement applications.  A drawback of these sensors is their non-linear response.  In other words, a big change in output voltage does not always indicate a big change in range.  To get range values in familiar units (like inches or centimeters), a user must find a function that converts the output voltage into a range value.

One approach that works well is to use sophisticated mathematics programs to generate a curve fit.  The functions that such programs generate are quite good but usually require floating point math and a good math library in order to implement them.  This isn't much help when using controllers that lack floating point capabilities.

Another good approach is to use a piecewise linear approximation to convert the output voltage to a range value.  This involves breaking up the response into small straight lines and doing a separate approximation for each line.  Straight line approximations are simple to compute and can be implemented with fairly good accuracy by using integer math.  The disadvantage is that they take up code space.

Ideally, it would be nice to have a single approximation function that works well with integer math.  Fortunately there are some simple calculations that can "linearize" the response of the Sharp sensors.

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.

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.

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.