關(guān)于單片機(jī)PID的算法實(shí)現(xiàn)
用整型變量來(lái)實(shí)現(xiàn)PID算法,由于是用整型數(shù)來(lái)做的,所以也不是很精確,但是對(duì)于很多的使用場(chǎng)合,這個(gè)精度也夠了,關(guān)于系數(shù)和采樣電壓全部是放大10倍處理的.所以精度不是很高. 但是也不是那么低,大部分的場(chǎng)合都?jí)蛄? 實(shí)在覺(jué)得精度不夠, 可以再放大10倍或者100倍處理,但是要注意不超出整個(gè)數(shù)據(jù)類型的范圍就可以了.本程序包括PID計(jì)算和輸出兩部分.當(dāng)偏差>10度全速加熱,偏差在10度以內(nèi)為PID計(jì)算輸出. 具體的參考代碼參見(jiàn)下面:*///================================================================// pid.H// Operation about PID algorithm procedure // C51編譯器 Keil 7.08//================================================================// 作者:zhoufeng// Date :2007-08-06// All rights reserved.//================================================================#include#includetypedef unsigned char uint8; typedef unsigned int uint16; typedef unsigned long int uint32; /**********函數(shù)聲明************/void PIDOutput ();void PIDOperation (); /*****************************/typedef struct PIDValue{uint32 Ek_Uint32[3]; //差值保存,給定和反饋的差值uint8 EkFlag_Uint8[3]; //符號(hào),1則對(duì)應(yīng)的為負(fù)數(shù),0為對(duì)應(yīng)的為正數(shù) uint8 KP_Uint8;uint8 KI_Uint8;uint8 KD_Uint8;uint16 Uk_Uint16; //上一時(shí)刻的控制電壓uint16 RK_Uint16; //設(shè)定值uint16 CK_Uint16; //實(shí)際值 }PIDValueStr;PIDValueStr PID;uint8 out ; // 加熱輸出uint8 count; // 輸出時(shí)間單位計(jì)數(shù)器/*********************************PID = Uk KP*[E(k)-E(k-1)] KI*E(k) KD*[E(k)-2E(k-1) E(k-2)];(增量型PID算式)函數(shù)入口: RK(設(shè)定值),CK(實(shí)際值),KP,KI,KD函數(shù)出口: U(K)//PID運(yùn)算函數(shù)********************************/void PIDOperation (void) { uint32 Temp[3]; //中間臨時(shí)變量uint32 PostSum; //正數(shù)和uint32 NegSum; //負(fù)數(shù)和Temp[0] = 0;Temp[1] = 0;Temp[2] = 0;PostSum = 0;NegSum = 0;if( PID.RK_Uint16 > PID.RK_Uint16 ) //設(shè)定值大于實(shí)際值否?{ if( PID.RK_Uint16 - PID.RK_Uint16 >10 ) //偏差大于10否? { PID.Uk_Uint16 = 100; } //偏差大于10為上限幅值輸出(全速加熱) else { Temp[0] = PID.RK_Uint16 - PID.CK_Uint16; //偏差<=10,計(jì)算E(k) PID.EkFlag_Uint8[1]=0; //E(k)為正數(shù) //數(shù)值移位 PID.Ek_Uint32[2] = PID.Ek_Uint32[1]; PID.Ek_Uint32[1] = PID.Ek_Uint32[0]; PID.Ek_Uint32[0] = Temp[0];/****************************************/ if( PID.Ek_Uint32[0] >PID.Ek_Uint32[1] ) //E(k)>E(k-1)否? { Temp[0]=PID.Ek_Uint32[0] - PID.Ek_Uint32[1]; //E(k)>E(k-1) PID.EkFlag_Uint8[0]=0; } //E(k)-E(k-1)為正數(shù) else{ Temp[0]=PID.Ek_Uint32[0] - PID.Ek_Uint32[1]; //E(k)<E(k-1) PID.EkFlag_Uint8[0]=1; } //E(k)-E(k-1)為負(fù)數(shù)/****************************************/ Temp[2]=PID.Ek_Uint32[1]*2 ; // 2E(k-1)if( (PID.Ek_Uint32[0] PID.Ek_Uint32[2])>Temp[2] ) //E(k-2) E(k)>2E(k-1)否? { Temp[2]=(PID.Ek_Uint32[0] PID.Ek_Uint32[2])-Temp[2]; //E(k-2) E(k)>2E(k-1) PID.EkFlag_Uint8[2]=0; } //E(k-2) E(k)-2E(k-1)為正數(shù) else{ Temp[2]=Temp[2]-(PID.Ek_Uint32[0] PID.Ek_Uint32[2]); //E(k-2) E(k)<2E(k-1) PID.EkFlag_Uint8[2]=1; } //E(k-2) E(k)-2E(k-1)為負(fù)數(shù)/****************************************/ Temp[0] = (uint32)PID.KP_Uint8 * Temp[0]; // KP*[E(k)-E(k-1)] Temp[1] = (uint32)PID.KI_Uint8 * PID.Ek_Uint32[0]; // KI*E(k) Temp[2] = (uint32)PID.KD_Uint8 * Temp[2]; // KD*[E(k-2) E(k)-2E(k-1)]/*以下部分代碼是講所有的正數(shù)項(xiàng)疊加,負(fù)數(shù)項(xiàng)疊加*/ /**********KP*[E(k)-E(k-1)]**********/if(PID.EkFlag_Uint8[0]==0) PostSum = Temp[0]; //正數(shù)和else NegSum = Temp[0]; //負(fù)數(shù)和/********* KI*E(k)****************/ if(PID.EkFlag_Uint8[1]==0) PostSum = Temp[1]; //正數(shù)和else ; //空操作,E(K)>0/****KD*[E(k-2) E(k)-2E(k-1)]****/ if(PID.EkFlag_Uint8[2]==0)PostSum = Temp[2]; //正數(shù)和else NegSum = Temp[2]; //負(fù)數(shù)和/***************U(K)***************/ PostSum = (uint32)PID.Uk_Uint16; if(PostSum > NegSum ) // 是否控制量為正數(shù){ Temp[0] = PostSum - NegSum;if( Temp[0] < 100 ) //小于上限幅值則為計(jì)算值輸出PID.Uk_Uint16 = (uint16)Temp[0];else PID.Uk_Uint16 = 100; //否則為上限幅值輸出}else //控制量輸出為負(fù)數(shù),則輸出0(下限幅值輸出) PID.Uk_Uint16 = 0;}}else { PID.Uk_Uint16 = 0; }}/*********************************函數(shù)入口: U(K)函數(shù)出口: out(加熱輸出)//PID運(yùn)算植輸出函數(shù)********************************/void PIDOutput (void) { static int i;i=PID.Uk_Uint16;if(i==0) out=1;else out=0;if((count )==5)//如定時(shí)中斷為40MS,40MS*5=0.2S(輸出時(shí)間單位),加熱周期20S(100等份){ //每20S PID運(yùn)算一次 count=0; i--;}}