SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
User avatar
By Selenaut
#113387
So I made an equation to get a very close match to the actual GP2Y0A02YK0F sharp ir sensor voltage curve. It is:

y=(0.011x-sqrt(2.25)-0.16)^2+0.45

where y is volts and x is distance. Trying to make it into x= form, I got this:

x=(1000*sqrt(y+2.3056))/11

So then i make an Arduino script that looks like this:
#include <SoftwareSerial.h>
#include <math.h>

void setup(){
int IRpin=0;
Serial.begin(9600);
Serial.println("Hello world!");
delay(1500);
Serial.println("This is a test.");
delay(3000);
}

void loop(){
float volts = analogRead(0)/694;
float distance_a = (1000*(sqrt(volts+2.3056)))/11;
float distance_b = (1000*(sqrt(volts+2.3056)))/11;
float distance_c = (1000*(sqrt(volts+2.3056)))/11;
float distance_d = (1000*(sqrt(volts+2.3056)))/11;
float distance_e = (1000*(sqrt(volts+2.3056)))/11;
float average_distance = (distance_a+distance_b+distance_c+distance_d+distance_e)/5;
Serial.println(average_distance);
Serial.println(volts);
delay(1000);
}
And it gives me junk data. The 694 in
"analogRead(0)/694" in line 14 is based on peak voltage outputs. Any ideas?
By waltr
#113404
Does analogRead(0) return a float or an unsigned int?
694 is a constant.
These should be cast to a float before assigning to volt.

Try:
float volts = (float)analogRead(0)/694.0;

and see if volt is a correct value now.
User avatar
By Selenaut
#113416
I tried reading the volts before and after converted into distance. The before is right, the after is wrong. There is something wrong with the conversion equation i came up with.
By waltr
#113417
One way to debug math is do one step at a time and output the intermittent calculations.
So instead of:
float distance_b = (1000*(sqrt(volts+2.3056)))/11;
Do:
float temp1 = volts+2.3056;
float temp2 = sqrt(temp1);
float temp3 = 1000*temp2;
float temp4 = temp3/11;
Serial.println(volts);
Serial.println(temp1);
Serial.println(temp2);
Serial.println(temp3);
Serial.println(temp4);

This way you can tell which of the math operations didn't work.
User avatar
By Selenaut
#113420
ah... Okay, I'll try that. But could i possibly do this?

(0.011distance-sqrt(2.25)-0.16)^2+0.45=volts

And get distance?
You can try getting it so that it's distance = (equation here), but i tried that.
User avatar
By Selenaut
#113421
Great, now i'm not even getting any analog input. AT ALL. >:(
By esklar81
#113507
Selenaut wrote:So I made an equation to get a very close match to the actual GP2Y0A02YK0F sharp ir sensor voltage curve. It is:
y=(0.011x-sqrt(2.25)-0.16)^2+0.45
where y is volts and x is distance. Trying to make it into x= form, I got this:
x=(1000*sqrt(y+2.3056))/11
For reasons known to only the malevolent gods, the detailed response I just wrote went to the Great Bit Bucket in the Sky.

In brief, you appear to have made one or more algebraic errors in inverting the equation. I tried substituting your second equation into your first, but didn't get an identity.

When I try to invert the first equation, I get:
y=(0.011x-sqrt(2.25)-0.16)^2+0.45
y=(0.011x-1.66)^2+0.45
y-0.45 = (0.011x-sqrt(2.25)-0.16)^2
sqrt(y-0.45)= (0.011x-1.66)
sqrt(y-0.45) + 1.66= 0.011x
(sqrt(y-0.45) + 1.66)/0.011 = x
which appears to work if you substitute it into the original equation.

Eric
User avatar
By phalanx
#113511
esklar81 wrote:(sqrt(y-0.45) + 1.66)/0.011 = x
I checked it by hand and then ran it through a CAS (since my algebra-fu is underutilized) and got the same result.

-Bill
User avatar
By Selenaut
#113518
Okay, I'll try that later when i have time, now that I got the analog working again. Thanks for the correction.
By nasos_i
#113615
Is the conversion equation based on the Sharp's curve? If so may also want to try this one:

x = 194.0/((7.14 + (y - 4.69)*y)*(0.18 + (y - 0.28)*y))

x: distance
y: voltage [0.45..2.75] ->x~ [150..15]

Which comes out from a least square fit of an expression of the curve in:
http://sharp-world.com/products/device/ ... 02yk_e.pdf

This fit has a deviation (absolute error) of less than 2% for most regions, except for x=150cm (y=0.45) where the error is at about 5%. The mean absolute error is about 1.3%

Furthermore if you want accuracy it is a good idea to create your own curves as they may be different from Sharp's.

One last thing: what do you mean "peak voltage" outputs? Isn't your output similar to that appearing on Sharp's datasheet? If so the voltage should be in the region: 0.45...2.75.