#define S_FUNCTION_NAME buckboost
#define S_FUNCTION_LEVEL 2 // 第一行注释注明了level 2,Level 1 M文件S函数----这种方式提供了一个简单的M文件接口,可以与少部分的S函数API交互。Matlab对于这种方式的支持更多的是为了保持与以前版本的兼容,现在推荐采用的是Level 2 M文件S函数
#define Step_time 0.000002 //一次采样时间(采样频率)
#include "simstruc.h" //包含头文件
double cnt=0;
double i_err=0;
double i_err_sum=0;
double duty=0;
double kp=10;
double ki=150;
double i=0;
/*====================*
* S-function methods *
*====================*/
/* 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)
{
ssSetNumSFcnParams(S, 0); /* Number of expected parameters */
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
/* Return if number of expected != number of actual parameters */
return;
}//实际参数与设定参数不符合则返回
if (!ssSetNumInputPorts(S, 1)) return;//若输入参数个数不为1,则返回
ssSetInputPortWidth(S, 0, 2);//输入参数维数,若有x个输入变量,此处将1改为x,0表示第一个输入
/*
* 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.
*/
ssSetInputPortDirectFeedThrough(S, 0, 1);//输入直通个数
if (!ssSetNumOutputPorts(S, 1)) return;
ssSetOutputPortWidth(S, 0, 1);//输出维度为1
ssSetNumSampleTimes(S, 1);//设置采样时间个数
ssSetNumRWork(S, 0);
ssSetNumIWork(S, 0);
ssSetNumPWork(S, 0);
ssSetNumModes(S, 0);
ssSetNumNonsampledZCs(S, 0);
/* Specify the sim state compliance to be same as a built-in block */
ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE);//?
}
/* 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, Step_time);
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)
{
}
#endif /* MDL_START *///同上
/* Function: mdlOutputs =======================================================
* Abstract:
* In this function, you compute the outputs of your S-function
* block.
*/
static void mdlOutputs(SimStruct *S, int_T tid)
{
InputRealPtrsType uptrs =ssGetInputPortRealSignalPtrs(S,0);//实现对输入信号的访问。这个宏返回一个指针向量,必须通过*uPtrs[i]来访问
real_T *y = ssGetOutputPortRealSignal(S,0);
cnt++;
if(cnt<=75000)
{
i=*uptrs[0];
i_err=1-i;
i_err_sum+=i_err;
if(i_err_sum>=16000) i_err_sum=16000;
if(i_err_sum<=-16000) i_err_sum=-16000;
duty=0.01*i_err+0.00005*i_err_sum;
if(duty>=0.8) duty=0.8;
if(duty<=0) duty=0;
y[0]=1-duty;
}
else
{
if(cnt==150000) cnt=0;
i=*uptrs[1];
i_err=1-i;
i_err_sum+=i_err;
if(i_err_sum>=8000) i_err_sum=8000;
if(i_err_sum<=-8000) i_err_sum=-8000;
duty=1*i_err+0.0001*i_err_sum;
if(duty>=0.8) duty=0.8;
if(duty<=0) duty=0;
y[0]=duty;
}
}
#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)
{
cnt=0;
}
/*=============================*
* 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
评论4