The S function is short for system function. It is used to write its own Simulink module. (Simple enough, ^ _ ^. For more information, see help.) You can use MATLAB, C, C ++, Fortran, Ada, and other languages, here I will only introduce how to use the MATLAB language to write it (mainly because it is relatively simple). I will first explain why the S function is used. I think the S function can use the rich resources of Matlab, instead, it is not limited to the modules provided by SIMULINK, but s functions written in C or C ++ can also operate on hardware ports, you can also perform operations on Windows APIs. First, you can introduce the simulation process (to understand the S function) of the chain. The simulation of the chain has two phases: one is initialization, and the other is mainly to set some parameters, such as the number of input and output, initial state values, and sampling time of the system. The second stage is the running stage, in which computing and output, discrete state updating, and continuous state computing are performed, this stage needs to be run repeatedly until it ends. In the workspace of Matlab, enter edit sfuntmpl (this is the S function template provided by MATLAB). Let's take a look at it to analyze the structure of S function. Its first line is like this: function [sys, x0, STR, ts] = sfuntmpl (t, x, u, flag) first describes the meaning of input and output variables: t indicates the sampling time, X indicates the state variable, and u indicates the input (which is used as the input of the simulink module ), flag is the status flag in the simulation process (it determines whether the current is initialized or running); sys output varies according to the flag (the following uses the flag to explain the meaning of sys ), x0 is the initial value of the state variable, and STR is the reserved parameter. (Mathworks hasn't thought about how to use it. You can leave it blank during initialization, STR = []), ts is a 1 × 2 vector, TS (1) is the sampling period, and TS (2) is the offset. The following is a combination of sfuntmpl. the specific structure of the Code in M: switch flag, % determine the flag, to see the current status case 0, [sys, x0, STR, ts] = mdlinitializesizes; flag = 0 indicates that it is in the initialization State. At this time, the mdlinitializesizes function is used for initialization. This function is in sfuntmpl. in the initialization state, sys is a struct. It is used to set some parameters of the module. The parameters are described as follows: size = simsizes; % struct used to set Module parameters. simsizes is used to generate sizes. numcontstates = 0; % Number of module continuous state variables sizes. numdiscstates = 0; % number of discrete state variables of the module sizes. numoutputs = 0; % Number of output variables of the module sizes. numinputs = 0; % Number of input variables in the module sizes. d Irfeedthrough = 1; % whether the module exists directly (I understand that the input can % directly control the output) sizes. numsampletimes = 1; % the number of sample times of the module, at least one sys = simsizes (sizes); % is assigned to SYS output after setting. For example, consider the following model: dx/dt = FC (t, x, u) can also be described by the continuous state equation: dx/dt = A * x + B * u x (k + 1) = FD (t, x, u) can also be described using discrete state equations: X (k + 1) = H * X (k) + G * U (k) y = fo (t, x, u) can also be described using the output state equation: y = C * x + D * u set the continuous state variables, discrete state variables, input variables, and output variables of the above model to one, we just need to change the above Code to: (generally, continuous state and discrete state will not be used together. I am here to facilitate the description) sizes. numcontstates = 1; sizes. numdiscstates = 1; sizes. n Umoutputs = 1; sizes. numinpu Ts = 1; others can be unchanged. Continue to look down in the mdlinitializesizes function: X0 = []; % The status variable is set to null, indicating that there is no status variable. In our above assumptions, you can change % to X0 = [0, 0] (for discrete and continuous state variables, we set its initial value to 0) STR = []; %. This is not required. Keep the parameter, it can be set to []. It is useless anyway. If % Can 7.0, it will give it some meaning Ts = [0 0]; % if the sampling period is set to 0, it indicates a continuous system, if the discrete system is described in the mdlget % timeofnextvarhit function below, the initialization is finished, and the next step should be faster in Row 3 of sfuntmpl. Continue to look down: Case 1, sys = mdlderivatives (t, x, u); flag = 1 indicates the differentiation to calculate the continuous state, that is, the dx/dt = FC (t, x, u) mentioned above) in dx/dt, find the mdlderivatives function (in row 193). If you set the number of consecutive state variables to 0, you only need to SYS = []; (Like in sfuntmpl), change sys = FC (t, x (1), u) according to the model we discussed above) or sys = A * x (1) + B * U % here X (1) is a continuous state variable, while X (2) is discrete, where only continuous is used, at this time, the output sys is the differential continuation. In the 112 rows of sfuntmpl: Case 2, sys = mdlupdate (t, x, u); flag = 2 indicates that the next discrete state is to be calculated, that is, the X (k + 1) = FD (t, x, u) mentioned above, find the mdlupd ate function (in row 206), and SYS = [] here; it indicates that there is no discrete state, and we can change it to SYS = FD (t, x (2), u) or sys = H * X (2) + G * U; % sys is X (k + 1). In the case 3, sys = mdloutputs (t, x, u); flag = 3 indicates to calculate the output at this time, that is, y = fo (t, x, u), find To the mdloutputs function (in row 218), as shown above, if sys = [] indicates no output, we change it to SYS = fo (t, x, u) or sys = C * x + D * U % sys it seems that output y is almost finished at this time. Hey, In case 4, sys = mdlgettimeofnextvarhit (t, x, u) of sfuntmpl's 124 rows ); flag = 4 indicates that the next sampling time is calculated at this time. It is only useful in the discrete sampling system (that is, the TS settings mentioned in the mdlinit ializesizes above are TS (1) Not 0) in a continuous system, you only need to write sys = [] in the mdlgettimeofnextvarhit function. This function is mainly used to set the variable step size. For specific implementation, you can use Edit vsfunc to view vsfunc. in the final example of M, in case 9, sys = mdlterminate (t, x, u) of the 130 rows of sfuntmpl, flag = 9 indicates that the system will end at this time. Generally, it is written in mdlterminate Write sys = [] in the function. If you want to set something at the end, write the template for the s function sfuntmpl in this function. The S function can also contain user parameters. The following is an example. It is similar to the gain module function in the simulink. Let's take a look. I went to bed and tired of function [sys, x0, STR, TS] = sfungain (t, x, u, flag, gain) switch flag, Case 0, sizes = simsizes; sizes. numcontstates = 0; sizes. numdiscstates = 0; sizes. numoutputs = 1; sizes. numinputs = 1; sizes. dirfeedthrough = 1; sizes. numsampletimes = 1; sys = simsizes (sizes); X0 = []; STR = []; TS = [0, 0]; Case 3, sys = gain * U; case {1, 2, 4,9}, sys = []; end SIMULINK The Design of S-function is provided with many built-in basic library modules. These modules are connected to form a system model. Modules that are frequently used are combined and encapsulated to build new modules that can be reused. However, they are still based on built-in modules originally provided by SIMULINK. Simulink S-function is a powerful new tool for extending the module library. (1) concept of S-function is a computer language description of a dynamic system. In Matlab, you can choose to write it in M files, you can also use C or Mex files. Here, we will only introduce how to use M files to write S-function. S-function provides a powerful tool to extend the library of the Simulink-Z module. It uses a specific call syntax to allow functions to interact with the Simulink-z- ressor. S-function is most widely used to customize the user's own Simulink module. It is widely used and supports continuous systems, discrete systems, and hybrid systems. (2) create M file S-function 1. Use the template file: sfuntmp1. M format: [sys, x0] = function (t, x, u, flag) the template file is located in the toolbox/Simulink/blocks directory under the MATLAB root directory. The structure of S-function in the template file is very simple. It only specifies the M file sub-function to be called for different flag values. For example, when the flag is 3, that is, when the module is in the simulation stage of computing output, the Child function called is sys = mdloutputs (t, x, u ). The template file uses the switch statement to complete this designation. Of course, this structure is not unique. You can also use the if statement to complete the same function. In addition, some values can be removed based on actual needs, because not every module needs to be called by all sub-functions. The template file is only a reference format provided by SIMULINK for the convenience of users. It is not required to write the S-function syntax. You can change the name of the sub-function, you can also directly write the code in the main function, but the advantage of using the template file is that it is convenient and clear. To write a S-function using a template, you only need to replace the S-function name with the expected function name. If you need additional input parameters, you need to add these parameters after the input parameter list, because the previous four parameters are automatically passed in when S-function is called by SIMULINK. We recommend that you do not modify the output parameters. The next step is to replace the code of each sub-function in the template with the corresponding code based on the task to be completed by the compiled S-function. In each simulation phase, the S-function is called. During the call, the simulation phase uses the flag to input different values, in addition, different roles will be specified for the return parameter SYS. That is to say, although it is the same sys variable, it has different meanings in different simulation stages. This change is automatically completed by the simulation system. Description of available sub-functions of the S-function in the M file: mdlinitializesizes (flag = 0): defines the basic features of the S-function module, including the sampling time, the initial conditions of continuous or discrete states, and the sizes array. Mdlderivatives (flag = 1): Calculate the differential equations of continuous state variables. Mdlupdate (flag = 2): update the discrete state, sampling time, and master time step. Mdloutputs (flag = 3): Calculate the output of S-function. Mdlgettimeofnextvarhit (flag = 4): calculates the absolute time of the next sampling point. This method only describes a variable discrete sampling time in mdlinitializesizes. In summary, the establishment of S-function can be divided into two separate tasks: the initialization module features include the width of the input and output signal, the initial conditions of the discrete continuous state and the sampling time. Place the algorithm in the appropriate S-function subfunction. 2. Define the initial information of S-function. In order to enable Simulink to identify an M file S-function, users must provide instructions on S-function in S-function, including initial conditions such as the sampling time, number of consecutive or discrete states. This part is mainly completed in the mdlinitializesizes subfunction. The sizes array is the carrier of the S-function information. Its internal field meaning is: numcontstates (sys (1): number of continuous States (the width of the continuous part of the state vector) numdiscstates (sys (2): number of discrete states (width of the discrete part of the state vector) numoutputs (sys (3): number of output variables (width of the output vector) numinputs (sys (4): number of input variables (input vector width) dirfeedthrough (sys (5): number of discontinuous roots numsampletimes (sys (6 )): the number of sampling times, with or without an Algebraic loop sign. If the width of the vector represented by the field is dynamically changeable, you can assign them to-1. Note that dirfeedthrough is a Boolean variable with values of 0 and 1. 0 indicates no direct feed, when writing the mdloutputs sub-function, you must ensure that the input variable U is not displayed in the sub-function code; 1 indicates that the sub-function is directly fed in. Numsampletimes indicates the number of sampling times, that is, the number of rows of TS variables, which is related to the user's definition of TS. Note that S-function ignores the port, so when there are multiple input variables or multiple output variables, the MUX module or Demux module must be used to combine multiple single inputs into one compound input vector or separate one compound output vector into multiple single outputs. 3. The input and output parameters indicate that the default four input parameters of S-function are t, x, u, and flag. Their order cannot be changed, meaning T: represents the current simulation time. This input parameter is usually used to determine the next sampling time, or in a multi-sampling rate system, to distinguish different sampling time points and perform different processing accordingly. X: indicates the state vector. this parameter is required, even when the system does not have a state. It can be used flexibly. U: indicates the input vector. Flag: it is a parameter that controls which sub-function is called in each simulation phase, and is automatically set by the value of the parameter when the simulation is called by the simulation system. The default four return parameters of S-function are sys and x0, and their order cannot be changed. The meaning of these parameters is: SYS: is a common return parameter, the meaning of the returned value depends on the flag value. X0: it is the initial status value (when there is no status, it is an empty matrix []). This return parameter is valid only when the flag value is 0, and will be ignored in other cases. I. There is a system as follows: dx1 = x2 dx2 = 9.81 * sin (x (1)-2 * X (2) + u calculates the state change curve of X1 under UNIT step input. Assume that X1 and X2 have an initial value of 0. Function [sys, x0] = Dong (t, x, u, flag) If flag = 0 sys = [2; 0; 2; 1; 0; 0]; x0 = [0; 0]; elseif flag = 1 sys = [X (2); 9.81 * sin (x (1)-2 * X (2) + u]; elseif flag = 3 sys = [x (1); X (2)]; else sys = []; End