Sie sind auf Seite 1von 556
® Model-Based and System-Based Design Writing S-Functions Version 5 Modeling Simulation Implementation
®
®
® Model-Based and System-Based Design Writing S-Functions Version 5 Modeling Simulation Implementation

Model-Based and System-Based Design

Writing S-Functions

Version 5

Modeling

® Model-Based and System-Based Design Writing S-Functions Version 5 Modeling Simulation Implementation

Simulation

® Model-Based and System-Based Design Writing S-Functions Version 5 Modeling Simulation Implementation

Implementation

® Model-Based and System-Based Design Writing S-Functions Version 5 Modeling Simulation Implementation

How to Contact The MathWorks:

How to Contact The MathWorks: www.mathworks.com Web comp.soft-sys.matlab Newsgroup support@mathworks.com

www.mathworks.com

Web

comp.soft-sys.matlab

Newsgroup

support@mathworks.comWeb comp.soft-sys.matlab Newsgroup suggest@mathworks.com bugs@mathworks.com doc@mathworks.com

suggest@mathworks.com

bugs@mathworks.com

doc@mathworks.com

service@mathworks.com

info@mathworks.com

Technical support Product enhancement suggestions Bug reports Documentation error reports Order status, license renewals, passcodes Sales, pricing, and general information

508-647-7000Phone

Phone

508-647-7001Fax

Fax

The MathWorks, Inc. 3 Apple Hill Drive Natick, MA 01760-2098 3 Apple Hill Drive Natick, MA 01760-2098

Mail

For contact information about worldwide offices, see the MathWorks Web site.

Writing S-Functions COPYRIGHT 1998 - 2002 by The MathWorks, Inc.

The software described in this document is furnished under a license agreement. The software may be used or copied only under the terms of the license agreement. No part of this manual may be photocopied or repro- duced in any form without prior written consent from The MathWorks, Inc.

FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation by or for the federal government of the United States. By accepting delivery of the Program, the government hereby agrees that this software qualifies as "commercial" computer software within the meaning of FAR Part 12.212, DFARS Part 227.7202-1, DFARS Part 227.7202-3, DFARS Part 252.227-7013, and DFARS Part 252.227-7014. The terms and conditions of The MathWorks, Inc. Software License Agreement shall pertain to the government’s use and disclosure of the Program and Documentation, and shall supersede any conflicting contractual terms or conditions. If this license fails to meet the government’s minimum needs or is inconsistent in any respect with federal procurement law, the government agrees to return the Program and Documentation, unused, to MathWorks.

MATLAB, Simulink, Stateflow, Handle Graphics, and Real-Time Workshop are registered trademarks, and TargetBox is a trademark of The MathWorks, Inc.

Other product or brand names are trademarks or registered trademarks of their respective holders.

Printing History: October 1998

First printing

Revised for Simulink 3.0 (Release 11)

November 2000

Second printing Revised for Simulink 4.0 (Release 12)

June 2001

Online only

Revised for Simulink 4.1 (Release 12.1)

July 2002

Online only

Revised for Simulink 5.0 (Release 13)

Contents

Overview of S-Functions

1

 

What Is an S-Function?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

1-2

Using S-Functions in Models Passing Parameters to S-Functions

 

1-3

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

1-4

When to Use an S-Function

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

1-5

How S-Functions Work

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

1-6

Mathematics of Simulink Blocks

Simulation Stages

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

1-6

1-6

. S-Function Callback Methods

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

1-9

Implementing S-Functions

.

M-File S-Functions

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

1-10

1-10

. MEX-File S-Functions

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

1-11

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

1-13

S-Function Concepts Direct Feedthrough

. Dynamically Sized Arrays Setting Sample Times and Offsets

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

1-13

1-13

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

1-15

S-Function Examples

1-18

 

Writing M S-Functions

2

 

Introduction

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-2

S-Function Arguments

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-2

S-Function Outputs

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-3

Defining S-Function Block Characteristics

 

2-5

Processing S-Function Parameters

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-6

Examples of M-File S-Functions

. Simple M-File S-Function Example

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-7

2-7

. Example - Continuous State S-Function

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-9

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-12

Example - Discrete State S-Function Example - Hybrid System S-Function

. Example - Variable Sample Time S-Function

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

2-14

2-17

3

Writing S-Functions in C

. . . . 2-14 2-17 3 Writing S-Functions in C Introduction . Creating C MEX

Introduction

. Creating C MEX S-Functions

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

3-2

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

. Callback Implementations

Defines and Includes

.

.

.

.

.

.

Templates for C S-Functions

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

3-27

3-27

Simulink/Real-Time Workshop Interface

 

3-29

Building the Timestwo Example

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

3-30

3-31

. S-Function Source File Requirements

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

3-31

. Compiling C S-Functions

The SimStruct

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

3-33

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

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

3-46

 

Creating C++ S-Functions

4

 

Source File Format

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

4-2

Making C++ Objects Persistent

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

4-6

Building C++ S-Functions

 

4-7

 

Creating Ada S-Functions

5

 

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

. Omitting Optional Callback Methods

Implementing Callbacks

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

5-7

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

.

.

.

.

. Level 1 Versus Level 2 S-Functions

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

6-2

6-2

Creating Level 1 Fortran S-Functions

6-3

The Fortran MEX Template File

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

6-3

. Inline Code Generation Example

.

Example

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

6-3

6-6

Creating Level 2 Fortran S-Functions

6-7

. C/Fortran Interfacing Tips Constructing the Gateway Example C-MEX S-Function Calling Fortran Code

.

Template File

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

6-7

6-7

6-11

 

6-13

Porting Legacy Code

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

6-14

Find the States Sample Times

. Use Flints If Needed

. Considerations for Real Time

. Multiple Instances

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

6-14

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

6-14

6-14

6-15

6-15

Implementing Block Features

7

 

Dialog Parameters

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.