Add battery monitoring to the monitoring manager, update relevant methods and log output

This commit is contained in:
m-RNA
2025-12-24 03:47:38 +08:00
parent 973afba994
commit 2a3f99b10c
3 changed files with 95 additions and 14 deletions

View File

@@ -3,6 +3,7 @@
#include <freertos/task.h>
#include <atomic>
#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<float> last_current_ma_{0.0f};
std::atomic<int> last_battery_mv_{0};
CurrentMonitor cm_;
BatteryMonitor bm_;
};

View File

@@ -1,26 +1,37 @@
#include "MonitoringManager.hpp"
#include <cmath>
#include <esp_log.h>
#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<float>(last_battery_mv_.load());
#else
return 0.0f;
#endif
}