Maxbotix ultrasonic distance sensor library
Fork of MaxbotixDriver by
sonar.h@4:95f696f59d94, 2020-01-30 (annotated)
- Committer:
- SomeRandomBloke
- Date:
- Thu Jan 30 11:52:10 2020 +0000
- Revision:
- 4:95f696f59d94
- Parent:
- 2:911740f19198
Added range averaging
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 |
SomeRandomBloke | 1:330989f98a6e | 23 | int readCm(); |
SomeRandomBloke | 1:330989f98a6e | 24 | |
SomeRandomBloke | 2:911740f19198 | 25 | /// Returns range in uS as int |
DanielC | 0:7e65f5077f5a | 26 | int read(); |
DanielC | 0:7e65f5077f5a | 27 | |
DanielC | 0:7e65f5077f5a | 28 | /// Returns the range in CM as an int |
DanielC | 0:7e65f5077f5a | 29 | operator int(); |
SomeRandomBloke | 4:95f696f59d94 | 30 | |
SomeRandomBloke | 4:95f696f59d94 | 31 | bool getRange( int *outRawDistance, int *outDistance, float temperature ); |
SomeRandomBloke | 4:95f696f59d94 | 32 | |
DanielC | 0:7e65f5077f5a | 33 | private: |
DanielC | 0:7e65f5077f5a | 34 | /// Inturrupt at start of pulse |
DanielC | 0:7e65f5077f5a | 35 | void pulseStart(); |
DanielC | 0:7e65f5077f5a | 36 | /// Interrupt at end of pulse |
DanielC | 0:7e65f5077f5a | 37 | void pulseStop(); |
DanielC | 0:7e65f5077f5a | 38 | |
DanielC | 0:7e65f5077f5a | 39 | /// Interrupt driver for the input pin |
DanielC | 0:7e65f5077f5a | 40 | InterruptIn interrupt; |
DanielC | 0:7e65f5077f5a | 41 | /// Reference to global timer instance |
DanielC | 0:7e65f5077f5a | 42 | Timer& time; |
DanielC | 0:7e65f5077f5a | 43 | /// Time of the start of the current pulse |
DanielC | 0:7e65f5077f5a | 44 | int pulseStartTime; |
DanielC | 0:7e65f5077f5a | 45 | /// The most recent sample |
DanielC | 0:7e65f5077f5a | 46 | int range; |
DanielC | 0:7e65f5077f5a | 47 | }; |