本文为作者学习笔记
欢迎交流讨论,喜欢的话点个赞吧
欢迎去看我的主页
: NicholasYe’s Hompage.
PID
PID方程
PID标准方程:
- P:
计算目标值与当前值的差值
Term P is proportional to the current value of the SP − PV error {\displaystyle e(t)}e(t). For example, if the error is large and positive, the control output will be proportionately large and positive, taking into account the gain factor “K”. Using proportional control alone will result in an error between the setpoint and the actual process value because it requires an error to generate the proportional response. If there is no error, there is no corrective response.
- I:
积分过去的差值
Term I accounts for past values of the SP − PV error and integrates them over time to produce the I term. For example, if there is a residual SP − PV error after the application of proportional control, the integral term seeks to eliminate the residual error by adding a control effect due to the historic cumulative value of the error. When the error is eliminated, the integral term will cease to grow. This will result in the proportional effect diminishing as the error decreases, but this is compensated for by the growing integral effect.
- D:
微分未来的差值
Term D is a best estimate of the future trend of the SP − PV error, based on its current rate of change. It is sometimes called “anticipatory control”, as it is effectively seeking to reduce the effect of the SP − PV error by exerting a control influence generated by the rate of error change. The more rapid the change, the greater the controlling or damping effect.
PID三者讲解推荐看:B站
PID算法
注:本文的算法均取自于arduino的PID库中部分代码,可能有部分函数为arduino独有。但PID算法的核心经过编程后可用于任意平台。
标准的PID控制器:
/*working variables*/
unsigned long lastTime;
double Input, Output, Setpoint;
double errSum, lastErr;
double kp, ki, kd;
void Compute()
{
/*How long since we last calculated*/
unsigned long now = millis();
//millis(): Returns the number of milliseconds passed since the Arduino board began running the current program.
double timeChange = (double)(now - lastTime);
/*Compute all the working error variables*/
double error = Setpoint - Input;//误差值
errSum += (error * timeChange);//误差的积分
double dErr = (error - lastErr) / timeChange;//误差的微分
/*Compute PID Output*/
Output = kp * error + ki * errSum + kd * dErr;
/*Remember some variables for next time*/
lastErr = error;
lastTime = now;
}
void SetTunings(double Kp, double Ki, double Kd)
{
kp = Kp;
ki = Ki;
kd = Kd;
}
此外,可在函数库里找到PID计算函数
bool PID::Compute()
{
if(!inAuto) return false;
unsigned long now = millis();
unsigned long timeChange = (now - lastTime);
if(timeChange>=SampleTime)
{
/*Compute all the working error variables*/
double input = *myInput;
double error = *mySetpoint - input;
double dInput = (input - lastInput);
outputSum+= (ki * error);
/*Add Proportional on Measurement, if P_ON_M is specified*/
if(!pOnE) outputSum-= kp * dInput;
if(outputSum > outMax) outputSum= outMax;
else if(outputSum < outMin) outputSum= outMin;
/*Add Proportional on Error, if P_ON_E is specified*/
double output;
if(pOnE) output = kp * error;
else output = 0;
/*Compute Rest of PID Output*/
output += outputSum - kd * dInput;
if(output > outMax) output = outMax;
else if(output < outMin) output = outMin;
*myOutput = output;
/*Remember some variables for next time*/
lastInput = input;
lastTime = now;
return true;
}
else return false;
}
关于此代码的链接:
请在转载文章过程中明确标注文章出处!尊重原创,尊重知识产权,谢谢!