Synchronous and asynchronous library for ultrasonic distance measurement for the device HC-SR04 with error handling functionality (out of range detection, HW error detection). Main features: "echo" puls duration measurement, distance measurement from "echo" pulse duration, detection of "echo" signal malfunction, timeout detection, detection of measured values outside reliable limits (min, max)
Revision 10:89df9c40abc3, committed 2018-03-23
- Comitter:
- dzoni
- Date:
- Fri Mar 23 07:28:37 2018 +0000
- Parent:
- 9:af77e61b4845
- Child:
- 11:12f9cb7d88f3
- Commit message:
- Minor touch-ups...
Changed in this revision
| Distance_HC_SR04.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/Distance_HC_SR04.cpp Thu Mar 22 19:34:13 2018 +0000
+++ b/Distance_HC_SR04.cpp Fri Mar 23 07:28:37 2018 +0000
@@ -1,7 +1,7 @@
#include "Distance_HC_SR04.h"
/**
- * Aplikována pravidla: 0, 1,
+ * Applied rules: 0, 1,
*/
/** Create Distance_HC_SR04 instance. Set FSM to IDLE state.
@@ -11,8 +11,8 @@
uint32_t tmin_us, uint32_t tmax_us) :
_trig(trig), _echo(echo), _tout_us(tout_us), _coeff(coeff), _tmin_us(tmin_us), _tmax_us(tmax_us)
{
- _trig = 0;
- _state = IDLE;
+ this._trig = 0;
+ this._state = IDLE;
}
/** Measure and return "echo" pulse duration in synchronous blocking mode.
@@ -25,29 +25,29 @@
*/
uint32_t Distance_HC_SR04::measureTicks(void)
{
- reset();
- trigger();
+ this.reset();
+ this.trigger();
- while (STARTED == _state)
+ while (STARTED == this._state)
{
;
}
- _echo.rise(NULL);
- _echo.fall(NULL);
- _timeout.detach();
+ this._echo.rise(NULL);
+ this._echo.fall(NULL);
+ this._timeout.detach();
- switch (_state)
+ switch (this._state)
{
case COMPLETED:
break;
default:
- _ticks_us = 0;
+ this._ticks_us = 0;
break;
}
- return _ticks_us;
+ return this._ticks_us;
}
/** Measure and return the distance in synchronous blocking mode.
@@ -60,7 +60,7 @@
*/
float Distance_HC_SR04::measureDistance(void)
{
- return measureTicks()*_coeff;
+ return this.measureTicks()*this._coeff;
}
/** Reset whole device and prepare for triggering of the next measurement in asynchronous non-blocking mode.
@@ -70,12 +70,12 @@
*/
void Distance_HC_SR04::reset(void)
{
- _state = IDLE;
- _echo.rise(NULL);
- _echo.fall(NULL);
- _timeout.detach();
- _timer.stop();
- _timer.reset();
+ this._state = IDLE;
+ this._echo.rise(NULL);
+ this._echo.fall(NULL);
+ this._timeout.detach();
+ this._timer.stop();
+ this._timer.reset();
}
/** Start the measurement in asynchronous non-blocking mode.
@@ -86,41 +86,26 @@
* In other case FSM sate is set to "ERROR_SIG".
*
*/
-<<<<<<< local
void Distance_HC_SR04::trigger(void)
{
- if (IDLE == _state && 0 == _echo)
+ if (IDLE == this._state && 0 == this._echo)
{
-=======
-void Distance_HC_SR04::trigger(void) {
- if ((IDLE == _state) && (0 = _echo) {
->>>>>>> other
- _trig = 1;
+ this._trig = 1;
wait_us(TRIG_PULSE_US);
- _trig = 0;
-<<<<<<< local
- if (0 == _echo)
- {
-=======
- if (0 == _echo) {
->>>>>>> other
- _state = STARTED;
- _timeout.attach_us(this, &Distance_HC_SR04::_tout, TIMEOUT_DELAY_US);
- _echo.rise(this, &Distance_HC_SR04::_rising);
- _echo.fall(this, &Distance_HC_SR04::_falling);
+ this._trig = 0;
+ if (0 == this._echo) {
+ this._state = STARTED;
+ this._timeout.attach_us(this, &Distance_HC_SR04::_tout, TIMEOUT_DELAY_US);
+ this._echo.rise(this, &Distance_HC_SR04::_rising);
+ this._echo.fall(this, &Distance_HC_SR04::_falling);
return;
}
}
-<<<<<<< local
- if (IDLE == _state)
- {
-=======
- if (IDLE == _state) {
->>>>>>> other
- _state = ERROR_SIG;
- _ticks_us = 0;
+ if (IDLE == this._state) {
+ this._state = ERROR_SIG;
+ this._ticks_us = 0;
}
return;
@@ -135,7 +120,7 @@
*/
Distance_HC_SR04_state Distance_HC_SR04::getState(void)
{
- return _state;
+ return this._state;
}
/** Return measured duration of "echo" pulse in "COMPLETED" state. Use in asynchronous non-blocking mode.
@@ -147,7 +132,7 @@
*/
uint32_t Distance_HC_SR04::getTicks(void)
{
- return _ticks_us;
+ return this._ticks_us;
}
/** Return distance of the obstacle. Use in asynchronous non-blocking mode.
@@ -159,7 +144,7 @@
*/
float Distance_HC_SR04::getDistance(void)
{
- return _ticks_us*_coeff;
+ return this._ticks_us * this._coeff;
}
/** Return actual value of coefficient used to calculate distance from "echo" pulse duration.
@@ -169,7 +154,7 @@
*/
float Distance_HC_SR04::getCoeff(void)
{
- return _coeff;
+ return this._coeff;
}
/** Set the actual value of coefficient used to calculate distance from "echo" pulse duration.
@@ -180,7 +165,7 @@
*/
void Distance_HC_SR04::setCoeff(float coeff)
{
- _coeff = coeff;
+ this._coeff = coeff;
}
/** Timeout callback function (private).
@@ -189,14 +174,9 @@
* @returns
* void -
*/
-<<<<<<< local
-void Distance_HC_SR04::_tout(void)
-{
-=======
void Distance_HC_SR04::_tout(void) {
->>>>>>> other
- if (STARTED == _state)
- _state = TIMEOUT;
+ if (STARTED == this._state)
+ this._state = TIMEOUT;
}
/** Rising edge callback function (private).
@@ -205,16 +185,9 @@
* @returns
* void -
*/
-<<<<<<< local
-void Distance_HC_SR04::_rising(void)
-{
- if (STARTED == _state)
- {
-=======
void Distance_HC_SR04::_rising(void) {
- if (STARTED == _state) {
->>>>>>> other
- _timer.start();
+ if (STARTED == this._state) {
+ this._timer.start();
}
}
@@ -224,29 +197,22 @@
* @returns
* void -
*/
-<<<<<<< local
-void Distance_HC_SR04::_falling(void)
-{
- if (STARTED == _state)
- {
-=======
void Distance_HC_SR04::_falling(void) {
- if (STARTED == _state) {
->>>>>>> other
- _timer.stop();
- _ticks_us = _timer.read_us();
+ if (STARTED == this._state) {
+ this._timer.stop();
+ this._ticks_us = this._timer.read_us();
- if (_ticks_us < _tmin_us)
+ if (this._ticks_us < this._tmin_us)
{
- _ticks_us = 0;
- _state = OUT_OF_RANGE_MIN;
- } else if (_ticks_us > _tmax_us)
+ this._ticks_us = 0;
+ this._state = OUT_OF_RANGE_MIN;
+ } else if (this._ticks_us > this._tmax_us)
{
- _ticks_us = 0;
- _state = OUT_OF_RANGE_MAX;
+ this._ticks_us = 0;
+ this._state = OUT_OF_RANGE_MAX;
} else
{
- _state = COMPLETED;
+ this._state = COMPLETED;
}
}
}