mirror of
https://github.com/MrUnknownDE/OpenIris-ESPIDF.git
synced 2026-05-05 13:36:04 +02:00
Add battery monitoring to the monitoring manager, update relevant methods and log output
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
#include <freertos/task.h>
|
#include <freertos/task.h>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include "CurrentMonitor.hpp"
|
#include "CurrentMonitor.hpp"
|
||||||
|
#include "BatteryMonitor.hpp"
|
||||||
|
|
||||||
class MonitoringManager
|
class MonitoringManager
|
||||||
{
|
{
|
||||||
@@ -12,7 +13,9 @@ public:
|
|||||||
void stop();
|
void stop();
|
||||||
|
|
||||||
// Latest filtered current in mA
|
// 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:
|
private:
|
||||||
static void taskEntry(void *arg);
|
static void taskEntry(void *arg);
|
||||||
@@ -20,5 +23,7 @@ private:
|
|||||||
|
|
||||||
TaskHandle_t task_{nullptr};
|
TaskHandle_t task_{nullptr};
|
||||||
std::atomic<float> last_current_ma_{0.0f};
|
std::atomic<float> last_current_ma_{0.0f};
|
||||||
|
std::atomic<int> last_battery_mv_{0};
|
||||||
CurrentMonitor cm_;
|
CurrentMonitor cm_;
|
||||||
|
BatteryMonitor bm_;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,26 +1,37 @@
|
|||||||
#include "MonitoringManager.hpp"
|
#include "MonitoringManager.hpp"
|
||||||
|
#include <cmath>
|
||||||
#include <esp_log.h>
|
#include <esp_log.h>
|
||||||
#include "sdkconfig.h"
|
#include "sdkconfig.h"
|
||||||
|
|
||||||
static const char *TAG_MM = "[MonitoringManager]";
|
static const char *TAG_MM = "[MonitoringManager]";
|
||||||
|
|
||||||
void MonitoringManager::setup()
|
void MonitoringManager::setup()
|
||||||
{
|
{
|
||||||
#ifdef CONFIG_MONITORING_LED_CURRENT
|
#if CONFIG_MONITORING_LED_CURRENT
|
||||||
cm_.setup();
|
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_INTERVAL_MS,
|
||||||
CONFIG_MONITORING_LED_SAMPLES,
|
CONFIG_MONITORING_LED_SAMPLES,
|
||||||
CONFIG_MONITORING_LED_GAIN,
|
CONFIG_MONITORING_LED_GAIN,
|
||||||
CONFIG_MONITORING_LED_SHUNT_MILLIOHM);
|
CONFIG_MONITORING_LED_SHUNT_MILLIOHM);
|
||||||
#else
|
#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
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void MonitoringManager::start()
|
void MonitoringManager::start()
|
||||||
{
|
{
|
||||||
#ifdef CONFIG_MONITORING_LED_CURRENT
|
#if CONFIG_MONITORING_LED_CURRENT || CONFIG_MONITORING_BATTERY_ENABLE
|
||||||
if (task_ == nullptr)
|
if (task_ == nullptr)
|
||||||
{
|
{
|
||||||
xTaskCreate(&MonitoringManager::taskEntry, "MonitoringTask", 2048, this, 1, &task_);
|
xTaskCreate(&MonitoringManager::taskEntry, "MonitoringTask", 2048, this, 1, &task_);
|
||||||
@@ -45,14 +56,79 @@ void MonitoringManager::taskEntry(void *arg)
|
|||||||
|
|
||||||
void MonitoringManager::run()
|
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)
|
while (true)
|
||||||
{
|
{
|
||||||
float ma = cm_.pollAndGetMilliAmps();
|
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);
|
last_current_ma_.store(ma);
|
||||||
vTaskDelay(pdMS_TO_TICKS(CONFIG_MONITORING_LED_INTERVAL_MS));
|
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
|
#else
|
||||||
vTaskDelete(nullptr);
|
vTaskDelete(nullptr);
|
||||||
#endif
|
#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<float>(last_battery_mv_.load());
|
||||||
|
#else
|
||||||
|
return 0.0f;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|||||||
@@ -23,7 +23,7 @@
|
|||||||
#include <RestAPI.hpp>
|
#include <RestAPI.hpp>
|
||||||
#include <main_globals.hpp>
|
#include <main_globals.hpp>
|
||||||
|
|
||||||
#ifdef CONFIG_MONITORING_LED_CURRENT
|
#if CONFIG_MONITORING_LED_CURRENT || CONFIG_MONITORING_BATTERY_ENABLE
|
||||||
#include <MonitoringManager.hpp>
|
#include <MonitoringManager.hpp>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -72,7 +72,7 @@ UVCStreamManager uvcStream;
|
|||||||
|
|
||||||
auto ledManager = std::make_shared<LEDManager>(BLINK_GPIO, CONFIG_LED_C_PIN_GPIO, ledStateQueue, deviceConfig);
|
auto ledManager = std::make_shared<LEDManager>(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> monitoringManager = std::make_shared<MonitoringManager>();
|
std::shared_ptr<MonitoringManager> monitoringManager = std::make_shared<MonitoringManager>();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -273,7 +273,7 @@ extern "C" void app_main(void)
|
|||||||
#endif
|
#endif
|
||||||
dependencyRegistry->registerService<LEDManager>(DependencyType::led_manager, ledManager);
|
dependencyRegistry->registerService<LEDManager>(DependencyType::led_manager, ledManager);
|
||||||
|
|
||||||
#ifdef CONFIG_MONITORING_LED_CURRENT
|
#if CONFIG_MONITORING_LED_CURRENT || CONFIG_MONITORING_BATTERY_ENABLE
|
||||||
dependencyRegistry->registerService<MonitoringManager>(DependencyType::monitoring_manager, monitoringManager);
|
dependencyRegistry->registerService<MonitoringManager>(DependencyType::monitoring_manager, monitoringManager);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -285,7 +285,7 @@ extern "C" void app_main(void)
|
|||||||
deviceConfig->load();
|
deviceConfig->load();
|
||||||
ledManager->setup();
|
ledManager->setup();
|
||||||
|
|
||||||
#ifdef CONFIG_MONITORING_LED_CURRENT
|
#if CONFIG_MONITORING_LED_CURRENT || CONFIG_MONITORING_BATTERY_ENABLE
|
||||||
monitoringManager->setup();
|
monitoringManager->setup();
|
||||||
monitoringManager->start();
|
monitoringManager->start();
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user