MATLAB Answers

Optimizing Simscape Generated Code

5 views (last 30 days)
Sean Bonaventure
Sean Bonaventure on 18 Oct 2020
Commented: Joel Van Sickel on 1 Dec 2020 at 17:07
Hello. I am using Simulink and Simscape electrical to model an electrical vehicle battery. The model consists of table based batteries along with resistors and mosfets used to model the balancing circuitry for a BMS. The end goal is to export this model to C code to a standalone program that can be used to interface and test our actual BMS code. I am using Simulink Coder to export the code, but the problem I am facing is the exported code is extremely slow. A single step takes around 150ms. This is much slower than when the model runs in Simulink itself. I tried messing around with solver, step size, and compiler settings, but can't seem to speed the code up. Does anyone have any suggestions? Is this a problem with using generated Simscape code?

  0 Comments

Sign in to comment.

Answers (1)

Joel Van Sickel
Joel Van Sickel on 20 Oct 2020
Hello Sean,
code generation for simscape should not be causing a slow down like this. It is often used for real-time tasks where execution timing is critical. I'm not sure what settings are required for simulink coder to work. I would try to run the model with these settings:
Simulink settings - used discrete step solver if possible, or a fixed step solver like ode3.
Simscape settings - set to fixed step in the solver configuration and check the box for fixed cost runtime.
If these settings don't help, would you be willing to share your model? Is your model accessing files or anything like that?
What are you using for your compiler?
Regards,
Joel

  2 Comments

Sean Bonaventure
Sean Bonaventure on 21 Oct 2020
Thanks for the tips. I wasn't able to use a discrete step solver, as I got the error: The "FixedStepDiscrete" solver cannot be used to simulate block diagram 'AccumulatorModel' because it contains continuous states". The ode3 solver also wasn't good enough and I need to use ode14x.
Changing the solver configuration to fixed cost run time helped, reducing the step time from 150ms to 50ms, but that is still too long.
My model is not accesing any files, but it is using the LUT based battery component. So it's reading look up tables.
I am using gcc on linux as my compiler. I have attacehd a zip file of my model.
Thanks for your help,
Sean
Joel Van Sickel
Joel Van Sickel on 1 Dec 2020 at 17:07
Hello Sean,
sorry, I haven't had much time to reviewing models lately. I took a look, and it seems like the local solver wasn't turned on for your model. I would recommend using this to set your simscape model to a fixed time step. If your simulation doesn't work with a large enough time step, I would recommend optimizing your battery model to not require as small a time step to simulate accurately..
Regards,
Joel

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by