Robot range measurement!

Hi,
I am trying to get a distance reading from Sharp GP2Y0A21YK0F Infrared range finder. I used the following code to get the distance reading;

main:
readadc10 0, w2
let w2=w2+1
let w2=6500/w2
let w2=w2-4
let w3=w2
debug w3
goto main

I am getting a fluctuating reading, the distance reading seems to vary +/- 7cm from the actual value. I tried to ground the unused ADC inputs but still the problem is the same. If anyone have any experience on this please help.
Thanks in advance
Bikram

Numbers

I looked at the PICAxe commands manual for readadc10 and guess that it returns a number from 0 to 1023 for a reading of 0 to 5 volts. This gives about 4.88 millivolts per ADC unit number. The Sharp GP2Y0A021YK starts out at 0.4 volts (± 0.15) for an 80 cm reading, adding up to 1.9 volts (± 0.25) to a high of 2.3 volts at 10 cm. In ADC numbers that would be a reading of 82 (±31) at 80 cm going to 471 (±51) at 10 cm. Now each cm would appear to change the ADC reading by 5.55 units (if the sensor was linear, which it is not), so 7 cm difference would appear to be about 39 raw ADC units. The sensor itself appears to be able to vary near this in upper and lower readings just in normal operation (the plus or minus values shown). The 7 cm variation appears to be within the expected variance of the readings of the sensor.

Yet another contributor to inaccurate readings is the non-linearity of the Sharps sensors. On page 5 of the datasheet, a sort-of inverse logrithmic curve is shown comparing the voltage reading to the actual distance. To get this into something more linear, the ADC data would need to raised to some power in the program, possibly just squared to get some linearity, then scaled to whatever units if needed. The additions and divisions in your posted program might work in handling linear data, but in the range of 10 to 20 cm I’d expect an even greater error than 7 cm. Another perhaps simpler method of dealing with the non-linear data might be to create a lookup table of ADC values corresponding to what the distances are.

I’d never ever use readadc10

I’d never ever use readadc10 on a sharp IR, they are WAY to inprecise!

Just readadc, and just bytes.

I connected the output of
I connected the output of the sharp rangefinder to a digital voltmeter.I removed the voltage controller and connected a 6volt battery with a diode to get 5.3V.
The output voltage is stable upto 1 decimal place but at two decimal place the voltage fluctuates .03 volts.For example according to the datasheet the output voltage at 60cm is approximately .50volts but the reading fluctuates from .50 volts to .53 volts.
I connected a 10uF near the power supply of sharp rangefinder it did not help much.The only improvement that I got was that the fluctuation reduced to .01 vots.Adding a .33uF tantalum capacitor helped to stabilize the output a bit more.
So in the end this fluctuation of voltage after 1 decimal place remains.
The actual robot is supposed to be a wall following robot.I guess with all the motors running the electrical noise would be higher.

Word

I can echo this as I just experienced the same thing.

I switched to a simple readadc and bam, all good.

You mean “Byte”(Sorry, crap

You mean “Byte”

(Sorry, crap joke)

Oh wow
Haha. No no, that was a good joke!

Need for precision?

Is it really needed to be so precise in following a wall? Or can it simply be good enough, with the readings you have? When I walk along a fence, I don’t really worry if I’m exactly 47.295 centimeters from it, I simply walk along and feel good that I haven’t drifted towards or away from it by more than 20 cm or so.

Besides, if the robot wanders towards the wall some then winds back away, weaving back and forth, this charecteristic can be interpreted as a sign of life, and maybe even joke about how it consumed a few too many high energy electrons.

Actually I want to implement
Actually I want to implement some sort of proportional control for the robot hence the need for precision.

Different sensor

For more precision, use a sonar for near 2.5 centimeter accuracy (using pulse width or serial readings) Or a laser ranger for even better, at a scary price tag.

More info on the Sharps. Check out the oscilloscope picture half to 2/3 down the page, showing the noise level. Talks about some methods of improving precision, lookup table, programming a filter of sorts.

HiI put a 20uF capacitor

Hi

I put a 20uF capacitor around Vcc and Gnd of rangefinder the voltage fluctuations have reduced.The fluctuations are at max .01volts.

I am using this code to read the output

--------------------------------------------------------------------------------------------------------------------------------------------

main:
let pins=%00000000
readadc 0,b3

sertxd("The value is ",#b3,13,10)

pause 1000

goto main
----------------------------------------------------------------------------------------------------------------------------------------------

I connected a voltmeter at the same time .But a strange thing happens.

For example when the voltmeter reading fluctuates from .51V to .52V ,We would expect the ADC reading to be 26.

But that does not happen , the ADC reading fluctuates ,here is a sample-

voltmeter reading- 0.51Volts to 0.52 volts

ADC readings-

The value is 25
The value is 27
The value is 27
The value is 31
The value is 26
The value is 24
The value is 25
The value is 25
The value is 23
The value is 26
The value is 31
The value is 25
The value is 25
The value is 25
The value is 25
The value is 25
The value is 29
The value is 24

I have a code to get average result but I would like to know if I am doing something wrong.



Meters and ADCs
A meter will not read and dispaly a voltage variation as quickly as an ADC pin will. Programming averaging will probably be your best option.