More processing of charge state

This commit is contained in:
Phillip Kühne 2025-02-14 21:09:04 +01:00
parent e6c7454e5b
commit 83674657b8
Signed by: phillip
GPG Key ID: E4C1C4D2F90902AA
2 changed files with 20 additions and 6 deletions

View File

@ -132,9 +132,9 @@ float Power::getBatteryChargePercent() { return percentRemaining; }
float Power::getBatteryChargeCoulombs() { return coloumbsRemaining; } float Power::getBatteryChargeCoulombs() { return coloumbsRemaining; }
float Power::getBatteryVoltageChargePercent() { float Power::getBatteryVoltageChargePercent() {
// Directly get the battery voltage and calculate the charge state based on // Directly get the battery voltage, correct the curve with an offset and
// the discharge curve. // calculate the charge state based on the discharge curve.
float batteryVoltage = getBatteryVoltage(); float batteryVoltage = getBatteryVoltage() + Power::fullVoltageOffset;
float chargeState = 0; float chargeState = 0;
// Clamp edge cases // Clamp edge cases
if (batteryVoltage >= if (batteryVoltage >=
@ -169,8 +169,13 @@ void Power::updatePowerStateHandler() {
Power::busPowered = !digitalRead(PowerParameters::PinConfig::VUSB_SENS); Power::busPowered = !digitalRead(PowerParameters::PinConfig::VUSB_SENS);
Power::chargingState = digitalRead(PowerParameters::PinConfig::BAT_CHG_STAT); Power::chargingState = digitalRead(PowerParameters::PinConfig::BAT_CHG_STAT);
ESP_LOGD(TAG, "Bus Powered: %d, Charging: %d", busPowered, chargingState); // If the battery is charging and fully charged
if (Power::busPowered && !Power::chargingState) {
// Calibrate voltage offset on full Battery
Power::fullVoltageOffset = PowerParameters::Battery::DISCHARGE_CURVE::VOLTAGES[0] - getBatteryVoltage();
}
ESP_LOGD(TAG, "Bus Powered: %d, Charging: %d", busPowered, chargingState);
float currentCurrent = powerScheduler->getCurrentCurrent(); float currentCurrent = powerScheduler->getCurrentCurrent();
int referenceCurrentMa = int referenceCurrentMa =
@ -180,7 +185,7 @@ void Power::updatePowerStateHandler() {
// 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 (!Power::busPowered) {
float coloumbsConsumedSinceLastUpdate = coloumbsConsumedSinceLastUpdate =
(currentCurrent / 1000) * (currentCurrent / 1000) *
((pdTICKS_TO_MS(xTaskGetTickCount() - lastPowerStateUpdate)) / 1000.0); ((pdTICKS_TO_MS(xTaskGetTickCount() - lastPowerStateUpdate)) / 1000.0);
@ -213,6 +218,10 @@ void Power::updatePowerStateHandler() {
ESP_LOGD(TAG, "Charge state from voltage (USB powered): %f", chargeState); ESP_LOGD(TAG, "Charge state from voltage (USB powered): %f", chargeState);
} }
// Bound value between 0 and 100
chargeState = chargeState > 100 ? 100 : chargeState;
chargeState = chargeState < 0 ? 0 : chargeState;
addSoCSample(chargeState); addSoCSample(chargeState);
for (int i = 0; i < PowerParameters::Battery::AVERAGING_SAMPLES; i++) { for (int i = 0; i < PowerParameters::Battery::AVERAGING_SAMPLES; i++) {
@ -224,7 +233,8 @@ 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 = sampleSum / PowerParameters::Battery::AVERAGING_SAMPLES; Power::percentRemaining =
sampleSum / PowerParameters::Battery::AVERAGING_SAMPLES;
// Update last update time // Update last update time
Power::lastPowerStateUpdate = xTaskGetTickCount(); Power::lastPowerStateUpdate = xTaskGetTickCount();
@ -358,6 +368,8 @@ bool Power::busPowered = false;
bool Power::chargingState = false; bool Power::chargingState = false;
float Power::fullVoltageOffset = 0;
Power::Power() {} Power::Power() {}
Power::~Power() {} Power::~Power() {}

View File

@ -71,6 +71,8 @@ protected:
static bool chargingState; static bool chargingState;
static float fullVoltageOffset;
/// @brief Circular array of last calculated values for current state of /// @brief Circular array of last calculated values for current state of
/// charge /// charge
static float lastSOC[PowerParameters::Battery::AVERAGING_SAMPLES]; static float lastSOC[PowerParameters::Battery::AVERAGING_SAMPLES];