Integrate TensorFlow Model into Simulink for Simulation and Code Generation - MATLAB
Video Player is loading.
Current Time 0:00
Duration 5:47
Loaded: 0%
Stream Type LIVE
Remaining Time 5:47
 
1x
  • Chapters
  • descriptions off, selected
  • en (Main), selected
    Video length is 5:47

    Integrate TensorFlow Model into Simulink for Simulation and Code Generation

    Watch a quick demonstration of how to use a pretrained TensorFlow™ network in Simulink® to implement a deep learning-based, state-of-charge estimation algorithm for a battery management system.

    This demo uses a neural network that has been trained in TensorFlow using battery discharge data measured in the lab.

    The example has two parts: importing a pretrained TensorFlow model into MATLAB® and using the imported model in Simulink for simulation and library-free C code generation. The first part shows how to use the importTensorFlowNetwork command to bring a neural network into MATLAB from TensorFlow and how to visualize an imported network in Deep Network Designer.

    The second part illustrates how to put an imported network into a Simulink model using Predict block. Using this block, the network is simulated and results are compared with the true state-of-charge level as well as an estimate obtained using an extended Kalman Filter. Finally, the imported network is used to generate library-free C code that can run on any microcontroller or ECU, including the NXP S32K boards.

    Published: 4 Apr 2022

    Hi, my name is Joe Hicklin. I'm a senior developer at The MathWorks. I'm going to show you how to use deep learning in your Simulink models. Deep learning is providing a lot of new, and often better, solutions to a wide variety of problems. And of course, Simulink users would like to use this new technology in their designs. We're going to do that today.

    The example I'm working on is from an electric vehicle. An important part of an electric vehicle is the battery management system. And an important part of the battery management system is the state of charge estimator. It's not practical to measure the actual state of charge in a vehicle. So we're going to estimate the state of charge from things we can measure, like temperature, voltage, current.

    I'm using extended Kalman filter to do that estimation, but my colleague down the hall says he's trained up a neural net to do the same thing and do a better job. I want to find out if that's true, and if it is, I want to use that network in my design. So let's get going.

    The first thing that I did was to take my Kalman filter, and extract it from the larger model into a small model all of its own. That allowed me to do things more easily and more quickly. And here it is. Extended Kalman filter-- its inputs are the current voltage and temperature. I stored these values, as well as the true state of charge, on disk. All of these values came from lab experiments where we can measure all these things quite accurately.

    If I run this simulation, I get this. The yellow curve is the ground truth for our state of charge. And the blue curve is the output of the extended Kalman filter, and we can see it's doing an OK job of tracking the truth.

    The neural net I'm going to use was written in TensorFlow. I need to import that into MATLAB, so that I can use it with Simulink and, later, generate code with it. To do that, I'm going to use the importTensorFlowNetwork command, which is going to bring this into MATLAB. So I'll run this now, and it brought the network in. It's called battery_SOC_net.

    Let's take a look at the network we just imported. This isn't absolutely necessary, but it makes me feel better. I'm going to run deepNetworkDesigner and open this network. I'll open the network I just imported from the workspace. There's only one. I'll open it.

    So here it is. Looks like a typical, simple, little network. No complaints about errors or anything like that. I think everything worked fine. I'm going to save that network to a MAT file, so I don't have to go through the import step again.

    Next, we need to add a block containing that network to our model. I'll open the library browser, we'll navigate to deep learning, and choose the Predict block. Bring it in. We'll double click to configure it. It wants to read its network from a MAT file and that will be the MAT file that I just saved. There it is.

    And now that block is configured. I'm going to run the output to the scope, so we can see what's going on. Oh, the input needs to be transposed. Let's get a Transpose block. I'm not sure where it is. And there it is-- Transpose.

    We'll bring that in here, put that guy in there, and hook this guy up to there. And if I now run this system, we have three traces instead of the two.

    So like before, the yellow line is the ground truth for our state of charge. The blue line is the output of the Kalman filter. But the new red line is the output of the neural net. And sure enough, it looks like the red line is tracking the yellow line better, and that means that the neural net is doing a better job of estimating the state of charge of the system.

    An astute viewer might notice that there are five inputs going to the neural net, not just the three going to the extended Kalman filter. This is because we're feeding the average current and the average voltage to the neural net, and that helps it do a much better job. The Kalman filter doesn't need that help. Now, just because in this case, the neural net worked better than the Kalman filter, you shouldn't assume that's always going to be the case. This probably reflects more on my ability to use Kalman filters than it does on the extended Kalman filter itself.

    Now, my next step could be to replace the Kalman filter, in my larger model, with this network. In your development organization, I imagine that you have quality control procedures that would cause you to do some more validation and make sure that this change works well across the entire state space of the model, and not just this tiny area that we've investigated here. But that's the basic idea.

    I'm not going to go into how to use the Simulink profiler, but here's the result of running the profiler on this system. The interesting thing here is that you can see that the neural net is taking slightly less time than the Kalman filter. So not only is it doing a more accurate job, it's taking less computational resources. Of course, now that we've put this all in Simulink, we could generate library-free C code from it, and we can embed that in a microcontroller or an ECU.

    And that's all it takes. With deep learning blocks, you can use neural nets in your Simulink models just like any other blocks. If you have any questions, or you want to learn more, you can follow the links on this page. Thank you very much.