本文將分析《手把手教你看懂並理解Arduino PID控制庫》中第四個問題:忽然開啓或者關閉PID控制對系統的影響。函數
先看問題的圖示:ui
圖中,藍色的豎虛線表示在時間軸上,PID控制由開啓狀態轉爲關閉的狀態。若是咱們對於關閉PID採起的操做是強制設定輸出爲「PID由開啓轉關閉對應的輸出值或更小或爲0」。因爲輸出被設爲定值,因爲輸出的「力度」不夠,那麼被控量會緩慢減小。但對於PID控制器來講,他會不知道PID已經關閉了,因爲被控量見笑了,他就會努力增長輸出,以求改變這種態勢。但現實是殘酷的,爲什麼我「放肆」增長輸出,被控量卻沒有按照我設定的方向發展。此時,若忽然又將PID打開了,將會有一個極大的輸出對系統做用,穩定性下降。spa
上述問題,並非一個大問題,其實在關閉PID控制的時候,僅須要退出PID計算循環,讓各控制參量仍保持原樣便可。if(!inAuto) return; 和 setMode函數完成了上述功能。.net
/*working variables*/ unsigned long lastTime; double Input, Output, Setpoint; double ITerm, lastInput; double kp, ki, kd; int SampleTime = 1000; //1 sec double outMin, outMax; bool inAuto = false; #define MANUAL 0 #define AUTOMATIC 1 void Compute() { if(!inAuto) return; unsigned long now = millis(); int timeChange = (now - lastTime); if(timeChange>=SampleTime) { /*Compute all the working error variables*/ double error = Setpoint - Input; ITerm+= (ki * error); if(ITerm> outMax) ITerm= outMax; else if(ITerm< outMin) ITerm= outMin; double dInput = (Input - lastInput); /*Compute PID Output*/ Output = kp * error + ITerm- kd * dInput; if(Output > outMax) Output = outMax; else if(Output < outMin) Output = outMin; /*Remember some variables for next time*/ lastInput = Input; lastTime = now; } } void SetTunings(double Kp, double Ki, double Kd) { double SampleTimeInSec = ((double)SampleTime)/1000; kp = Kp; ki = Ki * SampleTimeInSec; kd = Kd / SampleTimeInSec; } void SetSampleTime(int NewSampleTime) { if (NewSampleTime > 0) { double ratio = (double)NewSampleTime / (double)SampleTime; ki *= ratio; kd /= ratio; SampleTime = (unsigned long)NewSampleTime; } } void SetOutputLimits(double Min, double Max) { if(Min > Max) return; outMin = Min; outMax = Max; if(Output > outMax) Output = outMax; else if(Output < outMin) Output = outMin; if(ITerm> outMax) ITerm= outMax; else if(ITerm< outMin) ITerm= outMin; } void SetMode(int Mode) { inAuto = (Mode == AUTOMATIC); }
NOTE:若有不足之處請告知。^.^code
下一章將介紹若是在系統運行過程當中,忽然改變PID控制參數對系統的影響blog
NEXTget
PS:轉載請註明出處:歐陽天華it