matlab使用sfun编写数字pid,谁知道PID控制或者PDFF控制的S函数怎么写

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

如题,我这有一个模板

/* File : pid1.c * Abstract: * * Example C-file S-function for defining a discrete system. * * x(n+1) = Ax(n) + Bu(n) * y(n) = Cx(n) + Du(n) * * For more details about S-functions, see simulink/src/sfuntmpl_doc.c. * * Copyright 1990-2004 The MathWorks, Inc. * $Revision: 1.12.4.2 $ */

#define S_FUNCTION_NAME ST#define S_FUNCTION_LEVEL 2

#include "simstruc.h"//real_T kp=1352;//real_T ki=309;//real_T kp=3.9;//real_T ki=0.011;

real_T ki=3;real_T kp=1000;real_T kvfr=0//real_T ki=78;//#define U(element) (*uPtrs[element]) /* Pointer to Input Port0 *//*static real_T A[2][2]={ { -1.3839, -0.5097 } , { 1 , 0 } }; static real_T B[2][2]={ { -2.5559, 0 } , { 0 , 4.2382 } }; static real_T C[2][2]={ { 0 , 2.0761 } , { 0 , 7.7891 } }; static real_T D[2][2]={ { -0.8141, -2.9334 } , { 1.2426, 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; /* Parameter mismatch will be reported by Simulink */ }

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

if (!ssSetNumInputPorts(S, 1)) return; ssSetInputPortWidth(S, 0, 3); ssSetInputPortRequiredContiguous(S, 0, true); /*direct input signal access*/ ssSetInputPortDirectFeedThrough(S, 0, 1);

if (!ssSetNumOutputPorts(S, 1)) return; ssSetOutputPortWidth(S, 0, 1);

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

/* Take care when specifying exception free code - see sfuntmpl_doc.c */ ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE);}

/* Function: mdlInitializeSampleTimes ========================================= * Abstract: * Specifiy that we inherit our sample time from the driving block. */static void mdlInitializeSampleTimes(SimStruct *S){ // ssSetSampleTime(S, 0, 1.0); ssSetSampleTime(S, 0, 0.0001); ssSetOffsetTime(S, 0, 0.0); ssSetModelReferenceSampleTimeDefaultInheritance(S); }

#define MDL_INITIALIZE_CONDITIONS/* Function: mdlInitializeConditions ======================================== * Abstract: * Initialize both discrete states to one. */static void mdlInitializeConditions(SimStruct *S){ real_T *x = ssGetRealDiscStates(S); //int_T lp; // *x0=0; // for (lp=0;lp<2;lp++) { // *x0++=1.0; // } x[0]=0.0; x[1]=0.0; x[2]=0.0; x[3]=0.0;}

/* Function: mdlOutputs ======================================================= * Abstract: * y = Cx + Du */static void mdlOutputs(SimStruct *S, int_T tid){ real_T *y = ssGetOutputPortRealSignal(S,0); real_T *x = ssGetRealDiscStates(S); // InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0); // UNUSED_ARG(tid); /* not used in single tasking mode */

/* y=Cx+Du */ // y[0]=C[0][0]*x[0]+C[0][1]*x[1]+D[0][0]*U(0)+D[0][1]*U(1); // y[1]=C[1][0]*x[0]+C[1][1]*x[1]+D[1][0]*U(0)+D[1][1]*U(1);y[0]=x[0];}

#define MDL_UPDATE/* Function: mdlUpdate ====================================================== * Abstract: * xdot = Ax + Bu */static void mdlUpdate(SimStruct *S, int_T tid){ // real_T tempX[2] = {0.0, 0.0}; //real_T tempX = 0.0; real_T tempX; real_T *x = ssGetRealDiscStates(S); // InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0); // const real_T *u = (const real_T*) ssGetInputPortSignal(S,0); real_T *u = ssGetInputPortSignal(S,0); // UNUSED_ARG(tid); /* not used in single tasking mode */

/* xdot=Ax+Bu */ // tempX =x[0]+0.2*u[0]+80*(u[0]-x[1])+89560*(u[0]-2*x[1]+x[2]); tempX =x[0]+0.0001*ki*u[0]+kp*(u[0]-x[1]); if(tempX>15.0) tempX=15.0; else if(tempX

/* Function: mdlTerminate ===================================================== * Abstract: * No termination needed, but we are required to have this routine. */static void mdlTerminate(SimStruct *S){ UNUSED_ARG(S); /* unused input argument */}

#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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值