diff --git a/components/Monitoring/Monitoring/MonitoringManager.hpp b/components/Monitoring/Monitoring/MonitoringManager.hpp index 7fb6e78..fe8ee6e 100644 --- a/components/Monitoring/Monitoring/MonitoringManager.hpp +++ b/components/Monitoring/Monitoring/MonitoringManager.hpp @@ -3,6 +3,7 @@ #include #include #include "CurrentMonitor.hpp" +#include "BatteryMonitor.hpp" class MonitoringManager { @@ -12,7 +13,9 @@ public: void stop(); // Latest filtered current in mA - float getCurrentMilliAmps() const { return last_current_ma_.load(); } + float getCurrentMilliAmps() const; + // Latest battery voltage in mV + float getBatteryVoltageMilliVolts() const; private: static void taskEntry(void *arg); @@ -20,5 +23,7 @@ private: TaskHandle_t task_{nullptr}; std::atomic last_current_ma_{0.0f}; + std::atomic last_battery_mv_{0}; CurrentMonitor cm_; + BatteryMonitor bm_; }; diff --git a/components/Monitoring/Monitoring/MonitoringManager_esp32s3.cpp b/components/Monitoring/Monitoring/MonitoringManager_esp32s3.cpp index 2eec518..32f321a 100644 --- a/components/Monitoring/Monitoring/MonitoringManager_esp32s3.cpp +++ b/components/Monitoring/Monitoring/MonitoringManager_esp32s3.cpp @@ -1,26 +1,37 @@ #include "MonitoringManager.hpp" +#include #include #include "sdkconfig.h" static const char *TAG_MM = "[MonitoringManager]"; - void MonitoringManager::setup() { -#ifdef CONFIG_MONITORING_LED_CURRENT +#if CONFIG_MONITORING_LED_CURRENT cm_.setup(); - ESP_LOGI(TAG_MM, "Monitoring enabled. Interval=%dms, Samples=%d, Gain=%d, R=%dmΩ", + ESP_LOGI(TAG_MM, "LED current monitoring enabled. Interval=%dms, Samples=%d, Gain=%d, R=%dmΩ", CONFIG_MONITORING_LED_INTERVAL_MS, CONFIG_MONITORING_LED_SAMPLES, CONFIG_MONITORING_LED_GAIN, CONFIG_MONITORING_LED_SHUNT_MILLIOHM); #else - ESP_LOGI(TAG_MM, "Monitoring disabled by Kconfig"); + ESP_LOGI(TAG_MM, "LED current monitoring disabled by Kconfig"); +#endif + +#if CONFIG_MONITORING_BATTERY_ENABLE + bm_.setup(); + ESP_LOGI(TAG_MM, "Battery monitoring enabled. Interval=%dms, Samples=%d, R-Top=%dΩ, R-Bottom=%dΩ", + CONFIG_MONITORING_BATTERY_INTERVAL_MS, + CONFIG_MONITORING_BATTERY_SAMPLES, + CONFIG_MONITORING_BATTERY_DIVIDER_R_TOP_OHM, + CONFIG_MONITORING_BATTERY_DIVIDER_R_BOTTOM_OHM); +#else + ESP_LOGI(TAG_MM, "Battery monitoring disabled by Kconfig"); #endif } void MonitoringManager::start() { -#ifdef CONFIG_MONITORING_LED_CURRENT +#if CONFIG_MONITORING_LED_CURRENT || CONFIG_MONITORING_BATTERY_ENABLE if (task_ == nullptr) { xTaskCreate(&MonitoringManager::taskEntry, "MonitoringTask", 2048, this, 1, &task_); @@ -45,14 +56,79 @@ void MonitoringManager::taskEntry(void *arg) void MonitoringManager::run() { -#ifdef CONFIG_MONITORING_LED_CURRENT +#if CONFIG_MONITORING_LED_CURRENT || CONFIG_MONITORING_BATTERY_ENABLE + TickType_t now_tick = xTaskGetTickCount(); +#if CONFIG_MONITORING_LED_CURRENT + TickType_t next_tick_led = now_tick; + const TickType_t led_period = pdMS_TO_TICKS(CONFIG_MONITORING_LED_INTERVAL_MS); +#endif +#if CONFIG_MONITORING_BATTERY_ENABLE + TickType_t next_tick_bat = now_tick; + const TickType_t batt_period = pdMS_TO_TICKS(CONFIG_MONITORING_BATTERY_INTERVAL_MS); +#endif + + while (true) { - float ma = cm_.pollAndGetMilliAmps(); - last_current_ma_.store(ma); - vTaskDelay(pdMS_TO_TICKS(CONFIG_MONITORING_LED_INTERVAL_MS)); + now_tick = xTaskGetTickCount(); + TickType_t wait_ticks = pdMS_TO_TICKS(50); + +#if CONFIG_MONITORING_LED_CURRENT + if (now_tick >= next_tick_led) + { + float ma = cm_.getCurrentMilliAmps(); + last_current_ma_.store(ma); + next_tick_led = now_tick + led_period; + } + TickType_t to_led = (next_tick_led > now_tick) ? (next_tick_led - now_tick) : 1; + if (to_led < wait_ticks) + { + wait_ticks = to_led; + } +#endif + +#if CONFIG_MONITORING_BATTERY_ENABLE + if (now_tick >= next_tick_bat) + { + const int mv = bm_.getBatteryMilliVolts(); + if (mv > 0) + { + last_battery_mv_.store(mv); + } + next_tick_bat = now_tick + batt_period; + } + TickType_t to_batt = (next_tick_bat > now_tick) ? (next_tick_bat - now_tick) : 1; + if (to_batt < wait_ticks) + { + wait_ticks = to_batt; + } +#endif + + if (wait_ticks == 0) + { + wait_ticks = 1; + } + vTaskDelay(wait_ticks); } #else vTaskDelete(nullptr); #endif } + +float MonitoringManager::getCurrentMilliAmps() const +{ +#if CONFIG_MONITORING_LED_CURRENT + return last_current_ma_.load(); +#else + return 0.0f; +#endif +} + +float MonitoringManager::getBatteryVoltageMilliVolts() const +{ +#if CONFIG_MONITORING_BATTERY_ENABLE + return static_cast(last_battery_mv_.load()); +#else + return 0.0f; +#endif +} diff --git a/main/openiris_main.cpp b/main/openiris_main.cpp index 95fae32..fb9a945 100644 --- a/main/openiris_main.cpp +++ b/main/openiris_main.cpp @@ -23,7 +23,7 @@ #include #include -#ifdef CONFIG_MONITORING_LED_CURRENT +#if CONFIG_MONITORING_LED_CURRENT || CONFIG_MONITORING_BATTERY_ENABLE #include #endif @@ -72,7 +72,7 @@ UVCStreamManager uvcStream; auto ledManager = std::make_shared(BLINK_GPIO, CONFIG_LED_C_PIN_GPIO, ledStateQueue, deviceConfig); -#ifdef CONFIG_MONITORING_LED_CURRENT +#if CONFIG_MONITORING_LED_CURRENT || CONFIG_MONITORING_BATTERY_ENABLE std::shared_ptr monitoringManager = std::make_shared(); #endif @@ -273,7 +273,7 @@ extern "C" void app_main(void) #endif dependencyRegistry->registerService(DependencyType::led_manager, ledManager); -#ifdef CONFIG_MONITORING_LED_CURRENT +#if CONFIG_MONITORING_LED_CURRENT || CONFIG_MONITORING_BATTERY_ENABLE dependencyRegistry->registerService(DependencyType::monitoring_manager, monitoringManager); #endif @@ -285,7 +285,7 @@ extern "C" void app_main(void) deviceConfig->load(); ledManager->setup(); -#ifdef CONFIG_MONITORING_LED_CURRENT +#if CONFIG_MONITORING_LED_CURRENT || CONFIG_MONITORING_BATTERY_ENABLE monitoringManager->setup(); monitoringManager->start(); #endif