Library for Distance Sensors

I am starting this topic to inform you about a small lookup table based library for the Sharp GP2Y0A21YK analog IR distance sensor.
The main goal is to provide a library that simplifies the usage of this particular sensor in Arduino projects.

Usage

  • Download the source
  • Place the DistanceGP2Y0A21YK folder in your Arduino1.0+ "libraries" folder
  • Open example sketch: "file", "Examples", "DistanceGP2Y0A21YK", "Centimeter" (or "Voltage" or "Raw")
  • Connect the analog sensor to port A0 (and connect Vcc and GND)
  • Compile & upload code
  • Sensor data should be arriving over the serial port

http://code.google.com/p/gp2y0a21yk-library/

Hi

So this lib is pretty usefull. But i wonder, how can I interpret a distance measurement with a displacement at the same time? Like having a xx mm/sec or inch/min. Just like a feed rate
If you may have a hint, i'm stall on this and have not much idea.

thanks again

Depending on your program/goals you could:

  • Sample the sensor from time to time and use "millis()" to calculate the time that has passed and then calculate the average displacement
  • Sample the sensor using fixed timing, e.g. 10 samples per second, using a periodic wake-up of the board, and then calculate the average displacement. Periodic wake-up can be achieved using this nice library: GitHub - rocketscream/Low-Power: Low Power Library for Arduino

I would prefer the second option.

Hi doggenj,

good work,

Have you checked my multimap article - Arduino Playground - MultiMap - Wrote that because the standard map function could not handle the non-lineair function.
Multimap uses two small lookup tables with interpolation between points.

You could try to make your lib more generic by keep the device specific stuff separate.

2 cents

Is was not aware of your multimap code, but it might be interesting to add your approach as an extra option since it will result in a smaller code size, at the expense of some execution speed.

What you are doing in the multimap function is almost what I did in a spreadsheet to calculate my LUT values.
I measured some values and calculated the intermediate points using two polynomial approximation of the tranfer function. (one for 80 to 20 cm, an other one for 20 to 5 cm).
You can find the (messy) spreadsheet here: (the comments are in Dutch): Google Code Archive - Long-term storage for Google Code Project Hosting.

How exactly would this make the lib more generic? Since the only hardware specific code is the actual LUT.
I plan to make one generic "DistanceSensor" lib, that supports other distance sensors. (e.g. Google Code Archive - Long-term storage for Google Code Project Hosting. ('sonar' sensor))

How exactly would this make the lib more generic? Since the only hardware specific code is the actual LUT.

That means the lib should have a reference to an external LUT in the constructor. (Could not open the spreadsheet BTW..)

What do you think of an option for runtime calibration?
Or a separate sketch that can generate the LUT? put the sensor at 10 cm press button, 20 cm press button etc

I plan to make one generic "DistanceSensor" lib, that supports other distance sensors.

Great , think the multimap approach can help to keep the different LUT's small !

direct link to the spreadsheet: http://gp2y0a21yk-library.googlecode.com/hg/Calculations/CalculationsDistanceSensorTransferfunctionLUT.xls
(works fine for me using LibreOffice)

The option for runtime calibration is planned for "Version 0.6: Compare different sensors, calculate replacement LUT (at runtime) based on calibration points" (copy past from header file :slight_smile: )

I currently have 5 sensors that are tied together sitting on my desk.
My plan is to do this runtime testing/calibration using 5 sensors to check for inter-sensor differences.
I haven't done the experiment yet because I needed some extra wires + breadboard, to connect them all at the same time.

It think the best approach would be conditional compilation with 3 options:

  • speed optimized: my current code
  • code size optimed: the multimap version using your approach
  • default: 4(?) bit LUT combined with multimap

A more (too?) complex solution might be:

  • a standard generic LUT: 8 bits
    • a 'differential' LUT for each individual sensor: using calibrated values: 4 bits

Lots of possibilities...

I have a semi-decent version of the generic DistanceSensor class.
(warning: the code compiles but is not yet tested using actual sensors)

Top level class: DistanceSensor
Level1 classes: hardware type/group specific code: class for analog sensors and class for timing based/acoustic sensors (AccousticDistanceSensor, AnalogDistanceSensor)
Level2 classes: sensor type specific code: (DistanceSRF04, DistanceGP2Y0A21YK)

Now adding other sensors with different LUTs or timing options should be straightforward: just add a new "level2" class.

Source: Google Code Archive - Long-term storage for Google Code Project Hosting.

Doxygen documentation: http://arduino-distance-sensor-library.googlecode.com/hg/Documentation/latex/refman.pdf

Inheritance diagram:

Update: The current version provides a unified interface for four sensors:

SRF04 & SRF05 Ultrasonic Ranger
Sharp GP2Y0A21YK IR Distance sensor
Sharp GP2Y0A41SK IR Distance sensor

This version no longer uses a LUT, but calculates the transfer functions of 1/D at runtime (they are nearly linear).

Inheritance diagram:
http://arduino-distance-sensor-library.googlecode.com/hg/Documentation/InheritanceDiagram-DistanceSensor_v2.png

Hey, great work.
Can I use the GP2Y0A21YK library with Arduino due ?

thanks

The version with the LUT is AVR specific, but the generic library that calculates 1/D should work just fine on the Due.
(Google Code Archive - Long-term storage for Google Code Project Hosting.)

many thanks :slight_smile:

Hi Doggenj, I am newbi in Arduino Program.
Now I want to make detection obstacle using arduino and 3 pieces IR Sensor GP2Y0A21YK.
the IR will be working one by one by them self on 1 board Arduino.
They can detect the obstacle when IR near of the obstacle.

I hope you will answer me with fast. Thank you very much.