本文将分析《手把手教你看懂并理解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