1 sensor using RTOS Timer, another 2 over RTOS Semaphore

Dependents:   Autonomous_quadcopter

Fork of HCSR04 by Antoniolinux B.

Committer:
edy05
Date:
Tue May 22 19:35:58 2018 +0000
Revision:
13:e9b99635aa2d
Parent:
12:6f6469b2f016
Final updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
antoniolinux 0:86b2086be101 1 /* File: HCSR04.h
antoniolinux 0:86b2086be101 2 * Author: Antonio Buonanno
antoniolinux 0:86b2086be101 3 *Board: STM NUCLEO F401RE,
antoniolinux 0:86b2086be101 4 *Hardware: Ultrasonic Range HC-SR04,
antoniolinux 0:86b2086be101 5 *
antoniolinux 0:86b2086be101 6 *This work derived from Arduino library,
antoniolinux 0:86b2086be101 7 *
antoniolinux 0:86b2086be101 8 * Desc: driver for HCSR04 Ultrasonic Range Finder. The returned range
antoniolinux 0:86b2086be101 9 * is in units of meters.
antoniolinux 0:86b2086be101 10 *
antoniolinux 0:86b2086be101 11 *
antoniolinux 0:86b2086be101 12 *
antoniolinux 0:86b2086be101 13 */
antoniolinux 0:86b2086be101 14
antoniolinux 0:86b2086be101 15 /* EXAMPLE
antoniolinux 0:86b2086be101 16 #include "mbed.h"
antoniolinux 0:86b2086be101 17 #include "hcsr04.h"
antoniolinux 0:86b2086be101 18
antoniolinux 0:86b2086be101 19 //D12 TRIGGER D11 ECHO
antoniolinux 0:86b2086be101 20 HCSR04 sensor(D12, D11);
antoniolinux 0:86b2086be101 21 int main() {
antoniolinux 0:86b2086be101 22 while(1) {
antoniolinux 0:86b2086be101 23
antoniolinux 0:86b2086be101 24 long distance = sensor.distance();
antoniolinux 0:86b2086be101 25 printf("distanza %d \n",distance);
antoniolinux 0:86b2086be101 26 wait(1.0); // 1 sec
antoniolinux 0:86b2086be101 27
antoniolinux 0:86b2086be101 28 }
antoniolinux 0:86b2086be101 29 }
antoniolinux 0:86b2086be101 30 */
antoniolinux 0:86b2086be101 31 #ifndef hcsr04_H
antoniolinux 0:86b2086be101 32 #define hcsr04_H
antoniolinux 0:86b2086be101 33 #include "mbed.h"
edy05 1:53657de3246f 34 #include "rtos.h"
antoniolinux 0:86b2086be101 35
antoniolinux 0:86b2086be101 36
antoniolinux 0:86b2086be101 37
antoniolinux 0:86b2086be101 38 class HCSR04 {
antoniolinux 0:86b2086be101 39 public:
edy05 12:6f6469b2f016 40 HCSR04(PinName t1, PinName e1);
edy05 7:dcf0aa89bcd7 41
edy05 11:49a0feca5d71 42 float getDistan1();
edy05 7:dcf0aa89bcd7 43 uint16_t getDistan2();
antoniolinux 0:86b2086be101 44
antoniolinux 0:86b2086be101 45 private:
edy05 7:dcf0aa89bcd7 46 static void threadWorker1(void const *p);
edy05 7:dcf0aa89bcd7 47 static void threadWorker2(void const *p);
edy05 1:53657de3246f 48
edy05 7:dcf0aa89bcd7 49 DigitalOut trig1;
edy05 7:dcf0aa89bcd7 50 DigitalIn echo1;
edy05 7:dcf0aa89bcd7 51
edy05 7:dcf0aa89bcd7 52 // sonic thread can't run longer than thread frequency
edy05 7:dcf0aa89bcd7 53 uint32_t limit;
edy05 7:dcf0aa89bcd7 54
edy05 7:dcf0aa89bcd7 55 Timer timer1;
edy05 8:0a10bacaf501 56 Timer timerLimit;
edy05 7:dcf0aa89bcd7 57 Timer timer2;
edy05 7:dcf0aa89bcd7 58
edy05 13:e9b99635aa2d 59 osThreadId id;
edy05 13:e9b99635aa2d 60
edy05 7:dcf0aa89bcd7 61 RtosTimer *threadUpdateTimer1;
edy05 7:dcf0aa89bcd7 62 RtosTimer *threadUpdateTimer2;
edy05 7:dcf0aa89bcd7 63
edy05 11:49a0feca5d71 64 float distan1;
edy05 7:dcf0aa89bcd7 65 uint32_t distan2;
edy05 13:e9b99635aa2d 66 bool tooLong;
antoniolinux 0:86b2086be101 67 };
antoniolinux 0:86b2086be101 68
antoniolinux 0:86b2086be101 69 #endif