|
循迹小车出弯道时出现左右摇摆的现象,该如何解决?车子前后使用两个五路红外对管传感器- //=======================================================
- // 975=转向轮向左转 车身左侧电机1
- //=======================================================
- #include <Servo.h>
- #define SER1_BAUD 115200
- /*电机驱动引脚*/
- #define PWMB_IN1 11 //定义IN1引脚
- #define PWMB_IN2 6 //定义IN2引脚
- #define PWMA_IN1 5 //定义IN3引脚
- #define PWMA_IN2 3 //定义IN4引脚
- #define Servor_Pin 7
- #define BEEP_PIN 12 //定义蜂鸣器引脚D6
- #define NLED_PIN 13 //定义呼吸灯引脚D11
- #define IR1 A1
- #define IR2 A0
- #define IR3 A3
- #define IR4 A2
- #define IR5 10
- #define IH2 0
- #define IH3 A5
- #define IH4 A4
- #define NLED_MS_BETWEEN 500
- #define DUOJI_MS_BETWEEN 20.000
- int Out1,Out2,Out3,Out4,Out5,Out2H,Out3H,Out4H;
- int pwm_value=1435;
- int pwm_R=1140;
- int pwm_L=1860;
- int Echo =12;
- int Trig =13;
- int Distance = 50;
- double sv=130; //直线速度
- double bv=130; //24转速
- double cv=140; //curve弯道速度
- int c1=0; //矫正指示值
- int c5=0;
- int fore=0; //前部判断
- int f=0; //前部触发
- int dj=0; //舵机
- int hou=0; //尾部判断
- int h=0; //尾部光电
- int z=0; //障碍位置设定L==0,R==1
- double v=1; //累计车速
- char val=0;
- double num1=1;
- double num2=1;
- double num3=1;
- double num4=1;
- double num5=1;
- double n1=1;
- double n2=1;
- double n4=1;
- double n5=1;
- double st=1; //stop定时
- unsigned long time1;
- unsigned long timel=0;
- unsigned long timer=0;
- unsigned long dif=0;
- Servo myservo; //创建舵机伺服对象数组
- unsigned char handle_ms_between( unsigned long *time_ms, unsigned int ms_between);
- //void handle_nled();
- void Motor_Forward(char motor,char pwm);
- void Motor_Back(char motor,char pwm); //电机反转
- void Motor_Right();
- void Motor_Left();
- void Distance_test();
- void dida(u8 times, u8 frequency);
- void rear_judge();
- void fore_judge();
- void correct();
- void R_count();
- void L_count();
- void dis();
- void detect();
- void count();
- void setup()
- {
- pinMode(BEEP_PIN, OUTPUT);
- // pinMode(NLED_PIN, OUTPUT);
- pinMode(IR1, INPUT);
- pinMode(IR2, INPUT);
- pinMode(IR3, INPUT);
- pinMode(IR4, INPUT);
- pinMode(IR5, INPUT);
- pinMode(IH2, INPUT);
- pinMode(IH3, INPUT);
- pinMode(IH4, INPUT);
- pinMode(Echo, INPUT);
- pinMode(Trig, OUTPUT);
- Serial.begin(SER1_BAUD);
- myservo.attach(Servor_Pin);
- myservo.writeMicroseconds(pwm_value);
- dida(1, 1000);
- delay(1000);
- }
- void loop()
- {
- while(1)
- {
- Distance_test();
- time1=millis();
- Serial.print("time:");
- detect();
- fore_judge(); //前轮位置判断
- rear_judge(); //后轮位置判断
- count(); //计时
- // dif=abs(timel-timer);
- // Serial.print("timer:");
- // Serial.println(timer);
- // Serial.print("timel:");
- // Serial.println(timel);
- //if(dif<=1600 && dif>=10)
- // {
- // correct();
- // Motor_Forward(1,255);
- // Motor_Forward(2,255);
- // delay(5000);
- // Serial.print("dif:");
- // Serial.println(dif);
- // }
- // else
- // {;}
- // Serial.println(c1);
-
- //=======================================================
- // 主程序
- //=======================================================
- if(Out1==1 && Out2==1 && Out3==0 && Out4==1 && Out5==1) //直行
- {
- v=num3;
- if(hou==3)
- {
- Motor_Forward(1,sv-v);
- Motor_Forward(2,sv-v);
- myservo.writeMicroseconds(pwm_value);
- }
- else
- {
- v=num3;
- v=v*0.9;
- Motor_Forward(1,sv-v);
- Motor_Forward(2,sv-v);
- }
- }
-
- else if(Out1==0 && Out2==1 && Out3==1 && Out4==1 && Out5==1) //1左转
- {
- while( Out4==1 )
- {
- Motor_Left();
- timel=millis();
- }
- num3=0;
- }
-
- else if(Out1==1 && Out2==1 && Out3==1 && Out4==0 && Out5==1) //4右转
- {
- v=num3;
- v=v*0.7;
- dj=1;
- if(hou==3)
- {
- Motor_Forward(1,bv-v); //bv=160,v=110,num=20
- Motor_Forward(2,bv-20-v-num4);
- myservo.writeMicroseconds(pwm_value-80);
- }
- else if(hou==4 && h==0)
- {
- Motor_Forward(1,bv-v);
- Motor_Forward(2,bv-20-v-num4);
- myservo.writeMicroseconds(pwm_value-40);
- }
- else if(hou==2 && h==0)
- {
- Motor_Forward(1,bv-v);
- Motor_Forward(2,bv-20-v-num4);
- myservo.writeMicroseconds(pwm_value-130);
- }
- else if(h==1 && hou==4)
- {
- Motor_Forward(1,bv-v);
- Motor_Forward(2,bv-20-v-num4);
- myservo.writeMicroseconds(pwm_value);
- }
- else if(h==1 && hou==2)//-
- {
- Motor_Forward(1,bv-20-v-num4);
- Motor_Forward(2,bv-v);
- myservo.writeMicroseconds(pwm_value-180);
- }
- else
- {
- v=num3;
- Motor_Forward(1,bv-v);
- Motor_Forward(2,bv-v);
- }
- }
-
- else if(Out1==1 && Out2==1 && Out3==1 && Out4==1 && Out5==0) //5右转
- {
- while( Out2==1)
- {
- Motor_Right();
- timer=millis();
- }
- num3=0;
- }
-
- else if(Out1==1 && Out2==0 && Out3==1 && Out4==1 && Out5==1) //2左转
- {
- v=num3;
- v=v*0.7;
- dj=1;
- if(hou==3) //bv=160,v=110,num=20
- {
- Motor_Forward(1,bv-20-v-num2);
- Motor_Forward(2,bv-v);
- myservo.writeMicroseconds(pwm_value+80);
- }
- else if(hou==2 && h==0)
- {
- Motor_Forward(1,bv-20-v-num2);
- Motor_Forward(2,bv-v);
- myservo.writeMicroseconds(pwm_value+40);
- }
- else if(hou==4 && h==0)
- {
- Motor_Forward(1,bv-v);
- Motor_Forward(2,bv-30-v);
- myservo.writeMicroseconds(pwm_value+130);
- }
- else if(h==1 && hou==2)
- {
- Motor_Forward(1,bv-20-v-num2);
- Motor_Forward(2,bv-v);
- myservo.writeMicroseconds(pwm_value);
- }
- else if(h==1 && hou==4)
- {
- Motor_Forward(1,bv-v);
- Motor_Forward(2,bv-20-v-num2);
- myservo.writeMicroseconds(pwm_value+180);
- }
- else
- {
- Motor_Forward(1,bv-v);
- Motor_Forward(2,bv-v);
- }
- }
- else if(Out1==0 && Out2==0 && Out3==0 && Out4==0 && Out5==0) //stop计数
- {
- if(st>=100)
- {
- Motor_Forward(1,255);
- Motor_Forward(2,255);
- break;//还是用return
- }
- else
- {;}
- }
- else
- {
- v=num3;
- Motor_Forward(1,sv-v);
- Motor_Forward(2,sv-v);
- }
- dis();
- }
- }
- void Motor_Right() //5右转调用程序 wandaozhuanjiaodizeng
- {
- detect();
- fore_judge();
- rear_judge();
- R_count();
- if(Out1==1 && Out2==1 && Out3==1 && Out4==1 && Out5==0)
- {
- if(dj==1 && h==0)
- {
- Motor_Forward(1,170);
- Motor_Forward(2,80);
- myservo.writeMicroseconds(pwm_value-360);
- }
- else if(dj==1 && h==1 && hou==3)
- {
- Motor_Forward(1,170);
- Motor_Forward(2,80);
- myservo.writeMicroseconds(pwm_value-360);
- }
- else if(dj==1 && h==1 && hou==2) //-
- {
- Motor_Forward(1,170);
- Motor_Forward(2,80);
- myservo.writeMicroseconds(pwm_value-360);
- }
- else if(dj==1 && h==1 && hou==4)
- {
- Motor_Forward(1,170);
- Motor_Forward(2,80);
- myservo.writeMicroseconds(pwm_value-360);
- }
- else //*******
- {
- Motor_Forward(1,160);
- Motor_Forward(2,80);
- myservo.writeMicroseconds(pwm_value-360);
- }
- //Serial.print("55");
- }
- else if(Out1==1 && Out2==1 && Out3==1 && Out4==0 && Out5==1)
- {
- dj=0;
- Motor_Forward(1,cv-n4); //cv=150
- Motor_Forward(2,cv-60-n4);
- myservo.writeMicroseconds(pwm_value-310);
- // Serial.print("54");
- }
- else if(Out1==1 && Out2==1 && Out3==0 && Out4==1 && Out5==1)
- {
- Motor_Forward(1,cv);
- Motor_Forward(2,cv-30);
- myservo.writeMicroseconds(pwm_value-130);
- // Serial.print("53");
- }
- else if(Out1==1 && Out2==1 && Out3==1 && Out4==1 && Out5==1 && f==1 && fore==0)
- {
- dj=0;
- if(h==0 )
- {
- Motor_Forward(1,170);
- Motor_Forward(2,100);
- myservo.writeMicroseconds(pwm_value-450);
- // Serial.print("zou");
- }
- else if(h==1 && hou==3)
- {
- Motor_Forward(1,170);
- Motor_Forward(2,100);
- myservo.writeMicroseconds(pwm_value-450);
- // Serial.print("zou");
- }
- else if(h==1 && hou!=3) //-
- {
- Motor_Forward(1,170);
- Motor_Forward(2,100);
- myservo.writeMicroseconds(pwm_value-450);
- // Serial.print("zou");
- }
- else
- {
- Motor_Forward(1,cv);
- Motor_Forward(2,cv);
- }
- }
- else
- {
- Motor_Forward(1,cv);
- Motor_Forward(2,cv);
- }
- }
- void Motor_Left() //1左转调用程序
- {
- detect();
- fore_judge();
- rear_judge();
- L_count();
- if(Out1==0 && Out2==1 && Out3==1 && Out4==1 && Out5==1)
- {
- if(dj==1 && h==0)
- {
- myservo.writeMicroseconds(pwm_value+360);
- Motor_Forward(1,80);
- Motor_Forward(2,170);
- }
- else if(dj==1 && h==1 && hou==3)
- {
- myservo.writeMicroseconds(pwm_value+360);
- Motor_Forward(1,80);
- Motor_Forward(2,170);
- }
- else if(dj==1 && h==1 && hou==2)
- {
- Motor_Forward(1,80);
- Motor_Forward(2,170);
- myservo.writeMicroseconds(pwm_value+360);
- }
- else if(dj==1 && h==1 && hou==4)
- {
- Motor_Forward(1,80);
- Motor_Forward(2,170);
- myservo.writeMicroseconds(pwm_value+360);
- }
- else
- {
- Motor_Forward(1,120);
- Motor_Forward(2,160);
- myservo.writeMicroseconds(pwm_value+360);
- }
- }
- else if(Out1==1 && Out2==0 && Out3==1 && Out4==1 && Out5==1)
- {
- dj=0;
- Motor_Forward(1,cv-60-n2);//-
- Motor_Forward(2,cv-n2);
- myservo.writeMicroseconds(pwm_value+310);
- // Serial.print("12");
- }
- else if(Out1==1 && Out2==1 && Out3==0 && Out4==1 && Out5==1)
- {
- Motor_Forward(1,cv-30);//-
- Motor_Forward(2,cv);
- myservo.writeMicroseconds(pwm_value+130);
- // Serial.print("13");
- }
- else if(Out1==1 && Out2==1 && Out3==1 && Out4==1 && Out5==1 && f==1 && fore==0)//-
- {
- dj=0;
- if(h==0)
- {
- Motor_Forward(1,100);
- Motor_Forward(2,170);
- myservo.writeMicroseconds(pwm_value+450);
- // Serial.print("you");
- }
- else if(h==1 && hou==3)
- {
- Motor_Forward(1,100);
- Motor_Forward(2,170);
- myservo.writeMicroseconds(pwm_value+450);
- // Serial.print("you");
- }
- else if(h==1 && hou!=3)//-
- {
- Motor_Forward(1,100);
- Motor_Forward(2,170);
- myservo.writeMicroseconds(pwm_value+450);
- }
- else
- {
- Motor_Forward(1,160);
- Motor_Forward(2,160);
- }
- }
- else
- {
- Motor_Forward(1,cv);
- Motor_Forward(2,cv);
- }
- }
- void correct()
- {
- detect();
- if(c1==1)
- {
- while(Out3==0)
- {
- Out3 = digitalRead(IR3);
- myservo.writeMicroseconds(pwm_value+100);
- Motor_Forward(1,170);
- Motor_Forward(2,230);
- }
- }
- else if(c5==1)
- {
- while(Out3==0)
- {
- Out3 = digitalRead(IR3);
- myservo.writeMicroseconds(pwm_value-100);
- Motor_Forward(1,230);
- Motor_Forward(2,170);
- }
- }
- else
- {;}
- }
- void rear_judge()
- {
- if(Out2H==0 && Out3H==1 && Out4H==1)
- { hou=2;h=0; }
- else if(Out2H==1 && Out3H==0 && Out4H==1)
- { hou=3; h=0; }
- else if(Out2H==1 && Out3H==1 && Out4H==0)
- { hou=4; h=0; }
- else
- {h=1;}
- }
- void fore_judge()
- {
- if (Out2==0 || Out3==0 || Out4==0)
- {
- fore=1;
- f=0;
- }
- else if(Out1==0 && Out2==1 && Out3==1 && Out4==1 && Out5==1)
- {
- fore=0;f=0;
- c1=1;
- c5=0;
- }
- else if(Out1==1 && Out2==1 && Out3==1 && Out4==1 && Out5==0)
- {
- fore=0;f=0;
- c5=1;
- c1=0;
- }
- else
- {
- f=1;
- }
- }
- void dis()
- {
- if(Distance < 35) //避障
- {
- while(Distance<35)//再次判断是否有障碍物,若有则转动方向后,继续判断
- {
- Distance_test();//测量前方距离
- if(z==1)
- {
- Motor_Forward(1,200);
- Motor_Forward(2,150);
- myservo.writeMicroseconds(1160);// 右转 400ms
- }
- else
- {
- Motor_Forward(1,200);
- Motor_Forward(2,150);
- myservo.writeMicroseconds(1700);// 右转 400ms
- }
- }
- if(z==0)
- {
- Motor_Forward(1,180);
- Motor_Forward(2,180);
- myservo.writeMicroseconds(1430);// 右转 400ms
- delay(1000);
- }
- else
- {
- Motor_Forward(1,180);
- Motor_Forward(2,180);
- myservo.writeMicroseconds(1430);// 右转 400ms
- delay(1000);
- }
- if(z==0)
- {
- Out5 = digitalRead(IR5);
- while(Out5==0)
- {
- Out5 = digitalRead(IR5);
- Motor_Forward(1,200);
- Motor_Forward(2,150);
- myservo.writeMicroseconds(1160);// 右转 400ms
- }
- }
- else
- {
- Out1 = digitalRead(IR1);
- while(Out1==0)
- {
- Out1 = digitalRead(IR1);
- Motor_Forward(1,200);
- Motor_Forward(2,150);
- myservo.writeMicroseconds(1700);// 右转 400ms
- }
- }
- }
- }
- void R_count()
- {
- if(Out1==1 && Out2==1 && Out3==1 && Out4==0 && Out5==1) //4右转计数
- {
- n4=n4+0.1;
- if(n4>=50)
- { n4=50; }
- //Serial.print("num4:"); //输出num3
- //Serial.println(num4); //显示次数
- }
- else { n4=1; }
- if(Out1==1 && Out2==1 && Out3==1 && Out4==1 && Out5==0) //4右转计数
- {
- n5=n5+0.1;
- if(n5>=50)
- { n5=50; }
- //Serial.print("num4:"); //输出num3
- //Serial.println(num4); //显示次数
- }
- else { n5=1; }
- }
- void L_count()
- {
- if(Out1==1 && Out2==0 && Out3==1 && Out4==1 && Out5==1) //4右转计数
- {
- n2=n2+0.1;
- if(n2>=30)
- { n2=30; }
- //Serial.print("num4:"); //输出num3
- //Serial.println(num4); //显示次数
- }
- else { n2=1; }
- if(Out1==0 && Out2==1 && Out3==1 && Out4==1 && Out5==1) //4右转计数
- {
- n1=n1+0.1;
- if(n1>=30)
- { n1=30; }
- //Serial.print("num4:"); //输出num3
- //Serial.println(num4); //显示次数
- }
- else { n1=1; }
- }
- void Distance_test() // 量出前方距离
- {
- digitalWrite(Trig, LOW); // 给触发脚低电平2μs
- delayMicroseconds(2);
- digitalWrite(Trig, HIGH); // 给触发脚高电平10μs,这里至少是10μs
- delayMicroseconds(10);
- digitalWrite(Trig, LOW); // 持续给触发脚低电
- float Fdistance = pulseIn(Echo, HIGH); // 读取高电平时间(单位:微秒)
- Fdistance= Fdistance/58; //为什么除以58等于厘米, Y米=(X秒*344)/2
- Distance = Fdistance;
- if(Distance>=60)
- {
- Distance=60;
- }
- else if(Distance<=8)
- {
- Distance=60;
- }
- // Serial.println(Distance); //显示距离
- }
- void handle_nled()
- {
- static unsigned long systick_ms_bak = 0;
- if(!handle_ms_between(&systick_ms_bak, NLED_MS_BETWEEN))return;
- digitalWrite(NLED_PIN, val);
- val = ~val;
- }
- void dida(u8 times, u8 frequency)
- {
- for(byte i = 0; i < times; i++ )
- {
- digitalWrite(BEEP_PIN, LOW);
- delay(frequency);
- delay(frequency);
- digitalWrite(BEEP_PIN, HIGH );
- delay(frequency);
- delay(frequency);
- }
- }
- unsigned char handle_ms_between( unsigned long *time_ms, unsigned int ms_between)
- {
- if(millis() - *time_ms < ms_between)
- {
- return 0;
- }
- else
- {
- *time_ms = millis();
- return 1;
- }
- }
- void detect()
- {
- Out1 = digitalRead(IR1);
- Out2 = digitalRead(IR2);
- Out3 = digitalRead(IR3);
- Out4 = digitalRead(IR4);
- Out5 = digitalRead(IR5);
- Out2H = digitalRead(IH2);
- Out3H = digitalRead(IH3);
- Out4H = digitalRead(IH4);
- }
- void Motor_Forward(char motor,char pwm) //电机正转
- {
- if(motor==1)
- {
- analogWrite(PWMA_IN1,pwm);
- analogWrite(PWMA_IN2,255);
- }
- else if(motor==2)
- {
- analogWrite(PWMB_IN1,pwm);
- analogWrite(PWMB_IN2,255);
- }
- }
- void Motor_Back(char motor,char pwm) //电机反转
- {
- if(motor==1)
- {
- analogWrite(PWMA_IN1,255);
- analogWrite(PWMA_IN2,pwm);
- }
- else if(motor==2)
- {
- analogWrite(PWMB_IN1,255);
- analogWrite(PWMB_IN2,pwm);
- }
- }
- void count()
- {
- if(Out1==1 && Out2==1 && Out3==0 && Out4==1 && Out5==1) //3直行计数
- {
- v=v+0.5;
- if(v>=sv-10)
- { v=sv-10; }
- num3=v;
- }
- else {;}
-
- if(Out1==1 && Out2==1 && Out3==1 && Out4==0 && Out5==1) //4右转计数
- {
- num4=num4+0.2;
- if(num4>=20)
- { num4=20; }
- }
- else { num4=1; }
-
- if(Out1==1 && Out2==0 && Out3==1 && Out4==1 && Out5==1) //2左转计数
- {
- num2=num2+0.2;
- if(num2>=20)
- { num2=20; }
- }
- else { num2=1; }
- if(Out1==0 && Out2==0 && Out3==0 && Out4==0 && Out5==0) //stop计数
- {
- st=st+0.2;
- }
- else { st=1; }
- }
复制代码
|
|