test

Fork of HCSR04 by TVZ Mechatronics Team

Committer:
tgw
Date:
Sat Nov 25 02:02:51 2017 +0000
Revision:
7:e53b2476821e
Parent:
6:cf3e4e307d15
test

Who changed what in which revision?

UserRevisionLine numberNew 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;
tgw 7:e53b2476821e 19 //echoTimeout.attach_us(this, &HCSR04::stopTimer, 25000); // in case echo fall does not occur
tgw 7:e53b2476821e 20 echoTimeout.attach_us(callback(this, &HCSR04::stopTimer), 25000); // in case echo fall does not occur
tgw 7:e53b2476821e 21 echo.fall(callback(this, &HCSR04::stopTimer));
tbjazic 6:cf3e4e307d15 22 echo.rise(NULL);
tbjazic 6:cf3e4e307d15 23 }
tbjazic 0:8871082486ac 24 }
tbjazic 0:8871082486ac 25
tbjazic 0:8871082486ac 26 void HCSR04::stopTimer() {
tbjazic 0:8871082486ac 27 timer.stop(); // stop the timer
tbjazic 6:cf3e4e307d15 28 if (timerStarted) {
tbjazic 6:cf3e4e307d15 29 distance = timer.read() * 1e6 / 58;
tbjazic 6:cf3e4e307d15 30 if (distance < minDistance)
tbjazic 6:cf3e4e307d15 31 distance = minDistance;
tbjazic 6:cf3e4e307d15 32 if (distance > maxDistance)
tbjazic 6:cf3e4e307d15 33 distance = maxDistance;
tbjazic 6:cf3e4e307d15 34 newDataReady = true;
tbjazic 6:cf3e4e307d15 35 }
tbjazic 6:cf3e4e307d15 36 timer.reset();
tbjazic 6:cf3e4e307d15 37 timerStarted = false;
tbjazic 6:cf3e4e307d15 38 echoTimeout.detach();
tbjazic 6:cf3e4e307d15 39 echo.fall(NULL);
tbjazic 6:cf3e4e307d15 40 }
tbjazic 6:cf3e4e307d15 41
tbjazic 6:cf3e4e307d15 42 void HCSR04::turnOffTrigger() {
tbjazic 6:cf3e4e307d15 43 trigger = 0;
tbjazic 0:8871082486ac 44 }
tbjazic 0:8871082486ac 45
tbjazic 0:8871082486ac 46 void HCSR04::startMeasurement() {
tbjazic 0:8871082486ac 47 trigger = 1;
tgw 7:e53b2476821e 48 triggerTimeout.attach_us(callback(this, &HCSR04::turnOffTrigger), 10);
tgw 7:e53b2476821e 49 echo.rise(callback(this, &HCSR04::startTimer));
tbjazic 6:cf3e4e307d15 50 newDataReady = false;
tbjazic 0:8871082486ac 51 }
tbjazic 0:8871082486ac 52
tbjazic 0:8871082486ac 53 float HCSR04::getDistance_cm() {
tbjazic 6:cf3e4e307d15 54 newDataReady = false;
tbjazic 0:8871082486ac 55 return distance;
tbjazic 4:aae70f15357f 56 }
tbjazic 4:aae70f15357f 57
tbjazic 4:aae70f15357f 58 float HCSR04::getDistance_mm() {
tbjazic 6:cf3e4e307d15 59 newDataReady = false;
tbjazic 4:aae70f15357f 60 return distance * 10;
tbjazic 4:aae70f15357f 61 }
tbjazic 4:aae70f15357f 62
tbjazic 6:cf3e4e307d15 63 bool HCSR04::isNewDataReady() {
tbjazic 6:cf3e4e307d15 64 return newDataReady;
tbjazic 6:cf3e4e307d15 65 }
tbjazic 6:cf3e4e307d15 66
tbjazic 4:aae70f15357f 67 void HCSR04::setRanges(float minRange, float maxRange) {
tbjazic 4:aae70f15357f 68 if (minRange < maxRange) {
tbjazic 6:cf3e4e307d15 69 if (minRange >= 2 && minRange < 400) // bug from revs. 4 and 5 corrected
tbjazic 4:aae70f15357f 70 minDistance = minRange;
tbjazic 4:aae70f15357f 71 if (maxRange <= 400)
tbjazic 4:aae70f15357f 72 maxDistance = maxRange;
tbjazic 4:aae70f15357f 73 }
tbjazic 4:aae70f15357f 74 }
tbjazic 4:aae70f15357f 75
tbjazic 4:aae70f15357f 76 float HCSR04::getMinRange() {
tbjazic 4:aae70f15357f 77 return minDistance;
tbjazic 4:aae70f15357f 78 }
tbjazic 4:aae70f15357f 79
tbjazic 4:aae70f15357f 80 float HCSR04::getMaxRange() {
tbjazic 4:aae70f15357f 81 return maxDistance;
tbjazic 0:8871082486ac 82 }