- Design your Simulink model: Build your Simulink model with a configurable sample time parameter. You can use a MATLAB workspace variable or a Simulink block parameter to represent the sample time.
- Generate C code: Use Simulink Coder to generate the C code from your Simulink model. Ensure that the sample time parameter is appropriately propagated to the generated code.
- Create a command-line interface: Develop a command-line interface or a user interface that allows the user to input the desired sample time value. This can be done using MATLAB functions or external tools, depending on your preference.
- Modify the generated code: Within the generated C code, modify the section that sets the sample time to retrieve the user-specified value. This can be done by reading the input from the command-line interface or any other method you choose.
- Compile the code: Compile the modified C code using your desired build environment (e.g., Linux). Ensure that the necessary dependencies and libraries are included during the compilation process.
Is it possible to have a user defined sample time for compiled Simulink code?
6 views (last 30 days)
Show older comments
Setup:
My process consists of building Simulink models, followed by building the code (ie. generating the C-code from Simulink), and finally compiling the C-code in a separate environment (such as Linux).
Problem statement:
I would like follow the above process, however the "ask" is that the final compilation be able to work on 2 systems, each with their own distinct sample time. One solution on my part would be to make two executables, each compiled with their own sample time. Alternatively, would it be possible to have the sample time as an input for the executable? So that I would have 1 executable package and the user could specify whatever sample time was needed.
0 Comments
Answers (1)
Raghava S N
on 14 Jul 2023
Yes, it is possible to make the sample time of your Simulink model configurable at runtime, allowing the user to specify the desired sample time when executing the generated code. This approach would enable you to have a single executable package that can work with different sample times.
Here's a suggested approach to achieve this:
By following this approach, you will have a single executable package that can be used on different systems with distinct sample times. The user will be able to specify the sample time as an input when executing the compiled code.
It's important to note that this approach requires additional development and customization beyond the standard Simulink code generation process. You will need to modify the generated code and create the necessary user interface to handle the sample time input.
1 Comment
Travis Thomson
on 14 Jul 2023
Edited: Travis Thomson
on 14 Jul 2023
Thank you Raghava, I think the step i had missed was setting the sample time variable storage class to "ImportedExtern" after selecting "Build Model" in Simulink. Using "ImportedExtern" retains the sample time variable in C-code (as opposed to the default "Inlined" which replaces all variable instances with the value itself in the C-code. Your solution worked.
See Also
Categories
Find more on Simulink Coder 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!