mirror of
				https://gitlab.dit.htwk-leipzig.de/phillip.kuehne/dezibot.git
				synced 2025-10-31 15:20:01 +01:00 
			
		
		
		
	Add user-facing battery current calculation and fix logging
This commit is contained in:
		| @@ -89,6 +89,14 @@ float Power::getCurrentCurrent(void) { | ||||
|   return powerScheduler->getCurrentCurrent(); | ||||
| } | ||||
|  | ||||
| float Power::getBatteryCurrent(void) { | ||||
|   const float i_3v3 = getCurrentCurrent(); | ||||
|   const float u_3v3 = 3.3; | ||||
|   const float u_bat = getBatteryVoltage(); | ||||
|   const float eta = PowerParameters::BUCK_BOOST_EFFICIENCY; | ||||
|   return (u_3v3 * i_3v3) / (u_bat * eta); | ||||
| } | ||||
|  | ||||
| void Power::recalculateCurrentBudgets(void) { | ||||
|   return powerScheduler->recalculateCurrentBudgets(); | ||||
| } | ||||
| @@ -156,6 +164,14 @@ float Power::getBatteryVoltageChargePercent() { | ||||
| } | ||||
|  | ||||
| void Power::updatePowerStateHandler() { | ||||
|  | ||||
|   // Update supply and charge state flags | ||||
|   Power::busPowered = !digitalRead(PowerParameters::PinConfig::VUSB_SENS); | ||||
|   Power::chargingState = digitalRead(PowerParameters::PinConfig::BAT_CHG_STAT); | ||||
|  | ||||
|   ESP_LOGD(TAG, "Bus Powered: %d, Charging: %d", busPowered, chargingState); | ||||
|  | ||||
|  | ||||
|   float currentCurrent = powerScheduler->getCurrentCurrent(); | ||||
|   int referenceCurrentMa = | ||||
|       PowerParameters::Battery::DISCHARGE_CURVE::REFERENCE_CURRENT_A * 1000; | ||||
| @@ -163,7 +179,7 @@ void Power::updatePowerStateHandler() { | ||||
|   float coloumbsConsumedSinceLastUpdate; | ||||
|  | ||||
|   // Calculate remaining battery charge in Coulombs based on current and time | ||||
|   if (!busPowered) { | ||||
|   if (!Power::busPowered) { | ||||
|     float coloumbsConsumedSinceLastUpdate = | ||||
|         (currentCurrent / 1000) * | ||||
|         ((pdTICKS_TO_MS(xTaskGetTickCount() - lastPowerStateUpdate)) / 1000.0); | ||||
| @@ -175,11 +191,12 @@ void Power::updatePowerStateHandler() { | ||||
|  | ||||
|   // If current flow is close enough to reference, get battery charge state via | ||||
|   // voltage curve | ||||
|   if (!busPowered) { | ||||
|   if (!Power::busPowered) { | ||||
|     if ((currentCurrent > (referenceCurrentMa * 0.6)) && | ||||
|         (currentCurrent < (referenceCurrentMa * 1.4))) { | ||||
|       // Get battery charge state from voltage curve | ||||
|       chargeState = getBatteryVoltageChargePercent(); | ||||
|       ESP_LOGD(TAG, "Charge state from voltage: %f", chargeState); | ||||
|     } else { | ||||
|       // Estimate battery charge state from charge consumption | ||||
|       float oldChargeState = lastSOC[latestSoCIndex]; | ||||
| @@ -187,34 +204,36 @@ void Power::updatePowerStateHandler() { | ||||
|                     ((coloumbsConsumedSinceLastUpdate / | ||||
|                       PowerParameters::Battery::CELL_CHARGE_FULL_COLOUMB) * | ||||
|                      100); | ||||
|       ESP_LOGD(TAG, "Charge state from current: %f", chargeState); | ||||
|     } | ||||
|   } else { | ||||
|     // If we are charging, we can't estimate the charge state based on current | ||||
|     // consumption | ||||
|     chargeState = getBatteryVoltageChargePercent(); | ||||
|     ESP_LOGD(TAG, "Charge state from voltage (USB powered): %f", chargeState); | ||||
|   } | ||||
|  | ||||
|   addSoCSample(chargeState); | ||||
|  | ||||
|   for (int i = 0; i < PowerParameters::Battery::AVERAGING_SAMPLES; i++) { | ||||
|     ESP_LOGD(TAG, "SoC[%d]: %f", i, lastSOC[i]); | ||||
|   } | ||||
|  | ||||
|   // Update percentage remaining based on charge state average | ||||
|   float sampleSum = 0; | ||||
|   for (int i = 0; i < PowerParameters::Battery::AVERAGING_SAMPLES; i++) { | ||||
|     sampleSum += lastSOC[i]; | ||||
|   } | ||||
|   percentRemaining = sampleSum / PowerParameters::Battery::AVERAGING_SAMPLES; | ||||
|   Power::percentRemaining = sampleSum / PowerParameters::Battery::AVERAGING_SAMPLES; | ||||
|  | ||||
|   // Update last update time | ||||
|   lastPowerStateUpdate = xTaskGetTickCount(); | ||||
|   Power::lastPowerStateUpdate = xTaskGetTickCount(); | ||||
|  | ||||
|   // Update the available current (changes based on battery state of charge) | ||||
|   powerScheduler->recalculateCurrentBudgets(); | ||||
|   ESP_LOGV(TAG, "Current: %f mA, Charge: %f Coulombs, %d %%", currentCurrent, | ||||
|   Power::powerScheduler->recalculateCurrentBudgets(); | ||||
|   ESP_LOGD(TAG, "Current: %f mA, Charge: %f Coulombs, %f %%", currentCurrent, | ||||
|            coloumbsRemaining, percentRemaining); | ||||
|  | ||||
|   // Update supply and charge state flags | ||||
|   busPowered = digitalRead(PowerParameters::PinConfig::VUSB_SENS); | ||||
|   chargingState = digitalRead(PowerParameters::PinConfig::BAT_CHG_STAT); | ||||
|  | ||||
|   return; | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -122,10 +122,14 @@ public: | ||||
|   /// 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) | ||||
|   /// @brief Get currently granted current | ||||
|   /// @return the amount of current that is currently allocated (in mA) | ||||
|   static float getCurrentCurrent(void); | ||||
|  | ||||
|   /// @brief get the current theoretically flowing at the battery | ||||
|   /// @return the amount of current that is currently flowing (in mA) | ||||
|   static float getBatteryCurrent(void); | ||||
|  | ||||
|   /// @brief Responsible for recalculating the current budgets | ||||
|   /// @note these change based on the current state of charge | ||||
|   static void recalculateCurrentBudgets(void); | ||||
|   | ||||
| @@ -207,7 +207,7 @@ void PowerScheduler::recalculateCurrentBudgets(void) { | ||||
|   } | ||||
|   this->freeLimitCurrentBudget = tempFreeLimitCurrentBudget; | ||||
|   this->freeMaximumCurrentBudget = tempFreeMaximumCurrentBudget; | ||||
|   ESP_LOGV(TAG, "Current budgets recalculated: %f mA, %f mA", | ||||
|   ESP_LOGV(TAG, "Current budgets recalculated: %d mA, %d mA", | ||||
|            this->freeLimitCurrentBudget, this->freeMaximumCurrentBudget); | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user