xiao_sense_nrf52840_battery_lib icon indicating copy to clipboard operation
xiao_sense_nrf52840_battery_lib copied to clipboard

Automatically discover the current battery capacity.

Open Tjoms99 opened this issue 8 months ago • 0 comments

Battery State Determination and Capacity Calculation Enhancement

Summary

Currently, a lookup table is used to determine the battery's charge level based on voltage. This feature can be enhanced by allowing the MCU to determine the battery state dynamically.

Existing Implementation

The lookup table for voltage and capacity in percentage:

static BatteryState battery_states[BATTERY_STATES_COUNT] = {
    {4200, 100},
    {4160, 99},
    {4090, 91},
    {4030, 78},
    {3890, 63},
    {3830, 53},
    {3680, 36},
    {3660, 35},
    {3480, 14},
    {3420, 11},
    {3150, 1}, // 3240
    {0000, 0}  // Below safe level
};

Proposed Feature

Enable the MCU to determine the battery state by running a fully charged battery and discharging it down to just before a safe level. The safe level must be user-defined before running the test.

Discharge Functionality:

  • Discharge could be achieved by either powering all the LEDs or activating the BLE.
  • The MCU should monitor and record the voltage drop rate at specific voltages.

Improving Capacity Calculation: Currently, the capacity is determined using linear interpolation:

for (uint16_t i = 0; i < BATTERY_STATES_COUNT - 1; i++)
{
    // Find the two points battery_millivolt is between
    if (battery_states[i].voltage >= battery_millivolt && battery_millivolt >= battery_states[i + 1].voltage)
    {
        // Linear interpolation
        *battery_percentage = battery_states[i].percentage +
                              ((float)(battery_millivolt - battery_states[i].voltage) *
                               ((float)(battery_states[i + 1].percentage - battery_states[i].percentage) /
                                (float)(battery_states[i + 1].voltage - battery_states[i].voltage)));

        LOG_DBG("%d %%", *battery_percentage);
        return 0;
    }
}

Expected Outcome

  • Dynamic Battery State Determination: The MCU can determine the battery state dynamically, improving accuracy.
  • Enhanced Capacity Calculation: More accurate capacity calculation by considering the actual discharge rate instead of linear interpolation.

Tjoms99 avatar Jun 23 '24 21:06 Tjoms99