Sample time and stop time in the generated code of Simulink model (2024)

8 views (last 30 days)

Show older comments

Marco Pizzoli on 15 Dec 2023

  • Link

    Direct link to this question

    https://in.mathworks.com/matlabcentral/answers/2061077-sample-time-and-stop-time-in-the-generated-code-of-simulink-model

  • Link

    Direct link to this question

    https://in.mathworks.com/matlabcentral/answers/2061077-sample-time-and-stop-time-in-the-generated-code-of-simulink-model

Commented: Marco Pizzoli on 19 Dec 2023

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

replace-external-code-with-generated-code

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

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Answers (1)

Amish on 19 Dec 2023

  • Link

    Direct link to this answer

    https://in.mathworks.com/matlabcentral/answers/2061077-sample-time-and-stop-time-in-the-generated-code-of-simulink-model#answer_1373892

  • Link

    Direct link to this answer

    https://in.mathworks.com/matlabcentral/answers/2061077-sample-time-and-stop-time-in-the-generated-code-of-simulink-model#answer_1373892

Open in MATLAB Online

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 :

https://www.mathworks.com/help/simulink/ug/choosing-a-solver.html

https://www.mathworks.com/help/simulink/slref/simulationpacingoptions.html

https://www.mathworks.com/help/ecoder/ug/integrate-code-generated-from-simulink-models-with-external-code.html

Hope this helps!

1 Comment

Show -1 older commentsHide -1 older comments

Marco Pizzoli on 19 Dec 2023

Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/2061077-sample-time-and-stop-time-in-the-generated-code-of-simulink-model#comment_3003707

  • Link

    Direct link to this comment

    https://in.mathworks.com/matlabcentral/answers/2061077-sample-time-and-stop-time-in-the-generated-code-of-simulink-model#comment_3003707

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.

Sign in to answer this question.

See Also

Categories

Code GenerationEmbedded CoderDeployment, Integration, and Supported Hardware

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

Tags

  • c code
  • simulink
  • code generation
  • embedded coder
  • time

Products

  • MATLAB
  • Simulink
  • Embedded Coder
  • Simulink Coder

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Sample time and stop time in the generated code of Simulink model (4)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

Sample time and stop time in the generated code of Simulink model (2024)
Top Articles
Latest Posts
Article information

Author: Lakeisha Bayer VM

Last Updated:

Views: 5737

Rating: 4.9 / 5 (49 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Lakeisha Bayer VM

Birthday: 1997-10-17

Address: Suite 835 34136 Adrian Mountains, Floydton, UT 81036

Phone: +3571527672278

Job: Manufacturing Agent

Hobby: Skimboarding, Photography, Roller skating, Knife making, Paintball, Embroidery, Gunsmithing

Introduction: My name is Lakeisha Bayer VM, I am a brainy, kind, enchanting, healthy, lovely, clean, witty person who loves writing and wants to share my knowledge and understanding with you.