Rename Power.* to PowerManager.* for clarity

This commit is contained in:
Phillip Kühne 2025-02-16 01:10:33 +01:00
parent ff11ad95b0
commit 5efa7a5efd
Signed by: phillip
GPG Key ID: E4C1C4D2F90902AA
20 changed files with 152 additions and 152 deletions

View File

@ -19,7 +19,7 @@
#include "infraredLight/InfraredLight.h" #include "infraredLight/InfraredLight.h"
#include "communication/Communication.h" #include "communication/Communication.h"
#include "display/Display.h" #include "display/Display.h"
#include "power/Power.h" #include "power/PowerManager.h"
class Dezibot { class Dezibot {

View File

@ -1,7 +1,7 @@
#include "ColorDetection.h" #include "ColorDetection.h"
void ColorDetection::begin(void) { void ColorDetection::begin(void) {
if (!Power::waitForCurrentAllowance( if (!PowerManager::waitForCurrentAllowance(
PowerParameters::PowerConsumers::RGBW_SENSOR, PowerParameters::PowerConsumers::RGBW_SENSOR,
PowerParameters::CurrentConsumptions::CURRENT_SENSOR_RGBW, PowerParameters::CurrentConsumptions::CURRENT_SENSOR_RGBW,
COLOR_DETECTION_MAX_EXECUTION_DELAY_MS, NULL)) { COLOR_DETECTION_MAX_EXECUTION_DELAY_MS, NULL)) {
@ -100,4 +100,4 @@ float ColorDetection::modelCurrentConsumption() {
float ColorDetection::modelChargeConsumption(uint16_t durationMs) { float ColorDetection::modelChargeConsumption(uint16_t durationMs) {
return (PowerParameters::CurrentConsumptions::CURRENT_SENSOR_RGBW * return (PowerParameters::CurrentConsumptions::CURRENT_SENSOR_RGBW *
durationMs) / 10e6f; durationMs) / 10e6f;
} }

View File

@ -11,7 +11,7 @@
#ifndef ColorDetection_h #ifndef ColorDetection_h
#define ColorDetection_h #define ColorDetection_h
#include "../power/Power.h" #include "../power/PowerManager.h"
#include <Arduino.h> #include <Arduino.h>
#include <Wire.h> #include <Wire.h>
#include <stdint.h> #include <stdint.h>

View File

@ -45,7 +45,7 @@ void nodeTimeAdjustedCallback(int32_t offset) {
void vTaskUpdate(void *pvParameters) { void vTaskUpdate(void *pvParameters) {
for (;;) { for (;;) {
if (Power::waitForCurrentAllowance( if (PowerManager::waitForCurrentAllowance(
PowerParameters::PowerConsumers::WIFI, PowerParameters::PowerConsumers::WIFI,
PowerParameters::CurrentConsumptions::CURRENT_WIFI_PEAK + PowerParameters::CurrentConsumptions::CURRENT_WIFI_PEAK +
PowerParameters::CurrentConsumptions::CURRENT_WIFI_BASE, PowerParameters::CurrentConsumptions::CURRENT_WIFI_BASE,
@ -54,7 +54,7 @@ void vTaskUpdate(void *pvParameters) {
} else { } else {
ESP_LOGW(TAG, "Skipping mesh update after not being granted power"); ESP_LOGW(TAG, "Skipping mesh update after not being granted power");
} }
Power::waitForCurrentAllowance( PowerManager::waitForCurrentAllowance(
PowerParameters::PowerConsumers::WIFI, PowerParameters::PowerConsumers::WIFI,
PowerParameters::CurrentConsumptions::CURRENT_WIFI_BASE, PowerParameters::CurrentConsumptions::CURRENT_WIFI_BASE,
MESH_MAX_EXECUTION_DELAY_MS, NULL); MESH_MAX_EXECUTION_DELAY_MS, NULL);
@ -76,7 +76,7 @@ void Communication::begin(void) {
mesh.setDebugMsgTypes( mesh.setDebugMsgTypes(
ERROR | ERROR |
STARTUP); // set before init() so that you can see startup messages STARTUP); // set before init() so that you can see startup messages
if (!Power::waitForCurrentAllowance( if (!PowerManager::waitForCurrentAllowance(
PowerParameters::PowerConsumers::WIFI, PowerParameters::PowerConsumers::WIFI,
PowerParameters::CurrentConsumptions::CURRENT_WIFI_BASE, PowerParameters::CurrentConsumptions::CURRENT_WIFI_BASE,
MESH_MAX_EXECUTION_DELAY_MS, NULL)) { MESH_MAX_EXECUTION_DELAY_MS, NULL)) {
@ -95,4 +95,4 @@ void Communication::begin(void) {
xTaskCreate(vTaskUpdate, "vTaskMeshUpdate", 4096, &ucParameterToPass, xTaskCreate(vTaskUpdate, "vTaskMeshUpdate", 4096, &ucParameterToPass,
tskIDLE_PRIORITY, &xHandle); tskIDLE_PRIORITY, &xHandle);
configASSERT(xHandle); configASSERT(xHandle);
}; };

View File

@ -1,7 +1,7 @@
#ifndef Communication_h #ifndef Communication_h
#define Communication_h #define Communication_h
#include "../power/Power.h" #include "../power/PowerManager.h"
#include <Arduino.h> #include <Arduino.h>
#include <painlessMesh.h> #include <painlessMesh.h>
#include <stdint.h> #include <stdint.h>

View File

@ -4,7 +4,7 @@
* @brief Adds the ability to print to the display of the robot. * @brief Adds the ability to print to the display of the robot.
* @version 0.1 * @version 0.1
* @date 2024-06-05 * @date 2024-06-05
* *
* @copyright Copyright (c) 2024 * @copyright Copyright (c) 2024
*/ */
@ -14,7 +14,7 @@
void Display::begin(void){ void Display::begin(void){
if(!Power::waitForCurrentAllowance( if(!PowerManager::waitForCurrentAllowance(
PowerParameters::PowerConsumers::DISPLAY_OLED, PowerParameters::CurrentConsumptions::CURRENT_DISPLAY PowerParameters::PowerConsumers::DISPLAY_OLED, PowerParameters::CurrentConsumptions::CURRENT_DISPLAY
,DISPLAY_MAX_EXECUTION_DELAY_MS, NULL)){ ,DISPLAY_MAX_EXECUTION_DELAY_MS, NULL)){
ESP_LOGE(TAG,"Could not get power for Display"); ESP_LOGE(TAG,"Could not get power for Display");
@ -29,7 +29,7 @@ void Display::begin(void){
sendDisplayCMD(stopCompleteOn); sendDisplayCMD(stopCompleteOn);
/*which pixels are bright: normal = 1s are bright, inverese= 0s are bright*/ /*which pixels are bright: normal = 1s are bright, inverese= 0s are bright*/
sendDisplayCMD( setNormalMode); sendDisplayCMD( setNormalMode);
sendDisplayCMD( setOscFreq); sendDisplayCMD( setOscFreq);
sendDisplayCMD(0x80); sendDisplayCMD(0x80);
@ -74,7 +74,7 @@ void Display::updateLine(uint charAmount)
if(charAmount+this->charsOnCurrLine>16) if(charAmount+this->charsOnCurrLine>16)
{ {
this->currLine = (this->currLine+((charAmount+this->charsOnCurrLine)/16))%8; this->currLine = (this->currLine+((charAmount+this->charsOnCurrLine)/16))%8;
this->charsOnCurrLine = (charAmount+this->charsOnCurrLine)%17; //there can be 0-16 chars on one line, so the 17th char is on next line this->charsOnCurrLine = (charAmount+this->charsOnCurrLine)%17; //there can be 0-16 chars on one line, so the 17th char is on next line
} }
else else
{ {
@ -85,7 +85,7 @@ void Display::updateLine(uint charAmount)
void Display::print(char *value){ void Display::print(char *value){
char *nextchar; char *nextchar;
/* write data to the buffer */ /* write data to the buffer */
while(value && *value != '\0') //check if pointer is still valid and string is not terminated while(value && *value != '\0') //check if pointer is still valid and string is not terminated
{ {
//check if next character is a linebreak //check if next character is a linebreak
if(*value=='\n') if(*value=='\n')
@ -114,7 +114,7 @@ void Display::print(char *value){
} }
Wire.endTransmission(); Wire.endTransmission();
} }
value++; value++;
} }
}; };
@ -186,4 +186,4 @@ float modelCurrentConsumption() {
float modelChargeConsumptionOn(uint16_t durationMs) { float modelChargeConsumptionOn(uint16_t durationMs) {
return PowerParameters::CurrentConsumptions::CURRENT_DISPLAY * durationMs * 10e6; return PowerParameters::CurrentConsumptions::CURRENT_DISPLAY * durationMs * 10e6;
}; };

View File

@ -11,7 +11,7 @@
#ifndef Display_h #ifndef Display_h
#define Display_h #define Display_h
#include "../power/Power.h" #include "../power/PowerManager.h"
#include "DisplayCMDs.h" #include "DisplayCMDs.h"
#include <Arduino.h> #include <Arduino.h>
#include <stdint.h> #include <stdint.h>

View File

@ -13,7 +13,7 @@ InfraredLED::InfraredLED(uint8_t pin,ledc_timer_t timer, ledc_channel_t channel)
}; };
void InfraredLED::begin(void){ void InfraredLED::begin(void){
//we want to change frequency instead of //we want to change frequency instead of
pwmTimer = ledc_timer_config_t{ pwmTimer = ledc_timer_config_t{
.speed_mode = pwmSpeedMode, .speed_mode = pwmSpeedMode,
.duty_resolution = DUTY_RESOLUTION, .duty_resolution = DUTY_RESOLUTION,
@ -47,7 +47,7 @@ void InfraredLED::setState(bool state){
ledc_set_freq(pwmSpeedMode,timer,1); ledc_set_freq(pwmSpeedMode,timer,1);
if (state) { if (state) {
if (this->ledPin == IR_BOTTOM_PIN) { if (this->ledPin == IR_BOTTOM_PIN) {
if (!Power::waitForCurrentAllowance( if (!PowerManager::waitForCurrentAllowance(
PowerParameters::PowerConsumers::LED_IR_BOTTOM, PowerParameters::PowerConsumers::LED_IR_BOTTOM,
PowerParameters::CurrentConsumptions::CURRENT_LED_IR_BOTTOM, PowerParameters::CurrentConsumptions::CURRENT_LED_IR_BOTTOM,
IR_LED_MAX_EXECUTION_DELAY_MS, NULL)) { IR_LED_MAX_EXECUTION_DELAY_MS, NULL)) {
@ -56,7 +56,7 @@ void InfraredLED::setState(bool state){
return; return;
} }
} else if (this->ledPin == IR_FRONT_PIN) { } else if (this->ledPin == IR_FRONT_PIN) {
if (!Power::waitForCurrentAllowance( if (!PowerManager::waitForCurrentAllowance(
PowerParameters::PowerConsumers::LED_IR_FRONT, PowerParameters::PowerConsumers::LED_IR_FRONT,
PowerParameters::CurrentConsumptions::CURRENT_LED_IR_FRONT, PowerParameters::CurrentConsumptions::CURRENT_LED_IR_FRONT,
IR_LED_MAX_EXECUTION_DELAY_MS, NULL)) { IR_LED_MAX_EXECUTION_DELAY_MS, NULL)) {
@ -68,14 +68,14 @@ void InfraredLED::setState(bool state){
ledc_set_duty(pwmSpeedMode,channel,1023); ledc_set_duty(pwmSpeedMode,channel,1023);
} else { } else {
if (this->ledPin == IR_BOTTOM_PIN) { if (this->ledPin == IR_BOTTOM_PIN) {
Power::releaseCurrent(PowerParameters::PowerConsumers::LED_IR_BOTTOM); PowerManager::releaseCurrent(PowerParameters::PowerConsumers::LED_IR_BOTTOM);
} else { } else {
Power::releaseCurrent(PowerParameters::PowerConsumers::LED_IR_FRONT); PowerManager::releaseCurrent(PowerParameters::PowerConsumers::LED_IR_FRONT);
} }
ledc_set_duty(pwmSpeedMode,channel,0); ledc_set_duty(pwmSpeedMode,channel,0);
} }
ledc_update_duty(pwmSpeedMode,channel); ledc_update_duty(pwmSpeedMode,channel);
}; };
void InfraredLED::sendFrequency(uint16_t frequency){ void InfraredLED::sendFrequency(uint16_t frequency){
@ -83,12 +83,12 @@ void InfraredLED::sendFrequency(uint16_t frequency){
// Float to force float division without casting // Float to force float division without casting
constexpr float resolution = 1 << DUTY_RESOLUTION; constexpr float resolution = 1 << DUTY_RESOLUTION;
if (this->ledPin == IR_BOTTOM_PIN) { if (this->ledPin == IR_BOTTOM_PIN) {
Power::waitForCurrentAllowance( PowerManager::waitForCurrentAllowance(
PowerParameters::PowerConsumers::LED_IR_BOTTOM, PowerParameters::PowerConsumers::LED_IR_BOTTOM,
this->modelCurrentConsumption(duty), IR_LED_MAX_EXECUTION_DELAY_MS, this->modelCurrentConsumption(duty), IR_LED_MAX_EXECUTION_DELAY_MS,
NULL); NULL);
} else if (this->ledPin == IR_FRONT_PIN) { } else if (this->ledPin == IR_FRONT_PIN) {
Power::waitForCurrentAllowance( PowerManager::waitForCurrentAllowance(
PowerParameters::PowerConsumers::LED_IR_FRONT, PowerParameters::PowerConsumers::LED_IR_FRONT,
this->modelCurrentConsumption(duty), IR_LED_MAX_EXECUTION_DELAY_MS, this->modelCurrentConsumption(duty), IR_LED_MAX_EXECUTION_DELAY_MS,
NULL); NULL);

View File

@ -10,7 +10,7 @@
*/ */
#ifndef InfraredLight_h #ifndef InfraredLight_h
#define InfraredLight_h #define InfraredLight_h
#include "../power/Power.h" #include "../power/PowerManager.h"
#include "driver/ledc.h" #include "driver/ledc.h"
#include <Arduino.h> #include <Arduino.h>
#include <stdint.h> #include <stdint.h>

View File

@ -27,7 +27,7 @@ photoTransistors LightDetection::getBrightest(ptType type){
photoTransistors maxSensor; photoTransistors maxSensor;
uint16_t maxReading = 0; uint16_t maxReading = 0;
uint16_t currentReading = 0; uint16_t currentReading = 0;
if (type == IR){ if (type == IR){
maxSensor = IR_FRONT; maxSensor = IR_FRONT;
for(const auto pt : allIRPTs){ for(const auto pt : allIRPTs){
@ -47,15 +47,15 @@ photoTransistors LightDetection::getBrightest(ptType type){
} }
} }
} }
return maxSensor; return maxSensor;
}; };
uint32_t LightDetection::getAverageValue(photoTransistors sensor, uint32_t measurments, uint32_t timeBetween){ uint32_t LightDetection::getAverageValue(photoTransistors sensor, uint32_t measurments, uint32_t timeBetween){
TickType_t xLastWakeTime = xTaskGetTickCount(); TickType_t xLastWakeTime = xTaskGetTickCount();
TickType_t frequency = timeBetween / portTICK_PERIOD_MS; TickType_t frequency = timeBetween / portTICK_PERIOD_MS;
uint64_t cumulatedResult = 0; uint64_t cumulatedResult = 0;
for(int i = 0; i < measurments; i++){ for(int i = 0; i < measurments; i++){
cumulatedResult += LightDetection::getValue(sensor); cumulatedResult += LightDetection::getValue(sensor);
xTaskDelayUntil(&xLastWakeTime,frequency); xTaskDelayUntil(&xLastWakeTime,frequency);
@ -64,7 +64,7 @@ uint32_t LightDetection::getAverageValue(photoTransistors sensor, uint32_t measu
}; };
void LightDetection::beginInfrared(void){ void LightDetection::beginInfrared(void){
if(!Power::waitForCurrentAllowance( if(!PowerManager::waitForCurrentAllowance(
PowerParameters::PowerConsumers::PT_IR, PowerParameters::PowerConsumers::PT_IR,
PowerParameters::CurrentConsumptions::CURRENT_PT * 4, PowerParameters::CurrentConsumptions::CURRENT_PT * 4,
LIGHT_DETECTION_MAX_EXECUTION_DELAY_MS, NULL)) { LIGHT_DETECTION_MAX_EXECUTION_DELAY_MS, NULL)) {
@ -80,7 +80,7 @@ void LightDetection::beginInfrared(void){
}; };
void LightDetection::beginDaylight(void){ void LightDetection::beginDaylight(void){
if(!Power::waitForCurrentAllowance( if(!PowerManager::waitForCurrentAllowance(
PowerParameters::PowerConsumers::PT_DL, PowerParameters::PowerConsumers::PT_DL,
PowerParameters::CurrentConsumptions::CURRENT_PT * 2, PowerParameters::CurrentConsumptions::CURRENT_PT * 2,
LIGHT_DETECTION_MAX_EXECUTION_DELAY_MS, NULL)) { LIGHT_DETECTION_MAX_EXECUTION_DELAY_MS, NULL)) {
@ -113,7 +113,7 @@ uint16_t LightDetection::readIRPT(photoTransistors sensor){
default: default:
break; break;
} }
//Power::releaseCurrent(PowerParameters::PowerConsumers::PT_IR); //PowerManager::releaseCurrent(PowerParameters::PowerConsumers::PT_IR);
//digitalWrite(IR_PT_ENABLE,LOW); //digitalWrite(IR_PT_ENABLE,LOW);
return result; return result;
}; };
@ -132,7 +132,7 @@ uint16_t LightDetection::readDLPT(photoTransistors sensor){
default: default:
break; break;
} }
Power::releaseCurrent(PowerParameters::PowerConsumers::PT_DL); PowerManager::releaseCurrent(PowerParameters::PowerConsumers::PT_DL);
digitalWrite(DL_PT_ENABLE,LOW); digitalWrite(DL_PT_ENABLE,LOW);
return result; return result;
}; };
@ -148,4 +148,4 @@ float LightDetection::modelCurrentConsumption(photoTransistors sensor){
float LightDetection::modelChargeConsumptionOn(photoTransistors sensor, float LightDetection::modelChargeConsumptionOn(photoTransistors sensor,
uint16_t durationMs) { uint16_t durationMs) {
return (LightDetection::modelCurrentConsumption(sensor) * durationMs) / 10e6f; return (LightDetection::modelCurrentConsumption(sensor) * durationMs) / 10e6f;
} }

View File

@ -11,7 +11,7 @@
#ifndef LightDetection_h #ifndef LightDetection_h
#define LightDetection_h #define LightDetection_h
#include "../power/Power.h" #include "../power/PowerManager.h"
#include <Arduino.h> #include <Arduino.h>
#include <stdint.h> #include <stdint.h>

View File

@ -17,7 +17,7 @@
#include <freertos/task.h> #include <freertos/task.h>
#include "driver/ledc.h" #include "driver/ledc.h"
#include "motionDetection/MotionDetection.h" #include "motionDetection/MotionDetection.h"
#include "../power/Power.h" #include "../power/PowerManager.h"
#define LEDC_MODE LEDC_LOW_SPEED_MODE #define LEDC_MODE LEDC_LOW_SPEED_MODE
#define TIMER LEDC_TIMER_2 #define TIMER LEDC_TIMER_2
#define CHANNEL_LEFT LEDC_CHANNEL_3 #define CHANNEL_LEFT LEDC_CHANNEL_3

View File

@ -1,5 +1,5 @@
#include "Motion.h" #include "Motion.h"
#include "power/Power.h" #include "power/PowerManager.h"
#define MOTOR_LEFT_PIN 12 #define MOTOR_LEFT_PIN 12
#define MOTOR_RIGHT_PIN 11 #define MOTOR_RIGHT_PIN 11
@ -30,7 +30,7 @@ void Motor::begin(void){
bool Motor::setSpeed(uint16_t duty) { bool Motor::setSpeed(uint16_t duty) {
const float current = this->modelCurrentConsumption(duty); const float current = this->modelCurrentConsumption(duty);
if (this->pin == MOTOR_LEFT_PIN) { if (this->pin == MOTOR_LEFT_PIN) {
if (!Power::waitForCurrentAllowance( if (!PowerManager::waitForCurrentAllowance(
PowerParameters::PowerConsumers::MOTOR_LEFT, current, PowerParameters::PowerConsumers::MOTOR_LEFT, current,
MOTOR_MAX_EXECUTION_DELAY_MS, NULL)) { MOTOR_MAX_EXECUTION_DELAY_MS, NULL)) {
ESP_LOGW(TAG, ESP_LOGW(TAG,
@ -40,7 +40,7 @@ bool Motor::setSpeed(uint16_t duty) {
return false; return false;
} }
} else { } else {
if (!Power::waitForCurrentAllowance( if (!PowerManager::waitForCurrentAllowance(
PowerParameters::PowerConsumers::MOTOR_RIGHT, current, PowerParameters::PowerConsumers::MOTOR_RIGHT, current,
MOTOR_MAX_EXECUTION_DELAY_MS, NULL)) { MOTOR_MAX_EXECUTION_DELAY_MS, NULL)) {
ESP_LOGW(TAG, ESP_LOGW(TAG,

View File

@ -6,7 +6,7 @@ MotionDetection::MotionDetection(){
}; };
void MotionDetection::begin(void){ void MotionDetection::begin(void){
if (!Power::waitForCurrentAllowance( if (!PowerManager::waitForCurrentAllowance(
PowerParameters::PowerConsumers::IMU, PowerParameters::PowerConsumers::IMU,
PowerParameters::CurrentConsumptions::CURRENT_IMU, PowerParameters::CurrentConsumptions::CURRENT_IMU,
IMU_MAX_EXECUTION_DELAY_MS, NULL)) { IMU_MAX_EXECUTION_DELAY_MS, NULL)) {
@ -18,7 +18,7 @@ void MotionDetection::begin(void){
handler->begin(36,37,35,34); handler->begin(36,37,35,34);
// set Accel and Gyroscop to Low Noise // set Accel and Gyroscop to Low Noise
this->writeRegister(PWR_MGMT0,0x1F); this->writeRegister(PWR_MGMT0,0x1F);
//busy Wait for startup //busy Wait for startup
delayMicroseconds(250); delayMicroseconds(250);
//set accelconfig //set accelconfig
this->writeRegister(0x21,0x05); this->writeRegister(0x21,0x05);
@ -28,7 +28,7 @@ void MotionDetection::begin(void){
this->writeRegister(0x23,0x37); this->writeRegister(0x23,0x37);
//Enable Gyro and Acceldata in FIFO //Enable Gyro and Acceldata in FIFO
this->initFIFO(); this->initFIFO();
// TODO: Accelerometer Startup Time From sleep mode to valid data 10 // TODO: Accelerometer Startup Time From sleep mode to valid data 10
}; };
void MotionDetection::end(void){ void MotionDetection::end(void){
this->writeRegister(PWR_MGMT0,0x00); this->writeRegister(PWR_MGMT0,0x00);
@ -53,12 +53,12 @@ IMUResult MotionDetection::getRotation(){
}; };
float MotionDetection::getTemperature(){ float MotionDetection::getTemperature(){
int16_t raw_temperatur = readRegister(REG_TEMP_HIGH)<<8; int16_t raw_temperatur = readRegister(REG_TEMP_HIGH)<<8;
raw_temperatur |= readRegister(REG_TEMP_LOW); raw_temperatur |= readRegister(REG_TEMP_LOW);
return raw_temperatur/128 +25; return raw_temperatur/128 +25;
}; };
int8_t MotionDetection::getWhoAmI(){ int8_t MotionDetection::getWhoAmI(){
return readRegister(WHO_AM_I); return readRegister(WHO_AM_I);
}; };
bool MotionDetection::isShaken(uint32_t threshold ,uint8_t axis){ bool MotionDetection::isShaken(uint32_t threshold ,uint8_t axis){
@ -122,7 +122,7 @@ Orientation MotionDetection::getTilt(){
} }
//yAngle = -1*yAngle-90; //yAngle = -1*yAngle-90;
} }
return Orientation{xAngle,yAngle}; return Orientation{xAngle,yAngle};
@ -175,7 +175,7 @@ uint8_t MotionDetection::readRegister(uint8_t reg){
result = handler->transfer(cmdRead(reg)); result = handler->transfer(cmdRead(reg));
result = handler->transfer(0x00); result = handler->transfer(0x00);
digitalWrite(34,HIGH); digitalWrite(34,HIGH);
handler->endTransaction(); handler->endTransaction();
return result; return result;
}; };
@ -207,7 +207,7 @@ void MotionDetection::writeToRegisterBank(registerBank bank, uint8_t reg, uint8_
} }
uint8_t result = this->readRegister(PWR_MGMT0); uint8_t result = this->readRegister(PWR_MGMT0);
Serial.print("MADDR_W: "); Serial.print("MADDR_W: ");
Serial.println(readRegister(MADDR_W)); Serial.println(readRegister(MADDR_W));
//set Idle Bit //set Idle Bit
this->writeRegister(PWR_MGMT0,result|0x10); this->writeRegister(PWR_MGMT0,result|0x10);
switch(bank){ switch(bank){
@ -254,7 +254,7 @@ void MotionDetection::initFIFO(){
uint MotionDetection::getDataFromFIFO(FIFO_Package* buffer){ uint MotionDetection::getDataFromFIFO(FIFO_Package* buffer){
int16_t fifocount = 0; int16_t fifocount = 0;
int8_t fifohigh = this->readRegister(FIFO_COUNTH); int8_t fifohigh = this->readRegister(FIFO_COUNTH);
int8_t fifolow = this->readRegister(FIFO_COUNTL); int8_t fifolow = this->readRegister(FIFO_COUNTL);
fifocount = (fifohigh<<8)|fifolow; fifocount = (fifohigh<<8)|fifolow;
//fifocount |= this->readRegister(FIFO_COUNTL); //fifocount |= this->readRegister(FIFO_COUNTL);
//fifocount = (this->readRegister(FIFO_COUNTH)<<8); //fifocount = (this->readRegister(FIFO_COUNTH)<<8);
@ -267,20 +267,20 @@ uint MotionDetection::getDataFromFIFO(FIFO_Package* buffer){
handler->transfer(buf,16*fifocount); handler->transfer(buf,16*fifocount);
digitalWrite(34,HIGH); digitalWrite(34,HIGH);
handler->endTransaction(); handler->endTransaction();
writeRegister(0x02,0x04); writeRegister(0x02,0x04);
delayMicroseconds(10); delayMicroseconds(10);
for(int i = 0; i<fifocount;i++){ for(int i = 0; i<fifocount;i++){
buffer[i].header = buf[0x00+16*i]; buffer[i].header = buf[0x00+16*i];
buffer[i].accel.x = (buf[0x02+16*i]<<8)|buf[0x01+16*i]; buffer[i].accel.x = (buf[0x02+16*i]<<8)|buf[0x01+16*i];
buffer[i].accel.y = (buf[0x04+16*i]<<8)|buf[0x03+16*i]; buffer[i].accel.y = (buf[0x04+16*i]<<8)|buf[0x03+16*i];
buffer[i].accel.z = (buf[0x06+16*i]<<8)|buf[0x05+16*i]; buffer[i].accel.z = (buf[0x06+16*i]<<8)|buf[0x05+16*i];
buffer[i].gyro.x = (buf[0x08+16*i]<<8)|buf[0x07+16*i]; buffer[i].gyro.x = (buf[0x08+16*i]<<8)|buf[0x07+16*i];
buffer[i].gyro.y = (buf[0x0A+16*i]<<8)|buf[0x09+16*i]; buffer[i].gyro.y = (buf[0x0A+16*i]<<8)|buf[0x09+16*i];
buffer[i].gyro.z = (buf[0x0C+16*i]<<8)|buf[0x0B+16*i]; buffer[i].gyro.z = (buf[0x0C+16*i]<<8)|buf[0x0B+16*i];
buffer[i].temperature = buf[0x0D+16*i]; buffer[i].temperature = buf[0x0D+16*i];
buffer[i].timestamp = (buf[0x0F+16*i]<<8)|buf[0x0E +16*i]; buffer[i].timestamp = (buf[0x0F+16*i]<<8)|buf[0x0E +16*i];
} }
return fifocount; return fifocount;
}; };
@ -301,4 +301,4 @@ float MotionDetection::modelCurrentConsumption(){
float MotionDetection::modelChargeConsumption(uint16_t durationMs) { float MotionDetection::modelChargeConsumption(uint16_t durationMs) {
return (this->modelCurrentConsumption() * durationMs) / 10e6f; return (this->modelCurrentConsumption() * durationMs) / 10e6f;
} }

View File

@ -10,7 +10,7 @@
*/ */
#ifndef MotionDetection_h #ifndef MotionDetection_h
#define MotionDetection_h #define MotionDetection_h
#include "../power/Power.h" #include "../power/PowerManager.h"
#include "IMU_CMDs.h" #include "IMU_CMDs.h"
#include <Arduino.h> #include <Arduino.h>
#include <SPI.h> #include <SPI.h>

View File

@ -6,15 +6,15 @@ MultiColorLight::MultiColorLight()
}; };
void MultiColorLight::begin(void) { void MultiColorLight::begin(void) {
if (!Power::waitForCurrentAllowance( if (!PowerManager::waitForCurrentAllowance(
PowerParameters::PowerConsumers::LED_RGB_TOP_LEFT, PowerParameters::PowerConsumers::LED_RGB_TOP_LEFT,
PowerParameters::CurrentConsumptions::CURRENT_LED_RGB_BASE, PowerParameters::CurrentConsumptions::CURRENT_LED_RGB_BASE,
MULTI_COLOR_LIGHT_MAX_EXECUTION_DELAY_MS, NULL) && MULTI_COLOR_LIGHT_MAX_EXECUTION_DELAY_MS, NULL) &&
Power::waitForCurrentAllowance( PowerManager::waitForCurrentAllowance(
PowerParameters::PowerConsumers::LED_RGB_TOP_RIGHT, PowerParameters::PowerConsumers::LED_RGB_TOP_RIGHT,
PowerParameters::CurrentConsumptions::CURRENT_LED_RGB_BASE, PowerParameters::CurrentConsumptions::CURRENT_LED_RGB_BASE,
MULTI_COLOR_LIGHT_MAX_EXECUTION_DELAY_MS, NULL) && MULTI_COLOR_LIGHT_MAX_EXECUTION_DELAY_MS, NULL) &&
Power::waitForCurrentAllowance( PowerManager::waitForCurrentAllowance(
PowerParameters::PowerConsumers::LED_RGB_BOTTOM, PowerParameters::PowerConsumers::LED_RGB_BOTTOM,
PowerParameters::CurrentConsumptions::CURRENT_LED_RGB_BASE, PowerParameters::CurrentConsumptions::CURRENT_LED_RGB_BASE,
MULTI_COLOR_LIGHT_MAX_EXECUTION_DELAY_MS, NULL)) { MULTI_COLOR_LIGHT_MAX_EXECUTION_DELAY_MS, NULL)) {
@ -33,7 +33,7 @@ void MultiColorLight::setLed(uint8_t index, uint32_t color) {
float totalConsumption = modelCurrentConsumption(normalizedColor); float totalConsumption = modelCurrentConsumption(normalizedColor);
switch (index) { switch (index) {
case 0: case 0:
if (!Power::waitForCurrentAllowance( if (!PowerManager::waitForCurrentAllowance(
PowerParameters::PowerConsumers::LED_RGB_TOP_RIGHT, PowerParameters::PowerConsumers::LED_RGB_TOP_RIGHT,
totalConsumption, MULTI_COLOR_LIGHT_MAX_EXECUTION_DELAY_MS, NULL)) { totalConsumption, MULTI_COLOR_LIGHT_MAX_EXECUTION_DELAY_MS, NULL)) {
ESP_LOGW(TAG, ESP_LOGW(TAG,
@ -44,7 +44,7 @@ void MultiColorLight::setLed(uint8_t index, uint32_t color) {
} }
break; break;
case 1: case 1:
if (!Power::waitForCurrentAllowance( if (!PowerManager::waitForCurrentAllowance(
PowerParameters::PowerConsumers::LED_RGB_TOP_LEFT, totalConsumption, PowerParameters::PowerConsumers::LED_RGB_TOP_LEFT, totalConsumption,
MULTI_COLOR_LIGHT_MAX_EXECUTION_DELAY_MS, NULL)) { MULTI_COLOR_LIGHT_MAX_EXECUTION_DELAY_MS, NULL)) {
ESP_LOGW(TAG, ESP_LOGW(TAG,
@ -55,7 +55,7 @@ void MultiColorLight::setLed(uint8_t index, uint32_t color) {
} }
break; break;
case 2: case 2:
if (!Power::waitForCurrentAllowance( if (!PowerManager::waitForCurrentAllowance(
PowerParameters::PowerConsumers::LED_RGB_BOTTOM, totalConsumption, PowerParameters::PowerConsumers::LED_RGB_BOTTOM, totalConsumption,
MULTI_COLOR_LIGHT_MAX_EXECUTION_DELAY_MS, NULL)) { MULTI_COLOR_LIGHT_MAX_EXECUTION_DELAY_MS, NULL)) {
ESP_LOGW(TAG, ESP_LOGW(TAG,
@ -235,7 +235,7 @@ float MultiColorLight::modelChargeConsumption(leds leds, uint32_t color,
// TODO logging // TODO logging
break; break;
} }
return ledsConsumption; return ledsConsumption;
}; };
float MultiColorLight::modelChargeConsumption(leds leds, uint8_t red, float MultiColorLight::modelChargeConsumption(leds leds, uint8_t red,
@ -243,4 +243,4 @@ float MultiColorLight::modelChargeConsumption(leds leds, uint8_t red,
uint16_t durationMs) { uint16_t durationMs) {
return MultiColorLight::modelChargeConsumption( return MultiColorLight::modelChargeConsumption(
leds, MultiColorLight::color(red, green, blue), durationMs); leds, MultiColorLight::color(red, green, blue), durationMs);
}; };

View File

@ -11,7 +11,7 @@
*/ */
#ifndef MultiColorLight_h #ifndef MultiColorLight_h
#define MultiColorLight_h #define MultiColorLight_h
#include "../power/Power.h" #include "../power/PowerManager.h"
#include "ColorConstants.h" #include "ColorConstants.h"
#include <Adafruit_NeoPixel.h> #include <Adafruit_NeoPixel.h>

View File

@ -1,5 +1,5 @@
/** /**
* @file Power.h * @file PowerManager.h
* @author Phillip Kühne * @author Phillip Kühne
* @brief This component provides utilities for keeping track of power usage * @brief This component provides utilities for keeping track of power usage
* consumption. * consumption.
@ -7,19 +7,19 @@
* @date 2024-11-23 * @date 2024-11-23
*/ */
#include "Power.h" #include "PowerManager.h"
SemaphoreHandle_t Power::powerMutex = NULL; SemaphoreHandle_t PowerManager::powerMutex = NULL;
void vTaskUpdatePowerState(void *pvParameters) { void vTaskUpdatePowerState(void *pvParameters) {
for (;;) { for (;;) {
ESP_LOGV(TAG, "Updating Power State..."); ESP_LOGV(TAG, "Updating Power State...");
Power::updatePowerStateHandler(); PowerManager::updatePowerStateHandler();
vTaskDelay(pdMS_TO_TICKS(10)); vTaskDelay(pdMS_TO_TICKS(10));
} }
} }
void Power::begin() { void PowerManager::begin() {
// Check if another instance of us already initialized the power scheduler, // Check if another instance of us already initialized the power scheduler,
// if not, we will do it. // if not, we will do it.
ESP_LOGI(TAG, "Initializing Power Management"); ESP_LOGI(TAG, "Initializing Power Management");
@ -38,8 +38,8 @@ void Power::begin() {
powerScheduler = &PowerScheduler::getPowerScheduler( powerScheduler = &PowerScheduler::getPowerScheduler(
PowerParameters::Battery::CELL_CURRENT_1C_MA, PowerParameters::Battery::CELL_CURRENT_1C_MA,
PowerParameters::Battery::CELL_CURRENT_2C_MA); PowerParameters::Battery::CELL_CURRENT_2C_MA);
Power::initPowerState(); PowerManager::initPowerState();
Power::recalculateCurrentBudgets(); PowerManager::recalculateCurrentBudgets();
TaskHandle_t xHandle = NULL; TaskHandle_t xHandle = NULL;
xTaskCreate(vTaskUpdatePowerState, "vTaskPowerStateUpdate", 4096, NULL, xTaskCreate(vTaskUpdatePowerState, "vTaskPowerStateUpdate", 4096, NULL,
tskIDLE_PRIORITY, &xHandle); tskIDLE_PRIORITY, &xHandle);
@ -54,26 +54,26 @@ void Power::begin() {
} }
} }
float Power::getFreeLimitCurrentBudget() { float PowerManager::getFreeLimitCurrentBudget() {
return powerScheduler->getFreeLimitCurrentBudget(); return powerScheduler->getFreeLimitCurrentBudget();
} }
float Power::getFreeMaximumCurrentBudget() { float PowerManager::getFreeMaximumCurrentBudget() {
return powerScheduler->getFreeMaximumCurrentBudget(); return powerScheduler->getFreeMaximumCurrentBudget();
} }
bool Power::tryAccquireCurrentAllowance( bool PowerManager::tryAccquireCurrentAllowance(
PowerParameters::PowerConsumers consumer, uint16_t neededCurrent, PowerParameters::PowerConsumers consumer, uint16_t neededCurrent,
uint16_t requestedDurationMs) { uint16_t requestedDurationMs) {
return powerScheduler->tryAccquireCurrentAllowance(consumer, neededCurrent, return powerScheduler->tryAccquireCurrentAllowance(consumer, neededCurrent,
requestedDurationMs); requestedDurationMs);
} }
void Power::releaseCurrent(PowerParameters::PowerConsumers consumer) { void PowerManager::releaseCurrent(PowerParameters::PowerConsumers consumer) {
powerScheduler->releaseCurrent(consumer); powerScheduler->releaseCurrent(consumer);
} }
bool Power::waitForCurrentAllowance(PowerParameters::PowerConsumers consumer, bool PowerManager::waitForCurrentAllowance(PowerParameters::PowerConsumers consumer,
uint16_t neededCurrent, uint16_t neededCurrent,
uint16_t maxSlackTimeMs, uint16_t maxSlackTimeMs,
uint16_t requestedDurationMs) { uint16_t requestedDurationMs) {
@ -81,15 +81,15 @@ bool Power::waitForCurrentAllowance(PowerParameters::PowerConsumers consumer,
consumer, neededCurrent, maxSlackTimeMs, requestedDurationMs); consumer, neededCurrent, maxSlackTimeMs, requestedDurationMs);
} }
void Power::beginPermanentDeepSleep(void) { void PowerManager::beginPermanentDeepSleep(void) {
return powerScheduler->beginPermanentDeepSleep(); return powerScheduler->beginPermanentDeepSleep();
} }
float Power::getCurrentCurrent(void) { float PowerManager::getCurrentCurrent(void) {
return powerScheduler->getCurrentCurrent(); return powerScheduler->getCurrentCurrent();
} }
float Power::getBatteryCurrent(void) { float PowerManager::getBatteryCurrent(void) {
const float i_3v3 = getCurrentCurrent(); const float i_3v3 = getCurrentCurrent();
const float u_3v3 = 3.3; const float u_3v3 = 3.3;
const float u_bat = getBatteryVoltage(); const float u_bat = getBatteryVoltage();
@ -97,15 +97,15 @@ float Power::getBatteryCurrent(void) {
return (u_3v3 * i_3v3) / (u_bat * eta); return (u_3v3 * i_3v3) / (u_bat * eta);
} }
void Power::recalculateCurrentBudgets(void) { void PowerManager::recalculateCurrentBudgets(void) {
return powerScheduler->recalculateCurrentBudgets(); return powerScheduler->recalculateCurrentBudgets();
} }
float Power::getConsumerCurrent(PowerParameters::PowerConsumers consumer) { float PowerManager::getConsumerCurrent(PowerParameters::PowerConsumers consumer) {
return powerScheduler->getConsumerCurrent(consumer); return powerScheduler->getConsumerCurrent(consumer);
} }
float Power::getBatteryVoltage() { float PowerManager::getBatteryVoltage() {
// Get the battery voltage from the ADC and convert it to a voltage // Get the battery voltage from the ADC and convert it to a voltage
// using the voltage divider. // using the voltage divider.
pinMode(PowerParameters::PinConfig::BAT_ADC_EN, OUTPUT); pinMode(PowerParameters::PinConfig::BAT_ADC_EN, OUTPUT);
@ -127,14 +127,14 @@ float Power::getBatteryVoltage() {
return batteryVoltage; 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 // Directly get the battery voltage, correct the curve with an offset and
// calculate the charge state based on the discharge curve. // calculate the charge state based on the discharge curve.
float batteryVoltage = getBatteryVoltage() + Power::fullVoltageOffset; float batteryVoltage = getBatteryVoltage() + PowerManager::fullVoltageOffset;
float chargeState = 0; float chargeState = 0;
// Clamp edge cases // Clamp edge cases
if (batteryVoltage >= if (batteryVoltage >=
@ -163,16 +163,16 @@ float Power::getBatteryVoltageChargePercent() {
} }
} }
void Power::updatePowerStateHandler() { void PowerManager::updatePowerStateHandler() {
// Update supply and charge state flags // Update supply and charge state flags
Power::busPowered = !digitalRead(PowerParameters::PinConfig::VUSB_SENS); PowerManager::busPowered = !digitalRead(PowerParameters::PinConfig::VUSB_SENS);
Power::chargingState = digitalRead(PowerParameters::PinConfig::BAT_CHG_STAT); PowerManager::chargingState = digitalRead(PowerParameters::PinConfig::BAT_CHG_STAT);
// If the battery is charging and fully charged // If the battery is charging and fully charged
if (Power::busPowered && !Power::chargingState) { if (PowerManager::busPowered && !PowerManager::chargingState) {
// Calibrate voltage offset on full Battery // 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); ESP_LOGD(TAG, "Bus Powered: %d, Charging: %d", busPowered, chargingState);
@ -184,7 +184,7 @@ void Power::updatePowerStateHandler() {
float coloumbsConsumedSinceLastUpdate; float coloumbsConsumedSinceLastUpdate;
// Calculate remaining battery charge in Coulombs based on current and time // Calculate remaining battery charge in Coulombs based on current and time
if (!Power::busPowered) { if (!PowerManager::busPowered) {
coloumbsConsumedSinceLastUpdate = coloumbsConsumedSinceLastUpdate =
(currentCurrent / 1000) * (currentCurrent / 1000) *
((pdTICKS_TO_MS(xTaskGetTickCount() - lastPowerStateUpdate)) / 1000.0); ((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 // If current flow is close enough to reference, get battery charge state via
// voltage curve // voltage curve
if (!Power::busPowered) { if (!PowerManager::busPowered) {
if ((currentCurrent > (referenceCurrentMa * 0.6)) && if ((currentCurrent > (referenceCurrentMa * 0.6)) &&
(currentCurrent < (referenceCurrentMa * 1.4))) { (currentCurrent < (referenceCurrentMa * 1.4))) {
// Get battery charge state from voltage curve // Get battery charge state from voltage curve
@ -233,21 +233,21 @@ void Power::updatePowerStateHandler() {
for (int i = 0; i < PowerParameters::Battery::AVERAGING_SAMPLES; i++) { for (int i = 0; i < PowerParameters::Battery::AVERAGING_SAMPLES; i++) {
sampleSum += lastSOC[i]; sampleSum += lastSOC[i];
} }
Power::percentRemaining = PowerManager::percentRemaining =
sampleSum / PowerParameters::Battery::AVERAGING_SAMPLES; sampleSum / PowerParameters::Battery::AVERAGING_SAMPLES;
// Update last update time // Update last update time
Power::lastPowerStateUpdate = xTaskGetTickCount(); PowerManager::lastPowerStateUpdate = xTaskGetTickCount();
// Update the available current (changes based on battery state of charge) // 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, ESP_LOGD(TAG, "Current: %f mA, Charge: %f Coulombs, %f %%", currentCurrent,
coloumbsRemaining, percentRemaining); coloumbsRemaining, percentRemaining);
return; return;
} }
float Power::getMax3V3Current() { float PowerManager::getMax3V3Current() {
// Conversion from Thesis // Conversion from Thesis
float u_bat = getBatteryVoltage(); float u_bat = getBatteryVoltage();
float i_bat = PowerParameters::Battery::CELL_CURRENT_1C_MA; float i_bat = PowerParameters::Battery::CELL_CURRENT_1C_MA;
@ -256,7 +256,7 @@ float Power::getMax3V3Current() {
return (u_bat * i_bat * eta) / u_3v3; return (u_bat * i_bat * eta) / u_3v3;
} }
void Power::addSoCSample(float soc) { void PowerManager::addSoCSample(float soc) {
PowerMutex lock(powerMutex); PowerMutex lock(powerMutex);
if (!lock.isLocked()) { if (!lock.isLocked()) {
ESP_LOGE(TAG, "Could not take power to add SoC sample"); ESP_LOGE(TAG, "Could not take power to add SoC sample");
@ -267,7 +267,7 @@ void Power::addSoCSample(float soc) {
lastSOC[latestSoCIndex] = soc; lastSOC[latestSoCIndex] = soc;
} }
void Power::initPowerState(void) { void PowerManager::initPowerState(void) {
// Initialize the power state // Initialize the power state
lastPowerStateUpdate = xTaskGetTickCount(); lastPowerStateUpdate = xTaskGetTickCount();
// TODO: Get initial battery charge state based on voltage, set coloumbs based // 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); chargingState = digitalRead(PowerParameters::PinConfig::BAT_CHG_STAT);
} }
void Power::dumpPowerStatistics() { void PowerManager::dumpPowerStatistics() {
Serial.printf("======== Dezibot Power Statistics ========\r\n"); Serial.printf("======== Dezibot Power Statistics ========\r\n");
Serial.printf("Current: %f mA\r\n", Power::getCurrentCurrent()); Serial.printf("Current: %f mA\r\n", PowerManager::getCurrentCurrent());
Serial.printf("Battery Voltage: %f V\r\n", Power::getBatteryVoltage()); Serial.printf("Battery Voltage: %f V\r\n", PowerManager::getBatteryVoltage());
Serial.printf("Battery Charge: %f %%\r\n", Power::getBatteryChargePercent()); Serial.printf("Battery Charge: %f %%\r\n", PowerManager::getBatteryChargePercent());
Serial.printf("Battery Charge: %f Coulombs\r\n", 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", 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"); Serial.printf("=========================================\r\n");
} }
void Power::dumpConsumerStatistics() { void PowerManager::dumpConsumerStatistics() {
Serial.printf("======== Dezibot Consumer Statistics ========\r\n"); 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)); PowerParameters::PowerConsumers::ESP));
Serial.printf("WIFI: %f mA\r\n", Power::getConsumerCurrent( Serial.printf("WIFI: %f mA\r\n", PowerManager::getConsumerCurrent(
PowerParameters::PowerConsumers::WIFI)); PowerParameters::PowerConsumers::WIFI));
Serial.printf("LED_RGB_TOP_LEFT: %f mA\r\n", Serial.printf("LED_RGB_TOP_LEFT: %f mA\r\n",
Power::getConsumerCurrent( PowerManager::getConsumerCurrent(
PowerParameters::PowerConsumers::LED_RGB_TOP_LEFT)); PowerParameters::PowerConsumers::LED_RGB_TOP_LEFT));
Serial.printf("LED_RGB_TOP_RIGHT: %f mA\r\n", Serial.printf("LED_RGB_TOP_RIGHT: %f mA\r\n",
Power::getConsumerCurrent( PowerManager::getConsumerCurrent(
PowerParameters::PowerConsumers::LED_RGB_TOP_RIGHT)); PowerParameters::PowerConsumers::LED_RGB_TOP_RIGHT));
Serial.printf("LED_RGB_BOTTOM: %f mA\r\n", Serial.printf("LED_RGB_BOTTOM: %f mA\r\n",
Power::getConsumerCurrent( PowerManager::getConsumerCurrent(
PowerParameters::PowerConsumers::LED_RGB_BOTTOM)); PowerParameters::PowerConsumers::LED_RGB_BOTTOM));
Serial.printf( Serial.printf(
"RGBW_SENSOR: %f mA\r\n", "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", Serial.printf("LED_IR_BOTTOM: %f mA\r\n",
Power::getConsumerCurrent( PowerManager::getConsumerCurrent(
PowerParameters::PowerConsumers::LED_IR_BOTTOM)); PowerParameters::PowerConsumers::LED_IR_BOTTOM));
Serial.printf( Serial.printf(
"LED_IR_FRONT: %f mA\r\n", "LED_IR_FRONT: %f mA\r\n",
Power::getConsumerCurrent(PowerParameters::PowerConsumers::LED_IR_FRONT)); PowerManager::getConsumerCurrent(PowerParameters::PowerConsumers::LED_IR_FRONT));
Serial.printf( Serial.printf(
"PT_IR: %f mA\r\n", "PT_IR: %f mA\r\n",
Power::getConsumerCurrent(PowerParameters::PowerConsumers::PT_IR)); PowerManager::getConsumerCurrent(PowerParameters::PowerConsumers::PT_IR));
Serial.printf( Serial.printf(
"PT_DL: %f mA\r\n", "PT_DL: %f mA\r\n",
Power::getConsumerCurrent(PowerParameters::PowerConsumers::PT_DL)); PowerManager::getConsumerCurrent(PowerParameters::PowerConsumers::PT_DL));
Serial.printf( Serial.printf(
"LED_UV: %f mA\r\n", "LED_UV: %f mA\r\n",
Power::getConsumerCurrent(PowerParameters::PowerConsumers::LED_UV)); PowerManager::getConsumerCurrent(PowerParameters::PowerConsumers::LED_UV));
Serial.printf( Serial.printf(
"DISPLAY_OLED: %f mA\r\n", "DISPLAY_OLED: %f mA\r\n",
Power::getConsumerCurrent(PowerParameters::PowerConsumers::DISPLAY_OLED)); PowerManager::getConsumerCurrent(PowerParameters::PowerConsumers::DISPLAY_OLED));
Serial.printf( Serial.printf(
"MOTOR_LEFT: %f mA\r\n", "MOTOR_LEFT: %f mA\r\n",
Power::getConsumerCurrent(PowerParameters::PowerConsumers::MOTOR_LEFT)); PowerManager::getConsumerCurrent(PowerParameters::PowerConsumers::MOTOR_LEFT));
Serial.printf( Serial.printf(
"MOTOR_RIGHT: %f mA\r\n", "MOTOR_RIGHT: %f mA\r\n",
Power::getConsumerCurrent(PowerParameters::PowerConsumers::MOTOR_RIGHT)); PowerManager::getConsumerCurrent(PowerParameters::PowerConsumers::MOTOR_RIGHT));
Serial.printf("IMU: %f mA\r\n", Power::getConsumerCurrent( Serial.printf("IMU: %f mA\r\n", PowerManager::getConsumerCurrent(
PowerParameters::PowerConsumers::IMU)); PowerParameters::PowerConsumers::IMU));
Serial.printf("=============================================\r\n"); 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; } bool PowerManager::isBatteryFullyCharged() { return !chargingState && busPowered; }
int Power::latestSoCIndex = 0; int PowerManager::latestSoCIndex = 0;
float Power::lastSOC[PowerParameters::Battery::AVERAGING_SAMPLES] = {0}; float PowerManager::lastSOC[PowerParameters::Battery::AVERAGING_SAMPLES] = {0};
TickType_t Power::lastPowerStateUpdate = 0; TickType_t PowerManager::lastPowerStateUpdate = 0;
float Power::coloumbsRemaining = float PowerManager::coloumbsRemaining =
PowerParameters::Battery::CELL_CHARGE_FULL_COLOUMB; PowerParameters::Battery::CELL_CHARGE_FULL_COLOUMB;
float Power::percentRemaining = 100.0; float PowerManager::percentRemaining = 100.0;
PowerScheduler *Power::powerScheduler = nullptr; 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() {}

View File

@ -1,5 +1,5 @@
/** /**
* @file Power.h * @file PowerManager.h
* @author Phillip Kühne * @author Phillip Kühne
* @brief This component provides utilities for keeping track of power usage * @brief This component provides utilities for keeping track of power usage
* consumption. * consumption.
@ -21,7 +21,7 @@
enum TaskResumptionReason { POWER_AVAILABLE, TIMEOUT }; enum TaskResumptionReason { POWER_AVAILABLE, TIMEOUT };
class Power { class PowerManager {
private: private:
static SemaphoreHandle_t powerMutex; static SemaphoreHandle_t powerMutex;
@ -86,8 +86,8 @@ protected:
public: public:
static void begin(void); static void begin(void);
Power(); PowerManager();
~Power(); ~PowerManager();
/// @brief Get the current free current budget (to C1 discharge) /// @brief Get the current free current budget (to C1 discharge)
/// @return the amount of power that is currently available (in mA) /// @return the amount of power that is currently available (in mA)
static float getFreeLimitCurrentBudget(void); static float getFreeLimitCurrentBudget(void);
@ -193,6 +193,6 @@ public:
}; };
extern Power power; extern PowerManager power;
#endif // Power #endif // Power

View File

@ -11,7 +11,7 @@
*/ */
#include "PowerScheduler.h" #include "PowerScheduler.h"
#include "Power.h" #include "PowerManager.h"
bool PowerScheduler::tryAccquireCurrentAllowance( bool PowerScheduler::tryAccquireCurrentAllowance(
PowerParameters::PowerConsumers consumer, float neededCurrent, PowerParameters::PowerConsumers consumer, float neededCurrent,
@ -192,9 +192,9 @@ void PowerScheduler::recalculateCurrentBudgets(void) {
// Get the respective maximums and subtract currently flowing currents // Get the respective maximums and subtract currently flowing currents
ESP_LOGI(TAG, "Recalculating current budgets..."); ESP_LOGI(TAG, "Recalculating current budgets...");
float tempFreeLimitCurrentBudget = Power::getMax3V3Current(); float tempFreeLimitCurrentBudget = PowerManager::getMax3V3Current();
ESP_LOGI(TAG, "Got max 3V3 current: %.2f", tempFreeLimitCurrentBudget); ESP_LOGI(TAG, "Got max 3V3 current: %.2f", tempFreeLimitCurrentBudget);
float tempFreeMaximumCurrentBudget = Power::getMax3V3Current() * 2; float tempFreeMaximumCurrentBudget = PowerManager::getMax3V3Current() * 2;
PowerSchedulerMutex lock(powerSchedulerMutex); PowerSchedulerMutex lock(powerSchedulerMutex);
if (lock.isLocked() == false) { if (lock.isLocked() == false) {
return; return;
@ -315,4 +315,4 @@ PowerScheduler::~PowerScheduler() {
if (powerSchedulerMutex != NULL) { if (powerSchedulerMutex != NULL) {
vSemaphoreDelete(powerSchedulerMutex); vSemaphoreDelete(powerSchedulerMutex);
} }
} }