A driver library for Maxbotix / Maxsonar sonar range finders that uses the pulse width output function to achieve better accuracy with less load on the mbed processor.
Dependents: QuadCopter mbed-IBooth-ETH
sonar.h@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 "mbed.h" |
DanielC | 0:7e65f5077f5a | 2 | /*** @file Driver for Maxbotix sonar modules |
DanielC | 0:7e65f5077f5a | 3 | * Uses pulse width input to read range results. |
DanielC | 0:7e65f5077f5a | 4 | * @author Daniel Casner <http://www.danielcasner.org> |
DanielC | 0:7e65f5077f5a | 5 | * |
DanielC | 0:7e65f5077f5a | 6 | * Known Issues: |
DanielC | 0:7e65f5077f5a | 7 | * If a timer rollover occures during a sample, the driver will drop that sample. |
DanielC | 0:7e65f5077f5a | 8 | */ |
DanielC | 0:7e65f5077f5a | 9 | |
DanielC | 0:7e65f5077f5a | 10 | /** Object representation of a sonar instance |
DanielC | 0:7e65f5077f5a | 11 | */ |
DanielC | 0:7e65f5077f5a | 12 | class Sonar { |
DanielC | 0:7e65f5077f5a | 13 | public: |
DanielC | 0:7e65f5077f5a | 14 | /** Sets up the driver and starts taking measurements |
DanielC | 0:7e65f5077f5a | 15 | * @param input The pin to read pulses from |
DanielC | 0:7e65f5077f5a | 16 | * @param t A running timer instance used to measure pulse width. |
DanielC | 0:7e65f5077f5a | 17 | * Note that the timer must have been started or results will be |
DanielC | 0:7e65f5077f5a | 18 | * invalid. |
DanielC | 0:7e65f5077f5a | 19 | */ |
DanielC | 0:7e65f5077f5a | 20 | Sonar(PinName input, Timer& t); |
DanielC | 0:7e65f5077f5a | 21 | |
DanielC | 0:7e65f5077f5a | 22 | /// Returns range in cm as int |
DanielC | 0:7e65f5077f5a | 23 | int read(); |
DanielC | 0:7e65f5077f5a | 24 | |
DanielC | 0:7e65f5077f5a | 25 | /// Returns the range in CM as an int |
DanielC | 0:7e65f5077f5a | 26 | operator int(); |
DanielC | 0:7e65f5077f5a | 27 | |
DanielC | 0:7e65f5077f5a | 28 | private: |
DanielC | 0:7e65f5077f5a | 29 | /// Inturrupt at start of pulse |
DanielC | 0:7e65f5077f5a | 30 | void pulseStart(); |
DanielC | 0:7e65f5077f5a | 31 | /// Interrupt at end of pulse |
DanielC | 0:7e65f5077f5a | 32 | void pulseStop(); |
DanielC | 0:7e65f5077f5a | 33 | |
DanielC | 0:7e65f5077f5a | 34 | /// Interrupt driver for the input pin |
DanielC | 0:7e65f5077f5a | 35 | InterruptIn interrupt; |
DanielC | 0:7e65f5077f5a | 36 | /// Reference to global timer instance |
DanielC | 0:7e65f5077f5a | 37 | Timer& time; |
DanielC | 0:7e65f5077f5a | 38 | /// Time of the start of the current pulse |
DanielC | 0:7e65f5077f5a | 39 | int pulseStartTime; |
DanielC | 0:7e65f5077f5a | 40 | /// The most recent sample |
DanielC | 0:7e65f5077f5a | 41 | int range; |
DanielC | 0:7e65f5077f5a | 42 | }; |