找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4169|回复: 3
打印 上一主题 下一主题
收起左侧

4WD四轮驱动双层板4电机循迹,避障智能车 Arduino源码与资料分享

[复制链接]
跳转到指定楼层
楼主
智能小车资料包内容:



论文内容截图:




ultrasonic car-1602.pde Arduino综合程序:
  1. #include <IRremote.h>  
  2. #include <Servo.h>
  3. #include <Wire.h>
  4. #include <LiquidCrystal_I2C.h>

  5. //***********************定義馬達腳位*************************
  6. int MotorRight1=6;
  7. int MotorRight2=9;
  8. int MotorLeft1=10;
  9. int MotorLeft2=11;
  10. int counter=0;
  11. const int irReceiverPin = 3; //紅外線接收器 OUTPUT 訊號接在 pin 2

  12. //***********************設定所偵測到的IRcode*************************
  13. long IRfront= 0x00FF629D;        //前進碼
  14. long IRback=0x00FFA857;         //後退
  15. long IRturnright=0x00FF22DD;    //右轉
  16. long IRturnleft= 0x00FFC23D;     //左轉
  17. long IRstop=0x00FF02FD;         //停止
  18. long IRAutorun=0x00FF6897;      //超音波自走模式
  19. long IRturnsmallleft= 0x00FFB04F;
  20. IRrecv irrecv(irReceiverPin);  // 定義 IRrecv 物件來接收紅外線訊號
  21. decode_results results;     
  22. //*************************定義超音波腳位******************************
  23. int inputPin =A0 ; // 定義超音波信號接收腳位rx
  24. int outputPin =A1; // 定義超音波信號發射腳位'tx
  25. int Fspeedd = 0; // 前方距離
  26. int Rspeedd = 0; // 右方距離
  27. int Lspeedd = 0; // 左方距離
  28. int directionn = 0; // 前=8 後=2 左=4 右=6
  29. Servo myservo; // 設 myservo
  30. int delay_time = 250; // 伺服馬達轉向後的穩定時間
  31. int Fgo = 8; // 前進
  32. int Rgo = 6; // 右轉
  33. int Lgo = 4; // 左轉
  34. int Bgo = 2; // 倒車
  35. //********************************************************************(SETUP)
  36. LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display
  37. void setup()
  38. {  
  39.   Serial.begin(9600);
  40.   pinMode(MotorRight1, OUTPUT);  // 腳位 8 (PWM)
  41.   pinMode(MotorRight2, OUTPUT);  // 腳位 9 (PWM)
  42.   pinMode(MotorLeft1,  OUTPUT);  // 腳位 10 (PWM)
  43.   pinMode(MotorLeft2,  OUTPUT);  // 腳位 11 (PWM)
  44.   irrecv.enableIRIn();     // 啟動紅外線解碼
  45.   digitalWrite(3,HIGH);
  46.   pinMode(inputPin, INPUT); // 定義超音波輸入腳位
  47.   pinMode(outputPin, OUTPUT); // 定義超音波輸出腳位
  48.   myservo.attach(5); // 定義伺服馬達輸出第5腳位(PWM)
  49.   lcd.init();   // initialize the lcd
  50.   lcd.init();
  51. // Print a message to the LCD.
  52.   lcd.backlight();



  53. }
  54. //******************************************************************(Void)
  55. void advance(int a) // 前進
  56. {
  57.         digitalWrite(MotorRight1,LOW);
  58.         digitalWrite(MotorRight2,HIGH);
  59.         digitalWrite(MotorLeft1,LOW);
  60.         digitalWrite(MotorLeft2,HIGH);
  61.         delay(a * 100);
  62. }
  63. void right(int b) //右轉(單輪)
  64. {
  65.        digitalWrite(MotorLeft1,LOW);
  66.        digitalWrite(MotorLeft2,HIGH);
  67.        digitalWrite(MotorRight1,LOW);
  68.        digitalWrite(MotorRight2,LOW);
  69.        delay(b * 100);
  70. }
  71. void left(int c) //左轉(單輪)
  72. {
  73.       digitalWrite(MotorRight1,LOW);
  74.       digitalWrite(MotorRight2,HIGH);
  75.       digitalWrite(MotorLeft1,LOW);
  76.       digitalWrite(MotorLeft2,LOW);
  77.       delay(c * 100);
  78. }
  79. void turnR(int d) //右轉(雙輪)
  80. {
  81.       digitalWrite(MotorRight1,HIGH);
  82.       digitalWrite(MotorRight2,LOW);
  83.       digitalWrite(MotorLeft1,LOW);
  84.       digitalWrite(MotorLeft2,HIGH);
  85.       delay(d * 100);
  86. }
  87. void turnL(int e) //左轉(雙輪)
  88. {
  89.       digitalWrite(MotorRight1,LOW);
  90.       digitalWrite(MotorRight2,HIGH);
  91.       digitalWrite(MotorLeft1,HIGH);
  92.       digitalWrite(MotorLeft2,LOW);
  93.       delay(e * 100);
  94. }
  95. void stopp(int f) //停止
  96. {
  97.      digitalWrite(MotorRight1,LOW);
  98.      digitalWrite(MotorRight2,LOW);
  99.      digitalWrite(MotorLeft1,LOW);
  100.      digitalWrite(MotorLeft2,LOW);
  101.      delay(f * 100);
  102. }
  103. void back(int g) //後退
  104. {
  105.         digitalWrite(MotorRight1,HIGH);
  106.         digitalWrite(MotorRight2,LOW);
  107.         digitalWrite(MotorLeft1,HIGH);
  108.         digitalWrite(MotorLeft2,LOW);;
  109.         delay(g * 100);
  110. }
  111. void detection() //測量3個角度(前.左.右)
  112. {
  113.     int delay_time = 250; // 伺服馬達轉向後的穩定時間
  114.     ask_pin_F(); // 讀取前方距離

  115.     if(Fspeedd < 10) // 假如前方距離小於10公分
  116.    {
  117.       stopp(1); // 清除輸出資料
  118.       back(2); // 後退 0.2秒
  119.       

  120.    }
  121.     if(Fspeedd < 25) // 假如前方距離小於25公分
  122.    {
  123.       stopp(1); // 清除輸出資料
  124.       ask_pin_L(); // 讀取左方距離
  125.       delay(delay_time); // 等待伺服馬達穩定
  126.       ask_pin_R(); // 讀取右方距離
  127.       delay(delay_time); // 等待伺服馬達穩定

  128.       if(Lspeedd > Rspeedd) //假如 左邊距離大於右邊距離
  129.      {
  130.         directionn = Lgo; //向左走
  131.      }

  132.       if(Lspeedd <= Rspeedd) //假如 左邊距離小於或等於右邊距離
  133.       {
  134.         directionn = Rgo; //向右走
  135.       }

  136.       if (Lspeedd < 15 && Rspeedd < 15) //假如 左邊距離和右邊距離皆小於10公分
  137.      {
  138.         directionn = Bgo; //向後走
  139.       }
  140.     }
  141.     else //加如前方大於25公分
  142.    {
  143.       directionn = Fgo; //向前走
  144.    }
  145. }   
  146. //*********************************************************************************
  147. void ask_pin_F() // 量出前方距離
  148. {
  149. myservo.write(90);
  150. digitalWrite(outputPin, LOW); // 讓超聲波發射低電壓2μs
  151. delayMicroseconds(2);
  152. digitalWrite(outputPin, HIGH); // 讓超聲波發射高電壓10μs,這裡至少是10μs
  153. delayMicroseconds(10);
  154. digitalWrite(outputPin, LOW); // 維持超聲波發射低電壓
  155. float Fdistance = pulseIn(inputPin, HIGH); // 讀差相差時間
  156. Fdistance= Fdistance/5.8/10; // 將時間轉為距離距离(單位:公分)

  157. Fspeedd = Fdistance; // 將距離 讀入Fspeedd(前速)
  158. }
  159. //********************************************************************************
  160. void ask_pin_L() // 量出左邊距離
  161. {
  162. myservo.write(177);
  163. delay(delay_time);
  164. digitalWrite(outputPin, LOW); // 讓超聲波發射低電壓2μs
  165. delayMicroseconds(2);
  166. digitalWrite(outputPin, HIGH); // 讓超聲波發射高電壓10μs,這裡至少是10μs
  167. delayMicroseconds(10);
  168. digitalWrite(outputPin, LOW); // 維持超聲波發射低電壓
  169. float Ldistance = pulseIn(inputPin, HIGH); // 讀差相差時間
  170. Ldistance= Ldistance/5.8/10; // 將時間轉為距離距离(單位:公分)

  171. Lspeedd = Ldistance; // 將距離 讀入Lspeedd(左速)
  172. }
  173. //******************************************************************************
  174. void ask_pin_R() // 量出右邊距離
  175. {
  176. myservo.write(5);
  177. delay(delay_time);
  178. digitalWrite(outputPin, LOW); // 讓超聲波發射低電壓2μs
  179. delayMicroseconds(2);
  180. digitalWrite(outputPin, HIGH); // 讓超聲波發射高電壓10μs,這裡至少是10μs
  181. delayMicroseconds(10);
  182. digitalWrite(outputPin, LOW); // 維持超聲波發射低電壓
  183. float Rdistance = pulseIn(inputPin, HIGH); // 讀差相差時間
  184. Rdistance= Rdistance/5.8/10; // 將時間轉為距離距离(單位:公分)

  185. Rspeedd = Rdistance; // 將距離 讀入Rspeedd(右速)
  186. }
  187. //******************************************************************************(LOOP)
  188. void loop()
  189. {
  190.      
  191. //***************************************************************************正常遙控模式      
  192.   if (irrecv.decode(&results))
  193.     {         // 解碼成功,收到一組紅外線訊號
  194. /***********************************************************************/
  195.       if (results.value == IRfront)//前進
  196.        {
  197.       
  198.         lcd.setCursor(0,0);
  199.         lcd.print(" IR mode");
  200.         lcd.setCursor(0,1);
  201.         lcd.print("  advance ");
  202.         advance(20);//前進
  203.        }
  204. /***********************************************************************/
  205.       if (results.value ==  IRback)//後退
  206.        {
  207.         
  208.         lcd.setCursor(0,0);
  209.         lcd.print(" IR mode");
  210.         lcd.setCursor(0,1);
  211.         lcd.print("  back ");
  212.         back(20);//後退
  213.        }
  214. /***********************************************************************/
  215.       if (results.value == IRturnright)//右轉
  216.       {
  217.       
  218.         lcd.setCursor(0,0);
  219.         lcd.print(" IR mode");
  220.         lcd.setCursor(0,1);
  221.         lcd.print("  right ");
  222.         right(10); // 右轉
  223.          
  224.       }
  225. /***********************************************************************/
  226.      if (results.value == IRturnleft)//左轉
  227.      {
  228.      
  229.         lcd.setCursor(0,0);
  230.         lcd.print(" IR mode");
  231.         lcd.setCursor(0,1);
  232.         lcd.print(" left ");
  233.         left(10); // 左轉);
  234.      }
  235. /***********************************************************************/   
  236.     if (results.value == IRstop)//停止
  237.    {
  238.        lcd.setCursor(0,0);
  239.        lcd.print(" IR mode");
  240.        lcd.setCursor(0,1);
  241.        lcd.print(" stop ");
  242.      digitalWrite(MotorRight1,LOW);
  243.      digitalWrite(MotorRight2,LOW);
  244.      digitalWrite(MotorLeft1,LOW);
  245.      digitalWrite(MotorLeft2,LOW);
  246.    
  247.      
  248.     }

  249. //***********************************************************************超音波自走模式
  250. if (results.value ==IRAutorun )
  251.       {
  252.            while(IRAutorun)
  253.         {
  254.             myservo.write(90); //讓伺服馬達回歸 預備位置 準備下一次的測量
  255.             detection(); //測量角度 並且判斷要往哪一方向移動
  256.              if(directionn == 8) //假如directionn(方向) = 8(前進)
  257.             {
  258.               if (irrecv.decode(&results))
  259.            {
  260.              irrecv.resume();
  261.              Serial.println(results.value,HEX);
  262.              if(results.value ==IRstop)
  263.              {
  264.                digitalWrite(MotorRight1,LOW);
  265.                digitalWrite(MotorRight2,LOW);
  266.                digitalWrite(MotorLeft1,LOW);
  267.                digitalWrite(MotorLeft2,LOW);
  268.                break;
  269.              }
  270.            }
  271.                 results.value=0;
  272.                
  273.                
  274.                 lcd.setCursor(0,0);
  275.                 lcd.print(" aoto mode");
  276.                 lcd.setCursor(0,1);
  277.                 lcd.print(" Advance ");
  278.                 advance(1); // 正常前進
  279.             }
  280.            if(directionn == 2) //假如directionn(方向) = 2(倒車)
  281.           {
  282.             if (irrecv.decode(&results))
  283.            {
  284.              irrecv.resume();
  285.              Serial.println(results.value,HEX);
  286.              if(results.value ==IRstop)
  287.              {
  288.                digitalWrite(MotorRight1,LOW);
  289.                digitalWrite(MotorRight2,LOW);
  290.                digitalWrite(MotorLeft1,LOW);
  291.                digitalWrite(MotorLeft2,LOW);
  292.                break;
  293.              }
  294.            }
  295.               results.value=0;
  296.               
  297.             
  298.                lcd.setCursor(0,0);
  299.                 lcd.print(" aoto mode");
  300.                 lcd.setCursor(0,1);
  301.                 lcd.print(" Reverse ");
  302.                 back(8); // 倒退(車)
  303.               turnL(3); //些微向左方移動(防止卡在死巷裡)
  304.           }
  305.             if(directionn == 6) //假如directionn(方向) = 6(右轉)
  306.           {
  307.            if (irrecv.decode(&results))
  308.            {
  309.               irrecv.resume();
  310.               Serial.println(results.value,HEX);
  311.              if(results.value ==IRstop)
  312.              {
  313.                digitalWrite(MotorRight1,LOW);
  314.                digitalWrite(MotorRight2,LOW);
  315.                digitalWrite(MotorLeft1,LOW);
  316.                digitalWrite(MotorLeft2,LOW);
  317.                break;
  318.              }
  319.            }
  320.              results.value=0;
  321.                
  322.             
  323.                lcd.setCursor(0,0);
  324.                 lcd.print(" aoto mode");
  325.                 lcd.setCursor(0,1);
  326.                 lcd.print(" Right ");
  327.                  back(1);
  328.                turnR(3); // 右轉
  329.           }
  330.             if(directionn == 4) //假如directionn(方向) = 4(左轉)
  331.           {
  332.              if (irrecv.decode(&results))
  333.            {
  334.              irrecv.resume();
  335.              Serial.println(results.value,HEX);
  336.              if(results.value ==IRstop)
  337.              {
  338.                digitalWrite(MotorRight1,LOW);
  339.                digitalWrite(MotorRight2,LOW);
  340.                digitalWrite(MotorLeft1,LOW);
  341.                digitalWrite(MotorLeft2,LOW);
  342.                break;
  343.              }
  344.            }
  345.                 results.value=0;
  346.                
  347.                  lcd.setCursor(0,0);
  348.                 lcd.print(" aoto mode");
  349.                 lcd.setCursor(0,1);
  350.                 lcd.print(" Left ");
  351.                 back(1);
  352.                 turnL(3); // 左轉
  353.             
  354.            }
  355.             
  356.              if (irrecv.decode(&results))
  357.            {
  358.              irrecv.resume();
  359.              Serial.println(results.value,HEX);
  360.              if(results.value ==IRstop)
  361.              {
  362.                digitalWrite(MotorRight1,LOW);
  363.                digitalWrite(MotorRight2,LOW);
  364.                digitalWrite(MotorLeft1,LOW);
  365.                digitalWrite(MotorLeft2,LOW);
  366.                break;
  367.              }
  368.            }
  369.          }
  370.                results.value=0;
  371.        }
  372. /***********************************************************************/   
  373.      else
  374.     {
  375.            digitalWrite(MotorRight1,LOW);
  376.            digitalWrite(MotorRight2,LOW);
  377.            digitalWrite(MotorLeft1,LOW);
  378.            digitalWrite(MotorLeft2,LOW);
  379.      }
  380.       

  381.         irrecv.resume();    // 繼續收下一組紅外線訊號        
  382.    }  
  383. }
  384.    
复制代码



全部资料51hei下载地址:
遥控超声波测距智能车 (1).rar (9.79 MB, 下载次数: 26)
基于51单单片机的自动循迹小车.doc (4.32 MB, 下载次数: 13)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:339065 发表于 2018-5-27 14:39 | 只看该作者
垃圾。文章是以STC89C52单片机为控制核心,根本不是arduino ,骗黑比
回复

使用道具 举报

板凳
ID:340398 发表于 2018-5-29 10:32 | 只看该作者
的确是爱人duino程序,不懂不要乱说
回复

使用道具 举报

地板
ID:340398 发表于 2018-5-29 10:35 | 只看该作者
long IRfront= 0x00FF629D;        //前進碼
long IRback=0x00FFA857;         //後退
long IRturnright=0x00FF22DD;    //右轉
long IRturnleft= 0x00FFC23D;     //左轉
long IRstop=0x00FF02FD;         //停止
long IRAutorun=0x00FF6897;      //超音波自走模式
long IRturnsmallleft= 0x00FFB04F;

红外线键值,设置模式很好
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表