Clear Filters
Clear Filters

Sample time and stop time in the generated code of Simulink model

9 views (last 30 days)
Hi,
I want to generate a C code of a Simulink model to be integrated later with external code.
I have followed this MATLAB reference
to configure the model for code generation, however I still have a doubt:
How do I need to configure the solver sample time and stop time for the external code to initialise these parameters?
Thank you in advance
Marcp

Answers (1)

Amish
Amish on 19 Dec 2023
Hi Marco,
I see that you are integrating generated code from a Simulink model with external code. While doing so, it's important to configure the solver sample time and stop time in a way that aligns with the timing behavior of the external system.
Here is some input on how you can configure these parameters:
Sample Time Configuration : Depending upon your model, you might consider choosing a proper solver configuration.
  1. Fixed-step solver: Choose a fixed-step solver if your external system operates at a fixed rate. You can set the sample time to match the tick rate of the external system's scheduler.
  2. Variable-step solver: Although this is less common for code generation, as most real-time systems require a fixed-step execution. This is because they can change the step size during simulation to maintain accuracy, which is not predictable for real-time systems.However, if you're integrating with a system that can handle variable-step execution, you may choose a variable-step solver.
You can do this using the follwoing command:
set_param(model, 'Solver', 'FixedStepDiscrete');
set_param(model, 'FixedStep', 'sample_time_value'); % Replace 'sample_time_value' with the desired sample time
Stop Time Configuration : You can try the following options
  1. Set to inf: If the generated code is to run indefinitely, as is typical in embedded systems, you can set the stop time to infinity.
  2. Set a specific value: If you're generating code for batch simulations, you might want to specify a stop time that corresponds to the desired length of the simulation.
Remember to always validate the generated code with your external system to ensure that the timing behavior is as expected.
You can also refer th the following links for references :
Hope this helps!
  1 Comment
Marco Pizzoli
Marco Pizzoli on 19 Dec 2023
First of all, thank you very much for your reply Amish.
However, I think I was not very specific in my request. I will try to elaborate a little more on what my doubt is (also based on your reply).
Let us say that I would like to generate a C code (from the Simulink model) that interprets the step time and stop time as parameters to be read from outside (i.e. from the external code for example). From the documentation and this answer https://it.mathworks.com/matlabcentral/answers/378006-tunability-of-sample-time-in-code-embedded-coder I understood (i think) that it is possible to define the sample time as a Simulink Parameter and set the storage class as imported extern. But in this case, should sample time be treated as a Model Parameter or as an External Parameter? In the second case, how can I define a parameter as external? I hope I have clarified the problem better.
Thank you in advance for the support
Marco

Sign in to comment.

Categories

Find more on Deployment, Integration, and Supported Hardware in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!