|
基于arduino2560的循线小车的整体图
部分代码如下
- /*
- * 电机:
- * 10 -> 左侧:前进( > 90 ) ; 后退( < 90 ) ; 停止( 82 - 92)
- * 12 -> 右侧:前进( < 90 ) ; 后退( > 90 ) ; 停止( 93 - 103);
- * F表示前B为后,D是在双线上,S表示单线上
- *
- */
- /****************************整体实验例程****************************/
- #include <Arduino.h>
- #include <Wire.h>
- #include <MultiLCD.h>
- #include <ServoTimer2.h>
- //定义车行进方向
- enum Dir{
- FORWARD = 1, BACK, LEFT, RIGHT, STOP
- };
- enum Trackingmode{
- FD= 1, BD, FS, BS //举例FD就表示在双线上前进循迹,主要前传感器
- };//
- enum Turnmode{
- RFD= 1, LFD, RBD, LBD, RFS, LFS, RBS, LBS
- //举例RFD表示 R向右转弯,F转完后要前进,D表示在双线上
- };
- //Motor
- int left_motor_port = 4; //定义左侧电机引脚
- int right_motor_port = 7; //定义右侧电机引脚
- int motor_speed = 135; //定义电机停止速度
- void setup() {
- Serial.begin(9600);
- SensorInit();
- MotorInit();
-
- }
- /******************************设备初始化部分********************************/
- //传感器引脚初始化
- void SensorInit(){
- //pinMode(f[3], INPUT);
- //pinMode(rf_s, INPUT);
- for(int i=0;i<8;i++){pinMode(f[i], INPUT);} //前传感器数组
- //pinMode(b[3], INPUT);
- //pinMode(b[6], INPUT);
- for(int i=0;i<8;i++){pinMode(b[i], INPUT);} //后传感器数组
- //pinMode(l_s, INPUT);
- //pinMode(r_s, INPUT);
- for(int i=0;i<8;i++){pinMode(l[i], INPUT);} //中间传感器数组
- pinMode(debug_sensor, INPUT);
- //digitalWrite(A1,LOW);
- //digitalWrite(A5,LOW);
-
- }
- /******************************车行进控制部分********************************/
- int left_speed = 20;
- int right_speed = 20;
- void SpeedSet(int l, int r){
- left_speed = l; right_speed = r;
- }
- void Forward(){
- l_Servo.write(Angle2Pwm(motor_speed + left_speed));
- r_Servo.write(Angle2Pwm(motor_speed - right_speed));
- }
- void Back(){
- l_Servo.write(Angle2Pwm(motor_speed - left_speed));
- r_Servo.write(Angle2Pwm(motor_speed + right_speed));
- }
- void Left(){
- l_Servo.write(Angle2Pwm(motor_speed - 1.5*left_speed));
- r_Servo.write(Angle2Pwm(motor_speed - 1.5*right_speed));
- }
- void Right(){
- l_Servo.write(Angle2Pwm(motor_speed + 1.5*left_speed));
- r_Servo.write(Angle2Pwm(motor_speed + 1.5*right_speed));
- }
- void Stop(){
- l_Servo.write(Angle2Pwm(motor_speed));
- r_Servo.write(Angle2Pwm(motor_speed));
- delay(1000);
- }
- void Move(int dir){
- switch(dir){
- case FORWARD: Forward(); break;
- case BACK: Back(); break;
- case LEFT: Left(); break;
- case RIGHT: Right(); break;
- case STOP: Stop(); break;
- default: break;
- }
- }
- //电机速度写入函数,调速的时候使用
- void CorrectLeftF(){
- l_Servo.write(Angle2Pwm(motor_speed));
- r_Servo.write(Angle2Pwm(motor_speed - 20));
- }
- void CorrectRightF(){
- l_Servo.write(Angle2Pwm(motor_speed + 20));
- r_Servo.write(Angle2Pwm(motor_speed));
- }
- void CorrectRightB(){
- l_Servo.write(Angle2Pwm(motor_speed - 20));
- r_Servo.write(Angle2Pwm(motor_speed));
- }
- void CorrectLeftB(){
- l_Servo.write(Angle2Pwm(motor_speed ));
- r_Servo.write(Angle2Pwm(motor_speed + 20));
- }
- /*
- void Turn(int mode){ //转向函数
- switch(mode){
-
- case 1: //RF_D
- Move(RIGHT);
- while(!digitalRead(f[7]));
- //delay(500);
- while(!digitalRead(f[5]));
- SpeedSet(15,15);
- Move(RIGHT);
- //delay(dy);
- while(!digitalRead(f[2]));
- Move(STOP);
- SpeedSet(20,20);
- break;
-
- case 2: //LF_D
- Move(LEFT);
- while(!digitalRead(f[0]));
- //delay(500);
- while(!digitalRead(f[2]));
- SpeedSet(15,15);
- Move(LEFT);
- //delay(dy);
- while(!digitalRead(f[5]));
- Move(STOP);
- SpeedSet(20,20);
- break;
-
- case 3: //RB_D
- Move(RIGHT);
- while(!digitalRead(b[7]));
- //delay(500);
- while(!digitalRead(b[5]));
- SpeedSet(15,15);
- Move(RIGHT);
- //delay(dy);
- while(!digitalRead(b[2]));
- Move(STOP);
- SpeedSet(20,20);
- break;
-
- case 4: //LB_D
- Move(LEFT);
- while(!digitalRead(b[0]));
- //delay(500);
- while(!digitalRead(b[2]));
- SpeedSet(15,15);
- Move(LEFT);
- //delay(dy);
- while(!digitalRead(b[5]));
- Move(STOP);
- SpeedSet(20,20);
- break;
-
- case 5: //RF_S
- Move(RIGHT);
- while(!digitalRead(f[7]));
- //delay(500);
- while(!digitalRead(f[6]));
- SpeedSet(15,15);
- Move(RIGHT);
- //delay(dy);
- while(!digitalRead(f[3]));
- Move(STOP);
- SpeedSet(20,20);
- break;
-
- case 6: //LF_S
- Move(LEFT);
- while(!digitalRead(f[0]));
- //delay(500);
- while(!digitalRead(f[1]));
- SpeedSet(15,15);
- Move(LEFT);
- //delay(dy);
- while(!digitalRead(f[4]));
- Move(STOP);
- SpeedSet(20,20);
- break;
-
- case 7: //RB_S
- Move(RIGHT);
- while(!digitalRead(b[7]));
- //delay(500);
- while(!digitalRead(b[6]));
- SpeedSet(15,15);
- Move(RIGHT);
- //delay(dy);
- while(!digitalRead(b[3]));
- Move(STOP);
- SpeedSet(20,20);
- break;
-
- case 8: //LB_S
- Move(LEFT);
- while(!digitalRead(b[0]));
- //delay(500);
- while(!digitalRead(b[1]));
- SpeedSet(15,15);
- Move(LEFT);
- //delay(dy);
- while(!digitalRead(b[4]));
- Move(STOP);
- SpeedSet(20,20);
- break;
- }
- }
- /******************************路径部分********************************/
- /*
- * 循迹函数
- * 返回值:无
- * 参数:mode:
- * FD:前进双线循迹
- * BD:后退双线循迹
- * FS:前进单线循迹
- * BS:后退单线循迹
- * 功能:通过车底传感器检测路线进行循迹
- */
- void Tracking(int mode){
- if(mode==1){//FD
- if(digitalRead(f[2]) && digitalRead(f[5])){
- Move(FORWARD);
- delay(20);
- }
- else if(digitalRead(f[2])){
- CorrectLeftF();
- }
- else if(digitalRead(f[5])){
- CorrectRightF();
- }
- else
- Move(FORWARD);
- delay(20);
-
- }
-
- if(mode==2){ //BD
- if(digitalRead(b[2]) && digitalRead(b[5])){
- Move(BACK);
- delay(20);
- }
- else if(digitalRead(b[2])){
- CorrectRightB();
- }
- else if(digitalRead(b[5])){
- CorrectLeftB();
- }
- else
- Move(BACK);
- delay(20);
-
- }
-
- if(mode==3){ //FS
- if(digitalRead(f[3]) && digitalRead(f[4])){
- Move(FORWARD);
- delay(20);
- }
- else if(digitalRead(f[3])){
- CorrectLeftF();
- }
- else if(digitalRead(f[4])){
- CorrectRightF();
- }
- else
- Move(FORWARD);
- delay(20);
-
- }
-
- if(mode==4){ //BS
- if(digitalRead(b[3]) && digitalRead(b[4])){
- Move(BACK);
- delay(20);
- }
- else if(digitalRead(b[3])){
- CorrectRightB();
- }
- else if(digitalRead(b[4])){
- CorrectLeftB();
- }
- else
- Move(BACK);
- delay(20);
- }
- }
- /*
- * 循线计数函数
- * 返回值:无
- * 参数:mode:
- * FD:前进双线循迹
- * BD:后退双线循迹
- * FS:前进单线循迹
- * BS:后退单线循迹
- * 参数:line:表示准备走几条线
- * 功能:确定走的线数
- */
- void Trackingline(int mode, int line){
- switch(mode){
- case 1:
- for(int i=0;i<20;i++){Tracking(FD);}
- while(1){
- if(meridians < line){
- if(meridians < line-1){
- if(digitalRead(l[3])){
- meridians++;
- while(digitalRead(l[3]));
- delay(200);
- }
- else{
- Tracking(FD);
- }
- }
- else{
- if(digitalRead(l[3])){meridians++;}
- else{Tracking(FD);}
- }
- }
- else{
- //Move(BACK);
- //delay(200);
- //Move(STOP);
- meridians = 0;
- //Move(BACK);
- //while(!digitalRead(l[3]));
- Move(STOP);
- break;
- }
-
- }
- break;
- case 2:
- for(int i=0;i<20;i++){Tracking(BD);}
- while(1){
- if(meridians < line){
- if(meridians < line-1){
- if(digitalRead(l[4])){
- meridians++;
- while(digitalRead(l[4]));
- delay(200);
- }
- else{
- Tracking(BD);
- }
- }
- else{
- if(digitalRead(l[4])){meridians++;}
- else{Tracking(BD);}
- }
- }
- else{
- //Move(FORWARD);
- //delay(200) ;
- //Move(STOP);
- meridians = 0;
- //Move(FORWARD);
- //while(!digitalRead(l[4]));
- Move(STOP);
- break;
- }
- }
- break;
-
-
- case 3:
- for(int i=0;i<20;i++){Tracking(FS);}
- while(1){
- if(meridians < line){
- if(meridians < line-1){
- if(digitalRead(l[3])){
- meridians++;
- while(digitalRead(l[3]));
- delay(600);
- }
- else{
- Tracking(FS);
- }
- }
- else{
- if(digitalRead(l[3])){meridians++;}
- else{Tracking(FS);}
- }
- }
- else{
- //Move(BACK);
- //delay(200);
- //Move(STOP);
- meridians = 0;
- //Move(BACK);
- //while(!digitalRead(l[3]));
- Move(STOP);
- break;
- }
- }
- break;
- case 4:
- for(int i=0;i<20;i++){Tracking(BS);}
- while(1){
- if(meridians < line){
- if(meridians < line-1){
- if(digitalRead(l[4])){
- meridians++;
- while(digitalRead(l[4]));
- delay(600);
- }
- else{
- Tracking(BS);
- }
- }
- else{
- if(digitalRead(l[4])){meridians++;}
- else{Tracking(BS);}
- }
- }
- else{
- //Move(FORWARD);
- //delay(200);
- //Move(STOP);
- meridians = 0;
- //Move(FORWARD);
- //while(!digitalRead(l[4]));
- Move(STOP);
- break;
- }
- }
- break;
-
- }
- }
- /******************************路段部分********************************/
- //主函数部分,直接写入要走的路
- void loop (){
- Turn(LFD);
- Trackingline(FD,3);
- Turn(RBS);
- Trackingline(BS,1);
- Turn(LBD);
- Trackingline(BD,2);
- Turn(RFS);
- Trackingline(FS,1);
- }
复制代码 |
-
w.png
(935.92 KB, 下载次数: 142)
使用的八路巡线传感器 前中后各一 全部接在数字口上
-
e.jpg
(55.61 KB, 下载次数: 130)
小车跑的场地,由单双线交叉构成的网格,本程序让小车沿线跑
-
-
sketch_mar30a.zip
6.61 KB, 下载次数: 27, 下载积分: 黑币 -5
程序的源码
评分
-
查看全部评分
|