SIMULINK Model-Based and System-Based Design Modeling Simulation Implementation Writing S-Functions The mathWorks Version 5
Modeling Simulation Implementation Model-Based and System-Based Design ® Writing S-Functions Version 5
Contents Overview of s-Functions What Is an S-Function? 2 Using S-Functions in Models 1-3 Passing Parameters to S-Functions 1-4 When to Use an s-Function 15 How S-Functions Work l-6 Mathematics of simulink blocks Simulation Stages... l-6 S-Function Callback methods 1-9 Implementing S-Functions 1-10 M-File s-Functions l-10 MEX-File S-Functions l-11 S-Function Concepts l-13 Direct Feedthrough 1-13 Dynamically Sized Arrays l-13 Setting Sample Times and Offsets 1-15 S-Function Examples l-18 Writing M s-Functions Introduction 2-2 S-Function Arguments 2-2 S-Function Outputs Defining S-Function Block Characteristics
i Contents 1 Overview of S-Functions What Is an S-Function? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2 Using S-Functions in Models . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3 Passing Parameters to S-Functions . . . . . . . . . . . . . . . . . . . . . . 1-4 When to Use an S-Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-5 How S-Functions Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-6 Mathematics of Simulink Blocks . . . . . . . . . . . . . . . . . . . . . . . . 1-6 Simulation Stages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-6 S-Function Callback Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9 Implementing S-Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-10 M-File S-Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-10 MEX-File S-Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-11 S-Function Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-13 Direct Feedthrough . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-13 Dynamically Sized Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-13 Setting Sample Times and Offsets . . . . . . . . . . . . . . . . . . . . . . 1-15 S-Function Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-18 2 Writing M S-Functions Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2 S-Function Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2 S-Function Outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3 Defining S-Function Block Characteristics . . . . . . . . . . . . . . 2-5
Processing S-Function Parameters Examples of M-File S-Functions 27 Simple M-File S-Function Example 27 Example- Continuous State S-Function Example- Discrete State S-Function 2-12 Example- Hybrid System S-Function 2-14 Example- Variable Sample Time S-Function Writing s-Functions in C 3 Introduction 3-2 Creating C MEX S-Functions Building S-Functions Automatically 3-5 S-Function Builder Dialog Box 3-8 Setting the Include Path Example of a Basic C MEX S-Function Defines and includes ..327 Callback Implementations Simulink/Real-Time Workshop Interface 3-29 Building the Timestwo Example 330 Templates for C S-Functions 3-3 S-Function Source File Requirements 3-31 The insTruct Compiling c s-Functions ..3-34 How Simulink interacts with C s-Functions Process view Data view Writing Callback Methods Converting Level 1 C MEX S-Functions to Level 2
ii Contents Processing S-Function Parameters . . . . . . . . . . . . . . . . . . . . . 2-6 Examples of M-File S-Functions . . . . . . . . . . . . . . . . . . . . . . . . 2-7 Simple M-File S-Function Example . . . . . . . . . . . . . . . . . . . . . . 2-7 Example - Continuous State S-Function . . . . . . . . . . . . . . . . . . 2-9 Example - Discrete State S-Function . . . . . . . . . . . . . . . . . . . . 2-12 Example - Hybrid System S-Function . . . . . . . . . . . . . . . . . . . 2-14 Example - Variable Sample Time S-Function . . . . . . . . . . . . . 2-17 3 Writing S-Functions in C Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-2 Creating C MEX S-Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-3 Building S-Functions Automatically . . . . . . . . . . . . . . . . . . . . 3-5 S-Function Builder Dialog Box . . . . . . . . . . . . . . . . . . . . . . . . . . 3-8 Setting the Include Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-23 Example of a Basic C MEX S-Function . . . . . . . . . . . . . . . . . 3-25 Defines and Includes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-27 Callback Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-27 Simulink/Real-Time Workshop Interface . . . . . . . . . . . . . . . . . 3-29 Building the Timestwo Example . . . . . . . . . . . . . . . . . . . . . . . . 3-30 Templates for C S-Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 3-31 S-Function Source File Requirements . . . . . . . . . . . . . . . . . . . 3-31 The SimStruct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-33 Compiling C S-Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-34 How Simulink Interacts with C S-Functions . . . . . . . . . . . . 3-35 Process View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-35 Data View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-39 Writing Callback Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-43 Converting Level 1 C MEX S-Functions to Level 2 . . . . . . . 3-44
Obsolete macros Creating C++S-Functions Source file format 4-2 Making C++ Objects Persistent Building C++ S-Functions 47 Creating Ada s-Functions 5 Introduction Ada s-Function Source file format 5-3 Ada S-Function Specification Ada S-Function body Writing Callback Methods in Ada 5-6 Callbacks Invoked by Simulink 5-6 Implementing Callbacks Omitting Optional Callback Methods 5-7 SimStruct functions Building an Ada s-Function ...5-9 Ada Compiler Requirement Example of an Ada s-Function
iii Obsolete Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-46 4 Creating C++ S-Functions Source File Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2 Making C++ Objects Persistent . . . . . . . . . . . . . . . . . . . . . . . . . 4-6 Building C++ S-Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-7 5 Creating Ada S-Functions Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-2 Ada S-Function Source File Format . . . . . . . . . . . . . . . . . . . . . 5-3 Ada S-Function Specification . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-3 Ada S-Function Body . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-4 Writing Callback Methods in Ada . . . . . . . . . . . . . . . . . . . . . . . 5-6 Callbacks Invoked by Simulink . . . . . . . . . . . . . . . . . . . . . . . . . . 5-6 Implementing Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-7 Omitting Optional Callback Methods . . . . . . . . . . . . . . . . . . . . . 5-7 SimStruct Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-7 Building an Ada S-Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-9 Ada Compiler Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-9 Example of an Ada S-Function . . . . . . . . . . . . . . . . . . . . . . . . 5-10
Creating Fortran S-Functions 6 Introduction 6-2 Level 1 Versus level 2 S-Functions Creating Level 1 Fortran S-Functions The Fortran MEX Template File Example Inline Code Generation Example Creating Level 2 Fortran S-Functions 67 Template File .6-7 C/Fortran Interfacing Tips Constructing the Gateway 6-1 Example C-MeX S-Function Calling Fortran Code 6-13 Porting Legacy Code 6-14 Find the states 614 Sample Times 6-1 Multiple Instances 6-1 Use Flints If Needed 615 Considerations for real time 615 Implementing block Features 7 Dialog Parameters ......72 Tunable parameters Run-Time Parameters 7-5 Creating Run-Time Parameters 7-6 Updating Run-Time Parameters 77 Creating Input and Output Ports Creating Input Ports Creating Output Ports
iv Contents 6 Creating Fortran S-Functions Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2 Level 1 Versus Level 2 S-Functions . . . . . . . . . . . . . . . . . . . . . . 6-2 Creating Level 1 Fortran S-Functions . . . . . . . . . . . . . . . . . . . 6-3 The Fortran MEX Template File . . . . . . . . . . . . . . . . . . . . . . . . 6-3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-3 Inline Code Generation Example . . . . . . . . . . . . . . . . . . . . . . . . 6-6 Creating Level 2 Fortran S-Functions . . . . . . . . . . . . . . . . . . . 6-7 Template File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-7 C/Fortran Interfacing Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-7 Constructing the Gateway . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-11 Example C-MEX S-Function Calling Fortran Code . . . . . . . . . 6-13 Porting Legacy Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-14 Find the States . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-14 Sample Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-14 Multiple Instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-14 Use Flints If Needed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-15 Considerations for Real Time . . . . . . . . . . . . . . . . . . . . . . . . . . 6-15 7 Implementing Block Features Dialog Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-2 Tunable Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-3 Run-Time Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-5 Creating Run-Time Parameters . . . . . . . . . . . . . . . . . . . . . . . . . 7-6 Updating Run-Time Parameters . . . . . . . . . . . . . . . . . . . . . . . . . 7-7 Creating Input and Output Ports . . . . . . . . . . . . . . . . . . . . . . . 7-8 Creating Input Ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-8 Creating Output Ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-10