Mutila: Mouse's Utilities for Arduino
Oft-used utilities: debouncing buttons, averaging samples, and so on.
|
SonicRanger with EMA-smoothed values. More...
#include <EMASonicRanger.h>
Public Member Functions | |
EMASonicRanger (const uint8_t trigPin, const uint8_t echoPin, const uint16_t periodMs=10, const float alpha=0.5, const uint16_t minimumRange=3) | |
void | begin () |
void | update () |
uint16_t | getRange () |
Public Member Functions inherited from SonicRanger | |
SonicRanger (const uint8_t trigPin, const uint8_t echoPin) | |
void | begin () |
uint16_t | getRange () |
void | setMaxRange (uint16_t cm) |
void | setTimeoutMs (uint16_t ms) |
Additional Inherited Members | |
Static Public Attributes inherited from SonicRanger | |
static const uint16_t | DefaultMaxRangeCm = 200 |
static const uint8_t | DefaultTimeoutMs = 15 |
SonicRanger with EMA-smoothed values.
The update() function should be called frequently to sample the value from the ranger. The getRange() function is then used to fetch the moving average of values from the ranger.
Definition at line 13 of file EMASonicRanger.h.
EMASonicRanger::EMASonicRanger | ( | const uint8_t | trigPin, |
const uint8_t | echoPin, | ||
const uint16_t | periodMs = 10 , |
||
const float | alpha = 0.5 , |
||
const uint16_t | minimumRange = 3 |
||
) |
Constructor.
trigPin | the arduino pin conneced to the TRIG pin of the HC-SR04 device |
echoPin | the arduino pin conneced to the ECHO pin of the HC-SR04 device |
periodMs | the minimum delay between sampling (provided update() is called) |
alpha | the alpha value used in EMA calculation |
minimumRange | the minium range to try to detect (discard samples below this range) |
The reason for the mimimum range is that some units exhibit a problem where they randomly report skewing results.
Definition at line 5 of file EMASonicRanger.cpp.
void EMASonicRanger::begin | ( | ) |
Initialization
Typically called from setup() to intialize pins and so on.
Definition at line 13 of file EMASonicRanger.cpp.
uint16_t EMASonicRanger::getRange | ( | ) |
Fetch exponential moving average
Definition at line 33 of file EMASonicRanger.cpp.
void EMASonicRanger::update | ( | ) |
Update the ranger (get range from hardware)
NOTE: this function calls SonicRanger::getRange(), which may take significant time to execute - see SonicRanger::getRange documentation for details.
Definition at line 21 of file EMASonicRanger.cpp.