WebbotLib AVR library
WebbotLib It just does it
  C++ documentation  C documentation

Distance

Supports measurement of distances.
All reading from this library are in 'cm'.
What do they do? They measure the distance from the device to an obstruction in front of it.
Since all of the devices have been implemented in the same way then it means that you can swap one device for another by only changing the #include and the MAKE command used to create the device.
But note that you may not always get the same results.
Why not?
The devices that use an infra red light beam, like the Sharp sensors, have 'tunnel vision'. When they say there is nothing ahead then what they mean is 'there is sufficient gap to pass through a beam of light' - and your robot may be slightly wider than a beam of light. If not then let me know and perhaps we can become billionaires together. This is normally circumvented, to a degree, by mounting the sensor on a servo and flicking it from side to side like a 'laser light show'. But note that this could still miss thin objects like a chair leg.
Sonar devices emit a 'blip' of sound and listen for echoes. The width of this beam of sound dictates how 'fuzzy' the result is. Some sonars emit a very directed sound pulse whereas others use a fog-horn approach.
So think of sonars as being pessimistic and fuzzy, and light beams as optimistic and precise.
For 'exact' work you may want to use both - ie when the sonar says there is something ahead then use light beams to scan that area.
If you are using more than one of the same type then you need to be careful to avoid incorrect readings. For example: if you have two sonars sending out 'pings' of sound simultaneously then one sonar may hear the echo of the sound produced by the other sensor.This is called 'ghosting' - and also applies to light beam sensors.
The library already makes sure that a given sensor cannot receive ghost replies from the previous time it was read but if you are using more than one sensor then you may want to add extra delays between accessing each sensor.
So here is the generic way to work with a <DEVICE> of a given <MAKE> and <MODEL>:-
// Include the relevant sensor file
#include "Sensors/Distance/<MAKE>/<MODEL>.h"
// Create the device
<DEVICE> myDistance = MAKE_<DEVICE>(ADC pin);
In your appInitHardware you should initialise the device:-
distanceInit(myDistance);
Then in your main loop you can read the sensor using:-
distanceRead(myDistance);
The value can then be read independently into a variable of type 'DISTANCE_TYPE':-
DISTANCE_TYPE cm = myDistance.distance.cm;
Or dumped to the current rprintf destination using:-
distanceDump(myDistance);

 

File

 

Valid XHTML 1.0 Transitional