0% found this document useful (0 votes)
26 views4 pages

AEKF for Battery SoC on STM32

aSẤ

Uploaded by

pmd0793724602
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views4 pages

AEKF for Battery SoC on STM32

aSẤ

Uploaded by

pmd0793724602
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

BARROS ET AL.

ADAPTIVE EXTENDED KALMAN FILTERING FOR BATTERY STATE OF CHARGE ESTIMATION ON STM32 1

Adaptive Extended Kalman Filtering for Battery


State of Charge Estimation on STM32
António Barros, Edoardo Peretti, Davide Fabroni, Diego Carrera, Pasqualina Fragneto, Giacomo Boracchi

Abstract—Accurate and computationally light algorithms for


estimating the State of Charge (SoC) of a battery’s cells are R0 C1 C2
arXiv:2504.05936v1 [[Link]] 8 Apr 2025

crucial for effective battery management on embedded systems. ik


In this letter, we propose an Adaptive Extended Kalman Fil- R1 R2
+
ter (AEKF) for SoC estimation using a covariance adaptation OCV(zk ) Vk

technique based on maximum likelihood estimation - a novelty VR1 ,k VR2 ,k
in this domain. Furthermore, we tune a key design parameter
- the estimation window size - to obtain an optimal memory-
performance trade-off, and experimentally demonstrate our so- Fig. 1: Improved Thévenin model of a battery’s cell.
lution achieves superior estimation accuracy with respect to
existing alternative methods. Finally, we present a fully custom several hardware non-idealities, like sensor offsets, leading to
implementation of the AEKF for a general-purpose low-cost a significant SoC drift. Advanced methods, such as neural
STM32 microcontroller, showing it can be deployed with minimal
computational requirements adequate for real-world usage. networks, require extensive and often unavailable battery data,
while ODE-based models are too expensive to solve in real-
Index Terms—State of Charge Estimation, Adaptive Extended time on MCUs. Thus, most viable implementations rely on an
Kalman Filter, Embedded Implementation, STM32.
intermediate and computationally tractable solution: Kalman
Filters (KFs). In this case, the battery is approximated by
I. I NTRODUCTION an Equivalent Circuit Model (ECM) with mildly non-linear

T HE ongoing energy transition is accelerating the adoption


of battery-powered systems, e.g., electric vehicles, and, as
such, improving the reliability of battery management systems
dynamics, and its internal state is estimated by an Extended
Kalman Filter (EKF) or its adaptive variant (AEKF). Although
more advanced filters exist, e.g., sigma-point KFs and particle
has become a significant concern in recent years. Methods filters, they have higher computational complexity and provide
to track the State of Charge (SoC) of a battery cell are key superior performance only for highly non-linear systems. Since
components of most state of health assessment algorithms [1], that is not the case with the aforementioned ECM, they do not
which are essential for ensuring effective power management, offer any advantages for the considered application.
safe operation, and extending battery lifespan. Real-time diag- In this letter, we propose an AEKF for SoC estimation.
nostics require these algorithms to run on embedded devices, In particular, we put forward maximum likelihood estimation
which places an extra constraint on computational power. As (MLE) for covariance adaptation, which, although proven
a result, there has been a lot of interest in designing efficient successful in other domains, had never been attempted in this
yet computationally light algorithms that can be implemented context. Additionally, we provide two empirical contributions:
on microcontroller units (MCUs). first, we analyze the optimal estimation window size, a key
A battery’s cell is characterized by the total amount Qmax design criterion for this type of solution; second, we compare
of charge that it can store. We assume Qmax to be constant SoC estimation performance, both on simulated and real data,
over time, which is reasonable across a few charge-discharge against alternative solutions for MCUs, namely CC, a base
cycles. Denoting with Qk the amount of charge stored at time EKF and an AEKF using a covariance matching approach for
k, the SoC zk of the cell at time k is defined as covariance adaptation [5]. Last, we detail our implementation
Qk of the AEKF on a general-purpose STM32 MCU and analyze
zk = . (1) its performance (i.e. memory requirements, computation time,
Qmax
and power consumption), demonstrating its viability for real-
Directly measuring the SoC is not possible, thus each battery
time operation even on resource-constrained hardware.
cell is modeled as a black-box system, which exhibits a
voltage Vk at its terminals according to the applied current
II. S O C ESTIMATION ALGORITHM
ik . Therefore, the goal of a SoC estimation algorithm is to
estimate zk , given only the measures of ik and Vk . A. Second Order ECM
Many methods have been proposed for SoC estimation, We adopt a second-order ECM [6] for the battery model,
from naı̈ve approaches like Coulomb Counting (CC) [2] to known as the improved Thévenin model (Fig. 1). The ECM
complex ones like neural networks [3] and electrochemical employs electrical circuit elements to approximate the cell’s
models [4]. However, most of these are either inaccurate or too output voltage given a stimulus (current) and its state (stored
computationally demanding for real systems. Simple methods charge). When no load is attached, the cell exhibits a constant
like CC are sensitive to wrong initialization values and neglect output voltage, designated open circuit voltage (OCV). As it
BARROS ET AL. ADAPTIVE EXTENDED KALMAN FILTERING FOR BATTERY STATE OF CHARGE ESTIMATION ON STM32 2

Window size RAM Time Power


16 3.55kB 6.16ms 21.3mW
32 3.67kB 6.16ms 21.3mW
64 3.92kB 6.16ms 21.3mW
128 4.42kB 6.16ms 21.3mW

TABLE I: Resources required by our embedded implementa-


tion of the MLE scheme, for various window sizes.

heavily depends on the SoC, it is modeled as a controlled


voltage source. With a load, a current flows through the cell,
and the voltage drops below the OCV. This drop is modeled
with a resistance R0 in series with the source. Transient
effects during charge or discharge, known as concentration and
electrochemical polarizations, are modeled by two resistance-
capacitance blocks in series: (R1 , C1 ) and (R2 , C2 ).
The parameters of the ECM are the OCV(zk ) voltage
source, characterized by a non-linear function of the SoC, the
resistances R0 , R1 , R2 and the capacitances C1 , C2 . The ECM
in Fig. 1 has the following dynamics:
∆t


 zk+1 = zk + ik (2a)
Qmax




 ∆t ∆t
VR1 ,k+1 = e− R1 C1 VR1 ,k + R1 (1 − e− R1 C1 )ik (2b)
− R∆t − R∆t

VR2 ,k+1 = e 2 2 VR2 ,k + R2 (1 − e 2 2 )ik (2c)

 C C



Vk = OCV (zk ) + R0 ik + VR1 ,k + VR2 ,k (2d)

where VR1 and VR2 are the voltages on R1 and R2 , i and


V are the current and the voltage at the terminals, and ∆t is
the discretization step. Eq. (2a) is the discrete-time Coulomb
Counting equation, describing the evolution of the SoC zk by
accumulating the charge supplied by the current i. Eq. (2b)-
(2d) can be derived by standard electric circuit analysis.

B. Parameters Estimation
The first step is to estimate the ECM parameters for a given
battery cell, which consists in estimating the OCV-SoC curve
and the passive components (R0 , R1 , R2 , C1 , C2 ).
1) OCV-SoC Curve Estimation: When the current is very Fig. 2: Diagram of our embedded implementation of the EKF
low, the terminal voltage Vk approximates the OCV. Therefore, with MLE adaptive scheme. Red arrows represents communi-
we estimate the OCV-SoC relation by discharging a fully cation lines between the two modules. Black arrows represents
charged cell with a constant low current until the lower cut- access to modules’ data structures.
off voltage is reached, then recharging it with a constant low
current. By continuously measuring the terminal voltage, we where Vk is the measured voltage, and V̂k depends on
obtain a “discharge OCV” and a “charge OCV” curve. The R0 , R1 , R2 , C1 , C2 as in (2). We use the Levenberg-Marquadt
final OCV-SoC curve is the average of these two curves, reduc- method to solve the problem in (3). In particular, we adopt
ing the effects of hysteresis and small voltage drops due to the the incremental current test, which consists of charging the
battery’s ohmic resistance. In our embedded implementation, cell for many time intervals with a constant charge current,
we sample the curve at equally spaced SoC points (2% apart) interleaved with rest periods without any current. This test
to build a lookup table that is linearly interpolated at runtime. makes the circuit’s capacitive response very apparent and
We found this setting by incrementally halving the spacing avoids introducing quantization errors due to discretization,
from 16% until no performance gains could be observed. given that it can be described as a sum of current steps. In
2) Passive Component Estimation: Given a current profile general, the passive components may depend on some external
ik and an initialization of (R0 , R1 , R2 , C1 , C2 ), we estimate factors (e.g., temperature) or the SoC. Thus, if needed, the
the voltage V̂k , using the dynamics (2) of the ECM in parameter estimation is run for a set of discrete SoC points/
Fig. 1. Then, all ECM parameters are estimated by solving temperatures, and intermediate values are interpolated at run
the following non-linear least squares problem: time. In our simulations, we consider the temperature fixed and
R̂0 , R̂1 , R̂2 , Ĉ1 , Ĉ2 = arg min
X
(V̂k − Vk )2 (3) found the SoC dependence to be very weak, so we perform
R0 ,R1 ,R2 ,C1 ,C2
k
the above procedure only once.
BARROS ET AL. ADAPTIVE EXTENDED KALMAN FILTERING FOR BATTERY STATE OF CHARGE ESTIMATION ON STM32 3

1.1 6
3 MLE
1 CM
MAE [%]

4
EKF
0.9 2
CC
2
0.8
1
0.7 (N = 128, E = 0.74%) 0
1 2 4 8 16 32 64 128 256 5121024 −7 −6 −5 −4 −3 −20 −10 0 10 20
(a) Window Size (b) Noise Power [log10 ] (c) Parameter Error [%]
Fig. 3: 95% MAE confidence bands for synthetic data against (a) window size, (b) noise power and (c) parameters error.

C. AEKF Based on the Maximum Likelihood Principle extremely limited memory of the target platform, the size of N
Our variable of interest, the SoC zk , is one of the states of constitutes one of the main bottlenecks of our implementation.
the dynamical system in (2). The problem of estimating the In Section III, we extend the analysis in [7], and we look into
states of a dynamical system is known as filtering. A general how this parameter affects performance and how a size was
discrete-time dynamical system, with linear state transition, chosen to achieve an optimal trade-off.
non-linear output equation, and Gaussian noise, is given by:
( D. Embedded Implementation and Computational Complexity
xk = Axk−1 + Buk−1 + wk wk ∼ N (0, Σ) (4a)
We devised a fully custom C-language implementation of
yk = h(xk ) + Duk + vk vk ∼ N (0, σ 2 ) (4b) the MLE scheme and tested it on an STM32 NUCLEO-
In our case, the input is u = i, the output is y = V and G071RB development board. A diagram of the implementation
the state is x = (z, VR1 , VR2 )T . Comparing the two systems is shown in Fig. 2. Each major block represents a different
in (2) and (4), one can express A, B, D, h in terms of the ECM C module, responsible for aggregating both data structures
parameters. The KF is the optimal state estimator in case of — listed immediately below each module’s name — and
linear systems. However, since the output function h is non- functions. The two main ones are the circ_model.c,
linear (being OCV non-linear), we require an Extended KF, responsible for simulating the circuit dynamics, and the
which replaces h withh its first order approximation given by aekf_estimator.c, implementing the filter. Below the
data structures is also reported a detailed sequence of steps
i
∂h
the matrix C = ∂x = OCV (z) 1 1 .

corresponding to the algorithm’s core. Red arrows signal com-
The noise covariances, which heavily affect the filter’s
munications between the modules, while black arrows denote
performance, are notoriously hard to set. Heuristics-based
data flow within the module involving access to one of its data
selection criteria are often sub-optimal, even after extensive
structures. Two other modules encapsulate the main auxiliary
manual fine-tuning. Besides, the covariances can change over
libraries: matrix_lib.c implements core algebraic matrix
time if the system’s parameters fluctuate, for example, with
operations, queue_lib.c implements a queue data structure
varying operating conditions or aging. The adaptive EKFs
used to store past filter covariance matrices.
mitigate this issue by adjusting the covariances at runtime.
Table I shows the memory requirements, the computation
The most common approach is based on covariance matching
time, and the power consumption (averaged over a period
(CM) and has already been applied to SoC estimation [5].
of 10s) for one filter iteration as a function of window size
More recently, a superior covariance adaptation strategy based
with the internal clock speed of the MCU set to 64MHz.
on MLE has been proposed for a completely different task
RAM values are the ones reported by STM32CubeIDE after
- spacecraft navigation [7]. In this letter, we investigate this
compilation of the binary. In comparison, the EKF requires
scheme for SoC estimation. As proved in [7], the covariance
3.2kB, while CM has the same memory requirement as MLE.
matrices can be iteratively updated based on a window of past
The computation time was measured using an internal, highly
filter iterations. Using a sliding window of the previous N
precise hardware timer of the MCU, which was (re)initialized
samples, in our case the update equations become:
before each filter iteration and reset immediately afterward.
k
1 X  − 2 T Power was measured with an external X-NUCLEO-LPM01A
Σ̂k = Kk (ei ) Kk programmable power supply - configured to output 3.3V -
N
i=k−N +1 and its STM32CubeMonPwr software companion. As shown
k
(5)
1 X in Fig. 2, we adopt circular buffers to store previous filter’s
(e+ 2
CP+ T
 
σ̂k = i ) + i C states, which allows us to update the covariances in (5) in
N
i=k−N +1
constant time. Thus, execution time and power consumption do
+
where e− i and ei are the residuals after prediction and not depend on the window size. We observe that deploying our
correction, Ki is the Kalman gain, and P+i is the state estimate implementation on the entry-level STM32 MCU with 32kB of
covariance, all at time-step i. More details on these quantities RAM and a window size of 128 samples enables us to manage
can be found in Fig. 2. up to 7 cells with a single unit. Furthermore, considering a
As made evident in (5), the adaptive algorithm depends on typical sampling interval of hundreds of milliseconds for the
the size N of the sliding window. A large N potentially offers current and voltage measurements, its per-iteration execution
better performance but is more costly memory-wise. Given the time is well within real-time requirements.
BARROS ET AL. ADAPTIVE EXTENDED KALMAN FILTERING FOR BATTERY STATE OF CHARGE ESTIMATION ON STM32 4

Discharging Charging Profile EKF MLE CM


100 Charging 1 0.472% 0.175% 0.172%
Charging 2 0.410% 0.160% 0.240%
80 40 Discharging 1 0.785% 0.190% 0.485%
SoC [%]

60 TABLE II: MAE averaged over five cells for the three profiles.
30 CC EKF
40 CM MLE
GT B. Experiments on Real Data
20 20
0 30 60 90 120 150 0 30 60 90 120 150 We evaluate our algorithm on real data acquired with a
Time [min] Time [min] STEVAL-L99615C board from five LG INR18650MJ1 cells.
Fig. 4: SoC estimation results on real data. In this case, we fit SoC-dependent ECM parameters. For
evaluation, we consider one discharging and two charging
profiles. The OCV is approximately equal to the measured
III. E XPERIMENTS terminal voltage during rest periods, since the current is zero.
In this section, we evaluate our MLE AEKF, using both Therefore, we take the ground truth as the inverse of the
simulated and real data acquisitions. We compare the MLE OCV function at the terminal voltage Vk , and we evaluate
and CM adaptive EKFs with the naı̈ve Coulomb Counting the performance only during rest periods.
(CC) and the plain EKF. We measure estimation performance Fig. 4 shows the results for one cell on two profiles and
as the point-wise absolute error between the estimated SoC Table II reports the MAE of the three profiles, averaged over
and the ground truth, which in turn yields the mean absolute the five cells. The CC suffers from a severe offset, while the
error (MAE) following temporal averaging. EKF can match the ground truth but has strong overshooting.
The MLE scheme can precisely match the ground truth,
with greater precision than the CM, effectively achieving
A. Experiments on Simulated Data comparable or significantly lower estimation error.
We generate synthetic data using PyBaMM [4], a state-of-
the-art Python library for battery simulation, offering various IV. C ONCLUSION
electrochemical models and their parametrizations, along with We investigated SoC estimation algorithms suitable for im-
general differential equation solvers. For our experiments, plementation on embedded devices. Given the target platform,
we adopt the Doyle-Fuller-Newman battery model with pa- we focused on EKF-based solutions, comparing the CM and
rameters emulating an LG M50 cell. We generated syn- MLE adaptive schemes, introducing the latter as a novelty
thetic data using the WLTC profile, which simulates complex in the battery management literature. Although they perform
charge/discharge phases to stress an electric vehicle’s battery equivalently on simulated data, the MLE scheme proved
in various driving conditions. As described in Section II-B, we superior on real data, for which its optimal covariance adaption
fit SoC-independent ECM parameters, since we observe that better accounts for more significant model inaccuracies. With a
this model is accurate enough to match simulated data, with fully custom embedded implementation, we also demonstrated
ground truth provided by the simulator. Furthermore, we report it is suitable for real-world deployment on STM32 MCUs. In
the 95% confidence interval computed from 1000 Monte Carlo future work, we plan to adopt the MLE scheme in a combined
simulations using different noise realizations. SoC and state of health estimation [1].
1) Estimation window size: Both the MLE and the CM
schemes adapt the filtering using a sliding window of past R EFERENCES
filter samples. Since memory represents a strong limitation for
[1] M. A. Kamali, A. C. Caliwag, and W. Lim, “Novel soh estimation of
MCUs, we first analyze the window size. Fig. 3 shows, for all lithium-ion batteries for real-time embedded applications,” IEEE Embed-
methods, the MAE as a function of this parameter. We can ded Systems Letters, vol. 13, no. 4, 2021.
observe that both the MLE and the CM schemes achieve their [2] K. Movassagh, A. Raihan, B. Balasingam, and K. Pattipati, “A critical
look at coulomb counting approach for state of charge estimation in
minimum with a window size of N = 128. For all subsequent batteries,” Energies, vol. 14, no. 14, 2021.
experiments, we adopt this size. [3] K. L. Wong, M. Bosello, R. Tse, C. Falcomer, C. Rossi, and G. Pau, “Li-
ion batteries state-of-charge estimation using deep lstm at various battery
2) Robustness to noise power: We assess the robustness specifications and discharge cycles,” in ACM GoodIT, 2021.
of the filtering algorithms with respect to AWGN affecting [4] V. Sulzer, S. G. Marquis, R. Timms, M. Robinson, and S. J. Chapman,
the measured input current and output voltage. Fig. 3b shows “Python battery mathematical modelling (pybamm),” Journal of Open
Research Software, vol. 9, no. 1, 2021.
the MAE for increasing noise power. We can observe that, [5] R. Xiong, H. He, F. Sun, and K. Zhao, “Evaluation on state of charge
for reasonable noise powers, the MLE and CM schemes are estimation of batteries with adaptive extended kalman filter by experiment
equivalent, and both outperform the baselines. approach,” IEEE Transactions on Vehicular Technology, vol. 62, no. 1,
2012.
3) Robustness to modeling errors or to parameters that are [6] H. He, R. Xiong, X. Zhang, F. Sun, and J. Fan, “State-of-charge estimation
changing over time: Imprecise fitting might result in errors in of the lithium-ion battery using an adaptive extended kalman filter
the estimated ECM parameters. Fig. 3c shows the MAE as a based on an improved thevenin model,” IEEE Transactions on Vehicular
Technology, vol. 60, no. 4, 2011.
function of the relative errors in the ECM parameters. We can [7] C. T. Fraser and S. Ulrich, “Adaptive extended kalman filtering strategies
observe that the MLE and the CM are more robust for both for spacecraft formation relative navigation,” Acta Astronautica, vol. 178,
positive and negative errors. 2021.

You might also like