How can I load a final state of a Simscape model in generated code?

7 visualizaciones (últimos 30 días)
Hi!
I have a Simscape model of which I generate code for deployment. This works great if I start the simulation at defined simple start conditions. However we want to run a simulation for some time to bring it into specific situations and safe these states as initial states for later simulations.
This functionality does already exist within Simulink via model settings (Save: "Data Import/Export" -> "Save to warkspace of file" -> "Final states:" // Load: "Data Import/Export" -> "Load from workspace" -> "Initial state:").
How do I achieve this for saving in Simulink and loading in generated code?

Respuesta aceptada

Yifeng Tang
Yifeng Tang el 31 de Jul. de 2025
Some thoughts:
  1. I wasn't about to generate code when xInitial is enabled. Not sure if it's because that's not supported or because I did something wrong. Were you able to generate code with Simulink operating point enabled?
  2. I know it's possible to generate code with the Simscape part of the model initialized with a Simscape operating point (not Simulink op). More on Simscape OP here. The catch here is that the operating point is hardwired into the generated code. You can't load a different one like in desktop simulation when simulating with generated code.
  3. If you model is simple enough and you can explicitly specify the initial conditions in each block, you may be use the approach outlined in this MATLAB Answer post.
  4. Another possibility, is to NOT try to start from different saved initial conditions. Instead, design a transition period to guide your system to the initial conditions you desire. The inputs during this transition period would be the same as how you would generate the xInitial/xFinal.
  1 comentario
Christian
Christian el 26 de Ag. de 2025 a las 7:34
First of all thanks for your answer!
I will try 1. next time I work on a model. Points 3 and 4 won't be a solution, since I have complex models and initialization phases can take hours. Another use case is to stop a simulation and continue it at later times.
Never the less I will mark this answer as solved, since the outcome is: It's not fully possible at all. I need to find workaround and try to change my requirements.
In case I find different solution I will report here.

Iniciar sesión para comentar.

Más respuestas (1)

Vidhi Agarwal
Vidhi Agarwal el 8 de Jul. de 2025
You can try below given ways to implement to transfer the desired initial state from simulation into the deployed (generated) code:
  • Block Parameters: Set the initial conditions directly in the relevant Simulink or Simscape block parameters, such as "Initial position" or "Initial velocity." After running a simulation to reach a desired state, extract the key values from the simulation results and manually enter them into the block dialogs. When you generate code, these initial values will be hard-coded and used for every run.
  • Model Callbacks or Scripts: Automate the process of updating initial conditions by writing a MATLAB script or using model callbacks. This script runs a simulation, extracts the required state values, and programmatically updates block parameters before code generation. This ensures your generated code always starts from the latest desired state without manual intervention.
  • Custom Code Integration: For advanced use cases, you can modify the generated code to accept initial state values as inputs at runtime, such as reading from a file or through an API. This approach requires in-depth knowledge of the generated code structure and is not natively supported, making it complex and suitable only for experienced users with specific deployment needs.
Hope this helps!
  2 comentarios
Christian
Christian el 9 de Jul. de 2025
Thank you for your answer! I guess I should clarify what I need, since my question was not precise enough.
Christian
Christian el 14 de Jul. de 2025
The idea is to generate and build code for the model an then later to load different initial states right before runtime. Of course it's possible to investigate the model, point out the relevant parameters that determine the model state and to save and load these. In case of a small model that would probably be the way I would go. However this is not feasable for larger models.
That's why I mentioned the Simulink internal way via save to and load from workspace. The I don't have to worry about which variables are relevant since Simulink already takes care about it. But this seems not to be possible in generated code.
Your suggestions about "Custom Code Integration" seems to come the closest to what I need. What steps need to be done to achieve this? I don't want to modify the genereated code directly since it is override at each new code generation.

Iniciar sesión para comentar.

Categorías

Más información sobre General Applications en Help Center y File Exchange.

Productos


Versión

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by