Sensors:Exercise2:Infra-red Sensor
Contents |
Setup
This exercise requires the use of a infra-red range sensor. We use the Sharp GP2D12, which is available from Acroname. This sensor is easy to use, and provides fairly accurate results.
The Sharp GP2D12 from Acroname ships with cables that require a little manual installation. Read the instruction book that is supplied, and plug the cables into the appropriate slot on the JST connector.
The other end of the cable needs a 3 position connector, just like the one used for the photoresistor cable in Locomotion:Exercise2-Path Following.
Acroname also sells a sensor mount, which you might find useful. This mount is not necessary, but makes is much easy to attach the IR sensor to various platforms.
Sensor Calibration
Many sensors require calibration before they can be used properly. For example, stereo cameras need to be calibrated in order to create an idealized model of the physical characteristics of the camera, including lens distortions and orientation. In our case we need to calibrate a model for the GP2D12 sensor. Read through the data sheet for the GP2D12 sensor to get an idea of how the sensor works and what type of information it returns.
As you have read, this IR sensor returns a voltage value that can be used to determine the range reading. The problem is that the voltage to range function is non-linear. This leaves us with three approaches for determining the range reading. The first is to create a lookup table that matches a voltage to a range. The second option is to linearize the function and derive a simple equation for computing a range based on a voltage value. And the third is to generate a function that accurately fits the voltage-range curve of the GP2D12.
- Explain which option you would choose and why?
The option that we will pursue is linearization. Conveniently Sharp has done most of the work for us. On page 10 of the Sharp Device Specification for Distance Measuring Sensor Model No. GP2D120 there is a nice "almost" linear plot of a voltage-range function.
Where V is voltage and R is range.
However, this plot also uses the GP2D120 instead of the GP2D12. So, let's replace the 0.42 with k, which represents the constant that we need to determine for the GP2D12. In order to calculate this value, we need to gather some of our own data. This is the essential step in the sensor calibration process. Make sure to measure carefully and accurately since the performance of the sensor relies on it. The basic process of sensor calibration for the GP2D12 involves gathering a set of voltage:range values that can be used to determine the k value. Once we have this value, we can derive a nice linear function that maps any voltage value to a range.
First, hook up the IR sensor to an A2D port on either the Robostix or Command Module, and make sure that you can successfully read from the sensor. Try moving a flat obstacle in front of the sensor, and make sure the voltage values change.
Now that we have the sensor hooked up, secure it to a table so that it is facing parallel to the surface. With the sensor attached, use a ruler to mark out distances starting from the sensor. This sensor has a valid range of 10cm to 75cm, so place a mark every 2-5 cm starting at 10cm from the sensor to 75cm. Find a flat white object that can be placed at various position on the ruler you have just created. With this setup, record various voltage readings for different range values.
| Setup (Select images to enlarge) |
||
|---|---|---|
Here are some sample results: IR Sensor Calibration Data
With this data we can determine the constant of linearization for the IR sensor. Plot the data according to above equation, substituting in different values for . Start with a low value for
and slowly increase it until the plot looks linear. A good value for this IR is usually less than seven.
At this point you should have a plot of straight line which we can write and equation for using .
Substitute in the value for y and x
1/(R+k = m*V+b
Rearrange things a little.
R=1/(m*V+b)-k
This equation is fine for most uses, however embedded systems, such as the iRobot Command Module, do not handle floating point numbers. With a little more rearranging we arrive at our final equation.
R=m'/(V+b') - k
Where m' = 1/m and b'=b\m</tex>.
Since k and V are provided we only need to figure out two constants: the slope inverse m' and y-intercept inverse b'. This problem is made easy with a little program called calib located in exercises/gp2d12_calib/. This program makes use of a linear regression algorithm provided via the GNU Scientific Library. In order to use this program, you must organize your data into a file containing two columns separated by whitespace where the first column lists the range values and the second the analog voltage readings. There is a sample file called gp2d12_sample.data located in the same directory.
The program takes two command line arguments. The first argument is the data file and the second is the desired value for k. The following example demonstrates how to run the program with the sample data file and k=4.
$ ./calib gp2d12_sample.data 4
The program will output you data, just as a safety check, and the values of m' and b'.
R[10] V[519.9] R[12] V[447.7] R[14] V[390.29] R[16] V[348.6] R[18] V[315.1] R[20] V[284.5] R[22] V[259.89] R[24] V[247.5] R[26] V[227.5] R[28] V[210] R[30] V[197] R[32] V[187.89] R[34] V[178.1] R[36] V[165.1] R[38] V[161.69] R[40] V[152.39] R[42] V[149.3] R[44] V[133.8] R[46] V[134.5] R[48] V[132.39] R[50] V[124] R[52] V[118.3] R[54] V[115.19] R[56] V[106.59] R[58] V[104.69] R[60] V[107.4] R[62] V[102.69] R[64] V[101.59] R[66] V[98.5] R[68] V[94.3] R[70] V[86.19] R[72] V[83.8] R[74] V[82.9] R[76] V[86.8] R[78] V[84.3] R[80] V[82.69] B'[10.2077] M'[7236.53]
You have now completed the sensor calibration. Use range equation defined above with your value for k and the computed values for m' and b'. If you are using the Gumstix solution, then use the whole floating point values for all your numbers. If you are using the iRobot Command Module, then use only the integer portions.
Obstacle Avoidance
At this point you should have a working IR sensor. If not, then complete exercise 1 first. The first thing we will use this new sensor for is obstacle avoidance which is one of the most important features of a robot. The Roomba and Create robots were designed to be harmless when they bump into objects. However, we will now consider these collisions to be extremely bad. Fortunately, the IR sensor provides range data to objects before an actual collision. The purpose of this exercise is to create a robot controller that responds to the IR sensor data in a manner that prevents all collisions.
I order to prevent all collisions IR sensors should be placed in a ring around the robot. However, this would require significant hardware and dramatically increase the controller complexity. So we will narrow the problem to one of avoiding obstacles directly in front of the robot. Now each robot needs only one IR mounted on the front of the robot facing forward. Your job is to write a controller that will move the robot in a straight line, stop before hitting an obstacle, turn roughly 90 degrees, and continue moving forward until another obstacle is detected.
Solution
Here is one solution.
![[LOGO]](/workbook/skins/workbook/create_small2.png)



