• 发表于 1年前
• 阅读 1314
• 收藏 0
• 评论 1

# 解决方案

Setpoint这一项就会产生一个巨大的冲击，而且仅会产生一次，在下一个计算周期就会消失。处理的方法很多，最简单的就是讲Serpoint这一项移除，也就是认为对于微分项来说不存在设定值的改变。如果这么处理，系统会不会失控呢？这种玩法已经和我们根深蒂固的经典PID理论不一样了啊！！这怎么整！

# 代码

``````/*working variables*/
unsigned long lastTime;
double Input, Output, Setpoint;
double errSum, lastInput;
double kp, ki, kd;
int SampleTime = 1000; //1 sec
void Compute()
{
unsigned long now = millis();
int timeChange = (now - lastTime);
if(timeChange>=SampleTime)
{
/*Compute all the working error variables*/
double error = Setpoint - Input;
errSum += error;
double dInput = (Input - lastInput);

/*Compute PID Output*/
Output = kp * error + ki * errSum - kd * dInput;

/*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;
}
}``````

# 结论

1. Output梯度尖峰被去除
2. 控制量原来存在一个尖峰被消除
3. 被控量的微分变得较为平坦

NOTE：如有不足之处请告知。^.^

NEXT

PS：转载请注明出处：欧阳天华

×