Explorar el Código

Fixed HCSR04 submodule

xnecas hace 1 semana
padre
commit
24dacc64cc

+ 2 - 2
.gitmodules

@@ -1,3 +1,3 @@
-[submodule "lib/HCSR04"]
-	path = lib/HCSR04
+[submodule "main/lib/HCSR04"]
+	path = main/lib/HCSR04
 	url = rcvyskum01@repo.vyskumnecentrum.sk:esp32-drivers/HC-SR04.git

+ 0 - 0
lib/HCSR04 → main/lib/HCSR04


+ 0 - 22
main/lib/HCSR04/LICENSE

@@ -1,22 +0,0 @@
-The MIT License (MIT)
-
-Copyright (c) 2026 Matúš Nečas
-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.

+ 0 - 65
main/lib/HCSR04/src/HCSR04.cpp

@@ -1,65 +0,0 @@
-/*
-  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;
-    }  
-}

+ 0 - 60
main/lib/HCSR04/src/HCSR04.h

@@ -1,60 +0,0 @@
-/*
-  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

+ 38 - 33
main/src/nbus_app_driver/nbus_app_driver.cpp

@@ -1,28 +1,29 @@
 #include "nbus_app_driver.h"
+ 
+// Internal containers for data
+static tof_data_t _tof_data;
+static imu_data_t _imu_data;
+static env_data_t _env_data;
 
- tof_data_t tof_data;
- imu_data_t imu_data;
- env_data_t env_data;
+// Internal variable for phase
+phase_data_t _phase_data{IDLE_PH};
 
- phase_data_t _phase_data;
-
- int counter = 0;
-
-static nBusAppInterface_t appdrv = {
+// define pointers for nbus interface
+static nBusAppInterface_t _appdrv = {
     appdrv_init,     appdrv_reset,    appdrv_getType, appdrv_getSensorCount,    appdrv_getData, appdrv_setData,   appdrv_hasParam,
     appdrv_getParam, appdrv_setParam, appdrv_start,   appdrv_stop, appdrv_read, appdrv_store,   appdrv_calibrate, appdrv_getSensorFormat, 
     appdrv_find,     appdrv_device_ready
 };
 
-
 nBusAppInterface_t *getAppdrvDriver()
 {
-    return &appdrv;
+    return &_appdrv;
 }
 
 void appdrv_init(void *hw_interface, void *hw_config)
 {
-    dtaq_read_env_data(env_data);
+    // initialize environmental data
+    dtaq_read_env_data(_env_data);
 }
 
 void appdrv_reset()
@@ -48,7 +49,10 @@ nBus_sensorType_t appdrv_getType(uint8_t sensor_index)
         break;
     case HUM_ID:
         return TYPE_HYGROMETER;
-        break;   
+        break; 
+    case MES_ID:
+        return TYPE_METADATA;
+        break;
     default:
         return TYPE_UNKNOWN;
     }
@@ -56,7 +60,7 @@ nBus_sensorType_t appdrv_getType(uint8_t sensor_index)
 
 nBus_sensorCount_t appdrv_getSensorCount()
 {
-    return {.read_only_count=APP_SENSOR_CNT, .read_write_count=0};
+    return {.read_only_count=APP_SENSOR_CNT, .read_write_count=APP_SENSOR_RW_CNT};
 }
 
 uint8_t appdrv_getData(uint8_t sensor_index, uint8_t *data)
@@ -65,33 +69,33 @@ uint8_t appdrv_getData(uint8_t sensor_index, uint8_t *data)
 
     // measure data
     if (OR_EQ4(sensor_index, ALL_ID, TMP_ID, HUM_ID, TOF_ID))
-        dtaq_read_env_data(env_data);
+        dtaq_read_env_data(_env_data);
 
     if (OR_EQ2(sensor_index, ALL_ID, TOF_ID))
-        dtaq_read_tof_data(tof_data);
+        dtaq_read_tof_data(_tof_data);
 
     if (OR_EQ3(sensor_index, ALL_ID, ACC_ID, GYR_ID))
-        dtaq_read_imu_data(imu_data);
+        dtaq_read_imu_data(_imu_data);
 
     // validate payload size
-    if (NONE4(_phase_data.phase_num, tof_data.data_ready, imu_data.data_ready, env_data.data_ready))
+    if (NONE4(_phase_data.phase_num, _tof_data.data_ready, _imu_data.data_ready, _env_data.data_ready))
         return 0;
 
    // add data to tx buffer 
     if (OR_EQ2(sensor_index, ALL_ID, TOF_ID))
-        data_size = _write_sensor_values(data, data_size, TOF_ID, tof_data.payload.coordinates, APP_TOF_VCNT);
+        data_size = _write_sensor_values(data, data_size, TOF_ID, _tof_data.payload.coordinates, APP_TOF_VCNT);
 
     if (OR_EQ2(sensor_index, ALL_ID, ACC_ID))
-        data_size = _write_sensor_values(data, data_size, ACC_ID, imu_data.payload.acc, APP_ACC_VCNT);
+        data_size = _write_sensor_values(data, data_size, ACC_ID, _imu_data.payload.acc, APP_ACC_VCNT);
 
     if (OR_EQ2(sensor_index, ALL_ID, GYR_ID))
-        data_size = _write_sensor_values(data, data_size, GYR_ID, imu_data.payload.gyr, APP_GYR_VCNT);
+        data_size = _write_sensor_values(data, data_size, GYR_ID, _imu_data.payload.gyr, APP_GYR_VCNT);
     
     if (OR_EQ2(sensor_index, ALL_ID, TMP_ID))
-        data_size = _write_sensor_values(data, data_size, TMP_ID, &env_data.payload.temp, APP_TMP_VCNT);
+        data_size = _write_sensor_values(data, data_size, TMP_ID, &_env_data.payload.temp, APP_TMP_VCNT);
 
     if (OR_EQ2(sensor_index, ALL_ID, HUM_ID))
-        data_size = _write_sensor_values(data, data_size, HUM_ID, &env_data.payload.hum, APP_HUM_VCNT);
+        data_size = _write_sensor_values(data, data_size, HUM_ID, &_env_data.payload.hum, APP_HUM_VCNT);
 
     if (OR_EQ2(sensor_index, ALL_ID, MES_ID))
     {        
@@ -99,9 +103,7 @@ uint8_t appdrv_getData(uint8_t sensor_index, uint8_t *data)
         data[data_size++] = _phase_data.series_num;
         data[data_size++] = _phase_data.phase_num;
     }
-    
-    
-        counter++;
+
     return data_size;
 }
 
@@ -109,11 +111,11 @@ nBus_statusType_t appdrv_setData(uint8_t *data, uint8_t count, uint8_t *response
 {    
     if (data[0] == MES_ID)
     {
+        // update phase data
         _phase_data.series_num = data[1];
         _phase_data.phase_num = (app_phase_t)data[2];
 
-        //Serial.printf("%d -- %d %d\n", millis(), _phase_data.series_num,  _phase_data.phase_num );
-        
+        // set new working mode
         dtaq_set_working_mode(_phase_data.phase_num);
         
         return STATUS_SUCCESS;
@@ -144,8 +146,12 @@ nBus_statusType_t appdrv_start(void)
 
 nBus_statusType_t appdrv_stop(void)
 {
+    // reset phase data
     _phase_data.phase_num = IDLE_PH;
     _phase_data.series_num = 0;
+
+    // reset working mode
+    dtaq_set_working_mode(_phase_data.phase_num);
     
     return STATUS_SUCCESS;
 }
@@ -165,7 +171,6 @@ nBus_statusType_t appdrv_calibrate(uint8_t sensor_index)
     return STATUS_SUCCESS;
 }
 
-
 nBus_sensorFormat_t appdrv_getSensorFormat(uint8_t sensor_index)
 {
     switch (sensor_index)
@@ -222,11 +227,11 @@ nBus_sensorFormat_t appdrv_getSensorFormat(uint8_t sensor_index)
 
     case MES_ID:
         return (nBus_sensorFormat_t) {
-            .sign = 0,
-            .unit_multiplier = 0,
-            .value_multiplier = 0,
-            .byte_length = 1,
-            .samples = 2
+            .sign = APP_MES_VSGN,
+            .unit_multiplier = APP_MES_ULMUL,
+            .value_multiplier = APP_MES_VLMUL,
+            .byte_length = APP_MES_BCNT,
+            .samples = APP_MES_VCNT
         };
         break;