下面废话少说直接步入正题:
首先PID最重要的不过就是调参部分,这里我仍然重复一下我认为所谓的调参口诀吧。
PID中P调大增大响应速度,D调大增加预判,I消除稳差,这三个系数并不是越大越好,P太大会超调,D太大会震荡,i值有累计效应。
这里不懂具体什么是PID的建议看一下我上篇博客自控篇(一):PID算法的基础了解_靳小锅er的博客-CSDN博客
下面正式开始正题:
下面直接上有关PID代码
void CAM_PID_Controler(void) {
static int bias_i=0;
static int last_bias = 0;
switch(en_line) {
case front_line: {
double bias = rou[0] +theta[0] -offset;
double uk_Motor,uk_Steer;
bias_i += bias;
if(bias_i>=3000)bias_i = 1500;
if(bias_i<=-3000)bias_i = -1500;
uk_Motor = kp_Motor*bias + ki_Motor*bias_i + kd_Motor*(bias-last_bias);
uk_Steer = kp_Steer*bias + ki_Steer*bias_i + kd_Steer*(bias-last_bias);
targetL_A = targetL + uk_Motor;
targetR_A = targetR - uk_Motor;
pwmSteer2 = targetSteer2 + uk_Steer;
if(pwmSteer2 > 2000) pwmSteer2 = 1700;
else if(pwmSteer2 < 1000) pwmSteer2 = 1100;
last_bias = bias;
break;
}
case back_line: {
double bias= rou[0] +theta[0] -offset;
double uk_Motor,uk_Steer;
bias_i += bias;
if(bias_i>=3000)bias_i = 1500;
if(bias_i<=-3000)bias_i = -1500;
uk_Motor = rp_Motor*bias + /*ri_Motor*bias_i +*/ rd_Motor*(bias-last_bias);
uk_Steer = rp_Steer*bias + /*ri_Steer*bias_i +*/ rd_Steer*(bias-last_bias);
targetL_A = targetL + uk_Motor;
targetR_A = targetR - uk_Motor;
pwmSteer2 = targetSteer2 - uk_Steer;
if(pwmSteer2 > 2000) pwmSteer2 = 1700;
else if(pwmSteer2 < 1000) pwmSteer2 = 1100;
last_bias = bias;
break;
}
case front_left_line: {
double bias = (rou[0] -fabs(rou[1]))/2 +(theta[0]-fabs(theta[1]))/2 -offset;
double uk_Motor,uk_Steer;
bias_i += bias;
if(bias_i>=3000)bias_i = 1500;
if(bias_i<=-3000)bias_i = -1500;
uk_Motor = kp_Motor*bias + ki_Motor*bias_i + kd_Motor*(bias-last_bias);
uk_Steer = kp_Steer*bias + ki_Steer*bias_i + kd_Steer*(bias-last_bias);
targetL_A = targetL + uk_Motor;
targetR_A = targetR - uk_Motor;
pwmSteer2 = targetSteer2 + uk_Steer;
if(pwmSteer2 > 2000) pwmSteer2 = 1700;
else if(pwmSteer2 < 1000) pwmSteer2 = 1100;
last_bias = bias;
break;
}
case front_right_line: {
double bias = (rou[0] +fabs(rou[1]))/2 +(theta[0]+fabs(theta[1]))/2 -offset;
double uk_Motor,uk_Steer;
bias_i += bias;
if(bias_i>=3000)bias_i = 1500;
if(bias_i<=-3000)bias_i = -1500;
uk_Motor = kp_Motor*bias + ki_Motor*bias_i + kd_Motor*(bias-last_bias);
uk_Steer = kp_Steer*bias + ki_Steer*bias_i + kd_Steer*(bias-last_bias);
targetL_A = targetL + uk_Motor;
targetR_A = targetR - uk_Motor;
pwmSteer2 = targetSteer2 + uk_Steer;
if(pwmSteer2 > 2000) pwmSteer2 = 1700;
else if(pwmSteer2 < 1000) pwmSteer2 = 1100;
last_bias = bias;
break;
}
case dis_line:
default:
{
targetL_A = targetL;
targetR_A = targetR;
pwmSteer2 = targetSteer2;
break;
}
}
}
其实其中有关于PID的核心代码很短,不过就是分为三个量,Bias和last_bias还有Bias-last_bias;根据公式kp_Motor*bias + ki_Motor*bias_i + kd_Motor*(bias-last_bias)求出该设置的pwm占空比,这里不再一一赘述。
下面我们开始如何进行PID三个参数的调试。
明天再更