From 8617f420a2c85ade57e96e5f09dceb25dd11fd80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Phillip=20K=C3=BChne?= Date: Wed, 12 Feb 2025 16:27:54 +0100 Subject: [PATCH] Add special case for small power requests --- src/power/PowerParameters.h | 7 +++++++ src/power/PowerScheduler.cpp | 22 +++++++++++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/power/PowerParameters.h b/src/power/PowerParameters.h index fab44e7..6b21529 100644 --- a/src/power/PowerParameters.h +++ b/src/power/PowerParameters.h @@ -79,6 +79,13 @@ namespace PowerParameters { static constexpr float CURRENT_UV_LED = 200; }; + /* + * Single consumer current limit up to which requests are granted no matter what. + * The idea is, that this will allow Sensors (with their miniscule power draw) + * to always be granted power, which should massively improve behaviour. + */ + static constexpr float CURRENT_INSIGNIFICANT = 1; + struct PinConfig { static constexpr int BAT_ADC = 10; static constexpr int BAT_ADC_EN = 9; diff --git a/src/power/PowerScheduler.cpp b/src/power/PowerScheduler.cpp index 22a72da..8c1f434 100644 --- a/src/power/PowerScheduler.cpp +++ b/src/power/PowerScheduler.cpp @@ -18,8 +18,13 @@ bool PowerScheduler::tryAccquireCurrentAllowance( uint16_t requestedDurationMs) { portENTER_CRITICAL(&mux); float existingConsumption = getConsumerCurrent(consumer); - if ((this->freeLimitCurrentBudget + existingConsumption) > 0 && - (this->freeMaximumCurrentBudget + existingConsumption) >= neededCurrent) { + const bool currentAvailableBelowLimit = + this->freeLimitCurrentBudget + existingConsumption > 0; + const bool currentAvailableBelowMaximum = + this->freeMaximumCurrentBudget + existingConsumption >= neededCurrent; + const bool currentIsInsignificant = neededCurrent < 0.1; + if (currentIsInsignificant || + (currentAvailableBelowLimit && currentAvailableBelowMaximum)) { if (existingConsumption > 0) { releaseCurrent(consumer); } @@ -85,10 +90,17 @@ bool PowerScheduler::waitForCurrentAllowance( PowerScheduler::PowerWakeupReasons::POWER_AVAILABLE) { // We were woken up because new power is available, check if it is // enough + // Exclude existing consumption from the same consumer, as it will be + // gone if this is granted float existingConsumption = getConsumerCurrent(consumer); - if ((this->freeLimitCurrentBudget + existingConsumption) > 0 && - (this->freeMaximumCurrentBudget + existingConsumption) >= - neededCurrent) { + const bool currentAvailableBelowLimit = + this->freeLimitCurrentBudget + existingConsumption > 0; + const bool currentAvailableBelowMaximum = + this->freeMaximumCurrentBudget + existingConsumption >= + neededCurrent; + const bool currentIsInsignificant = neededCurrent < 0.1; + if (currentIsInsignificant || + (currentAvailableBelowLimit && currentAvailableBelowMaximum)) { // TODO Check if there is a currently active allowance for this // consumer and if so, replace it with the new one if (existingConsumption > 0) {