MATLAB编写S函数,C语言版本

#define S_FUNCTION_NAME MFAC // phi(k)=…=phi(k+p)
#define S_FUNCTION_LEVEL 2

#define N 2 // \tau_bar=N-1, the control prediction horizon U[0:N-1]

//#define SAMPLETIME 0.01

/*

  • Need to include simstruc.h for the definition of the SimStruct and
  • its associated macro definitions.
    */
    #include “simstruc.h”

/*====================*

  • S-function methods *
    *====================*/

double rho, lambda; //eta0, mu0, phik0,

/* Function: mdlInitializeSizes ===============================================

  • Abstract:
  • The sizes information is used by Simulink to determine the S-function
  • block’s characteristics (number of inputs, outputs, states, etc.).
    */
    static void mdlInitializeSizes(SimStruct S)
    {
    /
    See sfuntmpl_doc.c for more details on the macros below */
ssSetNumSFcnParams(S, 0);  /* Number of expected parameters */
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
    /* Return if number of expected != number of actual parameters */
    return;
}

ssSetNumContStates(S, 0);
ssSetNumDiscStates(S, 0);

if (!ssSetNumInputPorts(S, 1)) return;
ssSetInputPortWidth(S, 0, 1+1+1+N+1);  // timestamp/ phik/ yk/ r(k+1),...,r(k+tau_bar+1) / u(k-1)
ssSetInputPortRequiredContiguous(S, 0, true); 
/*
* Set direct feedthrough flag (1=yes, 0=no).
* A port has direct feedthrough if the input is used in either
* the mdlOutputs or mdlGetTimeOfNextVarHit functions.
* See matlabroot/simulink/src/sfuntmpl_directfeed.txt.
*/
ssSetInputPortDirectFeedThrough(S, 0, 1);


if (!ssSetNumOutputPorts(S, 1)) return;
ssSetOutputPortWidth(S, 0, N+1); // including the timestamp

ssSetNumSampleTimes(S, 0);
ssSetNumRWork(S, 0);
ssSetNumIWork(S, 0);
ssSetNumPWork(S, 0);
ssSetNumModes(S, 0);
ssSetNumNonsampledZCs(S, 0);

ssSetOptions(S, 0);

}

/* Function: mdlInitializeSampleTimes =========================================

  • Abstract:
  • This function is used to specify the sample time(s) for your
  • S-function. You must register the same number of sample times as
  • specified in ssSetNumSampleTimes.
    */
    static void mdlInitializeSampleTimes(SimStruct *S)
    {
    ssSetSampleTime(S, 0, -1);
    ssSetOffsetTime(S, 0, 0.0);

}

#define MDL_INITIALIZE_CONDITIONS /* Change to #undef to remove function /
#if defined(MDL_INITIALIZE_CONDITIONS)
/
Function: mdlInitializeConditions ========================================

  • Abstract:
  • In this function, you should initialize the continuous and discrete
  • states for your S-function block. The initial states are placed
  • in the state vector, ssGetContStates(S) or ssGetRealDiscStates(S).
  • You can also perform any other initialization activities that your
  • S-function may require. Note, this routine will be called at the
  • start of simulation and if it is present in an enabled subsystem
  • configured to reset states, it will be call when the enabled subsystem
  • restarts execution to reset the states.
    */
    static void mdlInitializeConditions(SimStruct *S)
    {

}
#endif /* MDL_INITIALIZE_CONDITIONS */

#define MDL_START /* Change to #undef to remove function /
#if defined(MDL_START)
/
Function: mdlStart =======================================================

  • Abstract:
  • This function is called once at start of model execution. If you
  • have states that should be initialized once, this is the place
  • to do it.
    */
    static void mdlStart(SimStruct S)
    {
    //eta0 = 1;
    //mu0 = 1;
    //初始化
    }
    #endif /
    MDL_START */

/* Function: mdlOutputs =======================================================

  • Abstract:
  • In this function, you compute the outputs of your S-function
  • block. Generally outputs are placed in the output vector, ssGetY(S).
    */
    static void mdlOutputs(SimStruct *S, int_T tid)
    {
    const real_T u0 = (const real_T) ssGetInputPortSignal(S,0); //timestamp / phik / y / r(k+1)…r(k+tau_b+1)
    real_T *y0 = ssGetOutputPortSignal(S,0); //timestamp + u[0 to N-1]
int i,j;
double TimePlant;
double phi;
double ref[N+1]; // use ref[1:N]
double yp[N], dyp[N];
double dup[N], dups[N]; // dups = sum(dup) for 0 to i
double uk1;

TimePlant=u0[0];
phi = u0[1];
uk1 = u0[N+1];

y0[1]=uk1 + dup[0];
y0[0]=TimePlant;

}

#define MDL_UPDATE /* Change to #undef to remove function /
#if defined(MDL_UPDATE)
/
Function: mdlUpdate ======================================================

  • Abstract:
  • This function is called once for every major integration time step.
  • Discrete states are typically updated here, but this function is useful
  • for performing any tasks that should only take place once per
  • integration step.
    */
    static void mdlUpdate(SimStruct *S, int_T tid)
    {

}
#endif /* MDL_UPDATE */

#define MDL_DERIVATIVES /* Change to #undef to remove function /
#if defined(MDL_DERIVATIVES)
/
Function: mdlDerivatives =================================================

  • Abstract:
  • In this function, you compute the S-function block’s derivatives.
  • The derivatives are placed in the derivative vector, ssGetdX(S).
    */
    static void mdlDerivatives(SimStruct S)
    {
    }
    #endif /
    MDL_DERIVATIVES */

/* Function: mdlTerminate =====================================================

  • Abstract:
  • In this function, you should perform any actions that are necessary
  • at the termination of a simulation. For example, if memory was
  • allocated in mdlStart, this is the place to free it.
    */
    static void mdlTerminate(SimStruct *S)
    {
    }

/*======================================================*

  • See sfuntmpl_doc.c for the optional S-function methods *
    *======================================================*/

/*=============================*

  • Required S-function trailer *
    *=============================*/

#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? /
#include “simulink.c” /
MEX-file interface mechanism /
#else
#include “cg_sfun.h” /
Code generation registration function */
#endif


版权声明:本文为sinat_35932715原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。