📄 incpid.c
字号:
/*
* sfuntmpl_basic.c: Basic 'C' template for a level 2 S-function.
*
* -------------------------------------------------------------------------
* | See matlabroot/simulink/src/sfuntmpl_doc.c for a more detailed template |
* -------------------------------------------------------------------------
*
* Copyright 1990-2002 The MathWorks, Inc.
* $Revision: 1.27 $
*/
/*
* You must specify the S_FUNCTION_NAME as the name of your S-function
* (i.e. replace sfuntmpl_basic with the name of your S-function).
*/
#define S_FUNCTION_NAME IncPID
#define S_FUNCTION_LEVEL 2
#include "simstruc.h"
#include "math.h"
#define Ref_macro *mxGetPr(ssGetSFcnParam(S,0))
#define Kp_macro *mxGetPr(ssGetSFcnParam(S,1)) /*define the macros,Kp,Ti,Td,DR,CP,ST are the parameters of this S Function*/
#define Ti_macro *mxGetPr(ssGetSFcnParam(S,2))
#define Td_macro *mxGetPr(ssGetSFcnParam(S,3))
#define DR_macro *mxGetPr(ssGetSFcnParam(S,4))
#define CP_macro *mxGetPr(ssGetSFcnParam(S,5))
#define ST_macro *mxGetPr(ssGetSFcnParam(S,6))
/*====================*
* 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)
{
ssSetNumContStates(S, 0);
/* 4 discrete states,respectively for e(k),e(k-1),e(k-2) and counter for control period */
ssSetNumDiscStates(S, 4);
/* 2 inputs, respectively for feedback and last control signal */
if (!ssSetNumInputPorts(S, 2)) return;
ssSetInputPortWidth(S, 0, 1);
ssSetInputPortWidth(S, 1, 1);
ssSetInputPortRequiredContiguous(S, 0, true);
ssSetInputPortRequiredContiguous(S, 1, true);
ssSetInputPortDirectFeedThrough(S, 0, 1);
ssSetInputPortDirectFeedThrough(S, 1, 1);
if (!ssSetNumOutputPorts(S, 1)) return;
ssSetOutputPortWidth(S, 0, 1);
ssSetNumSampleTimes(S, 1);
/* 7 sfunction parameters, respectively for reference,Kp,Ti,Td,DR,CP and ST */
ssSetNumSFcnParams(S, 7);
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)
{
real_T ST;
ST = ST_macro;
ssSetSampleTime(S, 0, ST);
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)
{
real_T *x = ssGetRealDiscStates(S);
int_T lp;
/*mdlinitialize all the disc states*/
for (lp=0;lp<3;lp++)
{
*x++=0;
}
/*initialize the control period*/
x[3] = CP_macro;
}
#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)
{
real_T *y = ssGetOutputPortSignal(S,0); /*initialize the output y*/
*y=0;
}
#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)
{
real_T *y = ssGetOutputPortRealSignal(S,0); /*get the output port real signal */
real_T *x = ssGetRealDiscStates(S); /*get real disc states */
const real_T *feedback = ssGetInputPortRealSignal(S,0); /*get feedback */
const real_T *lastcontrol = ssGetInputPortRealSignal(S,1); /*get last control signal */
real_T Ref,Kp, Ti, Td, DR, ST,deltau;
int_T CP;
/*get the parameters' value of this model*/
Ref = Ref_macro;
Kp = Kp_macro;
Ti = Ti_macro;
Td = Td_macro;
DR = DR_macro;
CP = CP_macro;
ST = ST_macro;
/*if the control period is up ,then compute the output */
if (x[3]<=0)
{
if (fabs(x[0])>(DR))
{
/* deltau(k) = Kp[(e(k)-e(k-1)) + T*e(k)/Ti + Td(e(k)-2*e(k-1)+e(k-2))/T] */
deltau=(Kp)*((x[0]-x[1])+CP*ST*x[0]/Ti+Td*(x[0]-2*x[1]+x[2])/(CP*ST));
/*compute the pid control output,*u(k)=u(k-1)+deltau*/
*y=(*lastcontrol)+deltau;
}
else /*if feedback is close enough to setvalue ,there is no need to compute pid*/
{
/*the output of block keeps its value*/
*y = *y;
}
}
else /*if control time is not up,keep the output value u(k) unchanged*/
{
*y=*y;
}
}
#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)
{
real_T *x = ssGetRealDiscStates(S); /*get real disc states x*/
const real_T *feedback = ssGetInputPortRealSignal(S,0);
const real_T *lastcontrol = ssGetInputPortRealSignal(S,1);
real_T Ref;
int_T CP;
Ref = Ref_macro;
CP = CP_macro;
x[2]=x[1]; /*e(k-2)=e(k-1)*/
x[1]=x[0]; /*e(k-1)=e(k)*/
x[0]=Ref-(*feedback); /*e(k)=setvalue-feedback */
/*refresh the counter's value, x[3]*/
if(x[3]<=0)
{
x[3] = CP-1;
}
else
{
x[3]=x[3]-1;
}
}
#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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -