|
这个采样时间的用法不太懂,在下面的程序中并没有使用采样时间
unsigned long lastTime;
doubleInput, Output, Setpoint;
doubleerrSum, lastErr;
doublekp, ki, kd;
void Compute()
{
/*上次计算时间*/
unsigned long now = millis();
doubletimeChange = (double)(now - lastTime);
/*按公式写出如下的式子*/
doubleerror = Setpoint - Input;
errSum += (error * timeChange);
doubledErr = (error - lastErr) / timeChange;
/*计算 PID的输出*/
Output = kp * error + ki * errSum + kd * dErr;
/*记录下一个采样时间PID参数的值*/
lastErr = error;
lastTime = now;
}
void SetTunings(doubleKp,doubleKi,doubleKd)
{
kp = Kp;
ki = Ki;
kd = Kd;
}
|
|