A Practical Methodology for Real-Time Adjustment of Kalman Filter Process Noise for Lithium Battery State-of-Charge Estimation

: The methodology presented in this work allows for the creation of a real-time adjustment of Kalman Filter process noise for lithium battery state-of-charge estimation. This work innovates by creating a methodology for adjusting the process (Q) and measurement (R) Kalman Filter noise matrices in real-time. The filter algorithm with this adaptative mechanism achieved an average accuracy of 99.56% in real tests by comparing the estimated battery voltage and measured battery voltage. A cell-balancing strategy was also implemented, capable of guaranteeing the safety and efficiency of the battery pack in all conducted tests. This work presents all the methods, equations, and simulations necessary for the development of a battery management system and applies the system in a practical, real environment. The battery management system hardware and firmware were developed, evaluated, and validated on a battery pack with eight LiFePO 4 cells, achieving excellent performance on all conducted tests.


Introduction
The extended Kalman filter for state-of-charge estimation is one of the most used methods due to its accuracy and ability to be implemented in an embedded microprocessor environment.Furthermore, the Kalman filter algorithm for estimating the state of charge (SOC) can include the coulomb counting method and the open circuit voltage (OCV) method in its structure, making it very efficient for estimating a battery's SOC.This is a method that requires dynamic battery knowledge, making use of estimators, but that has many challenges in incorporating the effects of nonlinearities and other phenomena, as well as identifying and classifying the parameters of its constituent models.
One of the biggest challenges in Kalman filter implementation is defining the process and measurement noise matrices.These matrices can be defined in different ways and must consider the entire system in which it will be applied.Some works were found in the literature that used fixed values in the process and measurement noise matrices without an explanation regarding this definition, as well as an offline tuning procedure [1][2][3].However, if these matrices are defined in any way, the accuracy of the extended Kalman filter will be affected.A common way to describe the measurement noise covariance is using available physical sensors; the authors of [4] presented a dynamic perception of measurement noise, modeling the measurement noise as a Gaussian mixture model.Regarding the process noise, an optimization procedure is commonly carried out in a heuristic manner, that is, either by trial and error or by means of regulation metrics, such as the L curve [5].If Q k and R are exceedingly small, the uncertainty range will be small, resulting in a biased estimation, and if Q k and R are high, it may cause divergence in the filter [6,7].The extended Kalman filter considers that these matrices are constant; however, in some applications, it is necessary to consider that noise may vary under operating conditions.To improve the estimation accuracy, adaptive Kalman filters were developed with the aim of adjusting noise variances in real time [8,9].These strategies can be classified into four main categories [9], namely Bayesian [10], maximum-likelihood estimation [11], correlation [12][13][14][15], and covariance matching [16] methods.These approaches are typically characterized by excessive computational costs, as they describe generic adaptive filtering schemes.They are generally based on searching for the most suitable process and steadystate measurement noise estimates rather than their current ideal values [9].In [6], matrices Q k and R were updated in real time using the sliding-window method, considering the error between the model output value (h( xk )) and the measured value (h(x k )).During tests of the SOC estimator, this method proved capable of adapting to different temperature conditions and different cell states of health (SOHs), in addition to achieving greater precision and better robustness compared to other filtering algorithms.The authors of [17] presented a similar method; however, it uses a forgetting factor in conjunction with the sliding window.Simulations and experimental results showed that the algorithm proposed in [17] was able to estimate the SOC accurately, even when the statistical noise characteristic deviated from the nominal values.In [18], an approach was developed that also considers the error between the model output value (h( xk )) and the measured value (h(x k )) using the sliding-window method but updating the Q k matrix considering the Kalman gain (K), while the update of matrix R considers the covariance matrix (P k ).This approach achieved 43.34% higher SOC accuracy than the traditional approach and resulted in only a 4.59% increase in computational time.Several other strategies have been found for calculating the Q k and R matrices in real time; however, all the process noise approaches found in the literature are similar in considering the error between the model output value (h( xk )) and the measured value (h(x k )).The authors of [19] correlated the process noise end the measurement noise, and the system noise matrix was estimated and updated in real-time according to measurement data; the process noise is related to measurement noise form the frequency-domain perspective, and the measurement noise changes with time.Also correlating the process and measurement noise, in [20], variance component estimation was proposed by tuning the variance of process and observation noise together.A unified system model was constructed by a time window of multiple historical epochs.A noise with distribution equivalence strategy was proposed to make the unified system model a standard linear mixed-effects model.
A very interesting approach was proposed in [9] for adaptive process noise covariance matrix tuning for joint input-state estimation; in this paper, both shaping of states and unknown input uncertainties are considered for optimization.Moreover, the covariance matrix process noise is time-variant, since the modeling uncertainty and the dynamic characteristics of the input may vary over time.
In this paper, a measurement noise time-varying method is presented, and the modeling uncertainty is used to define the process noise, but unlike the strategies found in the literature, this work considers the uncertainty of each model parameter individually, bringing a practical understanding to the definition of process noise.

Objectives and Contributions
This work applies a methodology for developing the adaptive extended Kalman filter algorithm for state-of-charge estimation that is able to define the process noise and measurement noise matrices, considering the individual cell voltage and current measurement, in real time.A cell-balancing strategy was also developed, enabling safe and efficient system operation.To corroborate the results of this work, the system was applied to a real battery pack with a specially developed battery management system (in hardware and software) to test and verify the functionalities, validating the contributions of this work.To achieve this purpose, the work previously done in [21,22] was used as a basis for this work.Mathematical modeling and the parameter identification were presented in these previous works and used to achieve the results presented in this paper.

Paper Organization
Section 2 presents the basic concepts of the Kalman filter and the methodology for multidimensional models.Section 3 presents the state-of-charge methodology and the Kalman filter process noise methodology, which constitute the contribution of this paper.Section 4 presents the state-of-charge algorithm.Section 5 presents the algorithm validation, and Section 6 presents the conclusions.

Kalman Filtering Revisited
In a stricter context, the Kalman filter can be understood as a learning process that uses an estimation problem model that distinguishes between what is capable of being observed and what is happening.In this "learning process", the state of knowledge about what is really happening can be deduced from what is capable of being observed.This state of knowledge is characterized by probabilistic distributions, which represent real-world knowledge, while the cumulative processing of this knowledge is the learning process [23].
From the Kalman gain, it is possible to determine the measurements precision and the estimation accuracy.If the Kalman gain value is closer to 1, the algorithm will consider that the measurements are accurate and the model estimates are unstable; in this case, the algorithm will give greater weight to the measurements and less weight to the estimates.However, if the Kalman gain value is closer to 0, the algorithm will consider that the measurements are imprecise and the model estimates are stable; in this case, the algorithm will give greater weight to the model estimates and less weight to measurements.
For the Kalman filter to be carried out computationally and with different state variables, it is necessary to represent the dynamic system in the form of state-space equations according to Equations (1) and (2).
where: x k is the state vector, A is the state matrix, u k is the input vector, y k is the output vector, B is the input matrix, C is the output matrix, and D is the direct transmission matrix.
In this way, it is possible to represent a linear dynamic system mathematically and compare its estimated output with a measured output of that system to then estimate its state.Then, by comparing the actual output (y k ) and the estimated output ( ŷ k ), the Kalman gain (K) is calculated by adjusting the estimated state ( xk ) so that the estimated output ( ŷ k ) is as close as possible to the real output (y k ) and, consequently, the estimated state ( xk ) is as close as possible to the real state (x k ).Therefore, the mathematical model must be as close as possible to the real system for the estimated output to be close to the real output and the state estimation to be accurate.
To better understand the logic of the Kalman filter in a state space, it is important to know some concepts.If the measurement noise matrix (R) is close to 0, then the Kalman gain (K) will be close to 1.This means that the updated state (X k ) will be adjusted first by considering the updated measurement data (Y k ).Otherwise, the updated state (X k ) will be adjusted first considering the predicted state (X k p ).If the covariance matrix (P k ) is close to 0, then the state update (X k ) will be performed through the predicted state (X k p ), practically ignoring the measurement data (Y k ).
As seen in the literature, the Kalman filter is commonly applied to battery state-ofcharge estimation [1,6,24,25] because, through a mathematical model, it is possible to compare the estimated battery voltage with the actually measured battery voltage.It is also possible to estimate the battery state of charge without carrying out direct measurements in relation to its electrochemical states.Furthermore, despite involving matrix calculations, depending on the model order, implementing the filter is viable and computationally possible.Therefore, the Kalman filter can result in good accuracy and reliable performance when used in BMS-embedded electronic systems.However, its application requires modifications due to nonlinearities (such as the use of extended filter techniques), in addition to procedures for inference (identification), parameterization, model adjustment, covariances, and initial estimates, which are discussed later in this work.

State-of-Charge Estimation Methodology
In this work, the extended Kalman filter presented in Section 2 is used to estimate the battery charge state.The model presented in [22] must be applied to the filter; however, for this, its conversion to the discrete time domain is necessary.The model in the discrete state space is represented by Equations ( 3) and (4).
where matrices A d , B d , C d , and D d are matrices A, B, C, and D of the system in the discrete sample space.These matrices, in continuous time, were presented in [22].To discretize the model, the backward Euler method was chosen, as it results in very simple and elementary arithmetic to represent numerical integration.The discrete extended Kalman filter system is shown in Equations ( 5) and ( 6).
where x k is the system state variable; y k is the system observation variable; u k is the system input; f is the nonlinear state function; h is the nonlinear measurement function; and w and v are the white noise Gaussians with mean 0 and covariance for Q k and R k , respectively.The extended Kalman filter uses Taylor series expansion to perform a linear approximation of a nonlinear function, which, in this case, is found in the total battery voltage equation, that is, at the output of the battery system (y 1 ) presented in [22].To define the total battery voltage, it is necessary to determine the battery's open-circuit voltage (V OCV ), which has a nonlinear relationship with the battery's state of charge (x 2 ).In this context, Taylor series expansion must be performed on the battery open circuit voltage equation, which can be represented by a polynomial of order n, as described in [21].In this way, fixed nominal trajectory linearization is performed.
The battery nonlinearities are approximated using linearization through Taylor series expansion, which is represented by matrices F and H.
Matrix F is a Jacobian matrix of partial derivatives of f , where f = ( Matrix F has a relationship with the previous states (x k−1 ) and the entry (u k ) and is expressed as follows: Therefore, matrix F is expressed as follows: Batteries 2024, 10, 233 5 of 20 To solve Equation (15), discretization using the Euler method is performed considering the states presented in Equations ( 9) and (10).This equation approach was detailed in [21]. .
x 2 (t) = f 2 (x 2 ).Matrix H is a Jacobian matrix of partial derivatives of h, where h = (h 1 (y), h 2 (y), • • • , h n (y)) T and y = (y 1 , y 2 , • • • , y n ) T .Matrix H is related to the states of the system and is expressed as follows: Therefore, matrix H is expressed as follows: where h 1 = y 1 (t) is expressed as follows: Taylor series expansion for the SOC-OCV relationship must be carried out to the necessary order.In this case, using piecewise linearized functions, as presented in [21], the approximation to the first order is sufficient, that is, where x 2 is the SOC.Therefore, the output equation becomes One of the biggest challenges in implementing the Kalman filter is defining the measurement noise (R) and process noise (Q k ) matrices.

Kalman Filter Process Noise Methodology
The definitions of process and measurement noise matrices are very important for the performance and accuracy of the Kalman Filter.The process noise matrix (Q k ) represents the entire mathematical noise variance inherent to the system (including approximations and truncations), and the measurement noise matrix (R) represents the system measurement noise variance.
In this work, the system measurement noise considers current and voltage noise.To calculate voltage measurement noise variance, voltage sensor accuracy is considered, together with noise variance due to EMI (Electromagnetic Interference)/EMC (Electromagnetic Compatibility) of conditioning circuits.This calculation is performed every three iterations of the algorithm.In this context, Equation ( 16) represents the voltage measurement noise variance.
where var r is the variance of the voltage measurement noise calculated every three iterations of the algorithm, and prec sens is the voltage measurement accuracy of the sensor reported by the manufacturer.
Thus, we have the measurement noise covariance matrix, as represented by Equation (17).
In an equivalent way, for current measurement, Equation ( 18) is obtained for the measurement noise variance.
where var r I is the current measurement noise variance calculated every three iterations of the algorithm, and prec sens I is the current sensor accuracy, as reported by the manufacturer.However, the algorithm must be able to differentiate between what is actually noise and what is just a change or a transient signal in the voltage or current amplitude.For this purpose, the standard variance (observed under steady-state operation) of the measurement noise was calculated through a laboratory test.In the embedded algorithm, the variance is calculated every three interactions; if this variance is greater than the standard variance, the algorithm ignores this value, as it represents a variation in the amplitude of the voltage or current and does not truly represent noise.
The process noise covariance matrix (Q k ) is calculated considering the data flow itself as a process to identify the parameters, where each one has its standard deviation, that is, its uncertainty.In this context, the process noise covariance matrix is represented by Equation ( 19), where the first term of the matrix is related to the derivative of the first state of the system ( .V RC = .
x 1 ) and the second term of the matrix is related to the derivative of the second state of the system ( .SOC = . x 2 ).
The calculation of Equation ( 19) is conducted based on the uncertainty combination of the parameters in Equations ( 20) and ( 21). . .
The uncertainty combination calculation must consider the algebraic relationship between the quantities and whether they are statistically independent or correlated; this methodology is presented in [26], and more detail is presented in [27].
To calculate the uncertainty combination, we consider that all variables are correlated.
To calculate the uncertainty of Equation ( 20), we have the following: where: u(R 1 ) is the variance of parameter R 1 , u(C 1 ) is the variance of parameter C 1 , u(I) is the variance of the current noise determined in Equation ( 18), and u(V RC ) is the variance of the calculation of voltage (V RC ) determined in Equation (26).Solving Equation ( 22), we have the following sequence of equations: By analyzing the equivalent circuit model presented in [27], the RC circuit voltage is calculated by Equation (25).
where V T is the total battery voltage.Therefore, the RC circuit voltage variance presented in Equation ( 35) is calculated according to Equation (39).
Next, using Equations ( 23), (24), and ( 26), it is possible to calculate Equation (22).And to calculate the uncertainty in Equation ( 21), we have: where u(Cn) is the variance of the Cn parameter, and u(I) is the variance of the current noise described by Equation (18).Solving Equation ( 27), we have the following sequence of equations.
Therefore, using Equations ( 23), ( 24), (26), and (28), it is possible to determine the process covariance matrix (Q k ) given by Equation (19).It is important to highlight that this approach is dependent on the accuracy of the battery's mathematical model parameters, in addition to the current and voltage measurement noise variance.Therefore, this definition of the Q k and R matrices must be associated with the model parameter identification approach.
This approach can also be used as a self-diagnosis of the BMS itself, indicating possible hardware problems in the cells, in signal conditioning, in analog filtering, etc.Since this method is linked to the real-time current measurement noise and voltage measurement noise, at times, when there are abrupt changes in any of their coefficients, it can be determined whether a serious failure is in progress, allowing protection and security measures to be taken.

State-of-Charge Estimation Algorithm
In this paper, all the tests were conducted with the same battery cell used in [21,22], which is a cylindrical LiFePO 4 battery with 6 Ah of capacity.The current profile used in this work is the same as that presented in detail in [21], which represents the use of an electric forklift, as an application used for practical validation.
The state of charge was estimated using the adaptive extended Kalman filter according to the methodology presented in Section 3. To apply the extended Kalman filter, it is necessary to discretize the model in the state space in continuous time, as presented in [22].Thus, applying discretization, it is possible to obtain the following model matrices in the domain of samples.It should be noted that a sampling time (T s ) of 10 ms was used.
Batteries 2024, 10, 233 8 of 20 Matrices F and H are Jacobian matrices necessary for the discretization of the system using the Euler method, with linearization through Taylor series expansion.In this way, by applying Equations ( 7)- (15) as detailed in Section 3, it is possible to obtain the following Jacobian matrices.
A real oscillography record obtained with the BMS hardware allows for observation of the battery voltage measurement noise, as shown in Figure 1, where a noise characteristic of a digitization and quantization process is noted, in addition to a slightly decreasing behavior, since the battery is in the process of discharging at that moment.Only the noise resulting from the conditioning, analog filtering, and quantization process is of interest in this analysis.Matrices  and  are Jacobian matrices necessary for the discretization of the system using the Euler method, with linearization through Taylor series expansion.In this way, by applying Equations ( 7)- (15) as detailed in Section 3, it is possible to obtain the following Jacobian matrices.A real oscillography record obtained with the BMS hardware allows for observation of the battery voltage measurement noise, as shown in Figure 1, where a noise characteristic of a digitization and quantization process is noted, in addition to a slightly decreasing behavior, since the battery is in the process of discharging at that moment.Only the noise resulting from the conditioning, analog filtering, and quantization process is of interest in this analysis.In another record, it is possible to observe the battery current measurement noise, as shown in Figure 2, where, in addition to noises related to conditioning, filtering, and quantization, other components and influences on the measured signal are noted.It is important to emphasize that the battery was subjected, on this occasion, to a constant current discharge regime, which comes from the BMS's own consumption.In another record, it is possible to observe the battery current measurement noise, as shown in Figure 2, where, in addition to noises related to conditioning, filtering, and quantization, other components and influences on the measured signal are noted.It is important to emphasize that the battery was subjected, on this occasion, to a constant current discharge regime, which comes from the BMS's own consumption.The covariances of voltage () and current () measurement noise, calculated through the analysis of Figures 1 and 2, are presented in Equations ( 35) and (36), respectively; however, it is important to highlight that these values are updated every three soft- The covariances of voltage (uV) and current (uI) measurement noise, calculated through the analysis of Figures 1 and 2, are presented in Equations ( 35) and (36), respectively; however, it is important to highlight that these values are updated every three software iterations (30.0 ms) for better result accuracy.To validate the extended Kalman filter algorithm in a computational environment, the "30 s high 30 s low" experiment described in [21] was used, as this represents an average use condition of a forklift, in addition to being sufficient to validate the algorithm before applying it to the BMS for validation in a real environment.In this case, an initial estimation error with a standard deviation of 33.33% (or 2 V, considering the cell used) was inserted for the voltage (V RC , (x 1 )), and an initial estimation error with a standard deviation of 10% was considered for the SOC (x 2 ).The initial values were set quite high, but they must be corrected by the algorithm itself at run time, converging to the observed values for these uncertainties.Therefore, the initial covariance matrix is presented in Equation (37).

𝐵
The values of Equation (37) are defined through the variance of each parameter, which is calculated by Equation (38).
where var is the variance of the parameter, and dp is the standard deviation of the parameter.
In the initial state matrix, 0 V was admitted for the voltage V RC , (x 1 ), and 100% was considered for the SOC (x 2 ).Thus, we have the initial state matrix presented in Equation (39).
The adaptive extended Kalman filter algorithm was created as presented in Section 3. Figure 3 presents a summary of calculations necessary for its execution.Where the measurements were presented in blue and the outputs were presented in green.
The result of the coefficients of the covariance matrix (P k p and P k ) related to state x 1 over time is presented in Figure 4.
Figure 4 shows the estimated covariance P k p in blue and the updated covariance (P k ) in red.In this Figure, it is possible to conclude that the two covariances (estimated and updated) have a very similar characteristic, with an average value of around 1.09 * 10 −4 .The covariance matrices (P k p and P k ) related to state x 2 are shown in Figure 5.
Analyzing Figure 5, it is also possible to conclude that the two covariances (estimated and updated) have similar characteristics, with an average value of around 1.0 * 10 −5 .To facilitate visualization of the difference between the two signals, Figure 6 presents an enlarged view.
Analyzing Figure 6, it is possible to verify that the updated covariance is always smaller than the estimated covariance, that is, the error in estimating the SOC state decreases throughout the algorithm's execution.Furthermore, the covariance is low-on the order of 2 * 10 −5 -which indicates that the error in estimating the state of charge is small, as desired for an estimator under these conditions.
The Kalman gain matrix (K) related to state x 1 is shown in Figure 7.The result of the coefficients of the covariance matrix ( and  ) related to state  over time is presented in Figure 4.The result of the coefficients of the covariance matrix ( and  ) related to state  over time is presented in Figure 4.The Kalman gain matrix (K) related to state x 2 is shown in Figure 8. Analyzing Figure 8, it is possible to observe that the greatest gains occur at the beginning and end of the test, when the battery presents greater nonlinearity.At these points, the Kalman filter give the battery voltage measurement greater weight in estimating the state.However, the central points of the test indicate that the Kalman filter gives the estimate of the previous state more weight in the estimation of the current state than the measurement of the battery voltage, as these are the points of lowest nonlinearity of the battery.Analyzing Figure 5, it is also possible to conclude that the two covariances (estimated and updated) have similar characteristics, with an average value of around 1.0 * 10 .To facilitate visualization of the difference between the two signals, Figure 6 presents an enlarged view.Analyzing Figure 6, it is possible to verify that the updated covariance is always smaller than the estimated covariance, that is, the error in estimating the SOC state decreases throughout the algorithm's execution.Furthermore, the covariance is low-on the order of 2 * 10 -which indicates that the error in estimating the state of charge is small, as desired for an estimator under these conditions.
The Kalman gain matrix  related to state  is shown in Figure 7.  Analyzing Figure 5, it is also possible to conclude that the two covariances (estimated and updated) have similar characteristics, with an average value of around 1.0 * 10 .To facilitate visualization of the difference between the two signals, Figure 6 presents an enlarged view.Analyzing Figure 6, it is possible to verify that the updated covariance is always smaller than the estimated covariance, that is, the error in estimating the SOC state decreases throughout the algorithm's execution.Furthermore, the covariance is low-on the order of 2 * 10 -which indicates that the error in estimating the state of charge is small, as desired for an estimator under these conditions.
The Kalman gain matrix  related to state  is shown in Figure 7.The Kalman gain matrix  related to state  is shown in Figure 8.The estimated battery voltage (through this mathematical model) and the measured voltage are presented in Figure 9.The Kalman filter algorithm makes a comparison between the two values to estimate the battery states (SOC and V RC ).The Kalman gain matrix  related to state  is shown in Figure 8. Analyzing Figure 8, it is possible to observe that the greatest gains occur at the beginning and end of the test, when the battery presents greater nonlinearity.At these points, the Kalman filter give the battery voltage measurement greater weight in estimating the state.However, the central points of the test indicate that the Kalman filter gives the estimate of the previous state more weight in the estimation of the current state than the measurement of the battery voltage, as these are the points of lowest nonlinearity of the battery.
The estimated battery voltage (through this mathematical model) and the measured voltage are presented in Figure 9.The Kalman filter algorithm makes a comparison between the two values to estimate the battery states ( and  ).Analyzing Figure 9, it is possible to observe that the measured and calculated values are remarkably similar.
The percentage difference between the measured voltage and the estimated voltage is a maximum of 5.4% at the point of greatest nonlinearity of the cell, as shown in Figure 10.However, on average, the percentage difference is 0.44%, achieving an accuracy of 99.56%, on average.The difference between the measured voltage and the estimated voltage is shown in Figure 11 and is used in the filter algorithm to calculate the Kalman gain.Analyzing Figure 9, it is possible to observe that the measured and calculated values are remarkably similar.
The percentage difference between the measured voltage and the estimated voltage is a maximum of 5.4% at the point of greatest nonlinearity of the cell, as shown in Figure 10.However, on average, the percentage difference is 0.44%, achieving an accuracy of 99.56%, on average.Analyzing Figure 9, it is possible to observe that the measured and calculated values are remarkably similar.
The percentage difference between the measured voltage and the estimated voltage is a maximum of 5.4% at the point of greatest nonlinearity of the cell, as shown in Figure 10.However, on average, the percentage difference is 0.44%, achieving an accuracy of 99.56%, on average.The difference between the measured voltage and the estimated voltage is shown in Figure 11 and is used in the filter algorithm to calculate the Kalman gain.The difference between the measured voltage and the estimated voltage is shown in Figure 11 and is used in the filter algorithm to calculate the Kalman gain.The difference between the measured voltage and the estimated voltage is shown in Figure 11 and is used in the filter algorithm to calculate the Kalman gain.In Figure 11, it is possible to observe the initial difference (initial delta) in blue, which is used to calculate the Kalman gain, and the final difference (final delta) in red, which is calculated at the end of each iteration of the algorithm.It is known that the initial difference must always be greater than the final difference.The largest final difference was 0.10 V, with an average of −0.01 V.The results and information presented in Figures 9-11 are capable of completely validating the mathematical model used to describe the battery dynamics, presenting an average difference of just 0.44% between the measured voltage and the voltage estimated by the model.The voltage in the RC circuit (V RC ), represented by state x 1 and calculated at the end of the Kalman filter algorithm, is shown in Figure 12.
Batteries 2024, 10, x FOR PEER REVIEW 14 of 21 In Figure 11, it is possible to observe the initial difference (initial delta) in blue, which is used to calculate the Kalman gain, and the final difference (final delta) in red, which is calculated at the end of each iteration of the algorithm.It is known that the initial difference must always be greater than the final difference.The largest final difference was 0.10 V, with an average of −0.01 V.The results and information presented in   The state of charge of the battery was validated by making a comparison between the state of charge calculated using the coulomb counting algorithm (SOC CC BMS) implemented in the BMS and the state of charge estimated using the extended Kalman filter algorithm (SOC Kalman).Therefore, the state of charge estimated by both is presented in Figure 13. Figure 12 presents the "measured" voltage (V RC , blue), which is calculated using Equation (38), and the calculated voltage (V RC , red), which is calculated at the end of the Kalman algorithm.It is possible to observe that the two values are remarkably close and have similar behavior, validating the Kalman algorithm and the battery model.
The state of charge of the battery was validated by making a comparison between the state of charge calculated using the coulomb counting algorithm (SOC CC BMS) implemented in the BMS and the state of charge estimated using the extended Kalman filter algorithm (SOC Kalman).Therefore, the state of charge estimated by both is presented in Figure 13.
have similar behavior, validating the Kalman algorithm and the battery model.
The state of charge of the battery was validated by making a comparison between the state of charge calculated using the coulomb counting algorithm (SOC CC BMS) implemented in the BMS and the state of charge estimated using the extended Kalman filter algorithm (SOC Kalman).Therefore, the state of charge estimated by both is presented in Figure 13.The result presented in Figure 13 indicates that the Kalman filter algorithm was successfully implemented and fully validated in the computational environment with digital simulations.It is important to highlight that the state of charge estimated by the Kalman The result presented in Figure 13 indicates that the Kalman filter algorithm was successfully implemented and fully validated in the computational environment with digital simulations.It is important to highlight that the state of charge estimated by the Kalman Filter algorithm has a more coherent final value than the coulomb counting algorithm, as in the test carried out in this study, the cell was discharged until the end, and the SOC should present a value of 0%; however, the final value presented by the coulomb counting algorithm was 1.56% SOC, and the final value presented by the Kalman filter algorithm was 0.59% SOC.
Therefore, the accuracy of the algorithm was calculated according to the difference between the voltage measured through the sensor and the voltage estimated through the mathematical model inserted in the adaptive Kalman filter algorithm; this percentage difference is presented in Figure 10.Evaluation of the algorithm's accuracy was carried out in this way, since the measured battery voltage is the only information taken from a sensor; therefore, if the accuracy evaluation is carried out in comparison with the state of charge estimated by the coulomb counting algorithm, the result will be inaccurate because coulomb counting is also an estimator and accumulates errors during its execution.

Algorithm Validation
The entire state-of-charge state estimation algorithm was properly embedded in a BMS specially designed for this work.In addition to several auxiliary components, the BMS hardware is based on the following components: • An integrated circuit called AFE (analogic front end) whose main function is to measure individual cell voltages; • A microcontroller capable of receiving and sending information to the AFE, controlling and configuring all necessary parameters, and executing the algorithms developed for all desired functionalities; • A current sensor with adequate accuracy.
The complete printed circuit board for this hardware is shown in Figure 14.With this hardware and a set of eight lithium cells connected in series, it was possible to validate the mathematical battery model presented in [22] and the state-of-charge estimation algorithm presented in this paper.The results of this implementation, testing, and validation are presented in the next sections.


A microcontroller capable of receiving and sending information to the AFE, controlling and configuring all necessary parameters, and executing the algorithms developed for all desired functionalities;  A current sensor with adequate accuracy.
The complete printed circuit board for this hardware is shown in Figure 14.With this hardware and a set of eight lithium cells connected in series, it was possible to validate the mathematical battery model presented in [22] and the state-of-charge estimation algorithm presented in this paper.The results of this implementation, testing, and validation are presented in the next sections.To fully validate the algorithm, it was implemented on real BMS hardware.Figure 15 presents a comparison between the algorithm executed in a simulation environment (MATLAB) and in the embedded environment.In the embedded environment, C language was used, with double precision floating point arithmetic and all routines developed in an authorial manner, including all calculations with matrices and vectors.In this way, it was possible to avoid ready-made routines with functions that are often unused, which would result in large unnecessary computational costs.This figure shows the SOC outputs of both implementations.To fully validate the algorithm, it was implemented on real BMS hardware.Figure 15 presents a comparison between the algorithm executed in a simulation environment (MATLAB) and in the embedded environment.In the embedded environment, C language was used, with double precision floating point arithmetic and all routines developed in an authorial manner, including all calculations with matrices and vectors.In this way, it was possible to avoid ready-made routines with functions that are often unused, which would result in large unnecessary computational costs.This figure shows the SOC outputs of both implementations.To validate the algorithm, the battery current data were artificially inserted into the embedded code using an external computer, which sent the synchronized data to the BMS so that it was possible to insert a large amount of information without resulting in extra computational time for the BMS.The entered data were the same as those used in the simulation environment (MATLAB), thus guaranteeing the same inputs for both algorithms, hoping to produce the same outputs.Analyzing Figure 15, it is possible to observe that the two signals are the same; thus, it was possible to completely validate the implementation of the Kalman filter algorithm embedded in the BMS.
Figure 16 shows the time that the BMS needs to perform a complete cycle of the algorithm and estimate the cell's charge state.Computational time is the high-logic period shown in the square wave.To validate the algorithm, the battery current data were artificially inserted into the embedded code using an external computer, which sent the synchronized data to the BMS so that it was possible to insert a large amount of information without resulting in extra computational time for the BMS.The entered data were the same as those used in the simulation environment (MATLAB), thus guaranteeing the same inputs for both algorithms, hoping to produce the same outputs.Analyzing Figure 15, it is possible to observe that the two signals are the same; thus, it was possible to completely validate the implementation of the Kalman filter algorithm embedded in the BMS.
Figure 16 shows the time that the BMS needs to perform a complete cycle of the algorithm and estimate the cell's charge state.Computational time is the high-logic period shown in the square wave.
embedded code using an external computer, which sent the synchronized data to the BMS so that it was possible to insert a large amount of information without resulting in extra computational time for the BMS.The entered data were the same as those used in the simulation environment (MATLAB), thus guaranteeing the same inputs for both algorithms, hoping to produce the same outputs.Analyzing Figure 15, it is possible to observe that the two signals are the same; thus, it was possible to completely validate the implementation of the Kalman filter algorithm embedded in the BMS.
Figure 16 shows the time that the BMS needs to perform a complete cycle of the algorithm and estimate the cell's charge state.Computational time is the high-logic period shown in the square wave.As shown in Figure 16, the time required for a complete cycle of the algorithm was 180 µs, using a microcontroller clocked at 240 MHz.This algorithm is the complete extended Kalman filter, including the implementation of the adaptive strategy for the readjustment of process noise and measurement noise.This adaptive strategy alone corresponds to approximately 30% of this computational time.In any case, the result of 180 µs is enough for the microcontroller to be able to calculate the state of charge of each cell in a set of several cells in series, guaranteeing enough time to implement all other necessary BMS functions.
It is also important to highlight that this specific filter algorithm can be heavily optimized in the future using simpler arithmetic (single precision, fixed point, or integer arithmetic), with the ability to be executed on smaller microcontrollers and DSPs (digital signal processors), achievinglower costs.It is also possible to envisage the creation of an ASIC (application-specific integrated circuit) for this purpose, synthesizing hardware dedicated to this function.
The charge state estimation algorithm was validated on a real set of eight cells in series.However, for the charge and discharge tests of this set of cells to be conducted safely, it was necessary to develop a balancing algorithm.In this context, a simple algorithm was created that consists of recognizing which cell has the highest voltage and activating an FET-type transistor connected in parallel to that cell, discharging its energy into a resistor.This balancing scheme, known as "dissipative passive", is carried out in the cell until another cell becomes the cell with the highest voltage.An adjustable hysteresis band is used in the voltage classifier so that the balancing process is only activated if a certain voltage level is detected in a cell.Through this strategy, it is possible to guarantee a minimum balance of all cells during algorithm validation tests.
Algorithm validation was carried out through a complete charge and discharge cycle in the eight cells, as shown in Figures 17 and 18.
Analyzing Figure 18, it is possible to verify the moment of cell balancing.Balancing begins in the charge cycle, close to the voltage of 3.4 V, as this is the moment when the difference in voltage between the cells is the greatest.During balancing, it is possible to visualize the change in a cell's voltage value because when one cell is balanced, its voltage decreases and another becomes the cell with the highest voltage.
In this context, it is possible to understand that the state of charge is directly affected by balancing, as the energy of the cells changes at these moments.The state of charge of each cell during the validation test is shown in Figure 19.
until another cell becomes the cell with the highest voltage.An adjustable hysteresis band is used in the voltage classifier so that the balancing process is only activated if a certain voltage level is detected in a cell.Through this strategy, it is possible to guarantee a minimum balance of all cells during algorithm validation tests.
Algorithm validation was carried out through a complete charge and discharge cycle in the eight cells, as shown in Figures 17 and 18.  Analyzing Figure 18, it is possible to verify the moment of cell balancing.Balancing begins in the charge cycle, close to the voltage of 3.4 V, as this is the moment when th difference in voltage between the cells is the greatest.During balancing, it is possible to visualize the change in a cell's voltage value because when one cell is balanced, its voltag decreases and another becomes the cell with the highest voltage.
In this context, it is possible to understand that the state of charge is directly affected by balancing, as the energy of the cells changes at these moments.The state of charge o each cell during the validation test is shown in Figure 19.Analyzing Figure 18, it is possible to verify the moment of cell balancing.begins in the charge cycle, close to the voltage of 3.4 V, as this is the moment difference in voltage between the cells is the greatest.During balancing, it is p visualize the change in a cell's voltage value because when one cell is balanced, i decreases and another becomes the cell with the highest voltage.
In this context, it is possible to understand that the state of charge is directl by balancing, as the energy of the cells changes at these moments.The state of each cell during the validation test is shown in Figure 19.It is possible to notice that at the end of the validation test, the individu charge is different; this is due to the type of balancing used.Through a better strategy, it is possible to minimize this difference and ensure that all cells have state of charge at the end of a charging cycle.Table 1 presents the SOC of each ce total SOC of the battery at the end of the validation test.It is possible to notice that at the end of the validation test, the individual state of charge is different; this is due to the type of balancing used.Through a better balancing strategy, it is possible to minimize this difference and ensure that all cells have the same state of charge at the end of a charging cycle.Table 1 presents the SOC of each cell and the total SOC of the battery at the end of the validation test.The battery's total state of charge is calculated considering the average of the individual states of charge, resulting in a value of 98.02%.Analyzing Table 1, it is possible to observe that all SOCs are remarkably close; in this case, the calculated standard deviation was only 0.92%.
Based on what has been presented, the state-of-charge algorithm can be considered validated, as it is capable of estimating the state of charge of each cell individually, considering the energy exchange between them at balancing moments.With this, it is possible to evaluate the state-of-charge algorithm embedded in hardware, reading several different cells, and responding appropriately to each of them.In this case, balancing was used to validate the state-of-charge algorithm for different situations at the same time.
Through this test, it was possible to commission the BMS hardware and firmware, which operated properly, corroborating the choices adopted for this research and its development, including the functioning of the mathematical model and the adaptive extended Kalman filter algorithm for state-of-charge estimation.

Conclusions
The adaptive extended Kalman filter algorithm was developed using a different strategy with real-time process noise and measurement noise matrices.These matrices are calculated in real time by the algorithm using battery voltage and current measurements.The process noise matrix uses the parameters' standard deviation identified through the mathematical model.Combining the parameter identification strategy with the adaptability of the process noise matrix, the state-of-charge estimation algorithm can achieve an average accuracy of 99.56%.
The presented strategy uses the uncertainty combination method, which can simplify the understanding of the process noise matrix, considering that it is composed of the modeling process itself and model parameter identification.Without computational optimizations, this strategy adds 30% processing time to the developed BMS and performs a complete iteration in 180 µs, in addition to being able to consider all system uncertainties together with the battery current and voltage measurement noises calculated in real time, becoming a robust and reliable algorithm.The advantages of the method presented in this paper are the low computational cost for an embedded system and the correlation of noise with uncertainty in identifying parameters.Furthermore, it is a physically easy method to understand, as process noise directly depends on the uncertainties in the method of identifying model parameters.It is important to emphasize that these process and measurement noise matrices can be used, in the future, for self-diagnosis of the BMS itself, indicating incipient hardware problems (in cells, analog conditioning, filtering, etc.) at times when there are abrupt changes in their coefficients.On such an occasion, it can determined be whether any serious failure is in progress, allowing protection and safety actions and measures to be implemented.This should be investigated in future work, as it also addresses advanced monitoring of the battery and its health status.
The BMS hardware and firmware were developed to guarantee the operation of all tasks and functionalities in real time.The battery charge state estimation algorithm was duly embedded in the BMS and validated on a battery pack with eight cells in series.For the tests on this battery pack to be carried out ensuring the integrity of the cells, a simple balancing strategy was developed.With this approach, it was possible to evaluate the

Figure 4
Figure 4 shows the estimated covariance  in blue and the updated covariance  in red.In this Figure, it is possible to conclude that the two covariances (estimated and updated) have a very similar characteristic, with an average value of around 1.09 * 10 .The covariance matrices ( and  ) related to state  are shown in Figure 5.

Figure 4
Figure 4 shows the estimated covariance  in blue and the updated covarianc  in red.In this Figure, it is possible to conclude that the two covariances (estimated and updated) have a very similar characteristic, with an average value of around 1.09 10 .The covariance matrices ( and  ) related to state  are shown in Figure 5.

Figure 6 .
Figure 6.Estimated and updated covariance, related to state   -extended signal.

Figure 6 .
Figure 6.Estimated and updated covariance, related to state   -extended signal.

Figure 6 .
Figure 6.Estimated and updated covariance, related to state x 2 (SOC)-extended signal.Batteries 2024, 10, x FOR PEER REVIEW 12 of 21

Figure 10 .
Figure 10.Percentage difference between measured and calculated voltage.

Figure 9 .
Figure 9. Measured and estimated total voltage.

Figure 10 .
Figure 10.Percentage difference between measured and calculated voltage.

Figure 10 .
Figure 10.Percentage difference between measured and calculated voltage.

Figure 10 .
Figure 10.Percentage difference between measured and calculated voltage.

Figure 11 .
Figure 11.Difference between measured voltage and estimated voltage.Figure 11.Difference between measured voltage and estimated voltage.

Figure 11 .
Figure 11.Difference between measured voltage and estimated voltage.Figure 11.Difference between measured voltage and estimated voltage.
Figures 9-11 are capable of completely validating the mathematical model used to describe the battery dynamics, presenting an average difference of just 0.44% between the measured voltage and the voltage estimated by the model.The voltage in the RC circuit  , represented by state  and calculated at the end of the Kalman filter algorithm, is shown in Figure 12.

Figure 12
Figure12presents the "measured" voltage ( , blue), which is calculated using Equation (38), and the calculated voltage ( , red), which is calculated at the end of the Kalman algorithm.It is possible to observe that the two values are remarkably close and have similar behavior, validating the Kalman algorithm and the battery model.The state of charge of the battery was validated by making a comparison between the state of charge calculated using the coulomb counting algorithm (SOC CC BMS) implemented in the BMS and the state of charge estimated using the extended Kalman filter algorithm (SOC Kalman).Therefore, the state of charge estimated by both is presented in Figure13.

Figure 13 .
Figure 13.State of charge calculated by the coulomb counting algorithm and state of charge estimated by the Kalman algorithm.

Figure 13 .
Figure 13.State of charge calculated by the coulomb counting algorithm and state of charge estimated by the Kalman algorithm.

Figure 15 .
Figure 15.Comparison between the algorithm executed with computer simulation (red) and the algorithm embedded in the BMS (blue).

Figure 15 .
Figure 15.Comparison between the algorithm executed with computer simulation (red) and the algorithm embedded in the BMS (blue).

Figure 16 .
Figure 16.Computational time of the algorithm in the BMS.Figure 16.Computational time of the algorithm in the BMS.

Figure 16 .
Figure 16.Computational time of the algorithm in the BMS.Figure 16.Computational time of the algorithm in the BMS.

Figure 17 .
Figure 17.Current used in the complete charge and discharge cycle test.Figure 17.Current used in the complete charge and discharge cycle test.

Figure 17 . 2 Figure 18 .
Figure 17.Current used in the complete charge and discharge cycle test.Figure 17.Current used in the complete charge and discharge cycle test.Batteries 2024, 10, x FOR PEER REVIEW 18 of 2

Figure 19 .
Figure 19.State of charge during the complete battery charge and discharge cycle.

Figure 18 .Figure 18 .
Figure 18.Cell voltage in the full cycle test.

Figure 19 .
Figure 19.State of charge during the complete battery charge and discharge cycle.

Figure 19 .
Figure 19.State of charge during the complete battery charge and discharge cycle.

Table 1 .
Total state of charge and states of charge of individual cells.

Table 1 .
Total state of charge and states of charge of individual cells.