logo资料库

MATLAB S Function 使用手册.pdf

第1页 / 共556页
第2页 / 共556页
第3页 / 共556页
第4页 / 共556页
第5页 / 共556页
第6页 / 共556页
第7页 / 共556页
第8页 / 共556页
资料共556页,剩余部分请下载后查看
Overview of S-Functions
What Is an S-Function?
Using S-Functions in Models
Passing Parameters to S-Functions
When to Use an S-Function
How S-Functions Work
Mathematics of Simulink Blocks
Simulation Stages
S-Function Callback Methods
Implementing S-Functions
M-File S-Functions
MEX-File S-Functions
MEX-File Versus M-File S-Functions
S-Function Concepts
Direct Feedthrough
Dynamically Sized Arrays
Setting Sample Times and Offsets
S-Function Examples
M-File S-Function Examples
C S-Function Examples
Fortran S-Function Examples
C++ S-Function Examples
Ada S-Function Examples
Writing M S-Functions
Introduction
S-Function Arguments
S-Function Outputs
Defining S-Function Block Characteristics
Processing S-Function Parameters
Examples of M-File S-Functions
Simple M-File S-Function Example
Example - Continuous State S-Function
Example - Discrete State S-Function
Example - Hybrid System S-Function
Example - Variable Sample Time S-Function
Writing S-Functions in C
Introduction
Creating C MEX S-Functions
Building S-Functions Automatically
Deploying the Generated S-Function
How the S-Function Builder Builds an S-Function
S-Function Builder Dialog Box
Initialization Pane
Number of discrete states
Discrete states IC
Number of continuous states
Continuous states IC
Sample mode
Sample time value
Input port width
Output port width
Number of parameters
Data Properties Pane
Input Ports Pane
Port name
Dimensions
Row
Column
Complexity
Frame
Output Ports Pane
Port name
Dimensions
Row
Column
Complexity
Frame
Specifies whether this port accepts frame-based signals generated by the Digital and Signal Proce...
Parameters Pane
Parameter name
Complexity
Libraries Pane
Library/Object/Source files
Includes
External function declarations
Outputs Pane
Code for the mdlOutputs function
Inputs are needed in the output function
Continuous Derivatives Pane
Discrete Update Pane
Build Info Pane
Compilation diagnostics
Show compile steps
Create a debuggable MEX-file
Generate wrapper TLC
Save code only
Setting the Include Path
Example of a Basic C MEX S-Function
Defines and Includes
Callback Implementations
mdlInitializeSizes
mdlInitializeSampleTimes
mdlOutputs
mdlTerminate
Simulink/Real-Time Workshop Interface
Building the Timestwo Example
Templates for C S-Functions
S-Function Source File Requirements
Statements Required at the Top of S-Functions
Statements Required at the Bottom of S-Functions
The SimStruct
Compiling C S-Functions
How Simulink Interacts with C S-Functions
Process View
Calling Structure for the Real Time Workshop
Alternate Calling Structure for External Mode
Data View
Accessing Signals Using Pointers
Accessing Contiguous Input Signals
Accessing Input Signals of Individual Ports
Writing Callback Methods
Converting Level 1 C MEX S-Functions to Level 2
Obsolete Macros
Creating C++ S-Functions
Source File Format
Making C++ Objects Persistent
Building C++ S-Functions
Creating Ada S-Functions
Introduction
Ada S-Function Source File Format
Ada S-Function Specification
Ada S-Function Body
Writing Callback Methods in Ada
Callbacks Invoked by Simulink
Implementing Callbacks
Omitting Optional Callback Methods
SimStruct Functions
Building an Ada S-Function
Ada Compiler Requirements
Example of an Ada S-Function
Timestwo Package Specification
mdlInitializeSizes
mdlOutputs
Building the Timestwo Example
Creating Fortran S-Functions
Introduction
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
Template File
C/Fortran Interfacing Tips
Mex Environment
Compiler Compatibility
Symbol Decorations
Fortran Math Library
CFortran
Obtaining a Fortran Compiler
Constructing the Gateway
Simple Case
Code with States
Setup Code
SUBROUTINE Versus PROGRAM
Arguments to a SUBROUTINE
Arguments to a FUNCTION
Interfacing to COMMON blocks
Example C-MEX S-Function Calling Fortran Code
Porting Legacy Code
Find the States
Sample Times
Multiple Instances
Use Flints If Needed
Considerations for Real Time
Implementing Block Features
Dialog Parameters
Tunable Parameters
Tuning Parameters in External Mode
Run-Time Parameters
Creating Run-Time Parameters
Creating Run-Time Parameters All at Once
Creating Run-Time Parameters Individually
Updating Run-Time Parameters
Updating All Parameters at Once
Updating Parameters Individually
Creating Input and Output Ports
Creating Input Ports
Initializing Input Port Dimensions
Sizing an Input Port Dynamically
Creating Output Ports
Scalar Expansion of Inputs
Masked Multiport S-Functions
Custom Data Types
Sample Times
Block-Based Sample Times
Specifying the Number of Sample Times in mdlInitializeSizes
Setting Sample Times and Specifying Function Calls in mdlInitializeSampleTimes
Example: mdlInitializeSampleTimes
Specifying Port-Based Sample Times
Specifying Inherited Sample Time for a Port
Specifying Constant Sample Time for a Port
Configuring Port-Based Sample Times for Use in Triggered Subsystems
Hybrid Block-Based and Port-Based Sample Times
Multirate S-Function Blocks
Example of Defining a Sample Time for a Continuous Block
Example of Defining a Sample Time for a Hybrid Block
Synchronizing Multirate S-Function Blocks
Work Vectors
Work Vectors and Zero Crossings
Example Involving a Pointer Work Vector
Memory Allocation
Function-Call Subsystems
Handling Errors
Exception Free Code
ssSetErrorStatus Termination Criteria
Checking Array Bounds
S-Function Examples
Example of a Continuous State S-Function
matlabroot/simulink/src/csfunc.c
Example of a Discrete State S-Function
matlabroot/simulink/src/dsfunc.c
Example of a Hybrid System S-Function
matlabroot/simulink/src/mixedm.c
Example of a Variable-Step S-Function
matlabroot/simulink/src/vsfunc.c
Example of a Zero Crossing S-Function
matlabroot/simulink/src/sfun_zc_sat.c
Example of a Time-Varying Continuous Transfer Function
matlabroot/simulink/src/stvctf.c
Writing S-Functions for Real-Time Workshop
Introduction
Classes of Problems Solved by S-Functions
Types of S-Functions
Noninlined S-Functions
Wrapper S-Functions
Fully Inlined S-Functions
Basic Files Required for Implementation
Noninlined S-Functions
S-Function Module Names for Real-Time Workshop Builds
Writing Wrapper S-Functions
MEX S-Function Wrapper
TLC S-Function Wrapper
How to Inline
The Inlined Code
Fully Inlined S-Functions
Multiport S-Function Example
Fully Inlined S-Function with the mdlRTW Routine
S-Function RTWdata
The Direct-Index Lookup Table Algorithm
The Direct-Index Lookup Table Example
Error Handling
User Data Caching
mdlRTW Usage
Example Model
matlabroot/simulink/src/sfun_directlook.c
matlabroot/simulink/src/lookup_index.c
matlabroot/toolbox/simulink/blocks/tlc_c/sfun_directlook.tlc
Creating Code-Reuse-Compatible S-Functions
S-Function Callback Methods
mdlCheckParameters
mdlDerivatives
mdlGetTimeOfNextVarHit
mdlInitializeConditions
mdlInitializeSampleTimes
mdlInitializeSizes
mdlOutputs
mdlProcessParameters
mdlRTW
mdlSetDefaultPortComplexSignals
mdlSetDefaultPortDataTypes
mdlSetDefaultPortDimensionInfo
mdlSetInputPortComplexSignal
mdlSetInputPortDataType
mdlSetInputPortDimensionInfo
mdlSetInputPortFrameData
mdlSetInputPortSampleTime
mdlSetInputPortWidth
mdlSetOutputPortComplexSignal
mdlSetOutputPortDataType
mdlSetOutputPortDimensionInfo
mdlSetOutputPortSampleTime
mdlSetOutputPortWidth
mdlSetWorkWidths
mdlStart
mdlTerminate
mdlUpdate
mdlZeroCrossings
SimStruct Functions
Introduction
Language Support
The SimStruct
SimStruct Macros and Functions Listed by Usage
Miscellaneous
Error Handling and Status
I/O Port
Dialog Box Parameters
Run-Time Parameters
Sample Time
State and Work Vector
Simulation Information
Function Call
Data Type
Real-Time Workshop
Macro Reference
ssCallExternalModeFcn
ssCallSystemWithTid
ssGetAbsTol
ssGetBlockReduction
ssGetContStateAddress
ssGetContStates
ssGetDataTypeId
ssGetDataTypeName
ssGetDataTypeSize
ssGetDataTypeZero
ssGetDiscStates
ssGetDTypeIdFromMxArray
ssGetDWork
ssGetDWorkComplexSignal
ssGetDWorkDataType
ssGetDWorkName
ssGetDWorkRTWIdentifier
ssGetDWorkRTWStorageClass
ssGetDWorkRTWTypeQualifier
ssGetDWorkUsedAsDState
ssGetDWorkWidth
ssGetdX
ssGetErrorStatus
ssGetInlineParameters
ssGetInputPortBufferDstPort
ssGetInputPortComplexSignal
ssGetInputPortConnected
ssGetInputPortDataType
ssGetInputPortDimensions
ssGetInputPortDirectFeedThrough
ssGetInputPortFrameData
ssGetInputPortNumDimensions
ssGetInputPortOffsetTime
ssGetInputPortOverWritable
ssGetInputPortRealSignal
ssGetInputPortRealSignalPtrs
ssGetInputPortRequiredContiguous
ssGetInputPortReusable
ssGetInputPortSampleTime
ssGetInputPortSampleTimeIndex
ssGetInputPortSignal
ssGetInputPortSignalAddress
ssGetInputPortSignalPtrs
ssGetInputPortWidth
ssGetIWork
ssGetIWorkValue
ssGetModelName
ssGetModeVector
ssGetModeVectorValue
ssGetNonsampledZCs
ssGetNumContStates
ssGetNumDataTypes
ssGetNumDiscStates
ssGetNumDWork
ssGetNumInputPorts
ssGetNumIWork
ssGetNumModes
ssGetNumNonsampledZCs
ssGetNumOutputPorts
ssGetNumParameters
ssGetNumRunTimeParams
ssGetNumPWork
ssGetNumRWork
ssGetNumSampleTimes
ssGetNumSFcnParams
ssGetOutputPortBeingMerged
ssGetOutputPortComplexSignal
ssGetOutputPortDataType
ssGetOutputPortDimensions
ssGetOutputPortFrameData
ssGetOutputPortNumDimensions
ssGetOutputPortOffsetTime
ssGetOutputPortRealSignal
ssGetOutputPortReusable
ssGetOutputPortSampleTime
ssGetOutputPortSignal
ssGetOutputPortSignalAddress
ssGetOutputPortWidth
ssGetParentSS
ssGetPath
ssGetPlacementGroup
ssGetPortBasedSampleTimeBlockIsTriggered
ssGetPWork
ssGetPWorkValue
ssGetRealDiscStates
ssGetRootSS
ssGetRunTimeParamInfo
ssGetRWork
ssGetRWorkValue
ssGetSampleTimeOffset
ssGetSampleTimePeriod
ssGetSFcnParam
ssGetSFcnParamsCount
ssGetSimMode
ssGetSolverMode
ssGetSolverName
ssGetStateAbsTol
ssGetStopRequested
ssGetT
ssGetTaskTime
ssGetTFinal
ssGetTNext
ssGetTStart
ssGetUserData
ssIsContinuousTask
ssIsFirstInitCond
ssIsMajorTimeStep
ssIsMinorTimeStep
ssIsSampleHit
ssIsSpecialSampleHit
ssIsVariableStepSolver
ssPrintf
ssRegDlgParamAsRunTimeParam
ssRegAllTunableParamsAsRunTimeParams
ssRegisterDataType
ssSampleAndOffsetAreTriggered
ssSetBlockReduction
ssSetCallSystemOutput
ssSetDataTypeSize
ssSetDataTypeZero
ssSetDWorkComplexSignal
ssSetDWorkDataType
ssSetDWorkName
ssSetDWorkRTWIdentifier
ssSetDWorkRTWStorageClass
ssSetDWorkRTWTypeQualifier
ssSetDWorkUsedAsDState
ssSetDWorkWidth
ssSetErrorStatus
ssSetExternalModeFcn
ssSetInputPortComplexSignal
ssSetInputPortDataType
ssSetInputPortDimensionInfo
ssSetInputPortDirectFeedThrough
ssSetInputPortFrameData
ssSetInputPortMatrixDimensions
ssSetInputPortOffsetTime
ssSetInputPortOverWritable
ssSetInputPortRequiredContiguous
ssSetInputPortReusable
ssSetInputPortSampleTime
ssSetInputPortSampleTimeIndex
ssSetInputPortVectorDimension
ssSetInputPortWidth
ssSetIWorkValue
ssSetModeVectorValue
ssSetNumContStates
ssSetNumDiscStates
ssSetNumDWork
ssSetNumInputPorts
ssSetNumIWork
ssSetNumModes
ssSetNumNonsampledZCs
ssSetNumOutputPorts
ssSetNumPWork
ssSetNumRunTimeParams
ssSetNumRWork
ssSetNumSampleTimes
ssSetNumSFcnParams
ssSetOffsetTime
ssSetOptions
ssSetOutputPortComplexSignal
ssSetOutputPortDataType
ssSetOutputPortDimensionInfo
ssSetOutputPortFrameData
ssSetOutputPortMatrixDimensions
ssSetOutputPortOffsetTime
ssSetOutputPortReusable
ssSetOutputPortSampleTime
ssSetOutputPortVectorDimension
ssSetOutputPortWidth
ssSetParameterName
ssSetParameterTunable
ssSetPlacementGroup
ssSetPWorkValue
ssSetRWorkValue
ssSetRunTimeParamInfo
ssSetSampleTime
ssSetSFcnParamNotTunable
ssSetSFcnParamTunable
ssSetSolverNeedsReset
ssSetStopRequested
ssSetTNext
ssSetUserData
ssSetVectorMode
ssUpdateAllTunableParamsAsRunTimeParams
ssUpdateRunTimeParamData
ssUpdateDlgParamAsRunTimeParam
ssUpdateRunTimeParamInfo
ssWarning
ssWriteRTW2dMatParam
ssWriteRTWMx2dMatParam
ssWriteRTWMxVectParam
ssWriteRTWParameters
ssWriteRTWParamSettings
ssWriteRTWScalarParam
ssWriteRTWStr
ssWriteRTWStrParam
ssWriteRTWStrVectParam
ssWriteRTWVectParam
ssWriteRTWWorkVect
Index
® Model-Based and System-Based Design Modeling Simulation Implementation Writing S-Functions Version 5
How to Contact The MathWorks: www.mathworks.com comp.soft-sys.matlab Web Newsgroup support@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-7000 508-647-7001 The MathWorks, Inc. 3 Apple Hill Drive Natick, MA 01760-2098 Phone Fax 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 November 2000 June 2001 July 2002 First printing Revised for Simulink 3.0 (Release 11) Second printing Revised for Simulink 4.0 (Release 12) Online only Online only Revised for Simulink 4.1 (Release 12.1) Revised for Simulink 5.0 (Release 13)
1 2 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 Writing M S-Functions Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2 S-Function Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2 S-Function Outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3 Defining S-Function Block Characteristics . . . . . . . . . . . . . . 2-5 Contents i
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 ii Contents
Obsolete Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-46 4 5 Creating C++ S-Functions Source File Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2 Making C++ Objects Persistent . . . . . . . . . . . . . . . . . . . . . . . . . 4-6 Building C++ S-Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-7 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 iii
6 7 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 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 iv Contents
Scalar Expansion of Inputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-11 Masked Multiport S-Functions . . . . . . . . . . . . . . . . . . . . . . . . . 7-12 Custom Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-14 Sample Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-15 Block-Based Sample Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-16 Specifying Port-Based Sample Times . . . . . . . . . . . . . . . . . . . . 7-18 Hybrid Block-Based and Port-Based Sample Times . . . . . . . . 7-22 Multirate S-Function Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-23 Synchronizing Multirate S-Function Blocks . . . . . . . . . . . . . . 7-24 Work Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-26 Work Vectors and Zero Crossings . . . . . . . . . . . . . . . . . . . . . . . 7-28 Example Involving a Pointer Work Vector . . . . . . . . . . . . . . . . 7-28 Memory Allocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-30 Function-Call Subsystems . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-31 Handling Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-33 Exception Free Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-33 ssSetErrorStatus Termination Criteria . . . . . . . . . . . . . . . . . . 7-34 Checking Array Bounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-35 S-Function Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-36 Example of a Continuous State S-Function . . . . . . . . . . . . . . . 7-36 Example of a Discrete State S-Function . . . . . . . . . . . . . . . . . . 7-41 Example of a Hybrid System S-Function . . . . . . . . . . . . . . . . . 7-45 Example of a Variable-Step S-Function . . . . . . . . . . . . . . . . . . 7-49 Example of a Zero Crossing S-Function . . . . . . . . . . . . . . . . . . 7-52 Example of a Time-Varying Continuous Transfer Function . . 7-64 Writing S-Functions for Real-Time Workshop 8 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-2 Classes of Problems Solved by S-Functions . . . . . . . . . . . . . . . . 8-2 v
Types of S-Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-3 Basic Files Required for Implementation . . . . . . . . . . . . . . . . . . 8-5 Noninlined S-Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-7 S-Function Module Names for Real-Time Workshop Builds . . . 8-7 Writing Wrapper S-Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 8-9 MEX S-Function Wrapper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-9 TLC S-Function Wrapper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-14 The Inlined Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-18 Fully Inlined S-Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-19 Multiport S-Function Example . . . . . . . . . . . . . . . . . . . . . . . . . 8-19 Fully Inlined S-Function with the mdlRTW Routine . . . . . 8-21 S-Function RTWdata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-22 The Direct-Index Lookup Table Algorithm . . . . . . . . . . . . . . . . 8-23 The Direct-Index Lookup Table Example . . . . . . . . . . . . . . . . . 8-24 Creating Code-Reuse-Compatible S-Functions . . . . . . . . . . 8-42 9 S-Function Callback Methods mdlCheckParameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-2 mdlDerivatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-4 mdlGetTimeOfNextVarHit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-5 mdlInitializeConditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-6 mdlInitializeSampleTimes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-8 mdlInitializeSizes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-12 mdlOutputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-16 mdlProcessParameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-17 mdlRTW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-19 mdlSetDefaultPortComplexSignals . . . . . . . . . . . . . . . . . . . . . 9-20 mdlSetDefaultPortDataTypes . . . . . . . . . . . . . . . . . . . . . . . . . . 9-21 mdlSetDefaultPortDimensionInfo . . . . . . . . . . . . . . . . . . . . . . 9-22 mdlSetInputPortComplexSignal . . . . . . . . . . . . . . . . . . . . . . . . 9-23 mdlSetInputPortDataType . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-24 vi Contents
分享到:
收藏