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)

Dependents:   TEST_Dist_lib

Files at this revision

API Documentation at this revision

Comitter:
dzoni
Date:
Fri Mar 23 10:03:36 2018 +0000
Parent:
10:89df9c40abc3
Child:
12:5bf5a7e62c5d
Commit message:
Refactoring - coding rules application finished. Compiles OK. Needs to be tested on actual HW.

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	Fri Mar 23 07:28:37 2018 +0000
+++ b/Distance_HC_SR04.cpp	Fri Mar 23 10:03:36 2018 +0000
@@ -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)
 {
-    this._trig = 0;
-    this._state = IDLE;
+    _trig = 0;
+    _state = IDLE;
 }
 
 /** Measure and return "echo" pulse duration in synchronous blocking mode.
@@ -25,29 +25,29 @@
  */    
 uint32_t Distance_HC_SR04::measureTicks(void)
 {
-    this.reset();
-    this.trigger();
+    reset();
+    trigger();
 
-    while (STARTED == this._state)
+    while (STARTED == _state)
     {
         ;
     }
 
-    this._echo.rise(NULL);
-    this._echo.fall(NULL);
-    this._timeout.detach();
+    _echo.rise(NULL);
+    _echo.fall(NULL);
+    _timeout.detach();
 
-    switch (this._state)
+    switch (_state)
     {
         case COMPLETED:
             break;
 
         default:
-            this._ticks_us = 0;
+            _ticks_us = 0;
             break;
     }
 
-    return this._ticks_us;
+    return _ticks_us;
 }
 
 /** Measure and return the distance in synchronous blocking mode.
@@ -60,7 +60,7 @@
  */    
 float Distance_HC_SR04::measureDistance(void)
 {
-    return this.measureTicks()*this._coeff;
+    return measureTicks()*_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)
 {
-    this._state = IDLE;
-    this._echo.rise(NULL);
-    this._echo.fall(NULL);
-    this._timeout.detach();
-    this._timer.stop();
-    this._timer.reset();
+    _state = IDLE;
+    _echo.rise(NULL);
+    _echo.fall(NULL);
+    _timeout.detach();
+    _timer.stop();
+    _timer.reset();
 }
 
 /** Start the measurement in asynchronous non-blocking mode.
@@ -88,24 +88,24 @@
  */
 void Distance_HC_SR04::trigger(void)
 {
-    if (IDLE == this._state && 0 == this._echo)
+    if (IDLE == _state && 0 == _echo)
     {
-        this._trig = 1;
+        _trig = 1;
         wait_us(TRIG_PULSE_US);
-        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);
+        _trig = 0;
+        if (0 == _echo) {
+            _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);
 
             return;
         }
     }
 
-    if (IDLE == this._state) {
-        this._state = ERROR_SIG;
-        this._ticks_us = 0;
+    if (IDLE == _state) {
+        _state = ERROR_SIG;
+        _ticks_us = 0;
     }
 
     return;
@@ -120,7 +120,7 @@
  */
 Distance_HC_SR04_state Distance_HC_SR04::getState(void)
 {
-   return this._state;
+   return _state;
 }
 
 /** Return measured duration of "echo" pulse in "COMPLETED" state. Use in asynchronous non-blocking mode.
@@ -132,7 +132,7 @@
  */
 uint32_t Distance_HC_SR04::getTicks(void)
 {
-    return this._ticks_us;
+    return _ticks_us;
 }
 
 /** Return distance of the obstacle. Use in asynchronous non-blocking mode.
@@ -144,7 +144,7 @@
  */
 float Distance_HC_SR04::getDistance(void)
 {
-    return this._ticks_us * this._coeff;
+    return _ticks_us * _coeff;
 }
 
 /** Return actual value of coefficient used to calculate distance from "echo" pulse duration.
@@ -154,7 +154,7 @@
  */
 float Distance_HC_SR04::getCoeff(void)
 {
-    return this._coeff;
+    return _coeff;
 }
 
 /** Set the actual value of coefficient used to calculate distance from "echo" pulse duration.
@@ -165,7 +165,7 @@
  */    
 void Distance_HC_SR04::setCoeff(float coeff)
 {
-    this._coeff = coeff;
+    _coeff = coeff;
 }
 
 /** Timeout callback function (private).
@@ -175,8 +175,8 @@
  *   void -
  */    
 void Distance_HC_SR04::_tout(void) {
-    if (STARTED == this._state)
-        this._state = TIMEOUT;
+    if (STARTED == _state)
+        _state = TIMEOUT;
 }
 
 /** Rising edge callback function (private).
@@ -186,8 +186,8 @@
  *   void -
  */    
 void Distance_HC_SR04::_rising(void) {
-    if (STARTED == this._state) {
-        this._timer.start();
+    if (STARTED == _state) {
+        _timer.start();
     }
 }
 
@@ -198,21 +198,21 @@
  *   void -
  */    
 void Distance_HC_SR04::_falling(void) {
-    if (STARTED == this._state) {
-        this._timer.stop();
-        this._ticks_us = this._timer.read_us();
+    if (STARTED == _state) {
+        _timer.stop();
+        _ticks_us = _timer.read_us();
 
-        if (this._ticks_us < this._tmin_us)
+        if (_ticks_us < _tmin_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_MIN;
+        } else if (_ticks_us > _tmax_us)
         {
-            this._ticks_us = 0;
-            this._state = OUT_OF_RANGE_MAX;
+            _ticks_us = 0;
+            _state = OUT_OF_RANGE_MAX;
         } else
         {
-            this._state = COMPLETED;
+            _state = COMPLETED;
         }
     }
 }