Maxbotix library
Fork of MaxbotixDriver by
sonar.cpp@0:7e65f5077f5a, 2012-11-21 (annotated)
- Committer:
- DanielC
- Date:
- Wed Nov 21 20:47:05 2012 +0000
- Revision:
- 0:7e65f5077f5a
Initial commit of probably working but untested sonar driver library.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DanielC | 0:7e65f5077f5a | 1 | #include "sonar.h" |
DanielC | 0:7e65f5077f5a | 2 | |
DanielC | 0:7e65f5077f5a | 3 | Sonar::Sonar(PinName input, Timer& t) : |
DanielC | 0:7e65f5077f5a | 4 | interrupt(input), |
DanielC | 0:7e65f5077f5a | 5 | time(t), |
DanielC | 0:7e65f5077f5a | 6 | pulseStartTime(0), |
DanielC | 0:7e65f5077f5a | 7 | range(0) { |
DanielC | 0:7e65f5077f5a | 8 | interrupt.rise(this, &Sonar::pulseStart); |
DanielC | 0:7e65f5077f5a | 9 | interrupt.fall(this, &Sonar::pulseStop); |
DanielC | 0:7e65f5077f5a | 10 | } |
DanielC | 0:7e65f5077f5a | 11 | |
DanielC | 0:7e65f5077f5a | 12 | int Sonar::read() { |
DanielC | 0:7e65f5077f5a | 13 | return range; |
DanielC | 0:7e65f5077f5a | 14 | } |
DanielC | 0:7e65f5077f5a | 15 | |
DanielC | 0:7e65f5077f5a | 16 | Sonar::operator int() { |
DanielC | 0:7e65f5077f5a | 17 | return read(); |
DanielC | 0:7e65f5077f5a | 18 | } |
DanielC | 0:7e65f5077f5a | 19 | |
DanielC | 0:7e65f5077f5a | 20 | void Sonar::pulseStart() { |
DanielC | 0:7e65f5077f5a | 21 | pulseStartTime = time.read_us(); |
DanielC | 0:7e65f5077f5a | 22 | } |
DanielC | 0:7e65f5077f5a | 23 | |
DanielC | 0:7e65f5077f5a | 24 | void Sonar::pulseStop() { |
DanielC | 0:7e65f5077f5a | 25 | int endTime = time.read_us(); |
DanielC | 0:7e65f5077f5a | 26 | if (endTime < pulseStartTime) return; // Escape if there's been a roll over |
DanielC | 0:7e65f5077f5a | 27 | range = (endTime - pulseStartTime) / 58; // 58uS per CM |
DanielC | 0:7e65f5077f5a | 28 | } |