⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pendulum_sfunc_c.c

📁 c program for caro game
💻 C
字号:
//Nguoi lap trinh: Lam Quoc Hung.
//Mon hoc: Lap trinh nang cao.
//File: Pendulum_Sfunc_C.m : Mo hinh S-function con lac nguoc su dung C-File.

#define S_FUNCTION_NAME Pendulum_Sfunc_C
#define S_FUNCTION_LEVEL 2
#include "simstruc.h"
#define U(element) (*uPtrs[element]) 
#define A_IDX 0
#define A_PARAM(S) ssGetSFcnParam(S,A_IDX)
#define B_IDX 1
#define B_PARAM(S) ssGetSFcnParam(S,B_IDX)
#define C_IDX 2
#define C_PARAM(S) ssGetSFcnParam(S,C_IDX)
#define D_IDX 3
#define D_PARAM(S) ssGetSFcnParam(S,D_IDX)
#define X0_IDX 4
#define X0_PARAM(S) ssGetSFcnParam(S,X0_IDX) 
#define NPARAMS 5 
#define NSTATES   mxGetM(A_PARAM(S))
#define NINPUTS   mxGetN(B_PARAM(S))
#define NOUTPUTS  mxGetM(C_PARAM(S))

static void mdlInitializeSizes(SimStruct *S)
{
    ssSetNumSFcnParams(S, NPARAMS); 
    {
        int iParam = 0;
        int nParam = ssGetNumSFcnParams(S);
        for ( iParam = 0; iParam < nParam; iParam++ )
        {
//            ssSetSFcnParamTunable( S, iParam, SS_PRM_TUNABLE );
        }
   }

ssSetNumContStates(S, NSTATES);
    ssSetNumDiscStates(S, 0); 
    if (!ssSetNumInputPorts(S, 1)) return;
    ssSetInputPortWidth(S, 0, NINPUTS);
    ssSetInputPortDirectFeedThrough(S, 0, 1); 
    if (!ssSetNumOutputPorts(S, 1)) return;
    ssSetOutputPortWidth(S, 0, NOUTPUTS); 
    ssSetNumSampleTimes(S, 1);
    ssSetNumRWork(S, 0);
    ssSetNumIWork(S, 0);
    ssSetNumPWork(S, 0);
    ssSetNumModes(S, 0);
    ssSetNumNonsampledZCs(S, 0);
    ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE);
}

static void mdlInitializeSampleTimes(SimStruct *S)
{
    ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
    ssSetOffsetTime(S, 0, 0.0);
//  ssSetModelReferenceSampleTimeDefaultInheritance(S);    
}

#define MDL_INITIALIZE_CONDITIONS
static void mdlInitializeConditions(SimStruct *S)
{
    real_T *x0 = ssGetContStates(S);
    int_T  i, nStates; 
    nStates = ssGetNumContStates(S);
    if (mxGetM(X0_PARAM(S)) != 0) {
        const real_T *pr = mxGetPr(X0_PARAM(S)); 
        for (i = 0; i < nStates; i++) {
            *x0++ = *pr++;
        }
    } else {
        for (i = 0; i < nStates; i++) {
            *x0++ = 0.0;
        }
    }
}
 
static void mdlOutputs(SimStruct *S, int_T tid)
{
    real_T            *y       = ssGetOutputPortRealSignal(S,0);
    real_T            *x       = ssGetContStates(S);
    InputRealPtrsType uPtrs    = ssGetInputPortRealSignalPtrs(S,0);
    const real_T      *cpr     = mxGetPr(C_PARAM(S));
    const real_T      *dpr     = mxGetPr(D_PARAM(S));
    int_T             nStates  = ssGetNumContStates(S);
    int_T             nInputs  = ssGetInputPortWidth(S,0);
    int_T             nOutputs = ssGetOutputPortWidth(S,0);
    int_T             i, j;
    real_T            accum;
     UNUSED_ARG(tid); 
for (i = 0; i < nOutputs; i++) {
        accum = 0.0;
 for (j = 0; j < nStates; j++) {
            accum += cpr[i + nOutputs*j] * x[j];
        }
         for (j = 0; j < nInputs; j++) {
            accum += dpr[i + nOutputs*j] * U(j);
        }
        y[i] = accum;
    }
}

#define MDL_DERIVATIVES
static void mdlDerivatives(SimStruct *S)
{
    real_T            *dx     = ssGetdX(S);
    real_T            *x      = ssGetContStates(S);
    InputRealPtrsType uPtrs   = ssGetInputPortRealSignalPtrs(S,0);
    const real_T      *apr    = mxGetPr(A_PARAM(S));
    const real_T      *bpr    = mxGetPr(B_PARAM(S));
    int_T             nStates = ssGetNumContStates(S);
    int_T             nInputs  = ssGetInputPortWidth(S,0);
    int_T i, j;
    real_T accum; 
    for (i = 0; i < nStates; i++) {
        accum = 0.0;
         for (j = 0; j < nStates; j++) {
            accum += apr[i + nStates*j] * x[j];
        } 
      for (j = 0; j < nInputs; j++) {
            accum += bpr[i + nStates*j] * U(j);
        } 
     dx[i] = accum;
    }
}

static void mdlTerminate(SimStruct *S)
{
    UNUSED_ARG(S);
} 
#ifdef  MATLAB_MEX_FILE
#include "simulink.c"      
#else
#include "cg_sfun.h"       
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -