Grundfunktionen für Micromouse
Dependencies: AutomationElements mbed
UltrasonicSensor/HCSR04.cpp@0:e38b500d6e74, 2018-04-19 (annotated)
- Committer:
- wengefa1
- Date:
- Thu Apr 19 11:31:49 2018 +0000
- Revision:
- 0:e38b500d6e74
- Child:
- 1:4808f55970e8
keine Regelung vorhanden
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wengefa1 | 0:e38b500d6e74 | 1 | #include "mbed.h" |
wengefa1 | 0:e38b500d6e74 | 2 | #include "HCSR04.h" |
wengefa1 | 0:e38b500d6e74 | 3 | |
wengefa1 | 0:e38b500d6e74 | 4 | float sampleTime = 0.5f; |
wengefa1 | 0:e38b500d6e74 | 5 | |
wengefa1 | 0:e38b500d6e74 | 6 | HCSR04::HCSR04(InterruptIn& echo, DigitalOut& trigger) : echo(echo), trigger(trigger) { |
wengefa1 | 0:e38b500d6e74 | 7 | |
wengefa1 | 0:e38b500d6e74 | 8 | ticker_sens.attach(callback(this, &HCSR04::startMeasurement), sampleTime); |
wengefa1 | 0:e38b500d6e74 | 9 | init(); |
wengefa1 | 0:e38b500d6e74 | 10 | //ticker.attach(callback(this, &Controller::run), PERIOD); |
wengefa1 | 0:e38b500d6e74 | 11 | } |
wengefa1 | 0:e38b500d6e74 | 12 | |
wengefa1 | 0:e38b500d6e74 | 13 | HCSR04::~HCSR04() |
wengefa1 | 0:e38b500d6e74 | 14 | { |
wengefa1 | 0:e38b500d6e74 | 15 | ticker_sens.detach(); // Stoppt den periodischen Task |
wengefa1 | 0:e38b500d6e74 | 16 | } |
wengefa1 | 0:e38b500d6e74 | 17 | |
wengefa1 | 0:e38b500d6e74 | 18 | void HCSR04::init() { |
wengefa1 | 0:e38b500d6e74 | 19 | distance = -1; // initial distance |
wengefa1 | 0:e38b500d6e74 | 20 | minDistance = 2; |
wengefa1 | 0:e38b500d6e74 | 21 | maxDistance = 400; |
wengefa1 | 0:e38b500d6e74 | 22 | newDataReady = timerStarted = false; |
wengefa1 | 0:e38b500d6e74 | 23 | } |
wengefa1 | 0:e38b500d6e74 | 24 | |
wengefa1 | 0:e38b500d6e74 | 25 | void HCSR04::startTimer() { |
wengefa1 | 0:e38b500d6e74 | 26 | if (!timerStarted) { |
wengefa1 | 0:e38b500d6e74 | 27 | timer.start(); // start the timer |
wengefa1 | 0:e38b500d6e74 | 28 | timerStarted = true; |
wengefa1 | 0:e38b500d6e74 | 29 | echoTimeout.attach_us(this, &HCSR04::stopTimer, 25000); // in case echo fall does not occur |
wengefa1 | 0:e38b500d6e74 | 30 | echo.fall(this, &HCSR04::stopTimer); |
wengefa1 | 0:e38b500d6e74 | 31 | echo.rise(NULL); |
wengefa1 | 0:e38b500d6e74 | 32 | } |
wengefa1 | 0:e38b500d6e74 | 33 | } |
wengefa1 | 0:e38b500d6e74 | 34 | |
wengefa1 | 0:e38b500d6e74 | 35 | void HCSR04::stopTimer() { |
wengefa1 | 0:e38b500d6e74 | 36 | timer.stop(); // stop the timer |
wengefa1 | 0:e38b500d6e74 | 37 | if (timerStarted) { |
wengefa1 | 0:e38b500d6e74 | 38 | distance = timer.read() * 1e6 / 58; |
wengefa1 | 0:e38b500d6e74 | 39 | if (distance < minDistance) |
wengefa1 | 0:e38b500d6e74 | 40 | distance = minDistance; |
wengefa1 | 0:e38b500d6e74 | 41 | if (distance > maxDistance) |
wengefa1 | 0:e38b500d6e74 | 42 | distance = maxDistance; |
wengefa1 | 0:e38b500d6e74 | 43 | newDataReady = true; |
wengefa1 | 0:e38b500d6e74 | 44 | } |
wengefa1 | 0:e38b500d6e74 | 45 | timer.reset(); |
wengefa1 | 0:e38b500d6e74 | 46 | timerStarted = false; |
wengefa1 | 0:e38b500d6e74 | 47 | echoTimeout.detach(); |
wengefa1 | 0:e38b500d6e74 | 48 | echo.fall(NULL); |
wengefa1 | 0:e38b500d6e74 | 49 | } |
wengefa1 | 0:e38b500d6e74 | 50 | |
wengefa1 | 0:e38b500d6e74 | 51 | void HCSR04::turnOffTrigger() { |
wengefa1 | 0:e38b500d6e74 | 52 | trigger = 0; |
wengefa1 | 0:e38b500d6e74 | 53 | } |
wengefa1 | 0:e38b500d6e74 | 54 | |
wengefa1 | 0:e38b500d6e74 | 55 | void HCSR04::startMeasurement() { |
wengefa1 | 0:e38b500d6e74 | 56 | trigger = 1; |
wengefa1 | 0:e38b500d6e74 | 57 | triggerTimeout.attach_us(this, &HCSR04::turnOffTrigger, 10); |
wengefa1 | 0:e38b500d6e74 | 58 | echo.rise(this, &HCSR04::startTimer); |
wengefa1 | 0:e38b500d6e74 | 59 | newDataReady = false; |
wengefa1 | 0:e38b500d6e74 | 60 | } |
wengefa1 | 0:e38b500d6e74 | 61 | |
wengefa1 | 0:e38b500d6e74 | 62 | float HCSR04::getDistance_cm() { |
wengefa1 | 0:e38b500d6e74 | 63 | newDataReady = false; |
wengefa1 | 0:e38b500d6e74 | 64 | return distance; |
wengefa1 | 0:e38b500d6e74 | 65 | } |
wengefa1 | 0:e38b500d6e74 | 66 | |
wengefa1 | 0:e38b500d6e74 | 67 | float HCSR04::getDistance_mm() { |
wengefa1 | 0:e38b500d6e74 | 68 | newDataReady = false; |
wengefa1 | 0:e38b500d6e74 | 69 | return distance * 10; |
wengefa1 | 0:e38b500d6e74 | 70 | } |
wengefa1 | 0:e38b500d6e74 | 71 | |
wengefa1 | 0:e38b500d6e74 | 72 | bool HCSR04::isNewDataReady() { |
wengefa1 | 0:e38b500d6e74 | 73 | return newDataReady; |
wengefa1 | 0:e38b500d6e74 | 74 | } |
wengefa1 | 0:e38b500d6e74 | 75 | |
wengefa1 | 0:e38b500d6e74 | 76 | void HCSR04::setRanges(float minRange, float maxRange) { |
wengefa1 | 0:e38b500d6e74 | 77 | if (minRange < maxRange) { |
wengefa1 | 0:e38b500d6e74 | 78 | if (minRange >= 2 && minRange < 400) // bug from revs. 4 and 5 corrected |
wengefa1 | 0:e38b500d6e74 | 79 | minDistance = minRange; |
wengefa1 | 0:e38b500d6e74 | 80 | if (maxRange <= 400) |
wengefa1 | 0:e38b500d6e74 | 81 | maxDistance = maxRange; |
wengefa1 | 0:e38b500d6e74 | 82 | } |
wengefa1 | 0:e38b500d6e74 | 83 | } |
wengefa1 | 0:e38b500d6e74 | 84 | |
wengefa1 | 0:e38b500d6e74 | 85 | float HCSR04::getMinRange() { |
wengefa1 | 0:e38b500d6e74 | 86 | return minDistance; |
wengefa1 | 0:e38b500d6e74 | 87 | } |
wengefa1 | 0:e38b500d6e74 | 88 | |
wengefa1 | 0:e38b500d6e74 | 89 | float HCSR04::getMaxRange() { |
wengefa1 | 0:e38b500d6e74 | 90 | return maxDistance; |
wengefa1 | 0:e38b500d6e74 | 91 | } |