Kildekode

Dependencies:   mbed

Fork of PRO1 by Kim Nielsen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HCSR04.h Source File

HCSR04.h

00001 /*
00002 Dette er header filen for ultralydssensoren
00003 */
00004 
00005 #ifndef HCSR04_H_TVZMT
00006 #define HCSR04_H_TVZMT
00007 
00008 /** A distance measurement class using ultrasonic sensor HC-SR04.
00009  *  
00010  * Example of use:
00011  * @code
00012  * #include "mbed.h"
00013  * #include "HCSR04.h"
00014  *
00015  * Serial pc(USBTX, USBRX);
00016  *
00017  * int main() {
00018  *     HCSR04 sensor(p5, p7);
00019  *     sensor.setRanges(10, 110);
00020  *     pc.printf("Min. range = %g cm\n\rMax. range = %g cm\n\r",
00021  *       sensor.getMinRange(), sensor.getMaxRange());
00022  *     while(1) {
00023  *         pc.printf("Distance: %5.1f mm\r", sensor.getDistance_mm());
00024  *         wait_ms(500);
00025  *     }
00026  * }
00027  * @endcode
00028  */
00029 class HCSR04 {
00030     
00031     public:
00032     
00033     /** Receives two PinName variables.
00034      * @param echoPin mbed pin to which the echo signal is connected to
00035      * @param triggerPin mbed pin to which the trigger signal is connected to
00036      */
00037     HCSR04(PinName echoPin, PinName triggerPin);
00038     
00039     /** Calculates the distance in cm, with the calculation time of approximatelly 23.7 ms.
00040      * @returns distance of the measuring object in cm.
00041      */
00042     float getDistance_cm();
00043     
00044     /** Calculates the distance in mm, with the calculation time of approximatelly 23.7 ms.
00045      * @returns distance of the measuring object in mm.
00046      */
00047     float getDistance_mm();
00048     
00049     /** Sets the minimum and maximum ranges between the factory values of 2 cm and 400 cm.
00050      *  @param minRange Minimum range in cm. Must be between 2 cm and maxRange.
00051      *  @param maxRange Maximum range in cm. Must be between minRange and 400 cm.
00052      */
00053     void setRanges(float minRange, float maxRange);
00054     
00055     /** Retreives the minimum sensor range set by the user.
00056      * @returns the minimum sensor range set by the user in cm.
00057      */
00058     float getMinRange();
00059     
00060     /** Retreives the maximum sensor range set by the user.
00061      * @returns the maximum sensor range set by the user in cm.
00062      */
00063     float getMaxRange();
00064     
00065     private:
00066     
00067     InterruptIn echo;       // echo pin
00068     DigitalOut trigger;     // trigger pin
00069     Timer timer;            // echo pulsewidth measurement
00070     float distance;         // store the distance in cm
00071     float minDistance;      // minimum measurable distance
00072     float maxDistance;      // maximum measurable distance
00073     
00074     /** Start the timer. */
00075     void startTimer();
00076     
00077     /** Stop the timer. */
00078     void stopTimer();
00079     
00080     /** Initialization. */
00081     void init();
00082     
00083     /** Start the measurement. */
00084     void startMeasurement();
00085 };
00086 
00087 #endif