Back

Transforming LiFePO4 Battery Management System: Hybrid CNN-GRU-TCN for Accurate SOC Estimation

By Xbattery Engineering Team
September 30, 2024
#Deep learning#SOC estimation#Lithium Iron Phosphate Batteries
blog3 her01

When you see the battery percentage on your phone or other devices, that number tells you how much charge is left in the battery. Technically, this is called State of Charge (SOC) estimation. For example, if your battery shows 60%, it means 60% of the charge is left and 40% has been used.

SOC cannot be directly measured like we measure fuel in the fuel tank. But it can be estimated with other parameters like voltage, current, and temperature. It shows indirect relationships with these parameters, technically called measurable parameters or signals. As the measurable parameters show complex non-linear characteristics, it makes the estimation with accuracy a bit of a challenging task.

To check the power left in the battery, stopping it from getting hot and helping it to last long, there is a battery helper called Battery Management System (BMS).

Why is SOC important?  

If SOC is not estimated accurately, then the BMS can’t function properly. This led to poor operating conditions for the batteries. Let’s say if you are charging a device that has 100 percent charged but due to incorrect SOC estimation it shows 90 percent, then we continue to charge the device, which heats up gradually and results in an explosion or may reduce the life of the battery.

There are different methods that are available for SOC estimation, which include:

1. Open Circuit Voltage (OCV): This method estimates the battery’s charge by measuring its voltage when it’s not being used for a while. But it needs the battery to rest for a long time to get accurate results. 

2. Ampere-Hour Integration (AhI): This method adds up the current used by the battery over time to estimate the charge left. However, it can become less accurate because of small errors in the current sensors. 

3. Model-based Approaches: These methods use detailed models of how a battery works to estimate its charge. But these models are complex and can be hard to set up, especially when the battery is being used in different ways. 

4. Filter-based Methods: Techniques like Kalman filters use math to improve charge estimates by filtering out errors from noisy data and making predictions based on the battery's condition over time. 

While these methods have their pros and cons, they rely on creating complicated models of the battery and require a deep understanding of how it works inside. A better approach? Using data-driven methods, which rely on analyzing the data from the battery instead of creating complex models.

BlogImage

Data-driven methods depend on only data taken from BMS, which is initially stored in a database. The data is then used to build relationships between measurable signals and SOC. Machine learning and deep learning algorithms can find the non-linear relationship without building complex battery models and internal characteristics of the battery. In this blog, we will explore a hybrid model, CNN-GRU-TCN, a novel architecture for estimating SOC where CNN is convolutional neural networks, GRU is gated recurrent units, and TCN is temporal convolutional networks. 

BlogImage

In the data-driven approach for SOC estimation, getting data, particularly LiFePO4 battery data, for training and validation of the model is a difficult task. The possible ways of getting data are from open-source or using the data that is acquired from one’s own BMS software. For this hybrid model, the data is taken from a home installation battery system under the control of a BMS—Battery Management System. The system has on-board a 14 kWh LiFePO4 battery in an 8s2p configuration with a total of 16 EVE 280 Ah cells, making robust energy storage feasible for residential applications.

The data of voltage, current, temperature, and SOC from the BMS are recorded in a database called InfuxDB for every 10 seconds over 24 days. Later, the data is derived from the database and is used for training and validation of the hybrid model.

To prioritize the impact of time on State of Charge (SOC) estimation, we include the previous SOC values (called Lag SOC) as part of the input features. This helps the model better understand how past SOC levels affect future predictions. Exploratory data analysis is performed on the acquired data from the database. As proved, deep learning models perform optimally when the data is in a certain range, say 0 and 1. To make sure that the data is in a range between 0 and 1, a normalization technique called min-max scaler is applied to the input features.

The formula to achieve min-max normalization is as follows:

$$ \overline{X} = \frac{x_i - x_{\text{min}}}{x_{\text{max}} - x_{\text{min}}}$$
Where:
- \(\overline{X}\) : Normalized vector after calculation
- \(x_i\) : Distribution of \(x\) in feature \(x\)
- \(x_{\text{min}}\) : Minimum value of \(x\) in the distribution
- \(x_{\text{max}}\) : Maximum value of \(x\) in the distribution

The data is recorded at constant intervals, which makes it time-series data. To use much of the time-series data, we need to create sequences of the data. If we are generating a sequence of length 3, the SOC at time 't' is estimated using the voltage, current, temperature, and the lag SOC values from time steps 't-3', 't-2', and 't-1'. For example, SOC at 10:50:51 is to be estimated by the voltage, current, temperature, and lag SOC values at 10:50:50, 10:50:49, and 10:50:48.

When the data is prepared and preprocessed, then it is ready to use for training and testing of the model. We defined simple deep learning models, namely LSTM and GRU, and hybrid models like CNN-GRU and CNN-GRU-LSTM, to compare them with the base model to make sure our model performs better. All the models are trained for 13 epochs (whole data is fed into the network 13 times), where we get the performance of the models each time. In this study, mean squared error (MSE) is used as an evaluation criteria for the performance of the model. The lower the MSE is, the better the model performs.

Here is how the model functions internally, from taking the input to outputting the values.

The input for the model is of the shape (batch size, time steps, features), where batch size is 20, time steps is the length of each sequence, which is 3, and features are the number of input signals, which is 4 (voltage, current, temperature, and lag soc).

Let

$$X \in \mathbb{R}^{T \times F}$$

represent the input sequence, where T is the number of time steps and F is the number of features.

The Conv1D layer performs feature extraction in the model. It takes input of shape (20, 3, 4) and applies 128 filters to it to extract spatial information. The kernel size of 2 will consider a pair of consecutive time steps to extract localized features from the data. For instance, how do voltage and current at time step t relate to SOC change? The ReLU activation function introduces non-linearity, which allows the model to capture more complex relationships.

$$ H_{\text{CNN}} = \text{ReLU}(W_{\text{CNN}} \cdot X + b_{\text{CNN}}) $$
Where:
$$
W_{\text{CNN}} \in \mathbb{R}^{K \times 128} \text{ with kernel size of 2, and } b_{\text{CNN}} \in \mathbb{R}^{128}
$$

The output from the CNN layer of shape (20, 2, 128) is considered as input for the GRU layer. The GRU layer consists of 64 layers and is a type of recurrent neural network used to capture temporal dependencies. GRU processes the sequential data time step by time step and learns how features at previous time steps influence the SOC at the current time step. It uses a hidden state to capture the sequential dependencies. A hyperparameter return sequence is set to be true to

pass the hidden state shape of the GRU layer to the TCN layer.

$$ H_t^{\text{GRU}} = \text{GRU}(H_{t-1}^{\text{GRU}}, H_{\text{CNN}, t}) $$
Where:
- \( H_t^{\text{GRU}} \): Hidden state at each time step \( t \)
- \( H_{t-1}^{\text{GRU}} \): Previous hidden state

The core of the architecture is Temporal Convolutional Networks. TCNs are powerful for time-series data because they can capture long-term dependencies using dilation layers. In the hybrid model [1, 2, 4, 8], dilation layers are used, which allow the TCN layer not only to look at neighboring time steps but also the data at [1, 2, 4, 8] time steps away, which enables the model to capture the long-term dependencies effectively. The causal layer allows the model to depend on the present and past time steps to estimate SOC at the current time step by not allowing future data to get leaked. Finally, 2 residual blocks are employed that help preserve the information flow through the network, preventing the vanishing gradient problem in deep networks. To know in depth about TCN, refer to this blog.

$$ H_{\text{TCN}} = \text{ReLU}(\text{TCN}(H_t^{\text{GRU}})) $$

Which includes convolution with dilation:

\[ W_{\text{TCN}} \ast_d H_t^{\text{GRU}} \]

Where ‘\(*d\)’ denotes the dilation convolution.

The output from the TCN layer is a 3D vector. But to use the dense layers, the data needs to be converted into a 1D vector, which is done using the flatten layer.

$$ H_{\text{flat}} = \text{Flatten}(H_{\text{TCN}}) $$

After the data is converted into a 1D vector, dense layers of 512, 128 units with the ReLU activation function are used, which allows the model to capture complex non-linear relationships and complex mapping between input features and SOC.

$$ H_{\text{dense}1} = \text{ReLU}(W_1 \cdot H_{\text{flat}} + b_1) \quad (6) $$ \[ H_{\text{dense}2} = \text{ReLU}(W_2 \cdot H_{\text{dense}1} + b_2) \quad (7) \]
Where:
- \( W_1 \in \mathbb{R}^{512 \times ((T-1) \times 128)} \)
- \( W_2 \in \mathbb{R}^{128 \times 512} \)
- \( b_1 \) and \( b_2 \): Corresponding biases

As SOC is a continuous value, the final layer (output layer) in the architecture contains only 1 unit and a linear activation function that outputs the SOC value from the hybrid model.

$$ y_{\text{out}} = W_{\text{out}} \cdot H_{\text{dense}2} + b_{\text{out}} \quad (8) $$
Where:
- \( W_{\text{out}} \in \mathbb{R}^{1 \times 128} \)
- \( b_{\text{out}} \in \mathbb{R} \)

Results:

The results of all the models, CNN, CNN-LSTM, GRU, CNN-GRU, CNN-GRU-LSTM, and CNN-GRU-TCN, are listed in the table below. Among them, CNN-GRU-TCN has the least mean square error, making it the best model compared to other models.

Model 

Training loss 

Validation loss 

LSTM 

0.1013 

0.0952 

CNN-LSTM 

0.0431 

0.0384 

GRU 

0.0489 

0.0478 

CNN-GRU 

0.0509 

0.0470 

CNN-GRU-LSTM 

0.1421 

0.1387 

CNN-GRU-TCN 

0.0325 

0.0281 

Visualizing the Results:  

Here are some key visualizations that demonstrate the performance of the hybrid model: 

1. Actual SOC to predicted SOC value from CNN-GRU-TCN 

2. Training and validation loss of CNN-GRU-TCN 

BlogImageBlogImage

We can see in the above image that the validation loss curve is below the training loss curve in most of the epochs, which says there is no overfitting in the hybrid model.

In conclusion, the hybrid model shows a new approach for SOC estimation in lithium iron phosphate (LiFePO4) batteries. The model used the individual powers of CNN, GRU, and TCN to achieve great accuracy and robustness for SOC estimation. The CNN-GRU-TCN outperformed all other compared deep learning models with a mean squared error of 0.0325 in training and 0.0281 in validation sets. It indicates that the hybrid model works very well on SOC prediction with less error and, therefore, could be a very promising approach for accurate SOC estimation in battery management systems. The training and validation losses across all models showed no signs of overfitting, further validating the robustness of our approach. The comparison between predicted and actual SOC values of the hybrid model highlights its precision and reliability. 

We have tried a new hybrid model called TCN + Transformers. Check it out here.

Related blogs

Xbattery™
Xbattery is building lithium battery packs in India, including electronics and software, to help businesses, EVs and grids store energy affordably and access it on demand.
Join us

© 2024 Xbattery Energy Private Limited. All rights reserved.