Power management progress

This commit is contained in:
2024-12-12 17:09:13 +01:00
parent 8eeb829a91
commit 51a3d9e8f6
8 changed files with 184 additions and 23 deletions

34
src/power/Consumptions.h Normal file
View File

@@ -0,0 +1,34 @@
/**
* @file Consumptions.h
* @author Phillip Kühne
* @brief
* @version 0.1
* @date 2024-11-28
*
* @copyright (c) 2024
*
*/
#ifndef Consumptions_h
#define Consumptions_h
// The power budget afforded by the LIR2450 button cell, 700mW
#define POWER_BUDGET 700
// The power consumptions of the different components are defined here.
// These are "typical worst case" values for now.
#define CONSUMPTION_ESP_BASE 96
#define CONSUMPTION_ESP_LOAD 100
#define CONSUMPTION_RGB_LED 56
#define CONSUMPTION_IR_BOTTOM 327
#define CONSUMPTION_IR_FRONT 492
#define CONSUMPTION_OLED 92 // Assumes lots of lit pixels
#define CONSUMPTION_MOTOR 323
// These are placeholders for completeness for now
#define CONSUMPTION_RGBW_SENSOR 1
#define CONSUMPTION_IR_PT 1
#define CONSUMPTION_UV_LED 200
#define CONSUMPTION_IMU 1
#endif //Consumptions_h

View File

@@ -4,21 +4,46 @@
* @brief This component provides utilities for keeping track of power usage
* consumption.
* @version 0.1
* @date 2024-11-23
* @date 2024-11-23
*/
#include "Power.h"
static portMUX_TYPE mux;
Power::Power()
{
this->free_power_budget = this->total_power_budget;
this->freePowerBudget = this->totalPowerBudget;
}
void Power::begin()
{
if (powerInstance == nullptr)
{
// Double check locking https://www.aristeia.com/Papers/DDJ%5FJul%5FAug%5F2004%5Frevised.pdf
taskENTER_CRITICAL(&mux);
if (powerInstance == nullptr)
{
powerInstance = new Power();
}
taskEXIT_CRITICAL(&mux);
}
else
{
// Power.begin() was called twice!
Serial.println("Power.begin() was called twice! No harm done, but this is a bug.");
}
}
Power *Power::getPowerManager()
{
return powerInstance;
}
bool Power::tryAccquirePowerAllowance(uint16_t neededPower)
{
if (this->free_power_budget >= neededPower)
if (this->freePowerBudget >= neededPower)
{
this->free_power_budget -= neededPower;
this->freePowerBudget -= neededPower;
return true;
}
else
@@ -29,12 +54,69 @@ bool Power::tryAccquirePowerAllowance(uint16_t neededPower)
void Power::releasePower(uint16_t power)
{
if (this->free_power_budget + power <= this->total_power_budget)
if (this->freePowerBudget + power <= this->totalPowerBudget)
{
this->free_power_budget += power;
this->freePowerBudget += power;
}
else // TODO: Maybe we should actually throw an error here, since obviouslsy someone used us wrong.
{
this->free_power_budget = this->total_power_budget;
this->freePowerBudget = this->totalPowerBudget;
}
// Check if there are tasks waiting for power
checkWaitingTasks();
}
bool Power::waitForPowerAllowance(uint16_t neededPower, TickType_t ticksToWait)
{
if (tryAccquirePowerAllowance(neededPower))
{
return true;
}
else
{
// Suspend the task while waiting for power to be available
TaskHandle_t currentTask = xTaskGetCurrentTaskHandle();
TickType_t initialTickCount = xTaskGetTickCount();
waitingTasks.push(currentTask);
uint32_t notificationValue;
BaseType_t notificationStatus = xTaskNotifyWait(0, 0, &notificationValue, ticksToWait);
// Code below will be executed after the task is woken up
while (notificationStatus == pdPASS)
{
if (notificationValue == POWER_AVAILABLE)
{
// We were woken up because new power is available, check if it is enough
if (tryAccquirePowerAllowance(neededPower))
{
return true;
}
else
{
// Still not enough power available for us. Wait the remaining ticks.
xTaskNotifyWait(0, 0, &notificationValue, ticksToWait - (xTaskGetTickCount() - initialTickCount));
}
}
}
if (notificationStatus == pdFALSE)
{
// We waited long enough...
return false;
}
else
{
// Should be impossible to reach
throw "Reached impossible state";
}
}
}
void Power::checkWaitingTasks(void)
{
// Check if there are tasks waiting for power
if (!waitingTasks.empty())
{
TaskHandle_t task = waitingTasks.front();
waitingTasks.pop();
xTaskNotify(task, POWER_AVAILABLE, eSetValueWithOverwrite);
}
}

View File

@@ -7,29 +7,54 @@
* @date 2024-11-23
*/
#include <queue>
#include <Arduino.h>
#include "Consumptions.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#ifndef Power_h
#define Power_h
#define TOTAL_POWER_MILLIWATTS 700
#define TOTAL_POWER_MILLIWATTS POWER_BUDGET
enum TaskResumptionReason {
POWER_AVAILABLE,
TIMEOUT
};
class Power {
protected:
static const uint16_t total_power_budget = TOTAL_POWER_MILLIWATTS;
uint16_t free_power_budget;
static const uint16_t totalPowerBudget = TOTAL_POWER_MILLIWATTS;
uint16_t freePowerBudget;
std::queue<TaskHandle_t> waitingTasks;
void checkWaitingTasks(void);
bool takePowerIfAvailable(uint16_t neededPower);
static Power* powerInstance;
Power();
public:
Power();
static void begin();
/// @brief Initialize the singleton instance of the power manager
/// @return reference to the power manager
static Power *getPowerManager();
uint16_t getFreePowerBudget(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
bool tryAccquirePowerAllowance(uint16_t neededPower);
/// @brief "Return" a certain amount of power when it is no longer needed
/// @param power the amount of power to return (in mW)
/// @param neededPower the amount of power to return (in mW)
/// @return whether the power
void releasePower(uint16_t power);
/// @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)
/// @param TicksToWait the amount of time to wait for the power to become available
/// @return whether the power could be successfully allocatedy
bool waitForPowerAllowance(uint16_t neededPower,TickType_t TicksToWait);
/// @brief Put the ESP32 into deep sleep mode, without a method to wake up again. Basically this is a shutdown.
void beginPermanentDeepSleep(void);
};
#endif //Power