mirror of
https://gitlab.dit.htwk-leipzig.de/phillip.kuehne/dezibot.git
synced 2025-09-18 11:28:04 +02:00
Rename Power.* to PowerManager.* for clarity
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @file Power.h
|
||||
* @file PowerManager.h
|
||||
* @author Phillip Kühne
|
||||
* @brief This component provides utilities for keeping track of power usage
|
||||
* consumption.
|
||||
@@ -7,19 +7,19 @@
|
||||
* @date 2024-11-23
|
||||
*/
|
||||
|
||||
#include "Power.h"
|
||||
#include "PowerManager.h"
|
||||
|
||||
SemaphoreHandle_t Power::powerMutex = NULL;
|
||||
SemaphoreHandle_t PowerManager::powerMutex = NULL;
|
||||
|
||||
void vTaskUpdatePowerState(void *pvParameters) {
|
||||
for (;;) {
|
||||
ESP_LOGV(TAG, "Updating Power State...");
|
||||
Power::updatePowerStateHandler();
|
||||
PowerManager::updatePowerStateHandler();
|
||||
vTaskDelay(pdMS_TO_TICKS(10));
|
||||
}
|
||||
}
|
||||
|
||||
void Power::begin() {
|
||||
void PowerManager::begin() {
|
||||
// Check if another instance of us already initialized the power scheduler,
|
||||
// if not, we will do it.
|
||||
ESP_LOGI(TAG, "Initializing Power Management");
|
||||
@@ -38,8 +38,8 @@ void Power::begin() {
|
||||
powerScheduler = &PowerScheduler::getPowerScheduler(
|
||||
PowerParameters::Battery::CELL_CURRENT_1C_MA,
|
||||
PowerParameters::Battery::CELL_CURRENT_2C_MA);
|
||||
Power::initPowerState();
|
||||
Power::recalculateCurrentBudgets();
|
||||
PowerManager::initPowerState();
|
||||
PowerManager::recalculateCurrentBudgets();
|
||||
TaskHandle_t xHandle = NULL;
|
||||
xTaskCreate(vTaskUpdatePowerState, "vTaskPowerStateUpdate", 4096, NULL,
|
||||
tskIDLE_PRIORITY, &xHandle);
|
||||
@@ -54,26 +54,26 @@ void Power::begin() {
|
||||
}
|
||||
}
|
||||
|
||||
float Power::getFreeLimitCurrentBudget() {
|
||||
float PowerManager::getFreeLimitCurrentBudget() {
|
||||
return powerScheduler->getFreeLimitCurrentBudget();
|
||||
}
|
||||
|
||||
float Power::getFreeMaximumCurrentBudget() {
|
||||
float PowerManager::getFreeMaximumCurrentBudget() {
|
||||
return powerScheduler->getFreeMaximumCurrentBudget();
|
||||
}
|
||||
|
||||
bool Power::tryAccquireCurrentAllowance(
|
||||
bool PowerManager::tryAccquireCurrentAllowance(
|
||||
PowerParameters::PowerConsumers consumer, uint16_t neededCurrent,
|
||||
uint16_t requestedDurationMs) {
|
||||
return powerScheduler->tryAccquireCurrentAllowance(consumer, neededCurrent,
|
||||
requestedDurationMs);
|
||||
}
|
||||
|
||||
void Power::releaseCurrent(PowerParameters::PowerConsumers consumer) {
|
||||
void PowerManager::releaseCurrent(PowerParameters::PowerConsumers consumer) {
|
||||
powerScheduler->releaseCurrent(consumer);
|
||||
}
|
||||
|
||||
bool Power::waitForCurrentAllowance(PowerParameters::PowerConsumers consumer,
|
||||
bool PowerManager::waitForCurrentAllowance(PowerParameters::PowerConsumers consumer,
|
||||
uint16_t neededCurrent,
|
||||
uint16_t maxSlackTimeMs,
|
||||
uint16_t requestedDurationMs) {
|
||||
@@ -81,15 +81,15 @@ bool Power::waitForCurrentAllowance(PowerParameters::PowerConsumers consumer,
|
||||
consumer, neededCurrent, maxSlackTimeMs, requestedDurationMs);
|
||||
}
|
||||
|
||||
void Power::beginPermanentDeepSleep(void) {
|
||||
void PowerManager::beginPermanentDeepSleep(void) {
|
||||
return powerScheduler->beginPermanentDeepSleep();
|
||||
}
|
||||
|
||||
float Power::getCurrentCurrent(void) {
|
||||
float PowerManager::getCurrentCurrent(void) {
|
||||
return powerScheduler->getCurrentCurrent();
|
||||
}
|
||||
|
||||
float Power::getBatteryCurrent(void) {
|
||||
float PowerManager::getBatteryCurrent(void) {
|
||||
const float i_3v3 = getCurrentCurrent();
|
||||
const float u_3v3 = 3.3;
|
||||
const float u_bat = getBatteryVoltage();
|
||||
@@ -97,15 +97,15 @@ float Power::getBatteryCurrent(void) {
|
||||
return (u_3v3 * i_3v3) / (u_bat * eta);
|
||||
}
|
||||
|
||||
void Power::recalculateCurrentBudgets(void) {
|
||||
void PowerManager::recalculateCurrentBudgets(void) {
|
||||
return powerScheduler->recalculateCurrentBudgets();
|
||||
}
|
||||
|
||||
float Power::getConsumerCurrent(PowerParameters::PowerConsumers consumer) {
|
||||
float PowerManager::getConsumerCurrent(PowerParameters::PowerConsumers consumer) {
|
||||
return powerScheduler->getConsumerCurrent(consumer);
|
||||
}
|
||||
|
||||
float Power::getBatteryVoltage() {
|
||||
float PowerManager::getBatteryVoltage() {
|
||||
// Get the battery voltage from the ADC and convert it to a voltage
|
||||
// using the voltage divider.
|
||||
pinMode(PowerParameters::PinConfig::BAT_ADC_EN, OUTPUT);
|
||||
@@ -127,14 +127,14 @@ float Power::getBatteryVoltage() {
|
||||
return batteryVoltage;
|
||||
}
|
||||
|
||||
float Power::getBatteryChargePercent() { return percentRemaining; }
|
||||
float PowerManager::getBatteryChargePercent() { return percentRemaining; }
|
||||
|
||||
float Power::getBatteryChargeCoulombs() { return coloumbsRemaining; }
|
||||
float PowerManager::getBatteryChargeCoulombs() { return coloumbsRemaining; }
|
||||
|
||||
float Power::getBatteryVoltageChargePercent() {
|
||||
float PowerManager::getBatteryVoltageChargePercent() {
|
||||
// Directly get the battery voltage, correct the curve with an offset and
|
||||
// calculate the charge state based on the discharge curve.
|
||||
float batteryVoltage = getBatteryVoltage() + Power::fullVoltageOffset;
|
||||
float batteryVoltage = getBatteryVoltage() + PowerManager::fullVoltageOffset;
|
||||
float chargeState = 0;
|
||||
// Clamp edge cases
|
||||
if (batteryVoltage >=
|
||||
@@ -163,16 +163,16 @@ float Power::getBatteryVoltageChargePercent() {
|
||||
}
|
||||
}
|
||||
|
||||
void Power::updatePowerStateHandler() {
|
||||
void PowerManager::updatePowerStateHandler() {
|
||||
|
||||
// Update supply and charge state flags
|
||||
Power::busPowered = !digitalRead(PowerParameters::PinConfig::VUSB_SENS);
|
||||
Power::chargingState = digitalRead(PowerParameters::PinConfig::BAT_CHG_STAT);
|
||||
PowerManager::busPowered = !digitalRead(PowerParameters::PinConfig::VUSB_SENS);
|
||||
PowerManager::chargingState = digitalRead(PowerParameters::PinConfig::BAT_CHG_STAT);
|
||||
|
||||
// If the battery is charging and fully charged
|
||||
if (Power::busPowered && !Power::chargingState) {
|
||||
if (PowerManager::busPowered && !PowerManager::chargingState) {
|
||||
// Calibrate voltage offset on full Battery
|
||||
Power::fullVoltageOffset = PowerParameters::Battery::DISCHARGE_CURVE::VOLTAGES[0] - getBatteryVoltage();
|
||||
PowerManager::fullVoltageOffset = PowerParameters::Battery::DISCHARGE_CURVE::VOLTAGES[0] - getBatteryVoltage();
|
||||
}
|
||||
|
||||
ESP_LOGD(TAG, "Bus Powered: %d, Charging: %d", busPowered, chargingState);
|
||||
@@ -184,7 +184,7 @@ void Power::updatePowerStateHandler() {
|
||||
float coloumbsConsumedSinceLastUpdate;
|
||||
|
||||
// Calculate remaining battery charge in Coulombs based on current and time
|
||||
if (!Power::busPowered) {
|
||||
if (!PowerManager::busPowered) {
|
||||
coloumbsConsumedSinceLastUpdate =
|
||||
(currentCurrent / 1000) *
|
||||
((pdTICKS_TO_MS(xTaskGetTickCount() - lastPowerStateUpdate)) / 1000.0);
|
||||
@@ -196,7 +196,7 @@ void Power::updatePowerStateHandler() {
|
||||
|
||||
// If current flow is close enough to reference, get battery charge state via
|
||||
// voltage curve
|
||||
if (!Power::busPowered) {
|
||||
if (!PowerManager::busPowered) {
|
||||
if ((currentCurrent > (referenceCurrentMa * 0.6)) &&
|
||||
(currentCurrent < (referenceCurrentMa * 1.4))) {
|
||||
// Get battery charge state from voltage curve
|
||||
@@ -233,21 +233,21 @@ void Power::updatePowerStateHandler() {
|
||||
for (int i = 0; i < PowerParameters::Battery::AVERAGING_SAMPLES; i++) {
|
||||
sampleSum += lastSOC[i];
|
||||
}
|
||||
Power::percentRemaining =
|
||||
PowerManager::percentRemaining =
|
||||
sampleSum / PowerParameters::Battery::AVERAGING_SAMPLES;
|
||||
|
||||
// Update last update time
|
||||
Power::lastPowerStateUpdate = xTaskGetTickCount();
|
||||
PowerManager::lastPowerStateUpdate = xTaskGetTickCount();
|
||||
|
||||
// Update the available current (changes based on battery state of charge)
|
||||
Power::powerScheduler->recalculateCurrentBudgets();
|
||||
PowerManager::powerScheduler->recalculateCurrentBudgets();
|
||||
ESP_LOGD(TAG, "Current: %f mA, Charge: %f Coulombs, %f %%", currentCurrent,
|
||||
coloumbsRemaining, percentRemaining);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
float Power::getMax3V3Current() {
|
||||
float PowerManager::getMax3V3Current() {
|
||||
// Conversion from Thesis
|
||||
float u_bat = getBatteryVoltage();
|
||||
float i_bat = PowerParameters::Battery::CELL_CURRENT_1C_MA;
|
||||
@@ -256,7 +256,7 @@ float Power::getMax3V3Current() {
|
||||
return (u_bat * i_bat * eta) / u_3v3;
|
||||
}
|
||||
|
||||
void Power::addSoCSample(float soc) {
|
||||
void PowerManager::addSoCSample(float soc) {
|
||||
PowerMutex lock(powerMutex);
|
||||
if (!lock.isLocked()) {
|
||||
ESP_LOGE(TAG, "Could not take power to add SoC sample");
|
||||
@@ -267,7 +267,7 @@ void Power::addSoCSample(float soc) {
|
||||
lastSOC[latestSoCIndex] = soc;
|
||||
}
|
||||
|
||||
void Power::initPowerState(void) {
|
||||
void PowerManager::initPowerState(void) {
|
||||
// Initialize the power state
|
||||
lastPowerStateUpdate = xTaskGetTickCount();
|
||||
// TODO: Get initial battery charge state based on voltage, set coloumbs based
|
||||
@@ -288,88 +288,88 @@ void Power::initPowerState(void) {
|
||||
chargingState = digitalRead(PowerParameters::PinConfig::BAT_CHG_STAT);
|
||||
}
|
||||
|
||||
void Power::dumpPowerStatistics() {
|
||||
void PowerManager::dumpPowerStatistics() {
|
||||
Serial.printf("======== Dezibot Power Statistics ========\r\n");
|
||||
Serial.printf("Current: %f mA\r\n", Power::getCurrentCurrent());
|
||||
Serial.printf("Battery Voltage: %f V\r\n", Power::getBatteryVoltage());
|
||||
Serial.printf("Battery Charge: %f %%\r\n", Power::getBatteryChargePercent());
|
||||
Serial.printf("Current: %f mA\r\n", PowerManager::getCurrentCurrent());
|
||||
Serial.printf("Battery Voltage: %f V\r\n", PowerManager::getBatteryVoltage());
|
||||
Serial.printf("Battery Charge: %f %%\r\n", PowerManager::getBatteryChargePercent());
|
||||
Serial.printf("Battery Charge: %f Coulombs\r\n",
|
||||
Power::getBatteryChargeCoulombs());
|
||||
PowerManager::getBatteryChargeCoulombs());
|
||||
Serial.printf("Max 3.3V Current in this state (1C, 2C): %f mA, %f mA \r\n",
|
||||
Power::getMax3V3Current(), Power::getMax3V3Current() * 2);
|
||||
PowerManager::getMax3V3Current(), PowerManager::getMax3V3Current() * 2);
|
||||
Serial.printf("=========================================\r\n");
|
||||
}
|
||||
|
||||
void Power::dumpConsumerStatistics() {
|
||||
void PowerManager::dumpConsumerStatistics() {
|
||||
Serial.printf("======== Dezibot Consumer Statistics ========\r\n");
|
||||
Serial.printf("ESP: %f mA\r\n", Power::getConsumerCurrent(
|
||||
Serial.printf("ESP: %f mA\r\n", PowerManager::getConsumerCurrent(
|
||||
PowerParameters::PowerConsumers::ESP));
|
||||
Serial.printf("WIFI: %f mA\r\n", Power::getConsumerCurrent(
|
||||
Serial.printf("WIFI: %f mA\r\n", PowerManager::getConsumerCurrent(
|
||||
PowerParameters::PowerConsumers::WIFI));
|
||||
Serial.printf("LED_RGB_TOP_LEFT: %f mA\r\n",
|
||||
Power::getConsumerCurrent(
|
||||
PowerManager::getConsumerCurrent(
|
||||
PowerParameters::PowerConsumers::LED_RGB_TOP_LEFT));
|
||||
Serial.printf("LED_RGB_TOP_RIGHT: %f mA\r\n",
|
||||
Power::getConsumerCurrent(
|
||||
PowerManager::getConsumerCurrent(
|
||||
PowerParameters::PowerConsumers::LED_RGB_TOP_RIGHT));
|
||||
Serial.printf("LED_RGB_BOTTOM: %f mA\r\n",
|
||||
Power::getConsumerCurrent(
|
||||
PowerManager::getConsumerCurrent(
|
||||
PowerParameters::PowerConsumers::LED_RGB_BOTTOM));
|
||||
Serial.printf(
|
||||
"RGBW_SENSOR: %f mA\r\n",
|
||||
Power::getConsumerCurrent(PowerParameters::PowerConsumers::RGBW_SENSOR));
|
||||
PowerManager::getConsumerCurrent(PowerParameters::PowerConsumers::RGBW_SENSOR));
|
||||
Serial.printf("LED_IR_BOTTOM: %f mA\r\n",
|
||||
Power::getConsumerCurrent(
|
||||
PowerManager::getConsumerCurrent(
|
||||
PowerParameters::PowerConsumers::LED_IR_BOTTOM));
|
||||
Serial.printf(
|
||||
"LED_IR_FRONT: %f mA\r\n",
|
||||
Power::getConsumerCurrent(PowerParameters::PowerConsumers::LED_IR_FRONT));
|
||||
PowerManager::getConsumerCurrent(PowerParameters::PowerConsumers::LED_IR_FRONT));
|
||||
Serial.printf(
|
||||
"PT_IR: %f mA\r\n",
|
||||
Power::getConsumerCurrent(PowerParameters::PowerConsumers::PT_IR));
|
||||
PowerManager::getConsumerCurrent(PowerParameters::PowerConsumers::PT_IR));
|
||||
Serial.printf(
|
||||
"PT_DL: %f mA\r\n",
|
||||
Power::getConsumerCurrent(PowerParameters::PowerConsumers::PT_DL));
|
||||
PowerManager::getConsumerCurrent(PowerParameters::PowerConsumers::PT_DL));
|
||||
Serial.printf(
|
||||
"LED_UV: %f mA\r\n",
|
||||
Power::getConsumerCurrent(PowerParameters::PowerConsumers::LED_UV));
|
||||
PowerManager::getConsumerCurrent(PowerParameters::PowerConsumers::LED_UV));
|
||||
Serial.printf(
|
||||
"DISPLAY_OLED: %f mA\r\n",
|
||||
Power::getConsumerCurrent(PowerParameters::PowerConsumers::DISPLAY_OLED));
|
||||
PowerManager::getConsumerCurrent(PowerParameters::PowerConsumers::DISPLAY_OLED));
|
||||
Serial.printf(
|
||||
"MOTOR_LEFT: %f mA\r\n",
|
||||
Power::getConsumerCurrent(PowerParameters::PowerConsumers::MOTOR_LEFT));
|
||||
PowerManager::getConsumerCurrent(PowerParameters::PowerConsumers::MOTOR_LEFT));
|
||||
Serial.printf(
|
||||
"MOTOR_RIGHT: %f mA\r\n",
|
||||
Power::getConsumerCurrent(PowerParameters::PowerConsumers::MOTOR_RIGHT));
|
||||
Serial.printf("IMU: %f mA\r\n", Power::getConsumerCurrent(
|
||||
PowerManager::getConsumerCurrent(PowerParameters::PowerConsumers::MOTOR_RIGHT));
|
||||
Serial.printf("IMU: %f mA\r\n", PowerManager::getConsumerCurrent(
|
||||
PowerParameters::PowerConsumers::IMU));
|
||||
Serial.printf("=============================================\r\n");
|
||||
}
|
||||
|
||||
bool Power::isUSBPowered() { return busPowered; }
|
||||
bool PowerManager::isUSBPowered() { return busPowered; }
|
||||
|
||||
bool Power::isBatteryPowered() { return !busPowered; }
|
||||
bool PowerManager::isBatteryPowered() { return !busPowered; }
|
||||
|
||||
bool Power::isBatteryCharging() { return chargingState && busPowered; }
|
||||
bool PowerManager::isBatteryCharging() { return chargingState && busPowered; }
|
||||
|
||||
bool Power::isBatteryDischarging() { return !chargingState && !busPowered; }
|
||||
bool PowerManager::isBatteryDischarging() { return !chargingState && !busPowered; }
|
||||
|
||||
bool Power::isBatteryFullyCharged() { return !chargingState && busPowered; }
|
||||
int Power::latestSoCIndex = 0;
|
||||
float Power::lastSOC[PowerParameters::Battery::AVERAGING_SAMPLES] = {0};
|
||||
TickType_t Power::lastPowerStateUpdate = 0;
|
||||
float Power::coloumbsRemaining =
|
||||
bool PowerManager::isBatteryFullyCharged() { return !chargingState && busPowered; }
|
||||
int PowerManager::latestSoCIndex = 0;
|
||||
float PowerManager::lastSOC[PowerParameters::Battery::AVERAGING_SAMPLES] = {0};
|
||||
TickType_t PowerManager::lastPowerStateUpdate = 0;
|
||||
float PowerManager::coloumbsRemaining =
|
||||
PowerParameters::Battery::CELL_CHARGE_FULL_COLOUMB;
|
||||
float Power::percentRemaining = 100.0;
|
||||
PowerScheduler *Power::powerScheduler = nullptr;
|
||||
float PowerManager::percentRemaining = 100.0;
|
||||
PowerScheduler *PowerManager::powerScheduler = nullptr;
|
||||
|
||||
bool Power::busPowered = false;
|
||||
bool PowerManager::busPowered = false;
|
||||
|
||||
bool Power::chargingState = false;
|
||||
bool PowerManager::chargingState = false;
|
||||
|
||||
float Power::fullVoltageOffset = 0;
|
||||
float PowerManager::fullVoltageOffset = 0;
|
||||
|
||||
Power::Power() {}
|
||||
PowerManager::PowerManager() {}
|
||||
|
||||
Power::~Power() {}
|
||||
PowerManager::~PowerManager() {}
|
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* @file Power.h
|
||||
* @file PowerManager.h
|
||||
* @author Phillip Kühne
|
||||
* @brief This component provides utilities for keeping track of power usage
|
||||
* consumption.
|
||||
@@ -21,7 +21,7 @@
|
||||
|
||||
enum TaskResumptionReason { POWER_AVAILABLE, TIMEOUT };
|
||||
|
||||
class Power {
|
||||
class PowerManager {
|
||||
|
||||
private:
|
||||
static SemaphoreHandle_t powerMutex;
|
||||
@@ -86,8 +86,8 @@ protected:
|
||||
|
||||
public:
|
||||
static void begin(void);
|
||||
Power();
|
||||
~Power();
|
||||
PowerManager();
|
||||
~PowerManager();
|
||||
/// @brief Get the current free current budget (to C1 discharge)
|
||||
/// @return the amount of power that is currently available (in mA)
|
||||
static float getFreeLimitCurrentBudget(void);
|
||||
@@ -193,6 +193,6 @@ public:
|
||||
|
||||
};
|
||||
|
||||
extern Power power;
|
||||
extern PowerManager power;
|
||||
|
||||
#endif // Power
|
||||
#endif // Power
|
@@ -11,7 +11,7 @@
|
||||
*/
|
||||
|
||||
#include "PowerScheduler.h"
|
||||
#include "Power.h"
|
||||
#include "PowerManager.h"
|
||||
|
||||
bool PowerScheduler::tryAccquireCurrentAllowance(
|
||||
PowerParameters::PowerConsumers consumer, float neededCurrent,
|
||||
@@ -192,9 +192,9 @@ void PowerScheduler::recalculateCurrentBudgets(void) {
|
||||
|
||||
// Get the respective maximums and subtract currently flowing currents
|
||||
ESP_LOGI(TAG, "Recalculating current budgets...");
|
||||
float tempFreeLimitCurrentBudget = Power::getMax3V3Current();
|
||||
float tempFreeLimitCurrentBudget = PowerManager::getMax3V3Current();
|
||||
ESP_LOGI(TAG, "Got max 3V3 current: %.2f", tempFreeLimitCurrentBudget);
|
||||
float tempFreeMaximumCurrentBudget = Power::getMax3V3Current() * 2;
|
||||
float tempFreeMaximumCurrentBudget = PowerManager::getMax3V3Current() * 2;
|
||||
PowerSchedulerMutex lock(powerSchedulerMutex);
|
||||
if (lock.isLocked() == false) {
|
||||
return;
|
||||
@@ -315,4 +315,4 @@ PowerScheduler::~PowerScheduler() {
|
||||
if (powerSchedulerMutex != NULL) {
|
||||
vSemaphoreDelete(powerSchedulerMutex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user