S-Function C a discrete S-function that changes at a specified rate should register the discrete sample time pair, [discrete sample time period, offset], te sample period >0.0 0≤ offset< discrete A discrete S-function that changes at a variable rate should register the [VARIABLE SAMPLE TIME, 0.0] The mdlGetTimeofNextvarHit routine is called to get the time of the next sample hit for the variable step discrete task If your S-function has no intrinsic sample time, you must indicate that your sample time is inherited. There are twe An S-function that changes as its input changes, even during minor ntegration steps, should register the INHERITED SAMPLE_ TIME ample time An s-function that changes as its input changes, but doesnt change during ninor integration steps(that is, remains fixed during minor time steps ), should register the [INHERITED SAMPLE_ TIME, FIXED IN MINOR STEP OFFSET] sample time The Scope block is a good example of this type of block. This block should run at the rate ofits driving block, either continuous or discrete but should never run in minor steps. If it did, the scope display would show the intermediate computations of the solver rather than the final result at each time point 17
S-Function Concepts 1-17 • A discrete S-function that changes at a specified rate should register the discrete sample time pair, [discrete_sample_time_period, offset], where discrete_sample_period > 0.0 and 0.0 ≤ offset < discrete_sample_period • A discrete S-function that changes at a variable rate should register the variable step discrete sample time. [VARIABLE_SAMPLE_TIME, 0.0] The mdlGetTimeOfNextVarHit routine is called to get the time of the next sample hit for the variable step discrete task. If your S-function has no intrinsic sample time, you must indicate that your sample time is inherited. There are two cases: • An S-function that changes as its input changes, even during minor integration steps, should register the [INHERITED_SAMPLE_TIME, 0.0] sample time. • An S-function that changes as its input changes, but doesn’t change during minor integration steps (that is, remains fixed during minor time steps), should register the [INHERITED_SAMPLE_TIME, FIXED_IN_MINOR_STEP_OFFSET] sample time. The Scope block is a good example of this type of block. This block should run at the rate of its driving block, either continuous or discrete, but should never run in minor steps. If it did, the scope display would show the intermediate computations of the solver rather than the final result at each time point
S-Function Examples Simulink comes with a library of s-function examples 1 Enter sfundemos at the matlab command line MATLAB displays the S-function demo library 墨图 Each block represents an S-function example 2 Click a block to d It might be helpful to examine some sample S-functions as you read the next chapters. Code for the examples is stored in these subdirectories under the MATLAB root direct M-files toolbox/simulink/ blocks C. C++, and Fortran Ad simulink/ada/
1 Overview of S-Functions 1-18 S-Function Examples Simulink comes with a library of S-function examples. To run an example: 1 Enter sfundemos at the MATLAB command line. MATLAB displays the S-function demo library. Each block represents an S-function example. 2 Click a block to open and run the example that it represents. It might be helpful to examine some sample S-functions as you read the next chapters. Code for the examples is stored in these subdirectories under the MATLAB root directory: M-files toolbox/simulink/blocks C, C++, and Fortran simulink/src Ada simulink/ada/examples
M-File S-Function Examples The simulink/blocks directory contains many M-file S-functions. Consider starting off by Filename Description csfunc m Define a continuous system in state-space format. dysfunc. m Define a discrete system in state-space format vsfunc. m illustrates how to create a variable sample time block. This block implements a variable step delay in which the first input is delayed by an amount of time determined by the second input xed. m Implement a hybrid system consisting of a continuous integrator in series with a unit delay apm. m Implement the Van der Pol equation(similar to the demo model, vdp). Simo. m Example state-space M-file S-function with internal A B, C, and D matrices. This S-function implements dx/at =AX+ By where x is the state vector, u is the input vector, and y is the output vector. The A, B, C, and D matrices are embedded in the M-file simom2. m Example state-space M-file S-function with external A B, C, and D matrices. The state-space structure is the same as in simon. m, but the a, B, c, and d matrices are provided externally as parameters to this file. imint. m Implement a continuous limited integrator where the output is bounded by lower and upper bounds and includes initial conditions sfun_varargm m Example M-file S-function showing how to use the MATLAB vararg facility
S-Function Examples 1-19 M-File S-Function Examples The simulink/blocks directory contains many M-file S-functions. Consider starting off by looking at these files. Filename Description csfunc.m Define a continuous system in state-space format. dsfunc.m Define a discrete system in state-space format. vsfunc.m Illustrates how to create a variable sample time block. This block implements a variable step delay in which the first input is delayed by an amount of time determined by the second input. mixed.m Implement a hybrid system consisting of a continuous integrator in series with a unit delay. vdpm.m Implement the Van der Pol equation (similar to the demo model, vdp). simom.m Example state-space M-file S-function with internal A, B, C, and D matrices. This S-function implements dx/at = Ax + By y = Cx + Du where x is the state vector, u is the input vector, and y is the output vector. The A, B, C, and D matrices are embedded in the M-file. simom2.m Example state-space M-file S-function with external A, B, C, and D matrices. The state-space structure is the same as in simom.m, but the A, B, C, and D matrices are provided externally as parameters to this file. limintm.m Implement a continuous limited integrator where the output is bounded by lower and upper bounds and includes initial conditions. sfun_varargm.m Example M-file S-function showing how to use the MATLAB vararg facility
1 Overview of s-functions Description vlimintm m Example of a continuous limited integrator S-function This illustrates how to use the size entry of-l to build an S-function that can accommodate a dynam input/state width vdlimintm m Example of a discrete limited integrator S-function This example is identical to vlimint. m, except that the limited integrator is discrete CS-Function Exampl The simulink/src directory also contains examples of C MEX S-functions many of which have an M-file S-function counterpart. These C MEX S-functions are listed in this table Filename Description marplot.c Access simulink signals without using the tandard block inputs csfunc.c Example C meX S-function for defining a dlimint.c Implement a discrete-time limited integrator ds func.c Example C MEX S-function for defining a discrete system fcncallgen. c Execute function-call subsystems n times at the designated rate(sample time) limintc. c Implement a limited integrator. mixed c Implement a hybrid dynamic system consisting of a continuous integrator (1s)in series with a unit delay(1/z) mixedmex. c Implement a hybrid dynamic system with a single output and two inputs
1 Overview of S-Functions 1-20 C S-Function Examples The simulink/src directory also contains examples of C MEX S-functions, many of which have an M-file S-function counterpart. These C MEX S-functions are listed in this table. vlimintm.m Example of a continuous limited integrator S-function. This illustrates how to use the size entry of −1 to build an S-function that can accommodate a dynamic input/state width. vdlimintm.m Example of a discrete limited integrator S-function. This example is identical to vlimint.m, except that the limited integrator is discrete. Filename Description barplot.c Access simulink signals without using the standard block inputs. csfunc.c Example C MEX S-function for defining a continuous system. dlimint.c Implement a discrete-time limited integrator. dsfunc.c Example C MEX S-function for defining a discrete system. fcncallgen.c Execute function-call subsystems n times at the designated rate (sample time). limintc.c Implement a limited integrator. mixedm.c Implement a hybrid dynamic system consisting of a continuous integrator (1/s) in series with a unit delay (1/z). mixedmex.c Implement a hybrid dynamic system with a single output and two inputs. Filename Description
Description Example MEX-file for a vectorized quantizer block Quantizes the input into steps as specified by the quantization interval parameter, q resetint. c A reset integrator. sdotproduct Compute dot product(multiply-accumulate)of two real or complex vectors sftable2.c Two-dimensional table lookup in S-function form stun atol.c Set different absolute tolerances for each stun bitop.c Perform the bitwise operations AND, OR, XOR, left shift, right shift, and one's complement on uint8, uint16, and uint32 input stun cplx.c Complex signal add with one input port and one parameter. stun_directlook c Direct 1-D lookup stun dtype io.c Example of the use of Simulink data types for inputs and output sfun_dtype_param.c Example of the use of Simulink data types for parameters. stun dynsizec Simple example of how to size outputs of an S-function dynamically stun errhdlc Simple example of how to check parameters using the mdlcheckParams s-function routine stun fcncallc Example of an S-function that is configured to execute function-call subsystems on the first and third output elements stun frmad c Frame-based a/d converter 1-21
S-Function Examples 1-21 quantize.c Example MEX-file for a vectorized quantizer block. Quantizes the input into steps as specified by the quantization interval parameter, q. resetint.c A reset integrator. sdotproduct Compute dot product (multiply-accumulate) of two real or complex vectors. sftable2.c Two-dimensional table lookup in S-function form. sfun_atol.c Set different absolute tolerances for each continuous state. sfun_bitop.c Perform the bitwise operations AND, OR, XOR, left shift, right shift, and one’s complement on uint8, uint16, and uint32 inputs. sfun_cplx.c Complex signal add with one input port and one parameter. sfun_directlook.c Direct 1-D lookup. sfun_dtype_io.c Example of the use of Simulink data types for inputs and outputs. sfun_dtype_param.c Example of the use of Simulink data types for parameters. sfun_dynsize.c Simple example of how to size outputs of an S-function dynamically. sfun_errhdl.c Simple example of how to check parameters using the mdlCheckParams S-function routine. sfun_fcncall.c Example of an S-function that is configured to execute function-call subsystems on the first and third output elements. sfun_frmad.c Frame-based A/D converter. Filename Description