Proof of concept for distance and temperature monitoring

Dependencies:   mbed mbedConnectorInterface mbedEndpointNetwork

Committer:
coyotebush
Date:
Mon May 18 00:44:59 2015 +0000
Revision:
10:338191178cbf
Parent:
8:c69fe28366d8
max distance

Who changed what in which revision?

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