Development and testing of ultrasonic distance measurement library for device HC-SR04.
Dependencies: TextLCD_improved mbed Distance_HC_SR04
Diff: main.cpp
- Revision:
- 0:6fd0fbcfc7e1
- Child:
- 2:aba8d0d53190
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun Dec 20 20:27:09 2015 +0000 @@ -0,0 +1,162 @@ +/* + * TSK_MAIN.CPP + */ +#include "mbed.h" + +#include "TextLCD.h" + +#define TIMEOUT_DELAY_US (25000) +#define CALC_COEFF (1000.0f*340.0f/2) +#define TICKS_RANGE_MAX (15000) +#define TICKS_RANGE_MIN (150) + +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 enum { IDLE, STARTED, COMPLETED, TIMEOUT, OUT_OF_RANGE_MIN, OUT_OF_RANGE_MAX, ERROR_SIG } state; + +void tout(void) { + if (state == STARTED) + state = TIMEOUT; +} + +int main() { + + trigDist = 0; + state = IDLE; + timer_ticks_min = 999999; + + wait_ms(250); + lcd.cls(); + + lcd.cls(); + lcd.printf("Row 1"); + lcd.locate(0, 1); + lcd.printf("Row 2"); + + while (true) { + // Priprava + state = STARTED; + timeout.attach_us(&tout, TIMEOUT_DELAY_US); + + // 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(); + + timer_ticks = timer.read_us(); + time_float = timer.read()*CALC_COEFF; + + timer_ticks_min = (timer_ticks_min < timer_ticks) ? timer_ticks_min : timer_ticks; + + 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); + lcd.locate(0, 1); + lcd.printf("TIMEOUT"); + } + } + 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