Creating Simulink S-functions by using Rational Rhapsody

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

  1. Create a Rational Rhapsody project.
  2. Import the Simulink profile into your project.
  3. Create a configuration and apply the S-FunctionConfig stereotype to it.
  4. Set the newly created configuration to be the active configuration.
  5. Create a class and apply the S-FunctionBlock stereotype to it.
  6. Add incoming flowports to the class to represent incoming data.
  7. Add outgoing flowports to the class to represent outgoing data.
  8. 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.
  9. Implement a statechart for the class.
  10. Generate code for the configuration you created.
  11. 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)
  12. Open MATLAB and go to the output directory containing the Rational Rhapsody code.
  13. 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:

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.


Feedback