diff --git a/src/Dezibot.h b/src/Dezibot.h index 4d4274c..ef3f691 100644 --- a/src/Dezibot.h +++ b/src/Dezibot.h @@ -19,7 +19,7 @@ #include "infraredLight/InfraredLight.h" #include "communication/Communication.h" #include "display/Display.h" -#include "power/Power.h" +#include "power/PowerManager.h" class Dezibot { diff --git a/src/colorDetection/ColorDetection.cpp b/src/colorDetection/ColorDetection.cpp index 19dcde9..06b3bcf 100644 --- a/src/colorDetection/ColorDetection.cpp +++ b/src/colorDetection/ColorDetection.cpp @@ -1,7 +1,7 @@ #include "ColorDetection.h" void ColorDetection::begin(void) { - if (!Power::waitForCurrentAllowance( + if (!PowerManager::waitForCurrentAllowance( PowerParameters::PowerConsumers::RGBW_SENSOR, PowerParameters::CurrentConsumptions::CURRENT_SENSOR_RGBW, COLOR_DETECTION_MAX_EXECUTION_DELAY_MS, NULL)) { @@ -100,4 +100,4 @@ float ColorDetection::modelCurrentConsumption() { float ColorDetection::modelChargeConsumption(uint16_t durationMs) { return (PowerParameters::CurrentConsumptions::CURRENT_SENSOR_RGBW * durationMs) / 10e6f; -} \ No newline at end of file +} diff --git a/src/colorDetection/ColorDetection.h b/src/colorDetection/ColorDetection.h index 4829e79..4323723 100644 --- a/src/colorDetection/ColorDetection.h +++ b/src/colorDetection/ColorDetection.h @@ -11,7 +11,7 @@ #ifndef ColorDetection_h #define ColorDetection_h -#include "../power/Power.h" +#include "../power/PowerManager.h" #include #include #include diff --git a/src/communication/Communication.cpp b/src/communication/Communication.cpp index 352e265..146e8ca 100644 --- a/src/communication/Communication.cpp +++ b/src/communication/Communication.cpp @@ -45,7 +45,7 @@ void nodeTimeAdjustedCallback(int32_t offset) { void vTaskUpdate(void *pvParameters) { for (;;) { - if (Power::waitForCurrentAllowance( + if (PowerManager::waitForCurrentAllowance( PowerParameters::PowerConsumers::WIFI, PowerParameters::CurrentConsumptions::CURRENT_WIFI_PEAK + PowerParameters::CurrentConsumptions::CURRENT_WIFI_BASE, @@ -54,7 +54,7 @@ void vTaskUpdate(void *pvParameters) { } else { ESP_LOGW(TAG, "Skipping mesh update after not being granted power"); } - Power::waitForCurrentAllowance( + PowerManager::waitForCurrentAllowance( PowerParameters::PowerConsumers::WIFI, PowerParameters::CurrentConsumptions::CURRENT_WIFI_BASE, MESH_MAX_EXECUTION_DELAY_MS, NULL); @@ -76,7 +76,7 @@ void Communication::begin(void) { mesh.setDebugMsgTypes( ERROR | STARTUP); // set before init() so that you can see startup messages - if (!Power::waitForCurrentAllowance( + if (!PowerManager::waitForCurrentAllowance( PowerParameters::PowerConsumers::WIFI, PowerParameters::CurrentConsumptions::CURRENT_WIFI_BASE, MESH_MAX_EXECUTION_DELAY_MS, NULL)) { @@ -95,4 +95,4 @@ void Communication::begin(void) { xTaskCreate(vTaskUpdate, "vTaskMeshUpdate", 4096, &ucParameterToPass, tskIDLE_PRIORITY, &xHandle); configASSERT(xHandle); -}; \ No newline at end of file +}; diff --git a/src/communication/Communication.h b/src/communication/Communication.h index 6f75d5c..090d99f 100644 --- a/src/communication/Communication.h +++ b/src/communication/Communication.h @@ -1,7 +1,7 @@ #ifndef Communication_h #define Communication_h -#include "../power/Power.h" +#include "../power/PowerManager.h" #include #include #include diff --git a/src/display/Display.cpp b/src/display/Display.cpp index ef906d9..342c48c 100644 --- a/src/display/Display.cpp +++ b/src/display/Display.cpp @@ -4,7 +4,7 @@ * @brief Adds the ability to print to the display of the robot. * @version 0.1 * @date 2024-06-05 - * + * * @copyright Copyright (c) 2024 */ @@ -14,7 +14,7 @@ void Display::begin(void){ - if(!Power::waitForCurrentAllowance( + if(!PowerManager::waitForCurrentAllowance( PowerParameters::PowerConsumers::DISPLAY_OLED, PowerParameters::CurrentConsumptions::CURRENT_DISPLAY ,DISPLAY_MAX_EXECUTION_DELAY_MS, NULL)){ ESP_LOGE(TAG,"Could not get power for Display"); @@ -29,7 +29,7 @@ void Display::begin(void){ sendDisplayCMD(stopCompleteOn); /*which pixels are bright: normal = 1s are bright, inverese= 0s are bright*/ sendDisplayCMD( setNormalMode); - + sendDisplayCMD( setOscFreq); sendDisplayCMD(0x80); @@ -74,7 +74,7 @@ void Display::updateLine(uint charAmount) if(charAmount+this->charsOnCurrLine>16) { 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 { @@ -85,7 +85,7 @@ void Display::updateLine(uint charAmount) void Display::print(char *value){ char *nextchar; /* 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 if(*value=='\n') @@ -114,7 +114,7 @@ void Display::print(char *value){ } Wire.endTransmission(); } - value++; + value++; } }; @@ -186,4 +186,4 @@ float modelCurrentConsumption() { float modelChargeConsumptionOn(uint16_t durationMs) { return PowerParameters::CurrentConsumptions::CURRENT_DISPLAY * durationMs * 10e6; -}; \ No newline at end of file +}; diff --git a/src/display/Display.h b/src/display/Display.h index 387cc35..8c9bc47 100644 --- a/src/display/Display.h +++ b/src/display/Display.h @@ -11,7 +11,7 @@ #ifndef Display_h #define Display_h -#include "../power/Power.h" +#include "../power/PowerManager.h" #include "DisplayCMDs.h" #include #include diff --git a/src/infraredLight/InfraredLED.cpp b/src/infraredLight/InfraredLED.cpp index a9b9b06..a09f1a3 100644 --- a/src/infraredLight/InfraredLED.cpp +++ b/src/infraredLight/InfraredLED.cpp @@ -13,7 +13,7 @@ InfraredLED::InfraredLED(uint8_t pin,ledc_timer_t timer, ledc_channel_t channel) }; void InfraredLED::begin(void){ - //we want to change frequency instead of + //we want to change frequency instead of pwmTimer = ledc_timer_config_t{ .speed_mode = pwmSpeedMode, .duty_resolution = DUTY_RESOLUTION, @@ -47,7 +47,7 @@ void InfraredLED::setState(bool state){ ledc_set_freq(pwmSpeedMode,timer,1); if (state) { if (this->ledPin == IR_BOTTOM_PIN) { - if (!Power::waitForCurrentAllowance( + if (!PowerManager::waitForCurrentAllowance( PowerParameters::PowerConsumers::LED_IR_BOTTOM, PowerParameters::CurrentConsumptions::CURRENT_LED_IR_BOTTOM, IR_LED_MAX_EXECUTION_DELAY_MS, NULL)) { @@ -56,7 +56,7 @@ void InfraredLED::setState(bool state){ return; } } else if (this->ledPin == IR_FRONT_PIN) { - if (!Power::waitForCurrentAllowance( + if (!PowerManager::waitForCurrentAllowance( PowerParameters::PowerConsumers::LED_IR_FRONT, PowerParameters::CurrentConsumptions::CURRENT_LED_IR_FRONT, IR_LED_MAX_EXECUTION_DELAY_MS, NULL)) { @@ -68,14 +68,14 @@ void InfraredLED::setState(bool state){ ledc_set_duty(pwmSpeedMode,channel,1023); } else { if (this->ledPin == IR_BOTTOM_PIN) { - Power::releaseCurrent(PowerParameters::PowerConsumers::LED_IR_BOTTOM); + PowerManager::releaseCurrent(PowerParameters::PowerConsumers::LED_IR_BOTTOM); } else { - Power::releaseCurrent(PowerParameters::PowerConsumers::LED_IR_FRONT); + PowerManager::releaseCurrent(PowerParameters::PowerConsumers::LED_IR_FRONT); } ledc_set_duty(pwmSpeedMode,channel,0); } ledc_update_duty(pwmSpeedMode,channel); - + }; void InfraredLED::sendFrequency(uint16_t frequency){ @@ -83,12 +83,12 @@ void InfraredLED::sendFrequency(uint16_t frequency){ // Float to force float division without casting constexpr float resolution = 1 << DUTY_RESOLUTION; if (this->ledPin == IR_BOTTOM_PIN) { - Power::waitForCurrentAllowance( + PowerManager::waitForCurrentAllowance( PowerParameters::PowerConsumers::LED_IR_BOTTOM, this->modelCurrentConsumption(duty), IR_LED_MAX_EXECUTION_DELAY_MS, NULL); } else if (this->ledPin == IR_FRONT_PIN) { - Power::waitForCurrentAllowance( + PowerManager::waitForCurrentAllowance( PowerParameters::PowerConsumers::LED_IR_FRONT, this->modelCurrentConsumption(duty), IR_LED_MAX_EXECUTION_DELAY_MS, NULL); diff --git a/src/infraredLight/InfraredLight.h b/src/infraredLight/InfraredLight.h index fae6df3..9ef00c5 100644 --- a/src/infraredLight/InfraredLight.h +++ b/src/infraredLight/InfraredLight.h @@ -10,7 +10,7 @@ */ #ifndef InfraredLight_h #define InfraredLight_h -#include "../power/Power.h" +#include "../power/PowerManager.h" #include "driver/ledc.h" #include #include diff --git a/src/lightDetection/LightDetection.cpp b/src/lightDetection/LightDetection.cpp index 498d2c2..1fbe3fa 100644 --- a/src/lightDetection/LightDetection.cpp +++ b/src/lightDetection/LightDetection.cpp @@ -27,7 +27,7 @@ photoTransistors LightDetection::getBrightest(ptType type){ photoTransistors maxSensor; uint16_t maxReading = 0; uint16_t currentReading = 0; - + if (type == IR){ maxSensor = IR_FRONT; for(const auto pt : allIRPTs){ @@ -47,15 +47,15 @@ photoTransistors LightDetection::getBrightest(ptType type){ } } } - + return maxSensor; }; uint32_t LightDetection::getAverageValue(photoTransistors sensor, uint32_t measurments, uint32_t timeBetween){ - + TickType_t xLastWakeTime = xTaskGetTickCount(); TickType_t frequency = timeBetween / portTICK_PERIOD_MS; - uint64_t cumulatedResult = 0; + uint64_t cumulatedResult = 0; for(int i = 0; i < measurments; i++){ cumulatedResult += LightDetection::getValue(sensor); xTaskDelayUntil(&xLastWakeTime,frequency); @@ -64,7 +64,7 @@ uint32_t LightDetection::getAverageValue(photoTransistors sensor, uint32_t measu }; void LightDetection::beginInfrared(void){ - if(!Power::waitForCurrentAllowance( + if(!PowerManager::waitForCurrentAllowance( PowerParameters::PowerConsumers::PT_IR, PowerParameters::CurrentConsumptions::CURRENT_PT * 4, LIGHT_DETECTION_MAX_EXECUTION_DELAY_MS, NULL)) { @@ -80,7 +80,7 @@ void LightDetection::beginInfrared(void){ }; void LightDetection::beginDaylight(void){ - if(!Power::waitForCurrentAllowance( + if(!PowerManager::waitForCurrentAllowance( PowerParameters::PowerConsumers::PT_DL, PowerParameters::CurrentConsumptions::CURRENT_PT * 2, LIGHT_DETECTION_MAX_EXECUTION_DELAY_MS, NULL)) { @@ -113,7 +113,7 @@ uint16_t LightDetection::readIRPT(photoTransistors sensor){ default: break; } - //Power::releaseCurrent(PowerParameters::PowerConsumers::PT_IR); + //PowerManager::releaseCurrent(PowerParameters::PowerConsumers::PT_IR); //digitalWrite(IR_PT_ENABLE,LOW); return result; }; @@ -132,7 +132,7 @@ uint16_t LightDetection::readDLPT(photoTransistors sensor){ default: break; } - Power::releaseCurrent(PowerParameters::PowerConsumers::PT_DL); + PowerManager::releaseCurrent(PowerParameters::PowerConsumers::PT_DL); digitalWrite(DL_PT_ENABLE,LOW); return result; }; @@ -148,4 +148,4 @@ float LightDetection::modelCurrentConsumption(photoTransistors sensor){ float LightDetection::modelChargeConsumptionOn(photoTransistors sensor, uint16_t durationMs) { return (LightDetection::modelCurrentConsumption(sensor) * durationMs) / 10e6f; -} \ No newline at end of file +} diff --git a/src/lightDetection/LightDetection.h b/src/lightDetection/LightDetection.h index b79fdac..7597e78 100644 --- a/src/lightDetection/LightDetection.h +++ b/src/lightDetection/LightDetection.h @@ -11,7 +11,7 @@ #ifndef LightDetection_h #define LightDetection_h -#include "../power/Power.h" +#include "../power/PowerManager.h" #include #include diff --git a/src/motion/Motion.h b/src/motion/Motion.h index c4c6f7d..e1bb259 100644 --- a/src/motion/Motion.h +++ b/src/motion/Motion.h @@ -17,7 +17,7 @@ #include #include "driver/ledc.h" #include "motionDetection/MotionDetection.h" -#include "../power/Power.h" +#include "../power/PowerManager.h" #define LEDC_MODE LEDC_LOW_SPEED_MODE #define TIMER LEDC_TIMER_2 #define CHANNEL_LEFT LEDC_CHANNEL_3 diff --git a/src/motion/Motor.cpp b/src/motion/Motor.cpp index 6482ded..78fc1f2 100644 --- a/src/motion/Motor.cpp +++ b/src/motion/Motor.cpp @@ -1,5 +1,5 @@ #include "Motion.h" -#include "power/Power.h" +#include "power/PowerManager.h" #define MOTOR_LEFT_PIN 12 #define MOTOR_RIGHT_PIN 11 @@ -30,7 +30,7 @@ void Motor::begin(void){ bool Motor::setSpeed(uint16_t duty) { const float current = this->modelCurrentConsumption(duty); if (this->pin == MOTOR_LEFT_PIN) { - if (!Power::waitForCurrentAllowance( + if (!PowerManager::waitForCurrentAllowance( PowerParameters::PowerConsumers::MOTOR_LEFT, current, MOTOR_MAX_EXECUTION_DELAY_MS, NULL)) { ESP_LOGW(TAG, @@ -40,7 +40,7 @@ bool Motor::setSpeed(uint16_t duty) { return false; } } else { - if (!Power::waitForCurrentAllowance( + if (!PowerManager::waitForCurrentAllowance( PowerParameters::PowerConsumers::MOTOR_RIGHT, current, MOTOR_MAX_EXECUTION_DELAY_MS, NULL)) { ESP_LOGW(TAG, diff --git a/src/motionDetection/MotionDetection.cpp b/src/motionDetection/MotionDetection.cpp index 178ef9f..cfe2c77 100644 --- a/src/motionDetection/MotionDetection.cpp +++ b/src/motionDetection/MotionDetection.cpp @@ -6,7 +6,7 @@ MotionDetection::MotionDetection(){ }; void MotionDetection::begin(void){ - if (!Power::waitForCurrentAllowance( + if (!PowerManager::waitForCurrentAllowance( PowerParameters::PowerConsumers::IMU, PowerParameters::CurrentConsumptions::CURRENT_IMU, IMU_MAX_EXECUTION_DELAY_MS, NULL)) { @@ -18,7 +18,7 @@ void MotionDetection::begin(void){ handler->begin(36,37,35,34); // set Accel and Gyroscop to Low Noise this->writeRegister(PWR_MGMT0,0x1F); - //busy Wait for startup + //busy Wait for startup delayMicroseconds(250); //set accelconfig this->writeRegister(0x21,0x05); @@ -28,7 +28,7 @@ void MotionDetection::begin(void){ this->writeRegister(0x23,0x37); //Enable Gyro and Acceldata in FIFO 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){ this->writeRegister(PWR_MGMT0,0x00); @@ -53,12 +53,12 @@ IMUResult MotionDetection::getRotation(){ }; float MotionDetection::getTemperature(){ 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; }; int8_t MotionDetection::getWhoAmI(){ - return readRegister(WHO_AM_I); + return readRegister(WHO_AM_I); }; bool MotionDetection::isShaken(uint32_t threshold ,uint8_t axis){ @@ -122,7 +122,7 @@ Orientation MotionDetection::getTilt(){ } //yAngle = -1*yAngle-90; } - + return Orientation{xAngle,yAngle}; @@ -175,7 +175,7 @@ uint8_t MotionDetection::readRegister(uint8_t reg){ result = handler->transfer(cmdRead(reg)); result = handler->transfer(0x00); digitalWrite(34,HIGH); - handler->endTransaction(); + handler->endTransaction(); return result; }; @@ -207,7 +207,7 @@ void MotionDetection::writeToRegisterBank(registerBank bank, uint8_t reg, uint8_ } uint8_t result = this->readRegister(PWR_MGMT0); Serial.print("MADDR_W: "); - Serial.println(readRegister(MADDR_W)); + Serial.println(readRegister(MADDR_W)); //set Idle Bit this->writeRegister(PWR_MGMT0,result|0x10); switch(bank){ @@ -254,7 +254,7 @@ void MotionDetection::initFIFO(){ uint MotionDetection::getDataFromFIFO(FIFO_Package* buffer){ int16_t fifocount = 0; 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 |= this->readRegister(FIFO_COUNTL); //fifocount = (this->readRegister(FIFO_COUNTH)<<8); @@ -267,20 +267,20 @@ uint MotionDetection::getDataFromFIFO(FIFO_Package* buffer){ handler->transfer(buf,16*fifocount); digitalWrite(34,HIGH); handler->endTransaction(); - + writeRegister(0x02,0x04); delayMicroseconds(10); for(int i = 0; imodelCurrentConsumption() * durationMs) / 10e6f; -} \ No newline at end of file +} diff --git a/src/motionDetection/MotionDetection.h b/src/motionDetection/MotionDetection.h index 6b87df7..8bdd63b 100644 --- a/src/motionDetection/MotionDetection.h +++ b/src/motionDetection/MotionDetection.h @@ -10,7 +10,7 @@ */ #ifndef MotionDetection_h #define MotionDetection_h -#include "../power/Power.h" +#include "../power/PowerManager.h" #include "IMU_CMDs.h" #include #include diff --git a/src/multiColorLight/MultiColorLight.cpp b/src/multiColorLight/MultiColorLight.cpp index 28b9024..e439252 100644 --- a/src/multiColorLight/MultiColorLight.cpp +++ b/src/multiColorLight/MultiColorLight.cpp @@ -6,15 +6,15 @@ MultiColorLight::MultiColorLight() }; void MultiColorLight::begin(void) { - if (!Power::waitForCurrentAllowance( + if (!PowerManager::waitForCurrentAllowance( PowerParameters::PowerConsumers::LED_RGB_TOP_LEFT, PowerParameters::CurrentConsumptions::CURRENT_LED_RGB_BASE, MULTI_COLOR_LIGHT_MAX_EXECUTION_DELAY_MS, NULL) && - Power::waitForCurrentAllowance( + PowerManager::waitForCurrentAllowance( PowerParameters::PowerConsumers::LED_RGB_TOP_RIGHT, PowerParameters::CurrentConsumptions::CURRENT_LED_RGB_BASE, MULTI_COLOR_LIGHT_MAX_EXECUTION_DELAY_MS, NULL) && - Power::waitForCurrentAllowance( + PowerManager::waitForCurrentAllowance( PowerParameters::PowerConsumers::LED_RGB_BOTTOM, PowerParameters::CurrentConsumptions::CURRENT_LED_RGB_BASE, 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); switch (index) { case 0: - if (!Power::waitForCurrentAllowance( + if (!PowerManager::waitForCurrentAllowance( PowerParameters::PowerConsumers::LED_RGB_TOP_RIGHT, totalConsumption, MULTI_COLOR_LIGHT_MAX_EXECUTION_DELAY_MS, NULL)) { ESP_LOGW(TAG, @@ -44,7 +44,7 @@ void MultiColorLight::setLed(uint8_t index, uint32_t color) { } break; case 1: - if (!Power::waitForCurrentAllowance( + if (!PowerManager::waitForCurrentAllowance( PowerParameters::PowerConsumers::LED_RGB_TOP_LEFT, totalConsumption, MULTI_COLOR_LIGHT_MAX_EXECUTION_DELAY_MS, NULL)) { ESP_LOGW(TAG, @@ -55,7 +55,7 @@ void MultiColorLight::setLed(uint8_t index, uint32_t color) { } break; case 2: - if (!Power::waitForCurrentAllowance( + if (!PowerManager::waitForCurrentAllowance( PowerParameters::PowerConsumers::LED_RGB_BOTTOM, totalConsumption, MULTI_COLOR_LIGHT_MAX_EXECUTION_DELAY_MS, NULL)) { ESP_LOGW(TAG, @@ -235,7 +235,7 @@ float MultiColorLight::modelChargeConsumption(leds leds, uint32_t color, // TODO logging break; } - return ledsConsumption; + return ledsConsumption; }; float MultiColorLight::modelChargeConsumption(leds leds, uint8_t red, @@ -243,4 +243,4 @@ float MultiColorLight::modelChargeConsumption(leds leds, uint8_t red, uint16_t durationMs) { return MultiColorLight::modelChargeConsumption( leds, MultiColorLight::color(red, green, blue), durationMs); -}; \ No newline at end of file +}; diff --git a/src/multiColorLight/MultiColorLight.h b/src/multiColorLight/MultiColorLight.h index 76ba711..b526e86 100644 --- a/src/multiColorLight/MultiColorLight.h +++ b/src/multiColorLight/MultiColorLight.h @@ -11,7 +11,7 @@ */ #ifndef MultiColorLight_h #define MultiColorLight_h -#include "../power/Power.h" +#include "../power/PowerManager.h" #include "ColorConstants.h" #include diff --git a/src/power/Power.cpp b/src/power/PowerManager.cpp similarity index 70% rename from src/power/Power.cpp rename to src/power/PowerManager.cpp index 09ce96c..760cfb6 100644 --- a/src/power/Power.cpp +++ b/src/power/PowerManager.cpp @@ -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() {} \ No newline at end of file +PowerManager::~PowerManager() {} diff --git a/src/power/Power.h b/src/power/PowerManager.h similarity index 98% rename from src/power/Power.h rename to src/power/PowerManager.h index 34be7cd..c5222ec 100644 --- a/src/power/Power.h +++ b/src/power/PowerManager.h @@ -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 \ No newline at end of file +#endif // Power diff --git a/src/power/PowerScheduler.cpp b/src/power/PowerScheduler.cpp index 2166073..0dbbbef 100644 --- a/src/power/PowerScheduler.cpp +++ b/src/power/PowerScheduler.cpp @@ -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); } -} \ No newline at end of file +}