# arduino黑线循迹小车程序_基于arduino的循迹小车（含有PID算法）

## 一、简单的闭环赛道

(1)逻辑部分：

(2)各程序片段

void loop(){     read_sensor_values();  //获取5个循迹模块的数值情况     calc_pid();            //pid计算出转向的pwm值     motor_control();       //电机转动}

void read_sensor_values(){  sensor[0] = digitalRead(leftA_track_PIN);  sensor[1] = digitalRead(leftB_track_PIN);  sensor[2] = digitalRead(middle_track_PIN);  sensor[3] = digitalRead(righA_track_PIN);  sensor[4] = digitalRead(righB_track_PIN);    if ((sensor[0] == 0) && (sensor[1] == 0) && (sensor[2] == 0) && (sensor[3] == 0) && (sensor[4] == 1)) {      error = 2;//          0 0 0 0 1    } else if ((sensor[0] == 0) && (sensor[1] == 0) && (sensor[2] == 0) && (sensor[3] == 1) && (sensor[4] == 0)) {      error = 1;//          0 0 0 1 0    } else if ((sensor[0] == 0) && (sensor[1] == 0) && (sensor[2] == 1) && (sensor[3] == 0) && (sensor[4] == 0)) {      error = 0;//          0 0 1 0 0    } else if ((sensor[0] == 0) && (sensor[1] == 1) && (sensor[2] == 0) && (sensor[3] == 0) && (sensor[4] == 0)) {      error = -1;//         0 1 0 0 0    } else if ((sensor[0] == 1) && (sensor[1] == 0) && (sensor[2] == 0) && (sensor[3] == 0) && (sensor[4] == 0)) {      error = -2;//         1 0 0 0 0    } else if ((sensor[0] == 0) && (sensor[1] == 0) && (sensor[2] == 0) && (sensor[3] == 0) && (sensor[4] == 0)) {      if (error == -2) {//  0 0 0 0 0        error = -3;      }else{        error = 3;      }    }  }

<1>  将读取到的五个循迹模块的数据存入数组sensor：

<2>  检测到哪种情况对应着哪种反应：

if ((sensor[0] == 0) && (sensor[1] == 0) && (sensor[2] == 0) && (sensor[3] == 0) && (sensor[4] == 1)) {

error = 2;//          0 0 0 0 1

} else if ((sensor[0] == 0) && (sensor[1] == 0) && (sensor[2] == 0) && (sensor[3] == 1) && (sensor[4] == 0)) {

error = 1;//          0 0 0 1 0

} else if ((sensor[0] == 0) && (sensor[1] == 0) && (sensor[2] == 1) && (sensor[3] == 0) && (sensor[4] == 0)) {

error = 0;//          0 0 1 0 0

} else if ((sensor[0] == 0) && (sensor[1] == 1) && (sensor[2] == 0) && (sensor[3] == 0) && (sensor[4] == 0)) {

error = -1;//         0 1 0 0 0

} else if ((sensor[0] == 1) && (sensor[1] == 0) && (sensor[2] == 0) && (sensor[3] == 0) && (sensor[4] == 0)) {

error = -2;//         1 0 0 0 0

} else if ((sensor[0] == 0) && (sensor[1] == 0) && (sensor[2] == 0) && (sensor[3] == 0) && (sensor[4] == 0)) {

if (error == -2) {//  0 0 0 0 0

error = -3;

}else{

error = 3;

}

}

else if ((sensor[0] == 0) && (sensor[1] == 0) && (sensor[2] == 0) && (sensor[3] == 0) && (sensor[4] == 0)) {

检测到为0000的情况

if (error == -2) {// //如果上一次 error == -2

error = -3;

}else{

error = 3;

}

}

void calc_pid(){  P = error;  I = I + error;  D = error - previous_error;   PID_value = (Kp * P) + (Ki * I) + (Kd * D);   previous_error = error;}

//速度设定范围(-255,255)void motorsWrite(int speedL, int speedR){  if (speedR > 0) {    analogWrite(leftA_PIN, speedR);    analogWrite(leftB_PIN, 0);  } else {    analogWrite(leftA_PIN, 0);    analogWrite(leftB_PIN, -speedR);  }   if (speedL > 0) {    analogWrite(righA_PIN, speedL);    analogWrite(righB_PIN, 0);  } else {    analogWrite(righA_PIN, 0);    analogWrite(righB_PIN, -speedL);  }}//速度设定范围(-100，100)void motorsWritePct(int speedLpct, int speedRpct) {  //speedLpct, speedRpct ranges from -100 to 100  motorsWrite(speedLpct * 2.55, speedRpct * 2.55);}void motor_control(){  int left_motor_speed = initial_motor_speed - PID_value;  int right_motor_speed = initial_motor_speed + PID_value;    if(left_motor_speed < -255){    left_motor_speed = -255;  }  if(left_motor_speed > 255){    left_motor_speed = 255;  }  motorsWrite(left_motor_speed,right_motor_speed);} 

(1)逻辑部分：

(2)各程序片段

void read_sensor_values(){  sensor[0] = digitalRead(leftA_track_PIN);  sensor[1] = digitalRead(leftB_track_PIN);  sensor[2] = digitalRead(middle_track_PIN);  sensor[3] = digitalRead(righA_track_PIN);  sensor[4] = digitalRead(righB_track_PIN);        if ((sensor[0] == 1) && (sensor[1] == 1) && (sensor[2] == 1) && (sensor[3] == 1) && (sensor[4] == 1)) {      decide = 1;//十字路口 1 1 1 1 1   直行    } else if ((sensor[0] == 0) && (sensor[1] == 1) && (sensor[2] == 1) && (sensor[3] == 1) && (sensor[4] == 0)) {      decide = 1;//十字路口 0 1 1 1 0   直行    } else if ((sensor[0] == 0) && (sensor[1] == 0) && (sensor[2] == 1) && (sensor[3] == 1) && (sensor[4] == 1)) {      decide = 2;//90度路口 0 0 1 1 1    右转90度    } else if ((sensor[0] == 0) && (sensor[1] == 0) && (sensor[2] == 1) && (sensor[3] == 1) && (sensor[4] == 0)) {      decide = 2;//90度路口 0 0 1 1 0    右转90度     } else if ((sensor[0] == 1) && (sensor[1] == 1) && (sensor[2] == 1) && (sensor[3] == 0) && (sensor[4] == 0)) {      decide = 3;//90度路口 1 1 1 0 0    左转90度     } else if ((sensor[0] == 0) && (sensor[1] == 1) && (sensor[2] == 1) && (sensor[3] == 0) && (sensor[4] == 0)) {      decide = 3;//90度路口 0 1 1 0 0    左转90度     } else if ((sensor[0] == 0) && (sensor[1] == 1) && (sensor[2] == 1) && (sensor[3] == 0) && (sensor[4] == 0)) {      decide = 3;//向上锐角 0 1 1 0 0    向上锐角     } else if ((sensor[0] == 0) && (sensor[1] == 0) && (sensor[2] == 0) && (sensor[3] == 0) && (sensor[4] == 1)) {      error = 2;//          0 0 0 0 1    } else if ((sensor[0] == 0) && (sensor[1] == 0) && (sensor[2] == 0) && (sensor[3] == 1) && (sensor[4] == 0)) {      error = 1;//          0 0 0 1 0    } else if ((sensor[0] == 0) && (sensor[1] == 0) && (sensor[2] == 1) && (sensor[3] == 0) && (sensor[4] == 0)) {      error = 0;//          0 0 1 0 0    } else if ((sensor[0] == 0) && (sensor[1] == 1) && (sensor[2] == 0) && (sensor[3] == 0) && (sensor[4] == 0)) {      error = -1;//         0 1 0 0 0    } else if ((sensor[0] == 1) && (sensor[1] == 0) && (sensor[2] == 0) && (sensor[3] == 0) && (sensor[4] == 0)) {      error = -2;//         1 0 0 0 0    } else if ((sensor[0] == 0) && (sensor[1] == 0) && (sensor[2] == 0) && (sensor[3] == 0) && (sensor[4] == 0)) {      if (error == -2) {//  0 0 0 0 0        error = -3;      }else{        error = 3;      }    } }

https://pan.baidu.com/s/10tJzqiAO0Ph86aIfmUzrHg

https://pan.baidu.com/s/1EckZUv66bMJdf7SecpYqcw

04-15
08-11 5万+
02-24 1万+
09-04 11万+
12-08 2489
02-03
05-19 6159
06-06 1万+
06-20 5815