Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of MaxbotixDriver by
sonar.cpp@1:330989f98a6e, 2015-12-27 (annotated)
- Committer:
- SomeRandomBloke
- Date:
- Sun Dec 27 19:47:07 2015 +0000
- Revision:
- 1:330989f98a6e
- Parent:
- 0:7e65f5077f5a
- Child:
- 3:c231deea4d36
updates to return raw pulse
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 | |
SomeRandomBloke | 1:330989f98a6e | 16 | int Sonar::readCm() { |
SomeRandomBloke | 1:330989f98a6e | 17 | return range / 58; // 58uS per CM |
SomeRandomBloke | 1:330989f98a6e | 18 | } |
SomeRandomBloke | 1:330989f98a6e | 19 | |
DanielC | 0:7e65f5077f5a | 20 | Sonar::operator int() { |
DanielC | 0:7e65f5077f5a | 21 | return read(); |
DanielC | 0:7e65f5077f5a | 22 | } |
DanielC | 0:7e65f5077f5a | 23 | |
DanielC | 0:7e65f5077f5a | 24 | void Sonar::pulseStart() { |
DanielC | 0:7e65f5077f5a | 25 | pulseStartTime = time.read_us(); |
DanielC | 0:7e65f5077f5a | 26 | } |
DanielC | 0:7e65f5077f5a | 27 | |
DanielC | 0:7e65f5077f5a | 28 | void Sonar::pulseStop() { |
DanielC | 0:7e65f5077f5a | 29 | int endTime = time.read_us(); |
DanielC | 0:7e65f5077f5a | 30 | if (endTime < pulseStartTime) return; // Escape if there's been a roll over |
SomeRandomBloke | 1:330989f98a6e | 31 | range = (endTime - pulseStartTime); // / 58; // 58uS per CM |
DanielC | 0:7e65f5077f5a | 32 | } |