Development and testing of ultrasonic distance measurement library for device HC-SR04.

Dependencies:   TextLCD_improved mbed Distance_HC_SR04

Branch:
CLASS_IMPLEMENTATION
Revision:
4:bb5819be6ac3
Parent:
3:cb5931861f4e
Child:
5:1a69f40177b0
--- a/main.cpp	Sun Dec 20 21:37:27 2015 +0000
+++ b/main.cpp	Sun Dec 20 22:29:18 2015 +0000
@@ -1,12 +1,12 @@
 /*
- * TSK_MAIN.CPP
+ * MAIN.CPP
  */
 #include "mbed.h"
 
 #include "TextLCD.h"
 
 #define TIMEOUT_DELAY_US    (25000)
-#define CALC_COEFF          (1000.0f*340.0f/2)
+#define CALC_COEFF          (340.0f/(2.0f*1000.0f))
 #define TICKS_RANGE_MAX     (15000)
 #define TICKS_RANGE_MIN     (150)
 #define TRIG_PULSE_US       (50)
@@ -17,28 +17,9 @@
 
 TextLCD lcd(PA_8, PA_7, PA_9, PA_1, PB_5, PA_10, TextLCD::LCD16x2);
 
-static DigitalOut trigDist(PB_9);
-static DigitalIn echoDist(PA_6);
-
-static uint32_t timer_ticks;
-static float time_float;
-static uint32_t timer_ticks_min;
-
-static Timer timer;
-
-static Timeout timeout;
-
-static volatile Distance_HC_SR04_state state;
-
-void tout(void) {
-    if (state == STARTED) 
-        state = TIMEOUT;
-}
-
-
 class Distance_HC_SR04 {
 public:
-    Distance_HC_SR04(PinName trig, PinName echo, uint32_t tout_us = TIMEOUT_DELAY_US, float coeff = CALC_COEFF) : _trig(trig), _echo(echo), _tout_us(tout_us), _coeff(coeff) {
+    Distance_HC_SR04(PinName trig, PinName echo, uint32_t tout_us = TIMEOUT_DELAY_US, float coeff = CALC_COEFF, uint32_t tmin_us = TICKS_RANGE_MIN, uint32_t tmax_us = TICKS_RANGE_MAX) : _trig(trig), _echo(echo), _tout_us(tout_us), _coeff(coeff), _tmin_us(tmin_us), _tmax_us(tmax_us) {
         _trig = 0;
         _state = IDLE;
     }
@@ -57,7 +38,7 @@
 
             if (_echo == 0) {
                 _state = STARTED;
-                timeout.attach_us(this, &Distance_HC_SR04::_tout, TIMEOUT_DELAY_US);
+                _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);
@@ -92,7 +73,7 @@
     }
     
     float getDistance(void) {
-        return _ticks_us/_coeff;
+        return _ticks_us*_coeff;
     }
     
     float getCoeff(void) {
@@ -104,7 +85,7 @@
     }
     
     float measureDistance(void) {
-        return measureTicks()/_coeff;   
+        return measureTicks()*_coeff;   
     }
     
     uint32_t measureTicks(void) {
@@ -146,7 +127,16 @@
         if (_state == STARTED) {
             _timer.stop();
             _ticks_us = _timer.read_us();
-            state = COMPLETED;
+
+            if (_ticks_us < _tmin_us) {
+                _ticks_us = 0;
+                _state = OUT_OF_RANGE_MIN;
+            } else if (_ticks_us > _tmax_us) {
+                _ticks_us = 0;
+                _state = OUT_OF_RANGE_MAX;
+            } else {
+                _state = COMPLETED;
+            }
         }
     }
  
@@ -154,22 +144,20 @@
     DigitalOut  _trig;
     InterruptIn _echo;
     uint32_t    _tout_us;
+    uint32_t    _tmin_us;
+    uint32_t    _tmax_us;
     float       _coeff;
     
     Timer       _timer;
     Timeout     _timeout;
     
     volatile Distance_HC_SR04_state _state;
-    uint32_t    _ticks_us;
+    uint32_t                        _ticks_us;
 };
  
 
 
 int main() {
-    
-    trigDist = 0;
-    state = IDLE;
-    timer_ticks_min = 999999;
 
     wait_ms(250);
     lcd.cls();
@@ -179,119 +167,60 @@
     lcd.locate(0, 1);    
     lcd.printf("Row 2");
 
-    while (true) {    
-        // Priprava
-        state = STARTED;
-        timeout.attach_us(&tout, TIMEOUT_DELAY_US);
+    Distance_HC_SR04 distFront(PB_9, PA_6);
+    uint32_t ticks_us;
+    float distance;        
 
-        // Dej puls na trig
-        trigDist = 1;
-        wait_us(500);
-        trigDist = 0;
-    
-        // Zkontroluj signal
-        if (echoDist != 0) {
-            state = ERROR_SIG;
-            timer_ticks = 0;
-            lcd.cls();
-            lcd.printf("Dist.: ---", timer_ticks);
-            lcd.locate(0, 1);    
-            lcd.printf("ERROR_SIG");
-        } else {
-            // Vynuluj timer
-            timer.stop();
-            timer.reset();
-                
-            // Cekej na hrany na Echo
-            while (echoDist == 0 && state == STARTED)
-                ;
-                
-            timer.start();
-
-            while (echoDist == 1 && state == STARTED)
-                ;
-                           
-            if (state == STARTED) {
-                state = COMPLETED;
-                timer.stop();
+    while (true) {
+        ticks_us = distFront.measureTicks();
+        distance = distFront.getDistance();
+        
+        lcd.cls();
 
-                timer_ticks = timer.read_us();
-                time_float = timer.read()*CALC_COEFF;
-
-                timer_ticks_min = (timer_ticks_min < timer_ticks) ? timer_ticks_min : timer_ticks;
+        switch (distFront.getState()) {
+            case COMPLETED:
+                lcd.printf("Dist.: %u", ticks_us);
+                lcd.locate(0, 1);    
+                lcd.printf("Dist.: %.3f", distance);
                 
-                if (timer_ticks < TICKS_RANGE_MIN) {
-                    timer_ticks = 0;
-                    time_float = 0.0f;
-
-                    state = OUT_OF_RANGE_MIN;
-
-                    lcd.cls();
-                    lcd.printf("Dist.: ---");
-                    lcd.locate(0, 1);    
-                    lcd.printf("OUT_OF_RANGE_MIN");
-                } else if (timer_ticks > TICKS_RANGE_MAX) {
-                    timer_ticks = 0;
-                    time_float = 0.0f;
-
-                    state = OUT_OF_RANGE_MAX;
-
-                    lcd.cls();
-                    lcd.printf("Dist.: ---");
-                    lcd.locate(0, 1);    
-                    lcd.printf("OUT_OF_RANGE_MAX");
-                } else {
-                    lcd.cls();
-                    lcd.printf("Dist.: %u %u", timer_ticks, timer_ticks_min);
-                    lcd.locate(0, 1);    
-                    lcd.printf("Dist.: %.3f", time_float);
-                }
-            } else {
-                timer.stop();
-                timer_ticks = 0;
-                time_float = 0.0f;
-
-                lcd.cls();
-                lcd.printf("Dist.: ---", timer_ticks);
+                break;
+                
+            case TIMEOUT:
+                lcd.printf("Dist.: ---");
                 lcd.locate(0, 1);    
                 lcd.printf("TIMEOUT");
-            }
+                
+                break;
+                
+            case ERROR_SIG:
+                lcd.printf("Dist.: ---");
+                lcd.locate(0, 1);    
+                lcd.printf("ERROR_SIG");
+                
+                break;
+                
+            case OUT_OF_RANGE_MIN:
+                lcd.printf("Dist.: ---");
+                lcd.locate(0, 1);    
+                lcd.printf("OUT_OF_RANGE_MIN");
+                
+                break;
+                
+            case OUT_OF_RANGE_MAX:
+                lcd.printf("Dist.: ---");
+                lcd.locate(0, 1);    
+                lcd.printf("OUT_OF_RANGE_MAX");
+                    
+                break;
+                
+            default:
+                lcd.printf("Dist.: ---");
+                lcd.locate(0, 1);    
+                lcd.printf("OTHER");
+                    
+                break;
         }
-        timeout.detach();
 
         wait_ms(100);
-
-        state = IDLE;
     }
 }
-
-
-/*
-// A class for flip()-ing a DigitalOut 
-class Flipper {
-public:
-    Flipper(PinName pin) : _pin(pin) {
-        _pin = 0;
-    }
-    void flip() {
-        _pin = !_pin;
-    }
-private:
-    DigitalOut _pin;
-};
- 
-DigitalOut led1(LED1);
-Flipper f(LED2);
-Timeout t;
- 
-int main() {
-    t.attach(&f, &Flipper::flip, 2.0); // the address of the object, member function, and interval
- 
-    // spin in a main loop. flipper will interrupt it to call flip
-    while(1) {
-        led1 = !led1;
-        wait(0.2);
-    }
-}
-
- */ 
\ No newline at end of file