您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 经营企划 > 利用S-Function实现PID算法
Byali1利用S-Function实现PID算法目录利用S-Function实现PID算法.................................................................................................11、连续PID........................................................................................................................22、离散PID........................................................................................................................5附录A:连续PID算法S-Function文件....................................................................................8附录B:离散PID算法S-Function文件..................................................................................20Byali21、连续PID图1PID控制系统模型图2PIDSubsystem内部结构Byali3图3封装子系统图4生成代码(S-Function)Byali4图5S-Function块幅值图6仿真结果对比(Kp=0.1;Ki=1;Kd=0.2;)Byali52、离散PID图1离散PID控制系统模型图2DiscretePIDSubsystem内部结构Byali6图3封装子系统图4生成代码(S-Function)Byali7图5S-Function块幅值Byali8图6仿真结果对比(Kp=0.1;Ki=1;Kd=0;N=50;)附录A:连续PID算法S-Function文件/**PID0_sf.c**CodegenerationformodelPID0_sf.**Modelversion:1.2*SimulinkCoderversion:8.4(R2013a)13-Feb-2013*Csourcecodegeneratedon:WedApr2714:26:392016**Targetselection:rtwsfcn.tlc*Note:GRTincludesextrainfrastructureandinstrumentationforprototyping*Embeddedhardwareselection:32-bitGeneric*Emulationhardwareselection:*Differsfromembeddedhardware(MATLABHost)*Codegenerationobjectives:Unspecified*Validationresult:Notrun*/#includemath.h#includePID0_sf.h#includePID0_sf_private.h#includesimstruc.h#includefixedpoint.h#ifdefined(RT_MALLOC)||defined(MATLAB_MEX_FILE)externvoid*PID0_malloc(SimStruct*S);#endif#ifndef__RTW_UTFREE__Byali9#ifdefined(MATLAB_MEX_FILE)externvoid*utMalloc(size_t);externvoidutFree(void*);#endif#endif/*#ifndef__RTW_UTFREE__*/#ifdefined(MATLAB_MEX_FILE)#includert_nonfinite.c#endifstaticconstchar_T*RT_MEMORY_ALLOCATION_ERROR=memoryallocationerroringeneratedS-Function;/*Initialconditionsforrootsystem:'Root'*/#defineMDL_INITIALIZE_CONDITIONSstaticvoidmdlInitializeConditions(SimStruct*S){B_PID0_T*_rtB;_rtB=((B_PID0_T*)ssGetLocalBlockIO(S));/*InitializeConditionsforDerivative:'S1/Derivative'*/((real_T*)ssGetDWork(S,0))[0]=rtInf;((real_T*)ssGetDWork(S,0))[2]=rtInf;/*InitializeConditionsforIntegrator:'S1/Integrator'*/((X_PID0_T*)ssGetContStates(S))-Integrator_CSTATE=0.0;}/*Startforrootsystem:'Root'*/Byali10#defineMDL_STARTstaticvoidmdlStart(SimStruct*S){/*instanceunderlyingS-Functiondata*/#ifdefined(RT_MALLOC)||defined(MATLAB_MEX_FILE)#ifdefined(MATLAB_MEX_FILE)/*non-finites*/rt_InitInfAndNaN(sizeof(real_T));/*Checkforinvalidswitchingbetweensolvertypes*/if(!ssIsVariableStepSolver(S)){ssSetErrorStatus(S,ThisSimulinkCodergeneratedS-Functioncannotbeusedinasimulationwithasolvertypeoffixed-stepbecausethisS-Functionwascreatedfromamodelwithsolvertypeofvariable-stepsolverandithascontinuoustimeblocks.SeetheSolverpageofthesimulationparametersdialog.);return;}#endifPID0_malloc(S);if(ssGetErrorStatus(S)!=(NULL)){return;}#endifByali11{}}/*Outputsforrootsystem:'Root'*/staticvoidmdlOutputs(SimStruct*S,int_Ttid){/*localblocki/ovariables*/real_Trtb_Derivative;real_Trtb_Integrator;real_Trtb_Gain;B_PID0_T*_rtB;_rtB=((B_PID0_T*)ssGetLocalBlockIO(S));/*Gain:'S1/Gain2'incorporates:*Inport:'Root/In1'*/_rtB-Gain2=(*(real_T*)(mxGetData(Kd(S))))**((constreal_T**)ssGetInputPortSignalPtrs(S,0))[0];/*Derivative:'S1/Derivative'*/{real_Tt=ssGetT(S);real_TtimeStampA=((real_T*)ssGetDWork(S,0))[0];real_TtimeStampB=((real_T*)ssGetDWork(S,0))[2];real_T*lastU=&((real_T*)ssGetDWork(S,0))[1];if(timeStampA=t&&timeStampB=t){rtb_Derivative=0.0;}else{real_TdeltaT;real_TlastTime=timeStampA;if(timeStampAtimeStampB){Byali12if(timeStampBt){lastTime=timeStampB;lastU=&((real_T*)ssGetDWork(S,0))[3];}}elseif(timeStampA=t){lastTime=timeStampB;lastU=&((real_T*)ssGetDWork(S,0))[3];}deltaT=t-lastTime;rtb_Derivative=(((B_PID0_T*)ssGetLocalBlockIO(S))-Gain2-*lastU++)/deltaT;}}/*Gain:'S1/Gain'incorporates:*Inport:'Root/In1'*/rtb_Gain=(*(real_T*)(mxGetData(Kp(S))))**((constreal_T**)ssGetInputPortSignalPtrs(S,0))[0];/*Gain:'S1/Gain1'incorporates:*Inport:'Root/In1'*/_rtB-Gain1=(*(real_T*)(mxGetData(Ki(S))))**((constreal_T**)ssGetInputPortSignalPtrs(S,0))[0];/*Integrator:'S1/Integrator'*/rtb_Integrator=((X_PID0_T*)ssGetContStates(S))-Integrator_CSTATE;/*Outport:'Root/Out1'incorporates:*Sum:'S1/Sum1'Byali13*/((real_T*)ssGetOutputPortSignal(S,0))[0]=(rtb_Gain+rtb_Integrator)+rtb_Derivative;/*tidisrequiredforauniformfunctioninterface.*Argumenttidisnotusedinthefunction.*/UNUSED_PARAMETER(tid);}/*Updateforrootsystem:'Root'*/#defineMDL_UPDATEstaticvoidmdlUpdate(SimStruct*S,int_Ttid){B_PID0_T*_rtB;_rtB=((B_PID0_T*)ssGetLocalBlockIO(S));/*UpdateforDerivative:'S1/Derivative'*/{real_TtimeStampA=((real_T*)ssGetDWork(S,0))[0];real_TtimeStampB=((real_T*)ssGetDWork(S,0))[2];real_T*lastTime=&((real_T*)ssGetDWork(S,0))[0];real_T*lastU=&((real_T*)ssGetDWork(S,0))[1];if(timeStampA!=rtInf){if(timeStampB==rtInf){lastTime=&((real_T*)ssGetDWork(S,0))[2];lastU=&((real_T*)ssGetDWork(S,0))[3];}elseif(timeStampA=timeStampB){lastTime=&((real_T*)ssGetDWork(S,0))[
本文标题:利用S-Function实现PID算法
链接地址:https://www.777doc.com/doc-1746063 .html