mirror of
				https://gitlab.dit.htwk-leipzig.de/phillip.kuehne/dezibot.git
				synced 2025-11-04 09:25:13 +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();
 | 
					  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) {
 | 
					void Power::recalculateCurrentBudgets(void) {
 | 
				
			||||||
  return powerScheduler->recalculateCurrentBudgets();
 | 
					  return powerScheduler->recalculateCurrentBudgets();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -156,6 +164,14 @@ float Power::getBatteryVoltageChargePercent() {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void Power::updatePowerStateHandler() {
 | 
					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();
 | 
					  float currentCurrent = powerScheduler->getCurrentCurrent();
 | 
				
			||||||
  int referenceCurrentMa =
 | 
					  int referenceCurrentMa =
 | 
				
			||||||
      PowerParameters::Battery::DISCHARGE_CURVE::REFERENCE_CURRENT_A * 1000;
 | 
					      PowerParameters::Battery::DISCHARGE_CURVE::REFERENCE_CURRENT_A * 1000;
 | 
				
			||||||
@@ -163,7 +179,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 (!busPowered) {
 | 
					  if (!Power::busPowered) {
 | 
				
			||||||
    float coloumbsConsumedSinceLastUpdate =
 | 
					    float coloumbsConsumedSinceLastUpdate =
 | 
				
			||||||
        (currentCurrent / 1000) *
 | 
					        (currentCurrent / 1000) *
 | 
				
			||||||
        ((pdTICKS_TO_MS(xTaskGetTickCount() - lastPowerStateUpdate)) / 1000.0);
 | 
					        ((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
 | 
					  // If current flow is close enough to reference, get battery charge state via
 | 
				
			||||||
  // voltage curve
 | 
					  // voltage curve
 | 
				
			||||||
  if (!busPowered) {
 | 
					  if (!Power::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
 | 
				
			||||||
      chargeState = getBatteryVoltageChargePercent();
 | 
					      chargeState = getBatteryVoltageChargePercent();
 | 
				
			||||||
 | 
					      ESP_LOGD(TAG, "Charge state from voltage: %f", chargeState);
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      // Estimate battery charge state from charge consumption
 | 
					      // Estimate battery charge state from charge consumption
 | 
				
			||||||
      float oldChargeState = lastSOC[latestSoCIndex];
 | 
					      float oldChargeState = lastSOC[latestSoCIndex];
 | 
				
			||||||
@@ -187,34 +204,36 @@ void Power::updatePowerStateHandler() {
 | 
				
			|||||||
                    ((coloumbsConsumedSinceLastUpdate /
 | 
					                    ((coloumbsConsumedSinceLastUpdate /
 | 
				
			||||||
                      PowerParameters::Battery::CELL_CHARGE_FULL_COLOUMB) *
 | 
					                      PowerParameters::Battery::CELL_CHARGE_FULL_COLOUMB) *
 | 
				
			||||||
                     100);
 | 
					                     100);
 | 
				
			||||||
 | 
					      ESP_LOGD(TAG, "Charge state from current: %f", chargeState);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  } else {
 | 
					  } else {
 | 
				
			||||||
    // If we are charging, we can't estimate the charge state based on current
 | 
					    // If we are charging, we can't estimate the charge state based on current
 | 
				
			||||||
    // consumption
 | 
					    // consumption
 | 
				
			||||||
    chargeState = getBatteryVoltageChargePercent();
 | 
					    chargeState = getBatteryVoltageChargePercent();
 | 
				
			||||||
 | 
					    ESP_LOGD(TAG, "Charge state from voltage (USB powered): %f", chargeState);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  addSoCSample(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
 | 
					  // Update percentage remaining based on charge state average
 | 
				
			||||||
  float sampleSum = 0;
 | 
					  float sampleSum = 0;
 | 
				
			||||||
  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];
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  percentRemaining = sampleSum / PowerParameters::Battery::AVERAGING_SAMPLES;
 | 
					  Power::percentRemaining = sampleSum / PowerParameters::Battery::AVERAGING_SAMPLES;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Update last update time
 | 
					  // Update last update time
 | 
				
			||||||
  lastPowerStateUpdate = xTaskGetTickCount();
 | 
					  Power::lastPowerStateUpdate = xTaskGetTickCount();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Update the available current (changes based on battery state of charge)
 | 
					  // Update the available current (changes based on battery state of charge)
 | 
				
			||||||
  powerScheduler->recalculateCurrentBudgets();
 | 
					  Power::powerScheduler->recalculateCurrentBudgets();
 | 
				
			||||||
  ESP_LOGV(TAG, "Current: %f mA, Charge: %f Coulombs, %d %%", currentCurrent,
 | 
					  ESP_LOGD(TAG, "Current: %f mA, Charge: %f Coulombs, %f %%", currentCurrent,
 | 
				
			||||||
           coloumbsRemaining, percentRemaining);
 | 
					           coloumbsRemaining, percentRemaining);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Update supply and charge state flags
 | 
					 | 
				
			||||||
  busPowered = digitalRead(PowerParameters::PinConfig::VUSB_SENS);
 | 
					 | 
				
			||||||
  chargingState = digitalRead(PowerParameters::PinConfig::BAT_CHG_STAT);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  return;
 | 
					  return;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -122,10 +122,14 @@ public:
 | 
				
			|||||||
  /// again. Basically this is a shutdown.
 | 
					  /// again. Basically this is a shutdown.
 | 
				
			||||||
  static void beginPermanentDeepSleep(void);
 | 
					  static void beginPermanentDeepSleep(void);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  //// @brief Get currently granted power
 | 
					  /// @brief Get currently granted current
 | 
				
			||||||
  /// @return the amount of power that is currently allocated (in mA)
 | 
					  /// @return the amount of current that is currently allocated (in mA)
 | 
				
			||||||
  static float getCurrentCurrent(void);
 | 
					  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
 | 
					  /// @brief Responsible for recalculating the current budgets
 | 
				
			||||||
  /// @note these change based on the current state of charge
 | 
					  /// @note these change based on the current state of charge
 | 
				
			||||||
  static void recalculateCurrentBudgets(void);
 | 
					  static void recalculateCurrentBudgets(void);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -207,7 +207,7 @@ void PowerScheduler::recalculateCurrentBudgets(void) {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
  this->freeLimitCurrentBudget = tempFreeLimitCurrentBudget;
 | 
					  this->freeLimitCurrentBudget = tempFreeLimitCurrentBudget;
 | 
				
			||||||
  this->freeMaximumCurrentBudget = tempFreeMaximumCurrentBudget;
 | 
					  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);
 | 
					           this->freeLimitCurrentBudget, this->freeMaximumCurrentBudget);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user