IBM® Rational® Rhapsody® can
be used to create Simulink S-functions that can then be plugged into
Simulink models.
About this task
This feature is only applicable in
IBM Rational Rhapsody Developer for C.
Procedure
- Create a Rational Rhapsody project.
- Import the Simulink profile into your project.
- Create a configuration and apply the S-FunctionConfig
stereotype to it.
- 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
- Rational Rhapsody framework
files (from the Rational Rhapsody IDF
framework)
- a Simulink C template file called RhapsSFunc_the_name_you_gave_to_the_block.c
(for example, RhapsSFunc_BlockSFA.c)
- a mex options file called MexOpts.txt
- a Simulink model file, representing the S-function
block, called RhapSFunc_the_name_you_gave_to_the_block_Model.mdl
(for example, RhapsSFunc_BlockSFA_Model.mdl)
- Open MATLAB and go to the output directory
containing the Rational Rhapsody code.
- Run the command mex @MexOpts.txt.
Results
Output of S-function creation
When you generate code for an S-FunctionConfig configuration, Rational 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 mex options file, containing the necessary
compiler switches and list of source files to use.
When you run the mex command using the mex
options file generated by Rational Rhapsody,
the MATLAB MEX compiler creates a binary file that can be used by
Simulink.
Timing and S-functions
For time-related events, Rational Rhapsody uses
the timing mechanism of the target operating system. Since Simulink
has its own timing mechanism, Rational Rhapsody takes
this into account when generating the S-function code. The Simulink
clock is added as an input to the S-function. This is not visible
to the user in Rational Rhapsody,
but when the resulting files are imported into Simulink, you see a
clock element in addition to the element representing the defined
S-function.