Erco Sharp ir range finder

hippy

Technical Support
Staff member
Unless someone can determine an alternative mathematical means or algorithm to calculate a value to that specific negative power ( x-1.15 ) which uses only PICAXE maths operations and functions, it would seem that a lookup table would be the easiest and simplest solution.
 

stan74

Senior Member
I found this to go on.
Code:
  dir ANALOG_0 In
  dir DIGITAL_10 in


  dim cm,cm10 as Integer
  dim math as Integer

  dim text as string * 16
  dim digit as string * 2

sub calc_distance
 ' from the datasheet the analog voltage is
 ' the inverse of distance, so distance can be calculated
 ' d = (1 / volts) then just scaled to suit the datasheet
 ' load ADC value in 16bit math variable.

          ' math = ADC_Read(0);
          ' math = ADRESH;
          ' math = (math * 255);
          ' math += ADRESL;

  math = readad10( 0 )
 ' now invert it; (1 / volts) use (6050 / volts) for scaling, 6050 is a constant;
 math = (6050 / math)
 if (math >= 2) then
    math = math - 2;   ' fix linear error (-2)
 end if
 if (math < 10) then
    math = 10;   ' min limit at 10cm
 end if
 if (math > 80) then
    math = 80;   ' max limit at 80cm
 end if
 ' convert from 0-99 to 2 decimal digits, 0-99cm
 cm10=0;
 do while (math >= 10)
  cm10++
  math = math - 10
  loop
 cm = math;
end sub

do forever

 CLS

 text = "Sharp 2Y0A21";
 Locate(0,0)
 Print text

 text = "Distance:";
 Locate(1,0)
 Print text
 text = "cm";
 Locate(1,13)
 Print text
 wait 3 s

 do forever

    if cm10 <> abs(cm10) Then
      locate 1,0
      Print "Error reading  "
    else
      'calc_distance;            ' Call the "calc_distance" sub;
      digit(1) = cm10 + 48;       ' tens digit;
      digit(2) = cm +48;          ' unit digit;
      locate 1, 10
      Print digit;        ' display on LCD from column 2, from character 10;
    end if
    wait 100 ms;
 loop
loop
 

hippy

Technical Support
Staff member
I found this to go on.
Code:
 ' from the datasheet the analog voltage is
 ' the inverse of distance, so distance can be calculated
 ' d = (1 / volts) then just scaled to suit the datasheet
 ' load ADC value in 16bit math variable.
The link in post #1 indicates distance calculation is more complicated than just being the inverse of the voltage which is what that code seems to calculate.

Before trying to figure out how to do it; someone needs to determine what the actual conversion function is.
 

stan74

Senior Member
Erco originally posted http://www.picaxeforum.co.uk/showthread.php?29578-Sharp-IR-Sensor-Algorithms and it's a formula to convert the non linear output voltage from the device to cm. The above code was dated 2011. I thought by now someone had figured a formula out. By the way I think they're expensive alternatives to hrs04 ultra sonic devices and not as erco said, a must have bit of kit. If I get accurate results to a cm with a tape measure I'll post the code but it's purely academic. I'd just want a couple of safe distances for a robot.
 

hippy

Technical Support
Staff member
The datasheet appears to be here -

http://www.dfrobot.com/image/data/SEN0014/gp2y0a21yk0f.pdf

That shows graphs for 'voltage against distance' and 'voltage against 1/distance' but no actual formula.

I imagine that's why there is no definitive formula per se, just various attempts to define a formula which matches and deliver a particular resolution, suited to the capabilities the programming language supports.

I would probably use a lookup table and interpolation if I were needing to measure distance as the first approach. If it's only a single distance to be detected; just measure what value that distance gives and simply use that.
 

stan74

Senior Member
I'm displaying results in a terminal and it's pretty accurate. A book 10cm away shows 10cm and so on. OK, it jumps about a bit but so does just reading adc10. I'm impressed!
 

jims

Senior Member
The datasheet appears to be here -

http://www.dfrobot.com/image/data/SEN0014/gp2y0a21yk0f.pdf

That shows graphs for 'voltage against distance' and 'voltage against 1/distance' but no actual formula.

I imagine that's why there is no definitive formula per se, just various attempts to define a formula which matches and deliver a particular resolution, suited to the capabilities the programming language supports.

I would probably use a lookup table and interpolation if I were needing to measure distance as the first approach. If it's only a single distance to be detected; just measure what value that distance gives and simply use that.
Like hippy says...it's pretty easy to do this empirically with a yard stick and a simple routine to take readings while moving a target along the yard stick. Observe the reading at the ranges that you want to use. JimS
 

stan74

Senior Member
If you have the sharp ir try this,you'll be surprised.
Code:
#picaxe 28x2
dirsa=0
start:
readadc10 a.0,w0
w0=6050/w0
if w0>=2 then
w0=w0-2
endif
if w0<10 or w0>80 then goto start
sertxd ("Distance=",#w0," cM",cr,lf)
goto start
 

AllyCat

Senior Member
Hi,

No I don't have one, but I'm not particularly surprised, it's what the data sheet indicates. ;)

The negative 1 (power) sign in the formula linked in #1 means 1/v and the 1.15 indicates that it is basically a straight line, but with a slightly exponential curvature. However, the spread of values in the data sheet (for 10 and 80 cms) suggest that any "off the shelf" formula is unlikley to be particularly accurate, so calibration may be needed. The formula in #2 of that other thread looks rather "suspicious", because (in addition to the unnecessarily high resolution of the values) it uses v, v^2, v^3 and v^4 terms, whilst what is clearly needed is a v^-1 term.

The data sheet is not clear whether the output voltage "tracks" with (i.e. is proprtional to) the supply rail so it would be wise to use a regulated 5v rail. At 80 cms the nominal output voltage is 400 mV, at 10 cms it's 2.3 volts (i.e. 1.9 + 0.4), which correspond to READADC10 values of 82 and 471 (relative to Vdd = 1023). Dividing those into 6050 gives 73 and 12 (integer result) respectively. w0 always will be >= 2 so basically it then subtracts a "fiddle factor" of 2.

It could probably be improved by including a small v^2 term, or of course using a simple interpolated lookup table from a few meaured points, as suggested above.

Cheers, Alan.
 

stan74

Senior Member
I wouldn't know Alan,I don't do sums. The video shows it works at 44cm :) honest, it works ok. For say a robot where it "thinks" ahead it's an option. I thought it was interesting and didn't take much effort to try. I tried to make the book perpendicular to the sensor, a few degrees off changes the reading. So you might get erroneous constants from a table anyway.
 

stan74

Senior Member
ps. Like us range finders,these sharp things must be used up-right. They are used horizontally because they look like eyes but used like that they "see" to one side...so I read.
cheers, stan
 

erco

Senior Member
I'm impressed!
Told you that you'd like 'em! Seems like a long time ago you ordered these. Did they just arrive?

Yeah, the exponent in that equation will make it tough to pull off with simple integer math. Looks like you're spot on at 44 cm. Depending how much accuracy you need , a simple equation might work.

Attached is a PDF from somewhere else. It uses an older Sharp sensor, not sure if the calibrations are the same as the new sensor. But the BASIC code is attached for a Stamp, showing how to use a lookup table with a few data points and interpolate in between. A little curve fitting goes a long way.

Code is quite similar to Picaxe BASIC.
 

Attachments

stan74

Senior Member
Hi erco. 1 sharp never arrived,just "bare with me" emails then ebay said complaint time expired. I got the 2nd from a uk hobby supplier but cost £12. hsr04 ultra sonic are £2. I like the straand beest you modded..wish list. Intention is a "finished" robot ie dip switches for different modes,object avoid,seek ir tv remote,line/wall follow...all at the same time maybe. Tracks look more impressive but I never finished the tank with 2 continuous 12g metal gear servos but it just needs programming,the chassis's done. I got a rev ed mp3 to fit and I'll sample some cartoon character voices.
 

stan74

Senior Member
ps. a xr2206 function generator arrived am....says..The welding installation considerations...
Where's my mig and helmet?
 
Top