Battery charge, health and anomaly#

The ESP-Mosaico has a small cell and a fuel gauge. OpenCarMind EdgeOS 2.1.0 estimates the cell’s state of charge and state of health, and checks it for faults. You see the results in plain words in the Health app. The fixed battery protection stays in charge of power: none of these models acts on it.

How the charge estimator works#

How the battery charge estimator works, hand-drawn. On the board with placeholder parameters. The gauge gives voltage V, current I and temperature T from the sample the board task already reads, with no new I2C traffic, and the gauge is never written. The cell model is one RC pair: an open-circuit voltage source OCV(z), a series resistance R0, then R1 in parallel with C1, so V equals OCV(z) plus v1 plus R0 times I. OCV is a 41-point table over the charge z from 0 to 1, and tau equals R1 times C1. The filter runs one step per second on the state z and v1 with covariance P. Predict: z minus equals z plus eta I delta t over 3600 Q SoH, v1 minus equals a v1 plus 1 minus a times R1 I with a equal to exp of minus delta t over tau, P minus equals F P F transpose plus W with F equal to diag 1, a. Expect: V hat equals OCV of z minus plus v1 minus plus R0 I, with H equal to dOCV/dz and 1. Correct: K equals P minus H transpose over H P minus H transpose plus R, x equals x minus plus K times V minus V hat, P equals P minus minus K H P minus. Anchors: after 300 s at rest z equals the inverse OCV of V, and at the end of a CV charge z equals 1. Tried on top of it and not shipped: a residual MLP with 11 inputs, 12 tanh units and one clamped output, and an EKF with a third state for the current offset. Results on the Mac: on the 18650 NMC cycling set RMSE 0.33 to 0.53 percent and worst 2.4 to 4.0 percent. Start 20 percent wrong: EKF 0.75 to 2.0 percent, coulomb counting 8.6 to 10.5 percent. NASA cells with the 18650 OCV curve: 7.9 to 21 percent. Cost 111 flops and 1 exp, 324 B.

The cell is modelled as a voltage source that depends on the charge, OCV(z), in series with a resistance R₀ and one resistor-capacitor pair R₁ ∥ C₁. R₀ gives the instant drop when current flows. The RC pair gives the slow part, with time constant τ = R₁C₁. The filter keeps two states, the charge z (0 to 1) and the voltage v₁ on the RC pair, and a 2 × 2 covariance P that says how sure it is of them. Plett laid out this use of the extended Kalman filter for battery management systems[21].

Every second it predicts. It counts the charge and lets the RC voltage decay towards its new level:

\[z_k^- = z_{k-1} + \frac{\eta\, I_k\, \Delta t}{3600\, Q\, \mathrm{SoH}}, \qquad v_{1,k}^- = a\, v_{1,k-1} + (1 - a)\, R_1 I_k, \qquad a = e^{-\Delta t/\tau}\]

Here I is the current in amps (positive while charging), Δt the seconds since the last step, Q the capacity in Ah, η the charge efficiency, and SoH the share of the capacity the cell still has. The covariance grows by the process noise W: P⁻ = F P Fᵀ + W, with F = diag(1, a).

Then it predicts the terminal voltage and compares it with the gauge:

\[\hat V_k = \mathrm{OCV}(z_k^-) + v_{1,k}^- + R_0 I_k, \qquad H_k = \left[\, \frac{d\,\mathrm{OCV}}{dz}\bigg|_{z_k^-} ,\quad 1 \,\right]\]

The gap V − V̂ is the innovation. The Kalman gain K decides how much of it goes into each state:

\[K_k = \frac{P_k^- H_k^{\top}}{H_k P_k^- H_k^{\top} + R}, \qquad x_k = x_k^- + K_k \left(V_k - \hat V_k\right), \qquad P_k = P_k^- - K_k H_k P_k^-\]

R is the variance of the voltage reading. Where the OCV curve is steep, the voltage says a lot about z, and the filter follows it. Where the curve is flat, dOCV/dz is small, K shrinks, and the filter trusts the charge count. That is how a wrong start fixes itself: with the start 20 % off, the EKF’s error stayed at 0.75 to 2.0 % RMSE, where plain coulomb counting stayed at 8.6 to 10.5 %.

Two anchors reset the charge outright. After 300 s at rest the voltage is the OCV, so z = OCV⁻¹(V). At the end of a CV charge, z = 1. Until the first anchor the Board page says “learning”. Two anchors at least 30 % of charge apart give a capacity sample for the health estimate.

In the firmware (main/bat_est.c, math in main/bat_est_core.c, model in main/bat_model.h) the step matches the Mac reference to within 1.1e-6 over 370 test steps (make -C tests/host bat BAT_MODEL=<fitted header>). It reads the gauge sample the board task already takes every 2 s, so there is no new I2C traffic, and the gauge is never written. The learnt values and the last SoC go to NVS at most every 10 minutes and on the safe-shutdown path. Every 10 s a training row (V, I, average I, T, the gauge’s SoC and remaining capacity, the estimator’s SoC, SoH and mode) goes to obd/bat/bNNNNNN.csv on the NAND, at most 2 MB in all, oldest file first out. Those rows are what the cell’s own OCV curve will be fitted from. Until then the firmware runs placeholder parameters for a 4.2 V cell, and the nominal capacity is CONFIG_OCM_BAT_NOMINAL_MAH, 65 mAh from the cell’s label (UNVERIFIED).

Tested on the Mac, held out from fitting:

Test

2-state EKF (shipped)

For comparison

An 18650 NMC cycling data set, held-out early, middle and late life

RMSE 0.33 to 0.53 %, worst 2.4 to 4.0 %

Start 20 % wrong

RMSE 0.75 to 2.0 %

coulomb counting 8.6 to 10.5 %

NASA cells[26], with the 18650 cell’s OCV curve

RMSE 7.9 to 21 %

the OCV curve is specific to the cell, so it has to come from this board’s logs

Current offset of +0.01C (0.65 mA on 65 mAh)

RMSE 5.3 %

the 3-state EKF: 0.27 to 0.79 %

Cost per step

111 flops and 1 exp, 324 B

I checked where the EKF’s remaining error comes from. It is a slow drift of about ±0.3 % from counting charge, and it does not sit at the low-charge knee, in the CV taper or with heat. An unscented Kalman filter[22] with one or two RC pairs gave the same RMSE. Neither the linearisation nor the model order is the limit. The biggest risk is a current offset, which the 3-state EKF in the last dropdown is kept ready for.

How the health estimate works#

How the battery health estimate works, hand-drawn. State of health is the capacity the cell still holds as a share of its label capacity. Card 1, counted capacity, on the board: two anchors at least 30 percent of charge apart give one capacity sample, and SoH equals the learnt capacity over the label capacity. A full charge counted to the cutoff: 0.12 percent error on the 18650 set and 1.2 percent on the NASA cells. Card 2, DC resistance trend, on the board: a current step gives the DC resistance over 120 s, with r of minus 0.88 against capacity, used as a flag, and 6.42 percent off as an SoH estimate on its own. Card 3, partial-charge regressor, validated on the Mac: features from an ordinary charge relative to the cell's first 10 cycles, the charge added between 3.9 and 4.1 V, dQ/dV peak height and position, CV duration and taper constant, DC resistance over 120 s and mean temperature. Ridge regression, 8 multiply-adds and 16 ops, 100 B, picked by forward cross-validation over a GBDT and an 8-unit MLP. Held-out cycles: on the 18650 set the ridge 1.45 percent, the DCIR trend 6.42 percent, the full-charge count 0.08 percent with a full cycle. On the NASA discharge-only cells the ridge 13.9 to 25.0 percent and the DCIR trend 8.5 to 19.2 percent.

SoH is the capacity the cell still holds divided by its label capacity. The board gets it two ways.

The firmware counts it. Two anchors (a rest, or the end of a CV charge) at least 30 % of charge apart give one sample: the charge counted between them divided by the change in z. A full charge counted down to the cutoff was off by 0.12 % on the 18650 set and by 1.2 % on the NASA cells. Current steps also give a DC resistance (DCIR) over 120 s. It rises as the cell fades (r = −0.88 against capacity on the 18650 set), but as an SoH estimate on its own it is off by 6.42 %, so the board uses it as a flag.

A count from full to the cutoff needs the board to run its cell flat, which a board on the car’s USB rarely does. So I trained a regressor on what an ordinary charge shows. Its features are taken relative to the cell’s first 10 cycles: the charge added between 3.9 and 4.1 V, the height and position of the dQ/dV peak (the charge added per volt of rise), the CV duration and its taper constant, the DCIR over 120 s and the mean temperature. Ridge regression won the forward cross-validation (0.83 % against 1.96 % for a tree ensemble (GBDT) and 20 % for an 8-unit MLP). It costs 8 multiply-adds and 16 ops, in 100 B.

Held-out cycles, forward in time (MAPE)

Partial-charge ridge

DCIR trend

Full-charge count

18650 NMC cycling set

1.45 %

6.42 %

0.08 %, needs a full cycle

NASA cells[26], discharge only

13.9 to 25.0 %

8.5 to 19.2 %

not compared

The regressor passes on the cell it learned and does not transfer to cells it never saw charge. It needs the board’s own first charges as its reference. The plan is the count when there is a full cycle and the regressor otherwise. The regressor is in the firmware since 2.1.0, where its C matches the Mac to 1.3e-7. It has not run on the board yet: it needs 10 deep charges of this cell as its reference, and in the 2.1.0 board check the cell stayed full (0 of 10).

How the anomaly detector works#

How the battery anomaly detector works, hand-drawn. Validated on the Mac, in the firmware since 2.1.0. Card 1, every 60 s, 9 features from the EKF and the gauge: innovation mean and RMS as slow averages, voltage drift and current at rest, temperature against a one-node heat model, voltage sag per C-rate against R0 at that temperature, the DC resistance ratio at a current step, load level and gauge read glitches. Card 2, three detectors: a 9-4-9 tanh autoencoder raises the alarm from its reconstruction error, a z-score with a frugal median and MAD and an online Gaussian are diagnostics only. False alarms on 388 h of held-out normal data: autoencoder 0.0026 per hour with a 95 percent upper bound of 0.012, z-score 0.245 and Gaussian 0.126, which drift with ageing. Card 3: a 24 h warm-up, learning only from clearly normal windows, an alarm after 3 windows in a row, a banner and a log line, and it never acts on power. Under 400 ops per window, 280 B of state and 640 B of parameters. Injected faults caught and median time: resistance plus 100 percent 92 percent in 72 min, temperature plus 15 C 83 percent in 164 min, soft short 5 percent per hour 75 percent in 176 min, resistance plus 30 percent 25 percent, soft short 0.5 percent per hour 8 percent, current offset 8 percent, intermittent reads 0 to 8 percent, temperature plus 5 C, current gain error and extra drain 0 percent.

The detector learns what normal looks like for this cell and raises an alarm when 60 s windows stop looking like it. Nobody labels a fault for it. Each window has 9 features from the EKF and the gauge: the innovation’s mean and RMS as slow averages, the voltage drift and the current at rest, the temperature against a one-node heat model, the voltage sag per C-rate against R₀ at that temperature, the DC resistance ratio at a current step, the load level and the gauge’s read glitches.

Three detectors ran side by side. One is a z-score against a frugal streaming median and MAD[25], one an online Gaussian, and one a 9 → 4 → 9 tanh autoencoder whose score is its reconstruction error. All three warm up for 24 h, learn only from clearly normal windows, and alarm after 3 windows in a row. On 388 h of held-out normal data the autoencoder gave 0.0026 false alarms per hour (95 % upper bound 0.012). The z-score gave 0.245 per hour and the Gaussian 0.126, because the cell’s ageing moved them. So only the autoencoder raises alarms, and the other two only log.

Injected fault

Caught

Median time to the alarm

Resistance +100 %

92 %

72 min

Temperature +15 °C

83 %

164 min

Soft short, 5 % of capacity per hour

75 %

176 min

Resistance +30 %

25 %

Soft short, 0.5 % per hour

8 %

Current offset

8 %

Intermittent reads

0 to 8 %

Temperature +5 °C, current gain error, extra drain

0 %

It catches large faults only. The thresholds were learned on public cell data, so they have to be learned again on the board’s own cell before an alarm means anything there. An alarm shows a banner and writes a log line. It never acts on power: the fixed battery protection above stays in charge of that. The cost is under 400 ops a window, with 280 B of state and 640 B of parameters. It is in the firmware since 2.1.0, within 1.05e-6 of the Mac, and the 2.1.0 board check admitted it and started its 24 h warm-up. A real fault has not been seen in use yet. The worn and fault cards shown so far come from a test build that writes a state into the Health screen only.

The status of each model on the board is in the table on AI services.