Bare Metal Programming Tool Kit
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
Static Public Member Functions | List of all members
bmptk::sr04< trigger, pulse > Class Template Reference

interface to an SR04 ultrasonic distance sensor More...

#include <sr04.h>

Static Public Member Functions

static void init ()
 constructor, supply the trigger and echo pins
 
static bmptk::time pulse_width_get (bmptk::time error_value=-1 *bmptk::us)
 measure and return the echo pulse witdh
 
static int distance_mm_get (int error_value=-1, int speed_of_sound=3430)
 measure and return the distance to a detected object
 

Detailed Description

template<typename trigger, typename pulse>
class bmptk::sr04< trigger, pulse >

interface to an SR04 ultrasonic distance sensor

This sensor can be triggered to output a short ultrasonic sound burst, and detect the reflection of this burst from a nearby solid object. The datasheet claims that it can detect objects at distances of 2 .. 400 cm. My estimate is that 4 .. 100 cm is more realistic.

The sensor needs a 5V supply (it won't work a 3.3 V), but its trigger input will trigger on a 3.3V pulse.

The sensor's echo output will produce a pulse that starts ~ 500 us after the trigger pulse, and ends when the echo is received. Hence the width of the pulse is proportional to the distance of the object.

The get_pulse_length() method (and hence the get_distance() method) uses busy waiting (in a tight loop, not by calling bmptk wait function) to generate the trigger pulse and to measure the echo pulse. When the pulse does not start within 1 ms after the trigger (expected time is 500 us), or when the echo pulse is longer than 30 ms (corresponding to > 5 m) an error is returned.

To avoid problems with late reflexions of a pulse influencing a next measurement it is advised to observe a time interval of at least 60 ms between calls to the get_pulse_length() or get_distance() methods.

Definition at line 66 of file sr04.h.

Member Function Documentation

template<typename trigger , typename pulse >
static int bmptk::sr04< trigger, pulse >::distance_mm_get ( int  error_value = -1,
int  speed_of_sound = 3430 
)
inlinestatic

measure and return the distance to a detected object

This method needs the speed of sound to calculate the distance from the echo pulse length it gets from get_pulse_width(). This speed must be supplied as units of distance per microsecond. The returned result is expressed in the same unit of distance. The default speed_of_sound parameter value is 3430 mm/us, hence the default unit of the returned value is mm.

This method uses busy waiting. It can take up to 32 ms. When no echo pulse is seen or it is too long the error_value is returned.

Definition at line 137 of file sr04.h.

template<typename trigger , typename pulse >
static bmptk::time bmptk::sr04< trigger, pulse >::pulse_width_get ( bmptk::time  error_value = -1 * bmptk::us)
inlinestatic

measure and return the echo pulse witdh

The unit is the bmptk timing unit, so to convert for instance to microseconds divide the result by bmptk::us.

This method uses busy waiting. It can take up to 32 ms. When no echo pulse is seen or it is too long the error_value is returned.

Definition at line 84 of file sr04.h.


The documentation for this class was generated from the following file: