Complete proxy functions in Power.h and refactor (mainly reorder) for clarity.

This commit is contained in:
Phillip Kühne 2025-02-12 17:42:22 +01:00
parent 8617f420a2
commit d64579eca4
Signed by: phillip
GPG Key ID: E4C1C4D2F90902AA
4 changed files with 68 additions and 39 deletions

View File

@ -26,8 +26,12 @@ void Power::begin() {
}
}
uint16_t Power::getFreeCurrentBudget(void) {
return powerScheduler->getFreeCurrentBudget();
float Power::getFreeLimitCurrentBudget() {
return powerScheduler->getFreeLimitCurrentBudget();
}
float Power::getFreeMaximumCurrentBudget() {
return powerScheduler->getFreeMaximumCurrentBudget();
}
bool Power::tryAccquireCurrentAllowance(
@ -37,20 +41,32 @@ bool Power::tryAccquireCurrentAllowance(
requestedDurationMs);
}
void Power::releaseCurrent(PowerParameters::PowerConsumers consumer) {
powerScheduler->releaseCurrent(consumer);
}
bool Power::waitForCurrentAllowance(PowerParameters::PowerConsumers consumer,
uint16_t neededCurrent,
TickType_t TicksToWait,
uint16_t maxSlackTimeMs,
uint16_t requestedDurationMs) {
return powerScheduler->waitForCurrentAllowance(
consumer, neededCurrent, TicksToWait, requestedDurationMs);
consumer, neededCurrent, maxSlackTimeMs, requestedDurationMs);
}
void Power::beginPermanentDeepSleep(void) {
return powerScheduler->beginPermanentDeepSleep();
}
void Power::releaseCurrent(PowerParameters::PowerConsumers consumer) {
powerScheduler->releaseCurrent(consumer);
float Power::getCurrentCurrent(void) {
return powerScheduler->getCurrentCurrent();
}
void Power::recalculateCurrentBudgets(void) {
return powerScheduler->recalculateCurrentBudgets();
}
float Power::getConsumerCurrent(PowerParameters::PowerConsumers consumer) {
return powerScheduler->getConsumerCurrent(consumer);
}
float Power::getBatteryVoltage() {
@ -73,19 +89,13 @@ float Power::getBatteryVoltage() {
return batteryVoltage;
}
int Power::getBatteryChargePercent() {
return percentRemaining;
}
float Power::getBatteryChargeCoulombs() {
return coloumbsRemaining;
}
int Power::getBatteryChargePercent() { return percentRemaining; }
float Power::getBatteryChargeCoulombs() { return coloumbsRemaining; }
int Power::getBatteryVoltageChargePercent() {
// Get the battery voltage and calculate the charge state based on the
// discharge curve.
// Directly get the battery voltage and calculate the charge state based on
// the discharge curve.
float batteryVoltage = getBatteryVoltage();
float chargeState = 0;
// Clamp edge cases
@ -161,7 +171,6 @@ void Power::updatePowerStateHandler() {
powerScheduler->recalculateCurrentBudgets();
return;
}
float Power::getMax3V3Current() {
@ -173,7 +182,8 @@ float Power::getMax3V3Current() {
}
void Power::addSoCSample(float soc) {
latestSoCIndex = (latestSoCIndex + 1) % PowerParameters::Battery::AVERAGING_SAMPLES;
latestSoCIndex =
(latestSoCIndex + 1) % PowerParameters::Battery::AVERAGING_SAMPLES;
lastSOC[latestSoCIndex] = soc;
}

View File

@ -18,33 +18,52 @@ class Power {
public:
static void begin(void);
Power();
uint16_t getFreeCurrentBudget(void);
/// @brief Request an allowance of a certain number of milliwatts from the
/// power scheduler
/// @param neededPower the amount of power we want to be accounted for (in mW)
/// @return whether the power could be successfully allocated
static bool
tryAccquireCurrentAllowance(PowerParameters::PowerConsumers consumer,
uint16_t neededCurrent,
uint16_t requestedDurationMs);
/// @brief "Return" the power consumed by an active consumer
/// @param neededPower the amount of power to return (in mW)
/// @return whether the power
/// @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);
/// @brief Get the current hard maximum free current (to C2 discharge)
/// @return the maximum amount of power that can be allocated (in mA)
static float getFreeMaximumCurrentBudget(void);
/// @brief Request an allowance of a certain number of milliamperes from the
/// power scheduler without waiting for it (meaning it will not be scheduled
/// for future allocation). Only one can be active per consumer.
/// @param neededCurrent the amount of current we want to be accounted for (in
/// mA)
/// @return whether the current could be successfully allocated
static bool tryAccquireCurrentAllowance(PowerParameters::PowerConsumers consumer,
uint16_t neededcurrent,
uint16_t requestedDurationMs = 0);
/// @brief "Return" the current currently allocated to a consumer
/// @param consumer the active consumer to release the current for
static void releaseCurrent(PowerParameters::PowerConsumers consumer);
/// @brief Wait for a certain amount of power to be available
/// @param neededPower the amount of power we want to be accounted for (in mW)
/// @brief Wait for a certain amount of current to be available. This will
/// "reseve a spot in the queue". Only one can be active per consumer.
/// @param neededCurrent the amount of power we want to be accounted for (in
/// mW)
/// @param TicksToWait the amount of time to wait for the power to become
/// available
/// @return whether the power could be successfully allocatedy
static bool waitForCurrentAllowance(PowerParameters::PowerConsumers consumer,
uint16_t neededCurrent,
TickType_t TicksToWait,
uint16_t maxSlackTimeMs,
uint16_t requestedDurationMs);
/// @brief Put the ESP32 into deep sleep mode, without a method to wake up
/// again. Basically this is a shutdown.
static void beginPermanentDeepSleep(void);
//// @brief Get currently granted power
/// @return the amount of power that is currently allocated (in mA)
static float getCurrentCurrent(void);
// @brief Responsible for recalculating the current budgets
static void recalculateCurrentBudgets(void);
// @brief Get current consumption of a consumer
static float getConsumerCurrent(PowerParameters::PowerConsumers consumer);
/// @brief Get battery voltage measurement.
/// @return Battery Terminal Voltage in Volts
static float getBatteryVoltage();

View File

@ -230,11 +230,11 @@ float PowerScheduler::getCurrentCurrent(void) {
return currentSum;
}
float PowerScheduler::getFreeCurrentBudget(void) {
float PowerScheduler::getFreeLimitCurrentBudget(void) {
return this->freeLimitCurrentBudget;
}
float PowerScheduler::getFreeHardMaxCurrent(void) {
float PowerScheduler::getFreeMaximumCurrentBudget(void) {
return this->freeMaximumCurrentBudget;
}

View File

@ -32,10 +32,10 @@ public:
float i_max_ma = 0);
/// @brief Get the current free current budget (to C1 discharge)
/// @return the amount of power that is currently available (in mA)
float getFreeCurrentBudget(void);
float getFreeLimitCurrentBudget(void);
/// @brief Get the current hard maximum free current (to C2 discharge)
/// @return the maximum amount of power that can be allocated (in mA)
float getFreeHardMaxCurrent(void);
float getFreeMaximumCurrentBudget(void);
/// @brief Request an allowance of a certain number of milliamperes from the
/// power scheduler without waiting for it (meaning it will not be scheduled