- 这是arduino的基本PID程序
- 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();
- 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;
- }
复制代码
这是arduino的基本PID程序
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();
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的基本程序,我看了一下,一知半解,如果用到我的这种小车里,
input:编码器检测小车速度值,
output:输出小车速度:
setpoint:设置小车直线标准速度
unsigned long now = millis();//这是arduino中计时的一个函数,即now为从程序执行到此的时间
我不太了解timechange计算是从哪开始到哪结束???????
它是不是就相当于执行PID的一个周期
一周热门 更多>