去年的今天我终于收到等了几个月的 ROB0118 Turtle 3PA三轮小车机器人套件。儿子一个小时搞定了安装,我两三天调试成功Arduino避障代码,着实兴奋了好长时间,让我的机器人的梦越来美!忍不住想发个“小乌龟机器人”美照:
15秒避障视频:
15秒循迹视频:
3分钟循迹观察中:
分享ARDUINO循迹代码:
- //20160319 ir循迹智能小车
- //我淘的模块是这样子:走黑线,反馈0或1;偏离黑线则一般反馈1000---1015
- int speedPin_M1 = 5; //M1 Speed Control
- int speedPin_M2 = 6; //M2 Speed Control
- int directionPin_M1 = 4; //M1 Direction Control
- int directionPin_M2 = 7; //M2 Direction Control
- int irPin_Left = A2;
- int irPin_MidLeft = A3;
- int irPin_MidRight = A4;
- int irPin_Right = A5;
- int irValue_Left = 0;
- int irValue_MidLeft = 0;
- int irValue_MidRight = 0;
- int irValue_Right = 0;
- int TestedPin_LED = 12;
- ////////////////////////////////////////////////////
- void setup() {
- pinMode(irPin_Left,INPUT);
- pinMode(irPin_MidLeft,INPUT);
- pinMode(irPin_MidRight,INPUT);
- pinMode(irPin_Right,INPUT);
-
- pinMode(TestedPin_LED,OUTPUT);
- for(int i=0;i<3;i++)
- {
- digitalWrite(TestedPin_LED,HIGH);
- delay(200);
- digitalWrite(TestedPin_LED,LOW);
- delay(200);
- }
- // Serial.begin(9600);
- }
- /////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////
- void loop(){
- irValue_Left = analogRead(irPin_Left );
- irValue_MidLeft = analogRead(irPin_MidLeft );
- irValue_MidRight = analogRead(irPin_MidRight );
- irValue_Right = analogRead(irPin_Right );
- // carAdvance(100,100); //Start
-
- if(irValue_Left>=800 && irValue_Right>=800 && (irValue_MidLeft<=100||irValue_MidRight<=100))
- {//
- carAdvance(100,100); //on line
- delay(5);
- }
- if((irValue_Left<=10||irValue_MidLeft<=10) && irValue_Right>=800 )
- {
- carTurnLeft(0,100); //veering off right
- delay(5);
- }
-
- if(irValue_Left>=800 && (irValue_Right<=10||irValue_MidRight<=10))
- {
- carTurnRight(100,0); //veering off left
- delay(5);
- }
-
- if(irValue_Left>=800 && irValue_MidLeft>=800 && irValue_MidRight>=800 && irValue_Right>=800)
- {
- carTurnLeft(70,70); // If line is lost try to reacquire
- delay(10);
- }
-
- if(irValue_Left<=100 && irValue_MidLeft<=100 && irValue_MidRight<=100 && irValue_Right<=100)
- {
- carTurnRight(70,70); //如果走到T形路或急转弯处,四个传感器同时全部检测到黑线,则右转
- delay(10);
- }
- }
- /////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////
- void carStop(){ // Motor Stop
- digitalWrite(speedPin_M2,0);
- digitalWrite(directionPin_M1,LOW);
- digitalWrite(speedPin_M1,0);
- digitalWrite(directionPin_M2,LOW);
- }
- void carAdvance(int leftSpeed,int rightSpeed){ //Move forward
- analogWrite (speedPin_M2,leftSpeed); //PWM Speed Control
- digitalWrite(directionPin_M1,HIGH);
- analogWrite (speedPin_M1,rightSpeed);
- digitalWrite(directionPin_M2,HIGH);
- }
- void carBack(int leftSpeed,int rightSpeed){ //Move backward
- analogWrite (speedPin_M2,leftSpeed);
- digitalWrite(directionPin_M1,LOW);
- analogWrite (speedPin_M1,rightSpeed);
- digitalWrite(directionPin_M2,LOW);
- }
- void carTurnRight(int leftSpeed,int rightSpeed){ //Turn Right
- analogWrite (speedPin_M2,leftSpeed);
- digitalWrite(directionPin_M1,LOW);
- analogWrite (speedPin_M1,rightSpeed);
- digitalWrite(directionPin_M2,HIGH);
- }
- void carTurnLeft(int leftSpeed,int rightSpeed){ //Turn Left
- analogWrite (speedPin_M2,leftSpeed);
- digitalWrite(directionPin_M1,HIGH);
- analogWrite (speedPin_M1,rightSpeed);
- digitalWrite(directionPin_M2,LOW);
- }
-
- //////////////////////////////////////////////////////////////////
复制代码
|