temperature and distance
Fork of TrashSensors by
hcsr04.h@0:3f335fc50cef, 2015-05-24 (annotated)
- Committer:
- coyotebush
- Date:
- Sun May 24 20:45:32 2015 +0000
- Revision:
- 0:3f335fc50cef
extract sensor code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
coyotebush | 0:3f335fc50cef | 1 | /* Copyright (c) 2013 Prabhu Desai |
coyotebush | 0:3f335fc50cef | 2 | * pdtechworld@gmail.com |
coyotebush | 0:3f335fc50cef | 3 | * |
coyotebush | 0:3f335fc50cef | 4 | * |
coyotebush | 0:3f335fc50cef | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
coyotebush | 0:3f335fc50cef | 6 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
coyotebush | 0:3f335fc50cef | 7 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
coyotebush | 0:3f335fc50cef | 8 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
coyotebush | 0:3f335fc50cef | 9 | * furnished to do so, subject to the following conditions: |
coyotebush | 0:3f335fc50cef | 10 | * |
coyotebush | 0:3f335fc50cef | 11 | * The above copyright notice and this permission notice shall be included in all copies or |
coyotebush | 0:3f335fc50cef | 12 | * substantial portions of the Software. |
coyotebush | 0:3f335fc50cef | 13 | * |
coyotebush | 0:3f335fc50cef | 14 | * For more details on the sensor : |
coyotebush | 0:3f335fc50cef | 15 | * http://www.elecfreaks.com/store/hcsr04-ultrasonic-sensor-distance-measuring-module-p-91.html?zenid=pgm8pgnvaodbe36dibq5s1soi3 |
coyotebush | 0:3f335fc50cef | 16 | * |
coyotebush | 0:3f335fc50cef | 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
coyotebush | 0:3f335fc50cef | 18 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
coyotebush | 0:3f335fc50cef | 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
coyotebush | 0:3f335fc50cef | 20 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
coyotebush | 0:3f335fc50cef | 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
coyotebush | 0:3f335fc50cef | 22 | */ |
coyotebush | 0:3f335fc50cef | 23 | |
coyotebush | 0:3f335fc50cef | 24 | #ifndef MBED_HCSR04_H |
coyotebush | 0:3f335fc50cef | 25 | #define MBED_HCSR04_H |
coyotebush | 0:3f335fc50cef | 26 | |
coyotebush | 0:3f335fc50cef | 27 | #include "mbed.h" |
coyotebush | 0:3f335fc50cef | 28 | |
coyotebush | 0:3f335fc50cef | 29 | /** HCSR04 Class(es) |
coyotebush | 0:3f335fc50cef | 30 | */ |
coyotebush | 0:3f335fc50cef | 31 | |
coyotebush | 0:3f335fc50cef | 32 | // TODO(CJF): steal clever logic from https://code.google.com/p/arduino-new-ping/ ? |
coyotebush | 0:3f335fc50cef | 33 | |
coyotebush | 0:3f335fc50cef | 34 | class HCSR04 |
coyotebush | 0:3f335fc50cef | 35 | { |
coyotebush | 0:3f335fc50cef | 36 | public: |
coyotebush | 0:3f335fc50cef | 37 | static const double kMicrosecondsToCentimeters = 343.0 / 20000; |
coyotebush | 0:3f335fc50cef | 38 | static const double kBogusPulse = 500 / kMicrosecondsToCentimeters; |
coyotebush | 0:3f335fc50cef | 39 | |
coyotebush | 0:3f335fc50cef | 40 | /** Create a HCSR04 object connected to the specified pin |
coyotebush | 0:3f335fc50cef | 41 | * @param pin i/o pin to connect to |
coyotebush | 0:3f335fc50cef | 42 | */ |
coyotebush | 0:3f335fc50cef | 43 | HCSR04(PinName TrigPin,PinName EchoPin); |
coyotebush | 0:3f335fc50cef | 44 | ~HCSR04(); |
coyotebush | 0:3f335fc50cef | 45 | |
coyotebush | 0:3f335fc50cef | 46 | /** Return the distance from obstacle in cm |
coyotebush | 0:3f335fc50cef | 47 | * @param distance in cms and returns -1, in case of failure |
coyotebush | 0:3f335fc50cef | 48 | */ |
coyotebush | 0:3f335fc50cef | 49 | double get_dist_cm(void); |
coyotebush | 0:3f335fc50cef | 50 | double get_max_dist_cm(void); |
coyotebush | 0:3f335fc50cef | 51 | /** Return the pulse duration equal to sonic waves travelling to obstacle and back to receiver. |
coyotebush | 0:3f335fc50cef | 52 | * @param pulse duration in microseconds. |
coyotebush | 0:3f335fc50cef | 53 | */ |
coyotebush | 0:3f335fc50cef | 54 | int get_pulse_us(void); |
coyotebush | 0:3f335fc50cef | 55 | /** Generates the trigger pulse of 10us on the trigger PIN. |
coyotebush | 0:3f335fc50cef | 56 | */ |
coyotebush | 0:3f335fc50cef | 57 | void start(void ); |
coyotebush | 0:3f335fc50cef | 58 | void isr_rise(void); |
coyotebush | 0:3f335fc50cef | 59 | void isr_fall(void); |
coyotebush | 0:3f335fc50cef | 60 | void fall (void (*fptr)(void)); |
coyotebush | 0:3f335fc50cef | 61 | void rise (void (*fptr)(void)); |
coyotebush | 0:3f335fc50cef | 62 | |
coyotebush | 0:3f335fc50cef | 63 | |
coyotebush | 0:3f335fc50cef | 64 | |
coyotebush | 0:3f335fc50cef | 65 | private: |
coyotebush | 0:3f335fc50cef | 66 | |
coyotebush | 0:3f335fc50cef | 67 | Timer pulsetime; |
coyotebush | 0:3f335fc50cef | 68 | DigitalOut trigger; |
coyotebush | 0:3f335fc50cef | 69 | InterruptIn echo; |
coyotebush | 0:3f335fc50cef | 70 | int pulsedur; |
coyotebush | 0:3f335fc50cef | 71 | int maxpulse; |
coyotebush | 0:3f335fc50cef | 72 | }; |
coyotebush | 0:3f335fc50cef | 73 | |
coyotebush | 0:3f335fc50cef | 74 | #endif |