diff --git a/src/power/Power.cpp b/src/power/Power.cpp index 3abf170..09ce96c 100644 --- a/src/power/Power.cpp +++ b/src/power/Power.cpp @@ -132,9 +132,9 @@ float Power::getBatteryChargePercent() { return percentRemaining; } float Power::getBatteryChargeCoulombs() { return coloumbsRemaining; } float Power::getBatteryVoltageChargePercent() { - // Directly get the battery voltage and calculate the charge state based on - // the discharge curve. - float batteryVoltage = getBatteryVoltage(); + // 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 chargeState = 0; // Clamp edge cases if (batteryVoltage >= @@ -169,8 +169,13 @@ void Power::updatePowerStateHandler() { 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); + // 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(); int referenceCurrentMa = @@ -180,7 +185,7 @@ void Power::updatePowerStateHandler() { // Calculate remaining battery charge in Coulombs based on current and time if (!Power::busPowered) { - float coloumbsConsumedSinceLastUpdate = + coloumbsConsumedSinceLastUpdate = (currentCurrent / 1000) * ((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); } + // Bound value between 0 and 100 + chargeState = chargeState > 100 ? 100 : chargeState; + chargeState = chargeState < 0 ? 0 : chargeState; + addSoCSample(chargeState); 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++) { sampleSum += lastSOC[i]; } - Power::percentRemaining = sampleSum / PowerParameters::Battery::AVERAGING_SAMPLES; + Power::percentRemaining = + sampleSum / PowerParameters::Battery::AVERAGING_SAMPLES; // Update last update time Power::lastPowerStateUpdate = xTaskGetTickCount(); @@ -358,6 +368,8 @@ bool Power::busPowered = false; bool Power::chargingState = false; +float Power::fullVoltageOffset = 0; + Power::Power() {} Power::~Power() {} \ No newline at end of file diff --git a/src/power/Power.h b/src/power/Power.h index 8c25fd2..34be7cd 100644 --- a/src/power/Power.h +++ b/src/power/Power.h @@ -71,6 +71,8 @@ protected: static bool chargingState; + static float fullVoltageOffset; + /// @brief Circular array of last calculated values for current state of /// charge static float lastSOC[PowerParameters::Battery::AVERAGING_SAMPLES];