2
* sfuntmpl_basic.c: Basic 'C' template for a level 2 S-function.
7
* You must specify the S_FUNCTION_NAME as the name of your S-function
8
* (i.e. replace sfuntmpl_basic with the name of your S-function).
11
#define S_FUNCTION_NAME sfuntmpl
12
#define S_FUNCTION_LEVEL 2
15
* Need to include simstruc.h for the definition of the SimStruct and
16
* its associated macro definitions.
25
* You should use the following technique to report errors encountered within
28
* ssSetErrorStatus(S,"Error encountered due to ...");
31
* Note that the 2nd argument to ssSetErrorStatus must be persistent memory.
32
* It cannot be a local variable. For example the following will cause
33
* unpredictable errors:
37
* char msg[256]; {ILLEGAL: to fix use "static char msg[256];"}
38
* sprintf(msg,"Error due to %s", string);
39
* ssSetErrorStatus(S,msg);
43
* See matlabroot/simulink/src/sfuntmpl_doc.c for more details.
46
/*====================*
47
* S-function methods *
48
*====================*/
50
/* Function: mdlInitializeSizes ===============================================
52
* The sizes information is used by Simulink to determine the S-function
53
* block's characteristics (number of inputs, outputs, states, etc.).
55
static void mdlInitializeSizes(SimStruct *S)
57
/* See sfuntmpl_doc.c for more details on the macros below */
59
ssSetNumSFcnParams(S, 0); /* Number of expected parameters */
60
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
61
/* Return if number of expected != number of actual parameters */
65
ssSetNumContStates(S, 0);
66
ssSetNumDiscStates(S, 0);
68
if (!ssSetNumInputPorts(S, 1)) return;
69
ssSetInputPortWidth(S, 0, 1);
70
ssSetInputPortRequiredContiguous(S, 0, true); /*direct input signal access*/
72
* Set direct feedthrough flag (1=yes, 0=no).
73
* A port has direct feedthrough if the input is used in either
74
* the mdlOutputs or mdlGetTimeOfNextVarHit functions.
75
* See matlabroot/simulink/src/sfuntmpl_directfeed.txt.
77
ssSetInputPortDirectFeedThrough(S, 0, 1);
79
if (!ssSetNumOutputPorts(S, 1)) return;
80
ssSetOutputPortWidth(S, 0, 1);
82
ssSetNumSampleTimes(S, 1);
87
ssSetNumNonsampledZCs(S, 0);
94
/* Function: mdlInitializeSampleTimes =========================================
96
* This function is used to specify the sample time(s) for your
97
* S-function. You must register the same number of sample times as
98
* specified in ssSetNumSampleTimes.
100
static void mdlInitializeSampleTimes(SimStruct *S)
102
ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
103
ssSetOffsetTime(S, 0, 0.0);
109
#define MDL_INITIALIZE_CONDITIONS /* Change to #undef to remove function */
110
#if defined(MDL_INITIALIZE_CONDITIONS)
111
/* Function: mdlInitializeConditions ========================================
113
* In this function, you should initialize the continuous and discrete
114
* states for your S-function block. The initial states are placed
115
* in the state vector, ssGetContStates(S) or ssGetRealDiscStates(S).
116
* You can also perform any other initialization activities that your
117
* S-function may require. Note, this routine will be called at the
118
* start of simulation and if it is present in an enabled subsystem
119
* configured to reset states, it will be call when the enabled subsystem
120
* restarts execution to reset the states.
122
static void mdlInitializeConditions(SimStruct *S)
125
#endif /* MDL_INITIALIZE_CONDITIONS */
129
#define MDL_START /* Change to #undef to remove function */
130
#if defined(MDL_START)
131
/* Function: mdlStart =======================================================
133
* This function is called once at start of model execution. If you
134
* have states that should be initialized once, this is the place
137
static void mdlStart(SimStruct *S)
140
#endif /* MDL_START */
144
/* Function: mdlOutputs =======================================================
146
* In this function, you compute the outputs of your S-function
147
* block. Generally outputs are placed in the output vector, ssGetY(S).
149
static void mdlOutputs(SimStruct *S, int_T tid)
151
const real_T *u = (const real_T*) ssGetInputPortSignal(S,0);
152
real_T *y = ssGetOutputPortSignal(S,0);
158
#define MDL_UPDATE /* Change to #undef to remove function */
159
#if defined(MDL_UPDATE)
160
/* Function: mdlUpdate ======================================================
162
* This function is called once for every major integration time step.
163
* Discrete states are typically updated here, but this function is useful
164
* for performing any tasks that should only take place once per
167
static void mdlUpdate(SimStruct *S, int_T tid)
170
#endif /* MDL_UPDATE */
174
#define MDL_DERIVATIVES /* Change to #undef to remove function */
175
#if defined(MDL_DERIVATIVES)
176
/* Function: mdlDerivatives =================================================
178
* In this function, you compute the S-function block's derivatives.
179
* The derivatives are placed in the derivative vector, ssGetdX(S).
181
static void mdlDerivatives(SimStruct *S)
184
#endif /* MDL_DERIVATIVES */
188
/* Function: mdlTerminate =====================================================
190
* In this function, you should perform any actions that are necessary
191
* at the termination of a simulation. For example, if memory was
192
* allocated in mdlStart, this is the place to free it.
194
static void mdlTerminate(SimStruct *S)
199
/*======================================================*
200
* See sfuntmpl_doc.c for the optional S-function methods *
201
*======================================================*/
203
/*=============================*
204
* Required S-function trailer *
205
*=============================*/
207
#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
208
#include "simulink.c" /* MEX-file interface mechanism */
210
#include "cg_sfun.h" /* Code generation registration function */