Scalar Expansion of Inputs 7-11 Masked Multiport s-Functions 7-12 Custom Data Types ample Times 7-1 Block-Based Sample Times 7 Specifying Port-Based Sample Times Hybrid Block-Based and Port-Based Sample Times Multirate s-function blocks 7-23 Synchronizing Multirate S-Function Blocks 7-24 Work Vectors 7-26 Work Vectors and Zero Crossings Example Involving a Pointer Work Vector 7-28 Memory Allocation 7-30 Function-Call Subsystems 7-31 Handling Errors Exception Free Code ss SetErrorStatus Termination Criteria ..7-34 Checking Array bounds 7-35 S-Function Examples ..7-36 Example of a Continuous State S-Function Example of a Discrete State S-Function Example of a Hybrid System S-Function 7-45 Example of a Variable-Step S-Function Example of a Zero Crossing S-Function Example of a Time-Varying Continuous Transfer Function..7-64 Writing S-Functions for Real-Time Workshop 8 Introduction Classes of Problems Solved by S-Functions
v 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 8 Writing S-Functions for Real-Time Workshop Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-2 Classes of Problems Solved by S-Functions . . . . . . . . . . . . . . . . 8-2
Types of s-Functions Basic Files Required for Implementation Noninlined S-Functions 87 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 The Inlined Code 8-18 Fully Inlined S-Functions Multiport S-Function Example 8-19 Fully Inlined S-Function with the mdIRTW Routine S-Function rtWdata 8-22 The Direct-Index Lookup Table algorithm The Direct-Index Lookup Table Example Creating Code-Reuse-Compatible s-Functions S-Function callback methods 9 md check Parameters ..9-2 mdlDerivatives mdGetTimeofNextvarHit 9-5 mdlInitialize Conditions 9-6 mdlInitialize SampleTimes 9-8 mdlInitializesizes ..9-12 mdlOutputs 9-16 mdlProcess Parameters 9-17 mdIRTW 9-19 mdIsetDefault Port Complex signals 920 mdISsetDefaultPortDataTypes 921 mdIsetDefaultPortDimensionInfo 922 mdlSetInput Port Complex Signal mdlSetInputPortData Type
vi Contents 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
mdISetInput PortDimensionInfo mdISetInputPortFrameData 927 mdlSetInputPortSampleTime 928 mdlSetlnputPortwidth 930 mdl SetOutput Port Complex signal 9-31 mdISetOutput PortDataType mdISetOutputPortDimensionInfo mdlSetOutput Time mdlSetOutput Width 936 mdIset Work Widths mdIStart mdTerminate mdlzero Crossings 9-41 Sim Struct functions 10 Introduction 102 Language Support 102 The insTruct 102 SimStruct Macros and Functions Listed by Usage 103 Miscellaneous 103 Error Handling and Status IO Port 10-4 Dialog box Parameters 0-9 Run-Time Parameters 10-10 Sample Time State and work vector 10-13 Simulation information 1017 Function call 10-19 Data Type 10-19 Real-Time Workshop 1020 Macro Reference 1022 ss CallExternalModeFcn 10-23
vii mdlSetInputPortDimensionInfo . . . . . . . . . . . . . . . . . . . . . . . . 9-25 mdlSetInputPortFrameData . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-27 mdlSetInputPortSampleTime . . . . . . . . . . . . . . . . . . . . . . . . . . 9-28 mdlSetInputPortWidth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-30 mdlSetOutputPortComplexSignal . . . . . . . . . . . . . . . . . . . . . . 9-31 mdlSetOutputPortDataType . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-32 mdlSetOutputPortDimensionInfo . . . . . . . . . . . . . . . . . . . . . . . 9-33 mdlSetOutputPortSampleTime . . . . . . . . . . . . . . . . . . . . . . . . . 9-35 mdlSetOutputPortWidth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-36 mdlSetWorkWidths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-37 mdlStart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-38 mdlTerminate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-39 mdlUpdate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-40 mdlZeroCrossings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-41 10 SimStruct Functions Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-2 Language Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-2 The SimStruct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-2 SimStruct Macros and Functions Listed by Usage . . . . . . . 10-3 Miscellaneous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-3 Error Handling and Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-3 I/O Port . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-4 Dialog Box Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-9 Run-Time Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-10 Sample Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-11 State and Work Vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-13 Simulation Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-17 Function Call . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-19 Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-19 Real-Time Workshop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-20 Macro Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-22 ssCallExternalModeFcn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-23
ss CallSystem WithTid 10-24 ss Getabs tol ......10-25 ssGet Block Reduction ssGetContStateaddress 10-27 ss Get Contstates ss Typed ss GetDataTypeName 10-30 ssGetData Type Size 10-31 ss GetData ssGetDiscStates ss GetDTypeld FromMxArray ssGetDWork 10-36 ss Work ComplexSignal 10-37 ss GetDWork DataTyp 10-38 ss GetDWorkName ssGetDWorkRTWIdentifier 10-40 ss GetDWorkRTWStorage Class 10-41 10-42 ssGetDWorkUsed AsDState 10-43 ssGetDWork width 0 ssGetdX 10-45 ss Get ErrorStatus 10-46 ss GetInlineParameters 10-47 ss GetInputPortBufferDstPort ss Getinput Port Complex Signal 10-49 ss GetInputPort Connected 10-50 ss GetInputPortDataType 10-51 ss GetInputPortDimensions ss GetInput PortDirect FeedThrough 10-53 sGetInput Data 1054 ss GetInputPortNumDimensions ss GetInputPortOffsetTime 10-56 s GetInput PortOver Writable 1057 ss GetInput Port RealSignal s Getlnput Port RealSignalPtrs 10-60 ss Getinput Port Required Contiguou ss GetInputPort Reusable 10-62 ssGetInputPortSampleTime.. .....10-63 ss GetInputPortSampleTimeIndex rlll Contents
viii Contents ssCallSystemWithTid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-24 ssGetAbsTol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-25 ssGetBlockReduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-26 ssGetContStateAddress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-27 ssGetContStates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-28 ssGetDataTypeId . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-29 ssGetDataTypeName . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-30 ssGetDataTypeSize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-31 ssGetDataTypeZero . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-32 ssGetDiscStates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-33 ssGetDTypeIdFromMxArray . . . . . . . . . . . . . . . . . . . . . . . . . . 10-34 ssGetDWork . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-36 ssGetDWorkComplexSignal . . . . . . . . . . . . . . . . . . . . . . . . . . 10-37 ssGetDWorkDataType . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-38 ssGetDWorkName . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-39 ssGetDWorkRTWIdentifier . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-40 ssGetDWorkRTWStorageClass . . . . . . . . . . . . . . . . . . . . . . . . 10-41 ssGetDWorkRTWTypeQualifier . . . . . . . . . . . . . . . . . . . . . . . 10-42 ssGetDWorkUsedAsDState . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-43 ssGetDWorkWidth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-44 ssGetdX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-45 ssGetErrorStatus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-46 ssGetInlineParameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-47 ssGetInputPortBufferDstPort . . . . . . . . . . . . . . . . . . . . . . . . . 10-48 ssGetInputPortComplexSignal . . . . . . . . . . . . . . . . . . . . . . . . 10-49 ssGetInputPortConnected . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-50 ssGetInputPortDataType . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-51 ssGetInputPortDimensions . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-52 ssGetInputPortDirectFeedThrough . . . . . . . . . . . . . . . . . . . . 10-53 ssGetInputPortFrameData . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-54 ssGetInputPortNumDimensions . . . . . . . . . . . . . . . . . . . . . . . 10-55 ssGetInputPortOffsetTime . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-56 ssGetInputPortOverWritable . . . . . . . . . . . . . . . . . . . . . . . . . 10-57 ssGetInputPortRealSignal . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-58 ssGetInputPortRealSignalPtrs . . . . . . . . . . . . . . . . . . . . . . . . 10-60 ssGetInputPortRequiredContiguous . . . . . . . . . . . . . . . . . . . . 10-61 ssGetInputPortReusable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-62 ssGetInputPortSampleTime . . . . . . . . . . . . . . . . . . . . . . . . . . 10-63 ssGetInputPortSampleTimeIndex . . . . . . . . . . . . . . . . . . . . . 10-64 ssGetInputPortSignal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-65
ss Getinput Port SignalAddress ss GetInput Port SignalPtrs 10-67 ss GetInputPortwidth 1068 ssGetIWork 10-69 ssGetIWork value 10.70 ssGetModelName 1071 sS vector 10.72 sSGetMode Vector Value ssGetNonsampledZCs sS GetNumContstates 10-75 ssGetNumData Type ssGetNumDiscStates 10T7 ssGetNumdWork ssGetNumInput Ports 10-79 ss GetNumlWork 1080 ssGetNumModes 1081 ssGetNumNonsampledZCs 10-82 ssGetNumOutput Ports 1083 sS Parameters 10-84 sS GetNum RunTimeParams 1085 ssGetNumPWork 10-86 sSGetNumRWork 10-87 ssGetNumSampleTimes 10-88 sS GetNumSFen Params ss GetOutput Port Being Merged ss GetOutput Port Complex Signal 10-91 ss GetOutputPortDataType 10-92 ssGetOutput Dimensions ss GetOutput PortFrameData s GetOutput PortNum Dimensions 10-95 ss GetOutput PortOffset'Time ss GetOutput Port RealSignal 10-97 s GetOutput Port Reusable 1098 ssGetOutput PortSampleTime s get Output Portsignal 10-100 ss GetOutput PortsignalAddress 10-101 ss GetOutputPortwidth 10-102 s Get Parents 10-103 ssGetPath 10-104 s GetPlacementGroup 10-105
ix ssGetInputPortSignalAddress . . . . . . . . . . . . . . . . . . . . . . . . . 10-66 ssGetInputPortSignalPtrs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-67 ssGetInputPortWidth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-68 ssGetIWork . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-69 ssGetIWorkValue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-70 ssGetModelName . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-71 ssGetModeVector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-72 ssGetModeVectorValue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-73 ssGetNonsampledZCs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-74 ssGetNumContStates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-75 ssGetNumDataTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-76 ssGetNumDiscStates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-77 ssGetNumDWork . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-78 ssGetNumInputPorts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-79 ssGetNumIWork . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-80 ssGetNumModes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-81 ssGetNumNonsampledZCs . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-82 ssGetNumOutputPorts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-83 ssGetNumParameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-84 ssGetNumRunTimeParams . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-85 ssGetNumPWork . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-86 ssGetNumRWork . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-87 ssGetNumSampleTimes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-88 ssGetNumSFcnParams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-89 ssGetOutputPortBeingMerged . . . . . . . . . . . . . . . . . . . . . . . . 10-90 ssGetOutputPortComplexSignal . . . . . . . . . . . . . . . . . . . . . . . 10-91 ssGetOutputPortDataType . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-92 ssGetOutputPortDimensions . . . . . . . . . . . . . . . . . . . . . . . . . 10-93 ssGetOutputPortFrameData . . . . . . . . . . . . . . . . . . . . . . . . . . 10-94 ssGetOutputPortNumDimensions . . . . . . . . . . . . . . . . . . . . . 10-95 ssGetOutputPortOffsetTime . . . . . . . . . . . . . . . . . . . . . . . . . . 10-96 ssGetOutputPortRealSignal . . . . . . . . . . . . . . . . . . . . . . . . . . 10-97 ssGetOutputPortReusable . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-98 ssGetOutputPortSampleTime . . . . . . . . . . . . . . . . . . . . . . . . . 10-99 ssGetOutputPortSignal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-100 ssGetOutputPortSignalAddress . . . . . . . . . . . . . . . . . . . . . . 10-101 ssGetOutputPortWidth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-102 ssGetParentSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-103 ssGetPath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-104 ssGetPlacementGroup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-105