State of Charge Estimation MATLAB Function
State of Charge Estimation MATLAB Function
Abstract—This paper proposes a Kalman filter based state- battery models. In [8], the KF was compared against an un-
of-charge (SOC) estimation MATLAB function using a second- scented KF, while in [9] an extended KF (EKF) was compared
order RC equivalent circuit model (ECM). The function requires to the central difference KF. In [10], an EKF was used with
2021 IEEE Transportation Electrification Conference & Expo (ITEC) | 978-1-7281-7583-6/21/$31.00 ©2021 IEEE | DOI: 10.1109/ITEC51675.2021.9490163
Authorized licensed use limited to: KLE Technological University. Downloaded on February 07,2022 at 08:51:50 UTC from IEEE Xplore. Restrictions apply.
− ∆t
Q η[k]
−∆t
B = R1 (1 − exp R1 C1 ) (9)
−∆t
R2 (1 − exp R2 C2 )
h i
∂VOC ∂V ∂V
∂V
C= ∂SOC ∂V1 ∂V2 = OC
∂SOC −1 −1 (10)
D = −R0 (11)
B. Extended Kalman Filter
Fig. 1. Second Order Resistor-Capacitor ECM Diagram.
The EKF, a version of the regular KF, is used to estimate the
states for a non-linear system. EKF uses a two-step prediction-
A. Battery Modelling correction algorithm as described in (12) to (16), where k
denotes a discrete point in time, K is the Kalman gain, P is
In Fig. 1, the OCV is represented by VOC , the output
the covariance of the measurement error, Q is the covariance of
terminal voltage by Vt , and the internal resistance of the
the process, and R is the covariance of the output [18]. First,
battery by R0 . The voltage across the first RC network is
a prediction or time update is done and then the correction
V1 and V2 across the second network. (2) to (4) describe the
or measurement update. This cycle repeats until the end of
ECM dynamics in state-space [16].
the data. Note, the hat symbol, ˆ, represents an estimate of a
η∆t i(k) variable, |k denotes predicted or a-priori estimate, and |k + 1
SOC(k + 1) = SOC(k) − (2) denotes updated or a-posteriori estimate.
Cn
Prediction (Time Update)
Vt (k) = VOC (k) − V1 (k) − V2 (k) − i(k)R0 (3) 1. Project the states ahead (a-priori):
−∆t −∆t
x̂k+1|k = Ax̂k|k + Buk (12)
V1 (k + 1) = exp R1 C1 V1 (k) + R1 (1 − exp R1 C1 )i(k)
−∆t −∆t (4) 2. Project the error covariance ahead:
V2 (k + 1) = exp R2 C2 V2 (k) + R2 (1 − exp R2 C2 )i(k)
The input to the model is the current i(k) at time step k. Pk+1|k = APk|k AT + Qk (13)
∆t is the sample time in seconds, R1 , C1 , R2 and C2 are Correction (Measurement Update)
the RC model parameters. VOC in (3) is calculated in (5)
as a function of SOC and battery surface temperature. Each 1. Compute the Kalman gain:
SOC-OCV curve is calculated using the Hybrid Pulse Power
Characterization (HPPC) test results obtained at 40°C, 25°C, Kk+1 = Pk+1|k C T (CPk+1|k C T + Rk+1 )−1 (14)
10°C, 0°C, and -10°C [17].
2. Update the estimate with measurement zk (a-posteriori):
VOC = f (SOC, T emperature) (5)
The state and measurement equations can be calculated in x̂k+1|k+1 = x̂k+1|k + Kk+1 (zk+1 − C x̂k+1|k ) (15)
(6) and (7) as follows: 3. Update the error covariance:
485
Authorized licensed use limited to: KLE Technological University. Downloaded on February 07,2022 at 08:51:50 UTC from IEEE Xplore. Restrictions apply.
III. EKF SOC E STIMATION F UNCTION
This section will review the required battery data as well
as the syntax and commands in order to utilize the EKF SOC
estimation function. One can find publicly available battery
test data at [17] [20] [21]. The MATLAB function, based on
[18], as well as the example illustrated below can be found at
[22].
A. Battery Parameters
Before using the EKF_SOC_Estimation function, users
will need the SOC-OCV curve, R0 , and the 2RC ECM battery
parameters for the specific battery that the SOC is being
estimated for. This data is loaded within the function and are
not passed in as function parameters.
Appropriate battery testing should be done to obtain data
for OCV as a function of SOC for a desired range of battery Fig. 3. Discharge resistance vs SOC of a Turnigy Graphene 5000mAh Li-ion
temperatures (i.e., multiple datasets, each for a different battery Battery at different temperatures.
temperature as shown in Fig. 2). This testing typically involves
charging and discharging the battery at low currents (0.05C),
however, the complete details of this testing may vary. Once The function can be called using:
finalized datasets are obtained, it is important to ensure that
there are no repeated SOC points (especially if these points 1 function [SOC_Estimated, Vt_Estimated, ...
have differing OCV values) as this will cause an interpolation Vt_Error] = EKF_SOC_Estimation(Current, ...
Vt_Actual, Temperature)
error when the function is running. This does not apply to
repeated OCV values. The function takes a drive cycle current measured in amps
Typical 2RC ECM parameters are R0 , R1 , C1 , R2 and (Current), Vt measured in volts (Vt_Actual), and battery
C2 , where R0 represents the internal resistance of the temperature measured in °C (Temperature), all as vectors
cell, and the rest of the parameters represent non-physical of type double as input. The length of all vectors must be
characteristics of the cell, which when grouped into RC the same. The function outputs a vector of the estimated SOC
branches, as shown in Fig. 3, describe the cell’s dynamics. To (SOC_Estimated), estimated Vt (Vt_Estimated), and the
obtain these parameters at different temperatures as functions error between Vt measured and Vt estimated (Vt_Error).
of SOC, a model-based parameter optimization approach The function by default loads the provided
within MATLAB [23] was used. As described by [24], HPPC 'BatteryModel.mat' and 'SOC-OCV.mat' files which
test data at different temperatures [17] was evaluated within are labeled tables. The BatteryModel table contains the
the optimization algorithm to ensure the ECM accurately SOC, R0 , R1 , C1 , R2 , C2 , and T data in columns 1 to 7,
described the behaviour of the battery. respectively. The SOC ranges from 0% to 100% by intervals
of 10%, however, users can vary the intervals as needed. The
SOC-OCV table contains the SOC, OCV , and T data in
columns 1 to 3, respectively.
486
Authorized licensed use limited to: KLE Technological University. Downloaded on February 07,2022 at 08:51:50 UTC from IEEE Xplore. Restrictions apply.
The KF has three tunable parameters: Rx , Px and Qx . IV. E XAMPLE S IMULATION
These will need to be adjusted for each battery either manually
To illustrate the use of the function, it was evaluated at
or through an optimization algorithm. The AEKF algorithm
40°C, 25°C, 10°C, 0°C, and -10°C using EKF and the battery
requires substantially different tuning from the EKF, and may
data available at [17].
be unstable when the same parameters are used.
A new Turnigy Graphene 5000mAh 65C cell was tested
extensively in a thermal chamber by [17]. SOC-OCV mapping
1 R_x = 2.5e-5; and HPPC tests were performed at 40°C, 25°C, 10°C, 0°C, and
2 P_x = [0.025 0 0;
3 0 0.01 0; -10°C. The tests cover SOC range from 100% to 5% with four
4 0 0 0.01]; different charging and discharging currents at 1, 2, 5 and 10
5 Q_x = [1.0e-6 0 0; C-rates. After the characterization, the battery was subjected
6 0 1.0e-5 0;
7 0 0 1.0e-5]; to driving cycles UDDS, HWFET, LA92, US06 as well as
combinations of these cycles. The drive cycles were sampled
The function allows for temperature dependent data and by every 0.1 seconds, and other tests were sampled at a slower
default utilizes five different temperatures: 40°C, 25°C, 10°C, or variable rate. [17].
0°C, and -10°. The function interpolates the battery parameters The SOC-OCV curve and battery model parameters were
between temperatures at each iteration of the cycle using the optimized for each temperature and SOC level from 100% to
output function from scatteredInterpolant. 0% at 10% increments using [23]. The battery data was then
re-sampled to every second. The convergence and estimation
of the SOC and Vt are highly dependent on the battery
1 F_R0 = scatteredInterpolant(param.T,...
2 param.SOC,param.R0); model parameters. The initial P , Q and R values in the EKF
were manually tuned so the average root mean squared error
(RMSE) of all temperatures SOC was less than 5% and Vt
1 R0 = F_R0(T,SOC);
was less than 100mV. The results of this tuning can be seen
in Fig. 4 and Fig. 5. The RMSE at 40°C was 1.75% for SOC
To use this feature, users will require internal resistance and 1mV for Vt for the LA92 drive cycle. Fig. 6 illustrates
data, and battery parameters for each temperature. If the the measured versus estimated Vt and SOC of the LA92 drive
datasets available to the user is not temperature dependent cycle at 40°C. Since there is only one set of KF parameters
or the temperature is unknown, the scatteredInterpolant for all the temperatures, the lower temperatures have higher
function should be replaced with pchip or interp1. error. One solution to this is to tune different parameters for
The SOC-OCV line is curve fitted using the polyfit each temperature similar to the battery parameters.
function in a least squares sense. Polynomial differentiation To test the robustness of the function, the initial SOC was
is then completed on the curve to be used to calculate the offset by 10%, and the current was offset by +/-0.1A. Fig.
matrix C. Both the regular curve SOCOCV and the differentiated 7 provides details of the SOC and Vt RMSE given these
one dSOCOCV are then evaluated within the KF loop using conditions. The system proves to be robust at 40°C as the
polyval. RMSE values do not differ from each other drastically. With
the 10% initial SOC offset, the system reaches within 5%
of the Coulomb counted SOC within 3 minutes (Fig. 8). A
1 SOCOCV = polyfit(param.SOC,param.OCV,11); % ...
calculate 11th order polynomial for the ... graph of the +/-0.1A current offset can be seen in Fig. 9.
SOC-OCV curve
2 dSOCOCV = polyder(SOCOCV); % derivative of ...
SOc-OCV curve for matrix C
487
Authorized licensed use limited to: KLE Technological University. Downloaded on February 07,2022 at 08:51:50 UTC from IEEE Xplore. Restrictions apply.
Fig. 7. SOC RMS Error and Vt RMS Error with no error, initial 10% SOC
offset, and +/- 0.1A current offset at 40°C for the LA92 drive cycle.
Fig. 8. (a). Measured vs. Estimated V t with 10% initial SOC offset at 40°C
for LA92 drive cycle. (b). SOC Coloumb Counting vs. SOC EKF Estimation
with 10% initial SOC offset at 40°C for LA92 drive cycle.
Fig. 6. (a). Measured vs. Estimated V t at 40°C for LA92 drive cycle. (b).
SOC Coloumb Counting vs. SOC EKF Estimation at 40°C for LA92 drive
cycle.
With further tuning of the battery parameters and the KF
parameters, the RMSE values can be improved further. This
The convergence rate and RMSE values of error input can be function can be further expanded upon to add a temperature
improved upon with more accurate KF tuning values P , Q, model for more accurate estimations of the battery temper-
and R. ature. It can also be simplified to remove the temperature
dependency as well as adaptive portion by adjusting those
V. C ONCLUSIONS respective lines.
In this paper, a Kalman filter based SOC estimation MAT- ACKNOWLEDGMENT
LAB function is proposed. The function is based on a second- The authors would like to thank Dr. Ryan Ahmed for his
order ECM. It allows users to load their specific battery data contribution and constructive feedback during the development
including the SOC-OCV curves, internal resistance, and the of the code.
battery model parameters. The function has the flexibility to be
used as an EKF or AEKF as well as using battery temperature R EFERENCES
based data. An example is illustrated with publicly available [1] Environment and Climate Change Canada, ”Cana-
Turnigy Graphene battery data where less than 2% average dian Environmental Sustainability Indicators: Green-
RMSE is achieved across the various temperatures. The users house gas emissions,” 2020. [Online]. Available:
https://www.canada.ca/content/dam/eccc/documents/pdf/cesindicators/ghg-
of the function have the ability to build on this function or emissions/2020/greenhouse-gas-emissions-en.pdf. [Accessed 12
integrate it into a more complex model. December 2020].
488
Authorized licensed use limited to: KLE Technological University. Downloaded on February 07,2022 at 08:51:50 UTC from IEEE Xplore. Restrictions apply.
Battery Low-Temperature Effects—Review,” in IEEE Transactions on
Vehicular Technology, vol. 68, no. 5, pp. 4560-4572, May 2019.
[14] C. Chang, Y. Zheng, and Y. Yu, “Estimation for Battery State of Charge
Based on Temperature Effect and Fractional Extended Kalman Filter,”
Energies, vol. 13, no. 22, p. 5947, Nov. 2020.
[15] Q.-Z. Zhang, Z.-Y. Wang, and H.-M. Yuan, Estimation for
SOC of Li-ion battery based on two-order RC temperature
model, 28-Jun-2018. [Online]. Available: https://ieeexplore-ieee-
org.libaccess.lib.mcmaster.ca/document/8398150. [Accessed: 2020].
[16] G. Plett, Battery Managment Systems, vol. 1, 2015.
[17] Kollmeyer, Phillip; Skells, Michael (2020), “Turnigy Graphene
5000mAh 65C Li-ion Battery Data”, Mendeley Data, V1, doi:
10.17632/4fx8cjprxm.1
[18] R. Ahmed, ”OCV-RRC Models in State Space, Kalman Filtering and
State of Charge Estimation,” MECH ENG 754 Management and Control
of Electric Vehicle Batteries Lecture 7, McMaster University, Hamilton,
Ontario, Fall 2020.
[19] Daoming Sun, Xiaoli Yu, Chongming Wang, Cheng Zhang, Rui Huang,
Quan Zhou, Taz Amietszajew, Rohit Bhagat ”State of charge estimation
for lithium-ion battery based on an Intelligent Adaptive Extended
Kalman Filter with improved noise estimator,” Energy, vol. 214, 2021.
[20] Kollmeyer, Phillip (2018), “Panasonic 18650PF Li-ion Battery Data”,
Mendeley Data, V1, doi: 10.17632/wykht8y7tg.1
[21] Naguib, Mina; Kollmeyer, Phillip; Skells, Michael (2020), “LG
Fig. 9. (a). Measured vs. Estimated V t with +/-0.1A current offset at 40°C 18650HG2 Li-ion Battery Data”, Mendeley Data, V1, doi:
for LA92 drive cycle. (b). SOC Coloumb Counting vs. SOC EKF Estimation 10.17632/b5mj79w5w9.1
with +/-0.1A current offset at 40°C for LA92 drive cycle. [22] Fauzia Khanum, Eduardo Louback, Federico Duperly, Colleen
Jenkins, Phillip Kollmeyer, Ali Emadi (2021). State of Charge
Estimation Function based on Kalman Filter. [Online]. Available:
https://www.mathworks.com/matlabcentral/fileexchange/90381-state-of-
[2] Electric Power Research Institute (EPRI), Enviromental Assessment of charge-estimation-function-based-on-kalman-filter.
Plug-In Hybrid Electric Vehicles, vol. 1, p. 46, 2007. [23] J. Gazzarri, ”Modeling Batteries Using Simulink
[3] A. Emadi, Advanced Electric Drive Vehicles, CRC Press, 2014. and Simscape,” Mathworks, [Online]. Available:
[4] M. Ismail, R. Dlyma, A. Elrakaybi, R. Ahmed and S. Habibi, ”Bat- https://www.mathworks.com/videos/modeling-batteries-using-simulink-
tery state of charge estimation using an Artificial Neural Network,” and-simscape-1562930245321.html. [Accessed 19 December 2020].
2017 IEEE Transportation Electrification Conference and Expo (ITEC), [24] The Idaho National Laboratory, ”Battery Test Manual for Plug-In Hybrid
Chicago, IL, USA, 2017, pp. 342-349. Electric Vehicles,” U.S. Department of Energy, Idaho Falls, Idaho, 2010.
[5] Jun Xu, Chunting Chris Mi, Binggang Cao and Junyi Cao, ”A new [25] Florian Knorn (2021). M-code LaTeX Package. [Online]. Avail-
method to estimate the state of charge of lithium-ion batteries based able: https://www.mathworks.com/matlabcentral/fileexchange/8015-m-
on the battery impedance model,” Journal of Power Sources, vol. 233, code-latex-package.
2013, pp. 277-284.
[6] A. Nugroho, E. Rijanto, F. D. Wijaya and P. Nugroho, ”Battery state
of charge estimation by using a combination of Coulomb Counting and
dynamic model with adjusted gain,” 2015 International Conference on
Sustainable Energy Engineering and Application (ICSEEA), Bandung,
Indonesia, 2015, pp. 54-58.
[7] I. Baccouche, S. Jemmali, B. Manai, R. Chaibi and N. E. Ben Amara,
”Hardware implementation of an algorithm based on kalman filtrer
for monitoring low capacity Li-ion batteries,” 2016 7th International
Renewable Energy Congress (IREC), Hammamet, Tunisia, 2016.
[8] I. Jokić, Ž. Zečević and B. Krstajić, ”State-of-charge estimation of
lithium-ion batteries using extended Kalman filter and unscented Kalman
filter,” 2018 23rd International Scientific-Professional Conference on
Information Technology (IT), Zabljak, Montenegro, 2018, pp. 1-4, doi:
10.1109/SPIT.2018.8350462.
[9] V. Sangwan, R. Kumar and A. K. Rathore, ”State-of-charge estimation
for li-ion battery using extended Kalman filter (EKF) and central
difference Kalman filter (CDKF),” 2017 IEEE Industry Applications
Society Annual Meeting, Cincinnati, OH, USA, 2017, pp. 1-6, doi:
10.1109/IAS.2017.8101722.
[10] L. Haoran, L. Liangdong, Z. Xiaoyin and S. Mingxuan, ”Lithium
Battery SOC Estimation Based on Extended Kalman Filtering Algo-
rithm,” 2018 IEEE 4th International Conference on Control Science and
Systems Engineering (ICCSSE), Wuhan, China, 2018, pp. 231-235, doi:
10.1109/CCSSE.2018.8724766.
[11] D. Yang, G. Qi and X. Li, ”State-of-charge estimation of LiFePO4/C
battery based on extended Kalman filter,” 2013 IEEE PES Asia-Pacific
Power and Energy Engineering Conference (APPEEC), Hong Kong,
China, 2013, pp. 1-5, doi: 10.1109/APPEEC.2013.6837188.
[12] Chirag (2021). Design and Test Lithium Ion Bat-
tery Management Algorithms. [Online]. Available:
https://www.mathworks.com/matlabcentral/fileexchange/72865-design-
and-test-lithium-ion-battery-management-algorithms. [Accessed: 2021].
[13] C. Vidal, O. Gross, R. Gu, P. Kollmeyer and A. Emadi, ”xEV Li-Ion
489
Authorized licensed use limited to: KLE Technological University. Downloaded on February 07,2022 at 08:51:50 UTC from IEEE Xplore. Restrictions apply.