Pages: [1]   Go Down
Author Topic: Library for Distance Sensors  (Read 6408 times)
0 Members and 1 Guest are viewing this topic.
Belgium
Offline Offline
Newbie
*
Karma: 2
Posts: 25
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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/
« Last Edit: May 21, 2012, 03:27:27 pm by doggenj » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Belgium
Offline Offline
Newbie
*
Karma: 2
Posts: 25
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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: https://github.com/rocketscream/Low-Power

I would prefer the second option.
« Last Edit: January 21, 2012, 07:17:47 am by doggenj » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Hi doggenj,

good work,

Have you checked my multimap article - http://arduino.cc/playground/Main/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

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Belgium
Offline Offline
Newbie
*
Karma: 2
Posts: 25
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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): http://code.google.com/p/gp2y0a21yk-library/source/browse/Calculations/CalculationsDistanceSensorTransferfunctionLUT.xls

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. http://code.google.com/p/srf04-library ('sonar' sensor))
« Last Edit: January 22, 2012, 02:47:53 pm by doggenj » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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

Quote
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 !
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Belgium
Offline Offline
Newbie
*
Karma: 2
Posts: 25
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 :-) )

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...

« Last Edit: January 22, 2012, 04:07:17 pm by doggenj » Logged

Belgium
Offline Offline
Newbie
*
Karma: 2
Posts: 25
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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: http://code.google.com/p/arduino-distance-sensor-library/

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

Inheritance diagram:
Logged

Belgium
Offline Offline
Newbie
*
Karma: 2
Posts: 25
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

thanks
Logged

Belgium
Offline Offline
Newbie
*
Karma: 2
Posts: 25
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The version with the LUT is AVR specific, but the generic library that calculates 1/D should work just fine on the Due.
(http://code.google.com/p/arduino-distance-sensor-library/)
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 32
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

many thanks  smiley
Logged

Pages: [1]   Go Up
Jump to: