ƒ

Battery SOC EKF

Advanced
Run the live example Free · no sign-up · runs in your browser

The BatterySocEkf block implements an Extended Kalman Filter (EKF) to estimate the State of Charge (SOC) of a battery cell from terminal voltage and load current measurements. Using an equivalent circuit model with one or two RC polarisation branches, it corrects for coulomb-counting integration drift and polarisation voltage lag while estimating current sensor bias and confidence bounds (±2σ\pm 2\sigma).

Open-circuit voltage (OCV) characteristics are configured in an Excel-style 2-column spreadsheet editor (SOC (0–1) and OCV (V)) with a live OCV curve preview (OCV = f(SOC)), evaluated via linear interpolation or monotonic cubic Hermite splines (smooth). Measured cell datasheet curves can be imported/exported via CSV or pasted directly from Excel (Ctrl+V). All 21 parameters are organized into four logical sections: _Initial State & Timing_, _Noise Covariances (Q & R)_, _Equivalent-Circuit Cell Model_, and _OCV Lookup Table & Solver_.

Mathematical Model

x=[SOC, V1, V2, b]T,I=Im−b\mathbf{x} = [\text{SOC},\ V_1,\ V_2,\ b]^T, \quad I = I_m - b

The continuous state dynamics and terminal voltage measurement equation are:

SOC˙=−η I3600 Q,V˙j=−VjRjCj+ICj,b˙=0\dot{\text{SOC}} = -\frac{\eta\, I}{3600\,Q}, \quad \dot{V}_j = -\frac{V_j}{R_j C_j} + \frac{I}{C_j}, \quad \dot{b} = 0
Vterm=OCV(SOC)−V1−V2−R0I,H=[∂ OCV∂ SOC, −1, −1, R0]V_{\text{term}} = \text{OCV}(\text{SOC}) - V_1 - V_2 - R_0 I, \quad \mathbf{H} = \left[\frac{\partial\,\text{OCV}}{\partial\,\text{SOC}},\ -1,\ -1,\ R_0\right]

State covariance extrapolation and iterated measurement correction follow discrete Kalman recursions:

x^k∣k=x^k∣k−1+Kk(Vmeas−h(x^k∣k−1)),Kk=Pk∣k−1HkT(HkPk∣k−1HkT+R)−1\hat{\mathbf{x}}_{k|k} = \hat{\mathbf{x}}_{k|k-1} + \mathbf{K}_k \left(V_{\text{meas}} - h(\hat{\mathbf{x}}_{k|k-1})\right), \quad \mathbf{K}_k = \mathbf{P}_{k|k-1} \mathbf{H}_k^T (\mathbf{H}_k \mathbf{P}_{k|k-1} \mathbf{H}_k^T + R)^{-1}

Inputs & Outputs

Direction ID Label Type Status
→ In in1 I number Required
→ In in2 V number Required
→ In in3 Reset number Optional
→ In in4 Dropout number Optional
← Out soc_hat SOC number Output
← Out soc_sigma σ SOC number Output
← Out v1_hat V₁ number Output
← Out v2_hat V₂ number Output
← Out bias_hat I bias number Output
← Out voc_hat OCV number Output
← Out jacobian dOCV/dSOC number Output
← Out k_gain_0 K_SOC number Output
← Out innovation Innov. number Output
← Out innovation_cov S number Output
← Out nis NIS number Output

Parameters

Parameters Label Type Default Description
sampleTime Sample Time number 1 Filter period in seconds
socInit Initial SOC Estimate number 0.5 Fraction 0…1
v1Init Initial V1 Estimate number 0
pSoc Initial SOC Variance number 0.01 How unsure the initial SOC is; 0.01 is σ = 10 % SOC
pV1 Initial RC Voltage Variance number 0.000001 Per RC branch; small, since a rested cell has V1 ≈ V2 ≈ 0
pBias Initial Bias Variance number 0 How unsure the current sensor's offset is; 0 leaves the offset out of the model, 0.01 is σ = 0.1 A
qSoc Process Noise SOC number 1e-8 Noise density per second, so the filter does not change with the sample time
qV1 Process Noise RC Voltage number 0.000001 Noise density per second, per RC branch
qBias Process Noise Bias number 0 How fast the sensor offset may drift, as a density per second; 0 for a constant offset
rMeas Measurement Noise number 0.0001 Variance of the voltage measurement in V²
r0 R0 (Ω) number 0.05 Ohmic resistance
r1 R1 (Ω) number 0.02 Polarisation resistance of the fast branch
c1 C1 (F) number 1000 Polarisation capacitance of the fast branch
r2 R2 (Ω) number 0 Resistance of a second, slow RC branch; 0 leaves the branch out
c2 C2 (F) number 30000 Capacitance of the second RC branch
capacity Capacity (Ah) number 3.2
chargeEfficiency Charge Efficiency number 1 Coulombic efficiency while charging (current < 0); Li-ion cells are typically 0.99–0.999
ocvSoc OCV Table SOC table 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 SOC breakpoints, ascending
ocvVoltage OCV Table Voltage text 3.00 3.50 3.65 3.72 3.78 3.84 3.90 3.97 4.05 4.14 4.20 Open-circuit voltage at each breakpoint in V
ocvInterpolation OCV Interpolation select linear Linear matches a LookupTable exactly; Smooth (monotone cubic) has no slope jumps at the breakpoints, which steadies the filter's gain
iterations Update Iterations number 1 1 is the classic EKF; 3 relinearises at the corrected estimate (iterated EKF), which helps where the OCV curve bends sharply

Usage Examples

Example 1: Resting Cell SOC Initialization

A rested lithium cell at 3.72 V is initialized with an uncertain guess (socInit=0.6\text{socInit} = 0.6). As terminal voltage measurements arrive, the EKF converges to the true 30% SOC within 1.5 s, reducing σsoc\sigma_{\text{soc}} from 10% to under 1%.

Example 2: Dynamic Drive Cycle with Current Sensor Bias

During pulsed electric vehicle discharge cycles, the filter estimates both cell state of charge and a 150 mA DC offset on the current sensor, preventing long-term integration drift.

Remarks & Best Practices

  • Interactive OCV Spreadsheet & CSV Import/Export: Use 📂 Import CSV, 💾 Export CSV (battery_ocv.csv), or direct clipboard paste (Ctrl+V from Excel) to load empirical OCV–SOC characterization tables and inspect the live OCV = f(SOC) curve immediately.
  • Uncertainty bounds: The soc_sigma output provides real-time estimation error bounds, allowing battery management systems to compute 95% confidence intervals (±2σ\pm 2\sigma).
  • Consistency monitoring: The normalized innovation squared (NIS) diagnostic tracks filter health, averaging 1.0 when noise covariance matrices QQ and RR match true operating conditions.
  • Sensor dropout resilience: Setting the optional Dropout input high pauses measurement updates, seamlessly falling back to coulomb counting while propagating covariance growth.
  • Nonlinear robustness: Using smooth cubic interpolation and multiple update iterations prevents divergent covariance jumps in regions where the OCV curve exhibits steep gradients.

Frequently Asked Questions (FAQ)

Q: Why is an Extended Kalman Filter (EKF) superior to Coulomb counting for battery SOC?

Pure Coulomb counting (SOC(t)=SOC0−1Qn∫IdtSOC(t) = SOC_0 - \frac{1}{Q_n} \int I dt) irreversibly drifts due to current sensor offsets and unmodeled parasitic reactions. An EKF pairs the Coulomb counting integrator with an Equivalent Circuit Model (ECM, R0+R1∥C1R_0 + R_1 \parallel C_1) and corrects the state estimate at each step using the nonlinear open-circuit voltage relationship Vt=OCV(SOC)−IR0−VRCV_t = OCV(SOC) - I R_0 - V_{RC}. This bounds SOC estimation error within ±2%\pm 2\% across temperature shifts and aging.

Related Components

  • KalmanFilter: Linear Kalman filter for multi-state estimation of linear dynamic systems.
  • LookupTable: One-dimensional piecewise linear table lookup for static nonlinearities.
  • RandomNumber: Gaussian noise source for simulating voltage and current sensor noise.