Download Writing S-Functions

Transcript
8
Writing S-Functions for Real-Time Workshop
Using an S-function wrapper to import algorithms in your Simulink model
means that the S-function serves as an interface that calls your C code
algorithms from mdlOutputs. S-function wrappers have the advantage that you
can quickly integrate large stand-alone C code into your model without having
to make changes to the code.
This is an example of a model that includes an S-function wrapper.
Figure 8-1: An Example Model That Includes an S-Function Wrapper
There are two files associated with wrapsfcn block, the S-function wrapper and
the C code that contains the algorithm. This is the S-function wrapper code for
this example, called wrapsfcn.c.
#define S_FUNCTION_NAME wrapsfcn
#define S_FUNCTION_LEVEL 2
#include “simstruc.h”
Declare my_alg as
extern.
extern real_T my_alg(real_T u);
/*
* mdlInitializeSizes - initialize the sizes array
*/
static void mdlInitializeSizes(SimStruct *S)
{
ssSetNumSFcnParams( S, 0); /*number of input arguments*/
if (!ssSetNumInputPorts(S, 1)) return;
ssSetInputPortWidth(S, 0, 1);
ssSetInputPortDirectFeedThrough(S, 0, 1);
if (!ssSetNumOutputPorts(S,1)) return;
ssSetOutputPortWidth(S, 0, 1);
ssSetNumSampleTimes( S, 1);
}
8-12