Rhapsody® can be
used to create Simulink S-functions that can then be plugged into Simulink models.
About this task
This feature is applicable in Rhapsody
Developer for C and C++.
Procedure
-
Create a Rhapsody
project.
- Import the Simulink profile into your project.
- Set the newly created configuration to be the active
configuration.
- Create a class and apply the S-FunctionBlock stereotype to
it.
- Add incoming flowports to the class to represent incoming
data.
- Add outgoing flowports to the class to represent outgoing
data.
- For each of the flowports you added, add an attribute to the class to
represent the flowport. The attribute must have the same name and be of the same type as the
corresponding flowport.
- Implement a statechart for the class.
- Generate code for the configuration you created.
- The output directory for the configuration to include the following items:
- generated source files for the model
- a Simulink C, or C++, template file called
RhapsSFunc_the_name_you_gave_to_the_block.c (for example,
RhapsSFunc_BlockSFA.c)
- a build_sfunction.m
- a create_sfunction_model.m
-
Open MATLAB and go to the output directory containing the Rhapsody
code.
- Run
build_sfunction.m
.
Results
Output of S-function creation
When you generate code for an S-FunctionConfig configuration, Rhapsody
performs the following actions:
- Completes the sfuntmpl_basic.c template provided by Simulink, and renames it to
reflect the name that you assigned to your S-function block.
- Takes the information you have entered for the S-function block in your project
and creates a corresponding Simulink model file, using the name that you assigned to your S-function
block.
- Generates a Simulink model containing the S-Function block, and builds the
S-Function using the MEX compiler.
Timing and S-functions
For time-related events, Rhapsody uses
the timing mechanism of the target operating system. Since Simulink has its own timing mechanism,
Rhapsody takes this into account when generating the S-function code.