How can I call an external function with enumerated parameters in generated code using Real-Time Workshop?

1 view (last 30 days)
I have an external function which I need to call in code generated using Real-Time Workshop. One of the function input parameters is an enumerated type. I want to set the value of this enumerated parameter in Simulink prior to compiling and use the enumerated constants, not their values, in the generated code.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 17 Oct 2017
The following is an example of how to call a custom function with enumerated parameters in generated code using an S-function block, a masked subsystem, and TLC.
This example uses an external function which is defined in the attached myfile.c and myfile.h files. It is a trivial function, but should provide a starting point for your own work.
The model, ex.mdl, contains an S-Function with a masked dialog, which passes two strings as parameters to the S-function. The parameters are specified in the masked dialog using a popup (or drop down) menu. The values in the menu correspond to the enumerated constants. These parameters are configured in the "Edit Mask" dialog box on the "Parameters" tab.
The S-Function in this example registers those two parameter values using the optional mdlRTW function of the S-Function. It also may or may not have a useful simulation function. In the example, the S-Function simply maps the input to the output during simulation.
The S-function has an associated TLC-file which generates code to call the external function based on the parameter values registered by the S-Function. The string parameters passed to the S-Function appear as the enumerated type values in the generated code. Thus, the actual C S-Function affects code generation only by registering those parameter values.
To run the example, follow these steps:
1. Place all attached files in a directory. There should be 5 files: ex.mdl, sfunex.c, sfunex.tlc, myfile.c, and myfile.h.
2. Make sure you have run mex -setup and chosen a compiler.
3. Execute the command:
mex sfunex.c
to compile the C-MEX version of the S-function.
4. Run the simulation. Please note that the settings in the pop-up dialog for the S-function will not affect simulation results, only the generated code.
5. Select some settings in the S-function masked dialog box and build the model. The HTML Code Generation Report is enabled, so you will be able to easily access the generated code. The model.rtw file is also retained, so that you can view the parameter settings in that file.
There is a similar example available in the "sfundemos" library. It is a little simpler, but may be of interest. To see it, execute the following command
sfundemos
at the MATLAB prompt, and then select "C-Files -> Signals and Parameters -> Run-Time Parameters".
If you want to read more about the features used in this demo, I suggest searching the documentation for the following macros and other keywords:
mdlRTW
ssWriteRTWParamSettings
The entire section in Real-Time Workshop -> Target Language Compiler may be useful if you have not written TLC files before:

More Answers (0)

Categories

Find more on Simulink Coder in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!