xnecas 1 долоо хоног өмнө
commit
ce41c0892d
3 өөрчлөгдсөн 146 нэмэгдсэн , 0 устгасан
  1. 21 0
      LICENSE
  2. 65 0
      src/HCSR04.cpp
  3. 60 0
      src/HCSR04.h

+ 21 - 0
LICENSE

@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) 2016 Martin Šošić
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.

+ 65 - 0
src/HCSR04.cpp

@@ -0,0 +1,65 @@
+/*
+  HCSR04 - Library for arduino, for HC-SR04 ultrasonic distance sensor.
+  Created by Martin Sosic, June 11, 2016.
+*/
+
+#include "Arduino.h"
+#include "HCSR04.h"
+
+UltraSonicDistanceSensor::UltraSonicDistanceSensor(
+        byte triggerPin, byte echoPin, unsigned short maxDistanceCm, unsigned long maxTimeoutMicroSec) {
+    this->triggerPin = triggerPin;
+    this->echoPin = echoPin;
+    this->maxDistanceCm = maxDistanceCm;
+    this->maxTimeoutMicroSec = maxTimeoutMicroSec;
+    pinMode(triggerPin, OUTPUT);
+    pinMode(echoPin, INPUT);
+}
+
+float UltraSonicDistanceSensor::measureDistanceCm() {
+    //Using the approximate formula 19.307°C results in roughly 343m/s which is the commonly used value for air.
+    return measureDistanceCm(19.307);
+}
+
+float UltraSonicDistanceSensor::measureDistanceCm(float temperature) {
+    return measureDistanceCm(temperature, 0);
+}
+
+float UltraSonicDistanceSensor::measureDistanceCm(float temperature, float humidity) {
+    unsigned long maxDistanceDurationMicroSec;
+
+    time_t t0 = millis();
+
+    // Make sure that trigger pin is LOW.
+    digitalWrite(triggerPin, LOW);
+    delayMicroseconds(2);
+    // Hold trigger for 10 microseconds, which is signal for sensor to measure distance.
+    digitalWrite(triggerPin, HIGH);
+    delayMicroseconds(10);
+    digitalWrite(triggerPin, LOW);
+    float speedOfSoundInCmPerMicroSec = 0.03313 + (0.0000606 * temperature) + (0.00000124 * humidity);  // Cair ≈ (331.3 + 0.606 ⋅ ϑ) m/s
+
+    // Compute max delay based on max distance with 25% margin in microseconds
+    maxDistanceDurationMicroSec = 2.5 * maxDistanceCm / speedOfSoundInCmPerMicroSec;
+    if (maxTimeoutMicroSec > 0) {
+    	maxDistanceDurationMicroSec = min(maxDistanceDurationMicroSec, maxTimeoutMicroSec);
+    }
+
+    // Measure the length of echo signal, which is equal to the time needed for sound to go there and back.
+    unsigned long durationMicroSec = pulseIn(echoPin, HIGH, maxDistanceDurationMicroSec); // can't measure beyond max distance
+
+    // Wait at least 60 ms (datasheet value)
+    time_t t_diff = HCSR04_MEASURE_DELAY - (millis() - t0);
+    
+    if (t_diff > 0)
+    {   
+        delay(t_diff);
+    }
+
+    float distanceCm = durationMicroSec / 2.0 * speedOfSoundInCmPerMicroSec;
+    if (distanceCm == 0 || distanceCm > maxDistanceCm) {
+        return 0.0;
+    } else {
+        return distanceCm;
+    }  
+}

+ 60 - 0
src/HCSR04.h

@@ -0,0 +1,60 @@
+/*
+  HCSR04 - Library for arduino, for HC-SR04 ultrasonic distance sensor.
+  Created by Martin Sosic, June 11, 2016.
+  Modified by Matus Necas September 23 2026
+*/
+
+#ifndef HCSR04_H
+#define HCSR04_H
+
+#include "Arduino.h"
+
+#define HCSR04_MEASURE_DELAY 60  ///< delay between measures
+
+class UltraSonicDistanceSensor {
+ public:
+    /**
+     * @param triggerPin  Digital pin that is used for controlling sensor (output).
+     * @param echoPin  Digital pin that is used to get information from sensor (input).
+     * @param maxDistanceCm  Maximum distance sensor can measure, defaults to 4m for HC-SR04.
+     *                       You might want to set this value if you are using different sensor than HC-SR04
+     *                       or if you don't care about distances larger than whatever you will set it to
+     *                       (therefore reducing time it takes for a single measurement).
+     * @param maxTimeoutMicroSec  Single measurement will never take longer than whatever value you provide here.
+     *   You might want to do this in order to ensure your program is never blocked for longer than some specific time,
+     *   since measurements are blocking.
+     *   By default, there is no limit on time (only on distance). By defining timeout, you are again limiting the distance. 
+     */
+    UltraSonicDistanceSensor(byte triggerPin, byte echoPin, unsigned short maxDistanceCm = 400, unsigned long maxTimeoutMicroSec = 0);
+
+    /**
+     * Measures distance by sending ultrasonic waves and measuring time it takes them
+     * to return.
+     * @returns Distance in centimeters, or negative value if distance is greater than 400cm.
+     */
+    float measureDistanceCm();
+
+    /**
+     * Measures distance by sending ultrasonic waves and measuring time it takes them
+     * to return. Measurement can not exceed duration calculated with maxDistanceCm or maxTimeoutMicroSec.
+     * @param temperature  Temperature in degrees celsius
+     * @returns Distance in centimeters, or negative value if distance is greater than 400cm.
+     */
+    float measureDistanceCm(float temperature);
+
+   /**
+     * Measures distance by sending ultrasonic waves and measuring time it takes them
+     * to return. Measurement can not exceed duration calculated with maxDistanceCm or maxTimeoutMicroSec.
+     * @param temperature  Temperature in degrees celsius
+     * @param humidity Relative humidity in %
+     * @returns Distance in centimeters, or negative value if distance is greater than 400cm.
+     */
+    float measureDistanceCm(float temperature, float humidity);
+
+ private:
+    byte triggerPin, echoPin;
+    unsigned short maxDistanceCm;
+    unsigned long maxTimeoutMicroSec;
+};
+
+#endif // HCSR04_H