?
所以,本人自己琢磨著弄了一個(gè)整型變量來(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ù)類型的范圍就可以了。
?
本人做的是帶死區(qū)控制的PID算法。
?
具體的參考代碼參見(jiàn)下面:
typedef?struct?PIDValue
{
????uint32?Ek_Uint32[3];?????????//差值保存,給定和反饋的差值
????uint8??EkFlag_Uint8[3];?????//符號(hào),1則對(duì)應(yīng)的Ek[i]為負(fù)數(shù),0為對(duì)應(yīng)的Ek[i]為正數(shù)
????uint8???KP_Uint8;
?uint8???KI_Uint8;
?uint8???KD_Uint8;
?uint8???B_Uint8;?????//死區(qū)電壓
?
?uint8???KP;??????//顯示修改的時(shí)候用
?uint8???KI;??????//
?uint8???KD;??????//
?uint8???B;???????//
?uint16??Uk_Uint16;????//上一時(shí)刻的控制電壓
}PIDValueStr;
?
PIDValueStr?xdata?PID;
/*******************************
**PID?=?Uk?+?(KP*E(k)?-?KI*E(k-1)?+?KD*E(k-2));
********************************/
void????PIDProcess(void)
{
?uint32?idata?Temp[3];??//
?uint32?idata?PostSum;??//正數(shù)和
?uint32?idata?NegSum;???//負(fù)數(shù)和
?Temp[0]?=?0;
????Temp[1]?=?0;
????Temp[2]?=?0;
?PostSum?=?0;
?NegSum?=?0;
?if(?ADPool.Value_Uint16[UINADCH]?>?ADPool.Value_Uint16[UFADCH]?)??//給定大于反饋,則EK為正數(shù)
?{
?????Temp[0]?=?ADPool.Value_Uint16[UINADCH]?-?ADPool.Value_Uint16[UFADCH];???//計(jì)算Ek[0]
????????if(?Temp[0]?>?PID.B_Uint8?)
????????{
??????//數(shù)值移位
????????????PID.Ek_Uint32[2]?=?PID.Ek_Uint32[1];
????????????PID.Ek_Uint32[1]?=?PID.Ek_Uint32[0];
????????????PID.Ek_Uint32[0]?=?Temp[0];
????????????//符號(hào)移位
???PID.EkFlag_Uint8[2]?=?PID.EkFlag_Uint8[1];
???PID.EkFlag_Uint8[1]?=?PID.EkFlag_Uint8[0];
???PID.EkFlag_Uint8[0]?=?0;???????????????????????//當(dāng)前EK為正數(shù)
????????????Temp[0]?=?(uint32)PID.KP_Uint8?*?PID.Ek_Uint32[0];????//?KP*EK0
????????????Temp[1]?=?(uint32)PID.KI_Uint8?*?PID.Ek_Uint32[1];????//?KI*EK1
????????????Temp[2]?=?(uint32)PID.KD_Uint8?*?PID.Ek_Uint32[2];????//?KD*EK2
????????}
?}
?else???//反饋大于給定
?{
?????Temp[0]?=?ADPool.Value_Uint16[UFADCH]?-?ADPool.Value_Uint16[UINADCH];???//計(jì)算Ek[0]
????????if(?Temp[0]?>?PID.B_Uint8?)
????????{
??????//數(shù)值移位
????????????PID.Ek_Uint32[2]?=?PID.Ek_Uint32[1];
????????????PID.Ek_Uint32[1]?=?PID.Ek_Uint32[0];
????????????PID.Ek_Uint32[0]?=?Temp[0];
????????????//符號(hào)移位
???PID.EkFlag_Uint8[2]?=?PID.EkFlag_Uint8[1];
???PID.EkFlag_Uint8[1]?=?PID.EkFlag_Uint8[0];
???PID.EkFlag_Uint8[0]?=?1;???????????????????????//當(dāng)前EK為負(fù)數(shù)
????????????Temp[0]?=?(uint32)PID.KP_Uint8?*?PID.Ek_Uint32[0];????//?KP*EK0
????????????Temp[1]?=?(uint32)PID.KI_Uint8?*?PID.Ek_Uint32[1];????//?KI*EK1
????????????Temp[2]?=?(uint32)PID.KD_Uint8?*?PID.Ek_Uint32[2];????//?KD*EK2
????????}
?}
?
/*以下部分代碼是講所有的正數(shù)項(xiàng)疊加,負(fù)數(shù)項(xiàng)疊加*/
????if(PID.EkFlag_Uint8[0]==0)
????{
????????PostSum?+=?Temp[0];???//正數(shù)和
?}
????else
?{
????????NegSum?+=?Temp[0];????//負(fù)數(shù)和
?}?????????????????????????//?KP*EK0
????if(PID.EkFlag_Uint8[1]!=0)??????
????{
????????PostSum?+=?Temp[1];???//正數(shù)和
?}
?else
?{
????????NegSum?+=?Temp[1];????//負(fù)數(shù)和
?}?????????????????????????//?-?kI?*?EK1
????if(PID.EkFlag_Uint8[2]==0)
????{
????????PostSum?+=?Temp[2];???//正數(shù)和
????}
?else
?{
????????NegSum?+=?Temp[2];????//負(fù)數(shù)和
?}?????????????????????????//?KD?*?EK2
????PostSum?+=?(uint32)PID.Uk_Uint16;????????//?
????if(?PostSum?>?NegSum?)?????????????//?是否控制量為正數(shù)
????{
????????Temp[0]?=?PostSum?-?NegSum;
????????if(?Temp[0]?(uint32)ADPool.Value_Uint16[UMAXADCH]?)???//小于限幅值則為計(jì)算值輸出
??{
????????????PID.Uk_Uint16?=?(uint16)Temp[0];
??}
??else
??{
????????????PID.Uk_Uint16?=?ADPool.Value_Uint16[UMAXADCH];????//否則為限幅值輸出
?????}
????}
????else???????????????//控制量輸出為負(fù)數(shù),則輸出0
????{
????????PID.Uk_Uint16?=?0;
????}
}
評(píng)論
查看更多