Libreria ultrasuoni

Dependents:   TextLCD_HelloWorld ES_4_P4

Files at this revision

API Documentation at this revision

Comitter:
Mattinico
Date:
Mon Oct 24 12:53:49 2016 +0000
Commit message:
ghj

Changed in this revision

HCSR04.cpp Show annotated file Show diff for this revision Revisions of this file
HCSR04.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HCSR04.cpp	Mon Oct 24 12:53:49 2016 +0000
@@ -0,0 +1,67 @@
+#include "mbed.h"
+#include "HCSR04.h"
+ 
+HCSR04::HCSR04(PinName echoPin, PinName triggerPin) : echo(echoPin), trigger(triggerPin) {
+    init();
+}
+ 
+void HCSR04::init() {
+    /** configure the rising edge to start the timer */
+    echo.rise(this, &HCSR04::startTimer);
+    
+    /** configure the falling edge to stop the timer */
+    echo.fall(this, &HCSR04::stopTimer);
+    
+    distance = -1;      // initial distance
+    minDistance = 2;
+    maxDistance = 400;
+}
+ 
+void HCSR04::startTimer() {
+    timer.start(); // start the timer
+}
+ 
+void HCSR04::stopTimer() {
+    timer.stop(); // stop the timer
+}
+ 
+void HCSR04::startMeasurement() {
+    trigger = 1;
+    wait_us(10);
+    trigger = 0;
+    wait_us(23660); // just enough time to measure 400 cm
+    timer.stop(); // just in case echo fall did not occur
+    distance = timer.read() * 1e6 / 58;
+    if (distance < minDistance)
+        distance = minDistance;
+    if (distance > maxDistance)
+        distance = maxDistance;
+    timer.reset();
+}
+ 
+float HCSR04::getDistance_cm() {
+    startMeasurement();
+    return distance;
+}
+ 
+float HCSR04::getDistance_mm() {
+    startMeasurement();
+    return distance * 10;
+}
+ 
+void HCSR04::setRanges(float minRange, float maxRange) {
+    if (minRange < maxRange) {
+        if (minRange >= 2) 
+            minDistance = minRange;
+        if (maxRange <= 400)
+            maxDistance = maxRange;
+    }
+}
+ 
+float HCSR04::getMinRange() {
+    return minDistance;
+}
+ 
+float HCSR04::getMaxRange() {
+    return maxDistance;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HCSR04.h	Mon Oct 24 12:53:49 2016 +0000
@@ -0,0 +1,83 @@
+#ifndef HCSR04_H_TVZMT
+#define HCSR04_H_TVZMT
+ 
+/** A distance measurement class using ultrasonic sensor HC-SR04.
+ *  
+ * Example of use:
+ * @code
+ * #include "mbed.h"
+ * #include "HCSR04.h"
+ *
+ * Serial pc(USBTX, USBRX);
+ *
+ * int main() {
+ *     HCSR04 sensor(p5, p7);
+ *     sensor.setRanges(10, 110);
+ *     pc.printf("Min. range = %g cm\n\rMax. range = %g cm\n\r",
+ *       sensor.getMinRange(), sensor.getMaxRange());
+ *     while(1) {
+ *         pc.printf("Distance: %5.1f mm\r", sensor.getDistance_mm());
+ *         wait_ms(500);
+ *     }
+ * }
+ * @endcode
+ */
+class HCSR04 {
+    
+    public:
+    
+    /** Receives two PinName variables.
+     * @param echoPin mbed pin to which the echo signal is connected to
+     * @param triggerPin mbed pin to which the trigger signal is connected to
+     */
+    HCSR04(PinName echoPin, PinName triggerPin);
+    
+    /** Calculates the distance in cm, with the calculation time of approximatelly 23.7 ms.
+     * @returns distance of the measuring object in cm.
+     */
+    float getDistance_cm();
+    
+    /** Calculates the distance in mm, with the calculation time of approximatelly 23.7 ms.
+     * @returns distance of the measuring object in mm.
+     */
+    float getDistance_mm();
+    
+    /** Sets the minimum and maximum ranges between the factory values of 2 cm and 400 cm.
+     *  @param minRange Minimum range in cm. Must be between 2 cm and maxRange.
+     *  @param maxRange Maximum range in cm. Must be between minRange and 400 cm.
+     */
+    void setRanges(float minRange, float maxRange);
+    
+    /** Retreives the minimum sensor range set by the user.
+     * @returns the minimum sensor range set by the user in cm.
+     */
+    float getMinRange();
+    
+    /** Retreives the maximum sensor range set by the user.
+     * @returns the maximum sensor range set by the user in cm.
+     */
+    float getMaxRange();
+    
+    private:
+    
+    InterruptIn echo;       // echo pin
+    DigitalOut trigger;     // trigger pin
+    Timer timer;            // echo pulsewidth measurement
+    float distance;         // store the distance in cm
+    float minDistance;      // minimum measurable distance
+    float maxDistance;      // maximum measurable distance
+    
+    /** Start the timer. */
+    void startTimer();
+    
+    /** Stop the timer. */
+    void stopTimer();
+    
+    /** Initialization. */
+    void init();
+    
+    /** Start the measurement. */
+    void startMeasurement();
+};
+ 
+#endif
\ No newline at end of file