A distance measurement class using ultrasonic sensor HC-SR04.
Dependents: Esercitazione4_4 HC-SR04 Group10_slave Oled_Gus ... more
The purpose of this library is to encourage students to develope their own classes. Instructions how to follow the development of this library for ultrasonic distance measurement are given here.
HCSR04.cpp@6:cf3e4e307d15, 2016-12-10 (annotated)
- Committer:
- tbjazic
- Date:
- Sat Dec 10 08:26:18 2016 +0000
- Revision:
- 6:cf3e4e307d15
- Parent:
- 4:aae70f15357f
Filtering with Ticker object corrected.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tbjazic | 0:8871082486ac | 1 | #include "mbed.h" |
tbjazic | 0:8871082486ac | 2 | #include "HCSR04.h" |
tbjazic | 0:8871082486ac | 3 | |
tbjazic | 0:8871082486ac | 4 | HCSR04::HCSR04(PinName echoPin, PinName triggerPin) : echo(echoPin), trigger(triggerPin) { |
tbjazic | 0:8871082486ac | 5 | init(); |
tbjazic | 0:8871082486ac | 6 | } |
tbjazic | 0:8871082486ac | 7 | |
tbjazic | 0:8871082486ac | 8 | void HCSR04::init() { |
tbjazic | 3:9a7899cf5e3a | 9 | distance = -1; // initial distance |
tbjazic | 4:aae70f15357f | 10 | minDistance = 2; |
tbjazic | 3:9a7899cf5e3a | 11 | maxDistance = 400; |
tbjazic | 6:cf3e4e307d15 | 12 | newDataReady = timerStarted = false; |
tbjazic | 0:8871082486ac | 13 | } |
tbjazic | 0:8871082486ac | 14 | |
tbjazic | 0:8871082486ac | 15 | void HCSR04::startTimer() { |
tbjazic | 6:cf3e4e307d15 | 16 | if (!timerStarted) { |
tbjazic | 6:cf3e4e307d15 | 17 | timer.start(); // start the timer |
tbjazic | 6:cf3e4e307d15 | 18 | timerStarted = true; |
tbjazic | 6:cf3e4e307d15 | 19 | echoTimeout.attach_us(this, &HCSR04::stopTimer, 25000); // in case echo fall does not occur |
tbjazic | 6:cf3e4e307d15 | 20 | echo.fall(this, &HCSR04::stopTimer); |
tbjazic | 6:cf3e4e307d15 | 21 | echo.rise(NULL); |
tbjazic | 6:cf3e4e307d15 | 22 | } |
tbjazic | 0:8871082486ac | 23 | } |
tbjazic | 0:8871082486ac | 24 | |
tbjazic | 0:8871082486ac | 25 | void HCSR04::stopTimer() { |
tbjazic | 0:8871082486ac | 26 | timer.stop(); // stop the timer |
tbjazic | 6:cf3e4e307d15 | 27 | if (timerStarted) { |
tbjazic | 6:cf3e4e307d15 | 28 | distance = timer.read() * 1e6 / 58; |
tbjazic | 6:cf3e4e307d15 | 29 | if (distance < minDistance) |
tbjazic | 6:cf3e4e307d15 | 30 | distance = minDistance; |
tbjazic | 6:cf3e4e307d15 | 31 | if (distance > maxDistance) |
tbjazic | 6:cf3e4e307d15 | 32 | distance = maxDistance; |
tbjazic | 6:cf3e4e307d15 | 33 | newDataReady = true; |
tbjazic | 6:cf3e4e307d15 | 34 | } |
tbjazic | 6:cf3e4e307d15 | 35 | timer.reset(); |
tbjazic | 6:cf3e4e307d15 | 36 | timerStarted = false; |
tbjazic | 6:cf3e4e307d15 | 37 | echoTimeout.detach(); |
tbjazic | 6:cf3e4e307d15 | 38 | echo.fall(NULL); |
tbjazic | 6:cf3e4e307d15 | 39 | } |
tbjazic | 6:cf3e4e307d15 | 40 | |
tbjazic | 6:cf3e4e307d15 | 41 | void HCSR04::turnOffTrigger() { |
tbjazic | 6:cf3e4e307d15 | 42 | trigger = 0; |
tbjazic | 0:8871082486ac | 43 | } |
tbjazic | 0:8871082486ac | 44 | |
tbjazic | 0:8871082486ac | 45 | void HCSR04::startMeasurement() { |
tbjazic | 0:8871082486ac | 46 | trigger = 1; |
tbjazic | 6:cf3e4e307d15 | 47 | triggerTimeout.attach_us(this, &HCSR04::turnOffTrigger, 10); |
tbjazic | 6:cf3e4e307d15 | 48 | echo.rise(this, &HCSR04::startTimer); |
tbjazic | 6:cf3e4e307d15 | 49 | newDataReady = false; |
tbjazic | 0:8871082486ac | 50 | } |
tbjazic | 0:8871082486ac | 51 | |
tbjazic | 0:8871082486ac | 52 | float HCSR04::getDistance_cm() { |
tbjazic | 6:cf3e4e307d15 | 53 | newDataReady = false; |
tbjazic | 0:8871082486ac | 54 | return distance; |
tbjazic | 4:aae70f15357f | 55 | } |
tbjazic | 4:aae70f15357f | 56 | |
tbjazic | 4:aae70f15357f | 57 | float HCSR04::getDistance_mm() { |
tbjazic | 6:cf3e4e307d15 | 58 | newDataReady = false; |
tbjazic | 4:aae70f15357f | 59 | return distance * 10; |
tbjazic | 4:aae70f15357f | 60 | } |
tbjazic | 4:aae70f15357f | 61 | |
tbjazic | 6:cf3e4e307d15 | 62 | bool HCSR04::isNewDataReady() { |
tbjazic | 6:cf3e4e307d15 | 63 | return newDataReady; |
tbjazic | 6:cf3e4e307d15 | 64 | } |
tbjazic | 6:cf3e4e307d15 | 65 | |
tbjazic | 4:aae70f15357f | 66 | void HCSR04::setRanges(float minRange, float maxRange) { |
tbjazic | 4:aae70f15357f | 67 | if (minRange < maxRange) { |
tbjazic | 6:cf3e4e307d15 | 68 | if (minRange >= 2 && minRange < 400) // bug from revs. 4 and 5 corrected |
tbjazic | 4:aae70f15357f | 69 | minDistance = minRange; |
tbjazic | 4:aae70f15357f | 70 | if (maxRange <= 400) |
tbjazic | 4:aae70f15357f | 71 | maxDistance = maxRange; |
tbjazic | 4:aae70f15357f | 72 | } |
tbjazic | 4:aae70f15357f | 73 | } |
tbjazic | 4:aae70f15357f | 74 | |
tbjazic | 4:aae70f15357f | 75 | float HCSR04::getMinRange() { |
tbjazic | 4:aae70f15357f | 76 | return minDistance; |
tbjazic | 4:aae70f15357f | 77 | } |
tbjazic | 4:aae70f15357f | 78 | |
tbjazic | 4:aae70f15357f | 79 | float HCSR04::getMaxRange() { |
tbjazic | 4:aae70f15357f | 80 | return maxDistance; |
tbjazic | 0:8871082486ac | 81 | } |