go_away

### Author Topic: Sharp IR data interpretation  (Read 3808 times)

0 Members and 1 Guest are viewing this topic.

#### nextscieng

• Beginner
• Posts: 6
##### Sharp IR data interpretation
« on: November 08, 2008, 01:06:57 AM »
Does anyone knows a formula to interpret the data from GP2D12 and convert it into actual distance. I'm working on basic 2D mapping and need to save some time.

Thanks,
nextscieng

#### airman00

• Contest Winner
• Supreme Robot
• Posts: 3,650
• narobo.com
##### Re: Sharp IR data interpretation
« Reply #1 on: November 08, 2008, 07:24:44 AM »
Its not linear data so follow these instructions as said in the Sharp IR tutorial - http://www.societyofrobots.com/sensors_sharpirrange.shtml

Quote
Non-Linear Ouput
The Sharp IR has a non-linear output. This means that as the distance increases linearly (by set increments), the analog output increases/decreases non-linearly. The image above is a typical expected output from your range finder. Notice the strange kink in the beginning of the graph. This is because the range finder is not capable of detecting very short distances. Refer to the particular range finder you are using to determine the range that your range finder is capable of.

Sharp IR Range Finder Calibration

To effectively use your Sharp IR Range Finder, you must have a voltage output versus distance chart to reference from. The manuals now come with a 'typical response curve' graph for you to use, but you should check just to make sure it is accurate. If you do not have a chart, or you would like to verify the chart, run an experiment that measures distance versus the output analog value. To do this, place an object in front of your sensor, measure the distance, then look at the printf output reading. Graph your data. I recommend reading my article on advanced sensor interpretation to help you make better sense of the data. Typically people either create a lookup table or create a representative equation of the distance function.

To minimize any noise, do this experiment in the environment you wish your robot to operate in. For example, if you want your robot to operate on a factory floor, run this experiment on a factory floor - this will make sure all ambient conditions are the same for highest accuracy. This should be a good rule of thumb for calibrating any sensor.
Check out the Roboduino, Arduino-compatible board!

www.Narobo.com

#### mbateman

• Full Member
• Posts: 82
##### Re: Sharp IR data interpretation
« Reply #2 on: November 08, 2008, 06:46:56 PM »
It is non-linear, but I found that when converting to an integer over the short distances that it can see, that the "real" formula was overkill and slow since it uses floating point math. You can simplify it greatly (as well as speed it up) and still get "good enough" with the following function to get distance in inches.

Code: [Select]
`//Sharp GP2D12 IR Range Sensor#define irRawMin 16 // Min valid raw reading (approx 36")#define irRawMax 130 // Max valid raw reading (approx 4" )int sensorsGetIrDistance(int pin) { unsigned int raw=a2dConvert8bit(pin); if (raw<irRawMin || raw>irRawMax) { return 0; } else { return 600/raw-1; }}`

#### nextscieng

• Beginner
• Posts: 6
##### Re: Sharp IR data interpretation
« Reply #3 on: November 08, 2008, 10:31:25 PM »
airman00:
I've read the tutorial throughly before and the whole idea is clear. All i'm trying to do is to figure out a formula that can tell me the correlation between the actual distance (let's say, in cm's) and the data from the sensor, aquired through "a2dConvert8bit(pin)" function.
Example:
Distance (cm): 30; 28; 23; 19; 17; 13; 10;
Sensor readings (A2D): 46; 50; 59; 70; 80; 100; 119;
mbateman's formula is 500/(reading-1), which is very close to actual distance.

mbateman:
Thanks a lot, you just made it much easier for me. Here's the comparison of the actual distance vs your formula:
Act. distance (inches):         11.8; 11.0; 9.1; 7.5; 6.7; 5.1; 3.9
Results using your equation:  11.1; 10.2; 8.6; 7.2; 6.3; 5.1; 4.2
BTW, How did you figure it out?

#### mbateman

• Full Member
• Posts: 82
##### Re: Sharp IR data interpretation
« Reply #4 on: November 09, 2008, 08:01:31 AM »
Check the formula again. It is (600/reading)-1 which is a little different than what you wrote. It gets the numbers a bit closer.

Since this is all integer math, I noticed that I did not need the level of precision of the floating point formula. I plugged some actual readings and distances into Excel and plotted two curves. One for the real distance and one for the formula distance. I then played around with some formulas until I got one that had the right basic shape. Then I just changed the numerator until I got the slope right and then applied the offset (-1) to get the lines to match up. It is crude and does not give fine precision, but for my needs it was close enough as well as simple and fast.

BTW, if you want the real conversion, it is in the sensors.c code supplied with the Axon...

Code: [Select]
`//Sharp GP2D12 IR Range Sensor -  claims 10cm to 80cm (I got 8cm to 150cm)int sharp_IR_interpret_GP2D12(int value) { return 1384.4*pow(value,-.9988); }`

#### nextscieng

• Beginner
• Posts: 6
##### Re: Sharp IR data interpretation
« Reply #5 on: November 09, 2008, 12:53:17 PM »
I see,
the text was too small, so i got it wrong - sorry...
Thanks again!

#### mbateman

• Full Member
• Posts: 82
##### Re: Sharp IR data interpretation
« Reply #6 on: December 01, 2008, 11:10:14 PM »
To get the X and Y distances, you need trig functions. Since real trig functions are floating point and slow, you can use lookup tables instead. In mine, I round to the nearest 5 degrees, then look up in a table. Once you have the X and Y, you can add in offsets based on the position of the sensor relative to the center of your robot. That way if you have multiple sesnsors, you can plot them all on one map.
Code: [Select]
`#define distX(deg,dist)      doTrig(deg,dist,0)#define distY(deg,dist)      doTrig(deg,dist,1)#define trigAngleResolution  5                      // only works in 5 degree incrementssigned int trigVals[73]={100,100,98,97,94,91,87,82,77,71,64,57,50,42,34,26,17,9,     0,-9,-17,-26,-34,-42,-50,-57,-64,-71,-77,-82,-87,-91,-94,-97,-98,-100,     -100,-100,-98,-97,-94,-91,-87,-82,-77,-71,-64,-57,-50,-42,-34,-26,-17,-9,     0,9,17,26,34,42,50,57,64,71,77,82,87,91,94,97,98,100,100};signed int doTrig(signed int deg, unsigned int dist, int cos) {     signed long int result=dist;     if (! cos) {          deg-=90;                                 // Phase shift -90 deg     }     while(deg<0)     {deg+=360; }                 // Normalize to 0 - 360     while(deg>360)  {deg-=360; }                  // Normalize to 0 - 360     result*=trigVals[deg/trigAngleResolution];    // Mult by sin/cos * 100     result/=100;                                  // Divide out 100 from above     return (result);}`
« Last Edit: December 01, 2008, 11:16:50 PM by mbateman »

• Supreme Robot
• Posts: 11,696
##### Re: Sharp IR data interpretation
« Reply #7 on: December 02, 2008, 03:50:07 AM »
As this has been getting asked a lot lately, I'll say it now . . .

The *best* non-experimental method to get an equation is to simply open up the datasheet to your sensor. This is how I generate all my equations from sensors (hint hint expert tip ).

In it, you'll see a distance vs. output voltage chart. If its a light sensor, you'll see a light vs output voltage chart, etc.

Now, copy that plot into excel, where 0V is 0 and 5V is 255 using this equation:

output_voltage*255/5 = 8 bit value

Then ask excel what the equation is. Finished! It literally takes me like 15 minutes to do any sensor you throw at me.

Attached is an excel sheet with examples. A tutorial can be found here:
http://www.societyofrobots.com/sensors_interpret.shtml

If anyone develops an equation for a sensor that isn't listed in my Axon source code, send it my way and I'll include it

note: there is one 'flaw' in the non-experimental method and that is your Axon will probably not be exactly 5V. You might have 4.98V or 5.01V, but for most people its good enough.

#### nextscieng

• Beginner
• Posts: 6
##### Re: Sharp IR data interpretation
« Reply #8 on: December 02, 2008, 10:58:10 AM »
mbateman:
Thanks for the info. While waiting for a reply, i started experimenting with servo feedback. Hooked up the servo pot to the Axon and used ADC. I'll see if i can make the values into a formula. I know that rotation degrees and servo cycles are in non-linear correlation, but i still want to make it more "automated", rather than having an array of values.

Also, i'm looking for a better way to make the servo rotate without interrupting with the rest of the program. In other words, i want to have the "scanning" servo rotating at all times, while the Sharp IR is scanning.
Is that what you did? Any suggestions on how to make such an upgrade (besides having a separate MCU board for the "scanning" servos)?

Thanks again!

I've used this option already. But! How do you generate a formula from the sensor data?
I got up to the point of building a graph and that's it.
BTW, how's the weather in Thailand?

• Supreme Robot
• Posts: 11,696
##### Re: Sharp IR data interpretation
« Reply #9 on: December 02, 2008, 10:43:07 PM »
Quote
I've used this option already. But! How do you generate a formula from the sensor data?
I got up to the point of building a graph and that's it.
read the tutorial, it gives explicit step-by-step instructions

Quote
BTW, how's the weather in Thailand?
winter time - a nice and chilly 85F every day