0.png (47.76 KB, 下載次數: 55)
下載附件
2018-1-22 03:17 上傳
遙控超聲波測距智能車的特點:
1.積木式硬件處理方式,便于理解和學習,
2.采用ARDUINO的編程方式,程序通俗易懂,不用很專業知識就可以看懂原代碼,也可以通過修改,做成不同功能的智能車.
3.采用給力板做主控,一片給力板就可以實現遙控,避障,測距離,顯示,外加驅動電機和舵機的功能,可見多么強大.而且給力板的電流也要比官方的板大很多,足見給力板穩定性.
4.拓展性強,智能車平臺是很簡易的東西,但是我們可以通過外接傳感,或者功能模塊,把它變成不同智能車.比如滅火機器人,搜救智能車,WIFI無線機器人,等等...............................
三. 遙控超聲波測距智能車參數:
1.電機參數:電壓范圍:1.5-12V,電機軸長:10mm,轉速6.0V 100rpm/min.
2.控制電機選用L298N驅動模塊,與單片機真正隔離.
3.避障部分采用HC-SR04 超聲波,性能穩定,測度距離精確.
4.顯示部分采用的1602 I2C的模塊,只須用到2個IO口.
5.遙控功能采用紅外遙控方式,NEC協議.
6.可接入外部7~12V的電壓。并能搭載多款傳感器模塊,根據您的想象力實現各種功能.
先來介紹一下Arduino
我們看看Arduino團隊如何為其定義的:
Arduino是一個開放源碼電子原型平臺,擁有靈活、 易用的硬件和軟件。 Arduino專為設計師,工藝美術人員,業余愛好者,以及對開發互動裝置或互動式開發環境感興趣的人而設的。Arduino可以接收來自各種傳感器的輸入信號從而檢測出運行環境,并通過控制光源,電機以及其他驅動器來影響其周圍環境。板上的微控制器編程使用Arduino編程語言(基于Wiring)和Arduino開發環境(以Processing為基礎)。Arduino可以獨立運行,也可以與計算機上運行的軟件(例如,Flash,Processing,MaxMSP)進行通信。
Arduino硬件電路板可以自行焊接組裝,也可以購買已經組裝好的,軟件則可以從Arduino
網站免費下載使用。您可以根據開源許可獲得硬件參考設計(CAD文件),并自由地修改使其適應您的需求。
Arduino的定義仍然有點模糊,這也正是Arduino的優勢所在。 Arduino是人們連接各種任務
的粘合劑。要給Arduino下一個最準確的定義,最好用一些實例來描述。
您想當咖啡煮好時,咖啡壺就發出“吱吱”聲提醒您嗎?
您想當郵箱有新郵件時,電話會發出警報通知您嗎?
想要一件閃閃發光的絨毛玩具嗎?
想要一款具備語音和酒水配送功能的X教授蒸汽朋克風格輪椅嗎?
想要一套按下快捷鍵就可以進行實驗測試蜂音器嗎?
想為您的兒子自制一個《銀河戰士》手臂炮嗎?
想自制一個心率監測器,將每次騎腳踏車的記錄存進存儲卡嗎?
想過自制一個能在地面上繪圖,能在雪中馳騁的機器人嗎?
0.jpg (82.94 KB, 下載次數: 60)
下載附件
2018-1-22 03:11 上傳
那就是Arduino,他們尚未意識到這一點。
看看Arduino項目的實例,你將發現制造者們對于這些電子產品“是什么”更加感興趣,而不是制作方法。 這些Arduino發燒友常常表示Arduino并沒有教授基本的電子產品,“呸,這哪是真正的電子產品啊,”他們說,“太容易了!”是的,的確如此。如果您是個藝術家或設計人員,在不使用Arduino情況下,想讓發光二極管閃爍,或者電機轉動,那么祝您好運了。 當然,如果您愿意花錢并且帶著您厚重的電子產品技術教材來展示一番,這也未嘗不可。但是對于那兒的其他人而言,他們不過是想用發光二級管來裝飾火人節服裝而已。
對于一些傳統微控制器社區是如何看待Arduino這一問題,我認為最佳的例子就是AVR Freaks,該官方網站專注于AVR處理器(同樣用于Arduino)。您會想AVR Freaks社區可能會喜愛Arduino,因為Arduino能夠將AVR微控制器帶給大眾。然而,該網站的很多人并不喜歡這些非工程師們制造的所有古怪玩意兒,因為這些會破壞了他們的等級制度。我最喜歡引用一句話(我希望把這句話印在T恤上)。
“Arduino編程語言,小孩都能看懂,用一次就上癮”—— ArnoldB,AVRfreaks網站事實上,這種錯誤的態度卻推動Arduino粉絲去建立自己的社區,幫助了Arduino建立一個兼收并蓄,而非高高在上的社區。
Arduino是簡單,但不是過于簡單。它圍繞這些觀點來建立,即學生使用Arduino去實現目的:接收傳感器信號,獲得一些代碼,再利用這些信號和代碼。或許甚至不會寫代碼,那他們剪切粘貼代碼后就可以開始。 Arduino是熱粘結劑,而非精確的焊接。 因此沒有人會被切掉一只手,也不會有實驗室被燒毀。Arduino團隊成員之一會教授工藝美術人員和設計者如何使用。 每天,Arduino在學習,教授,和共享代碼的工程中不斷建立和完善。 這些工藝美術人員和設計者正在Macs系統上使用Processing語言并修改。(Processing是Arduino的老大哥)
說到此處,Arduino就像是一個熱情,沒有界限,藝術氛圍濃厚的集會。這就是Arduino成為“自己動手做”成功典范的原因嗎?不僅僅如此,我們來了解更多具體信息。程序庫——簡單任務,復雜任務輕松搞定大量的封裝庫被用來完成復雜的任務,例如寫入SD記憶卡,寫入液晶顯示屏驅動程序,
解析GPS。也有一些程序庫用來做簡單的事情,比如轉動插腳或按鍵消抖。如果有10個芯片,我們就得將UART安裝代碼寫10次,坦白說,這讓人厭煩。如果調用Serial.begin(9600)函數來處理寄存器數據的話,那就容易多了。輕量型,直接運行在硬件底層 上使用一款經認證的,易于理解的編譯器(我們甚至可以說avr-gcc是AVR默認或標準的編譯器),代碼能直接運行在硬件底層 上。 其編譯方式與NET 語言及BASIC語言不同。 該編譯器運行快,體積小,重量輕,并且您能使用HEX(十六進制)文件為成批的新芯片進行編程。
傳感器
Arduino真正騰飛的原因是其能夠實現將模擬輸入轉換為數字輸入,換言之,您可以將光線,溫度,聲音,或者市場上已有的任何低成本的傳感器信號輸入,Arduino都能識別。對于數字傳感器,Arduino支持 SPI( 高速同步串行口)和I2C總線。這一功能覆蓋市場上99%的傳感器。使用其他開發平臺是不易實現的——想想如果把一塊Beagleboard(偉大的產品)和Arduino綁在一起,僅僅是為了獲得傳感器的數據,那真是太奇怪了!
0.jpg (59.84 KB, 下載次數: 77)
下載附件
2018-1-22 03:14 上傳
0.jpg (44.83 KB, 下載次數: 65)
下載附件
2018-1-22 03:15 上傳
0.jpg (22.96 KB, 下載次數: 57)
下載附件
2018-1-22 03:15 上傳
上面的 D4、D5 指的就是數字端口的 4、5 號引腳,下面還有具體的連接實物可供參考好了,我們這次要做的就是學會如何使用它去測距離,并在電腦的顯示屏上顯示出來,當然如果你要做的好看些,可以加個 lcd 或是數碼管之類的,我們這里就是做個功能測試,就是個啟發過程。
嗯,下面有我們的測試代碼。
0.jpg (42.37 KB, 下載次數: 69)
下載附件
2018-1-22 03:16 上傳
arduino源程序如下:
- #include <IRremote.h>
- #include <Servo.h>
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- //***********************定義馬達腳位*************************
- int MotorRight1=6;
- int MotorRight2=9;
- int MotorLeft1=10;
- int MotorLeft2=11;
- int counter=0;
- const int irReceiverPin = 3; //紅外線接收器 OUTPUT 訊號接在 pin 2
- //***********************設定所偵測到的IRcode*************************
- long IRfront= 0x00FF629D; //前進碼
- long IRback=0x00FFA857; //後退
- long IRturnright=0x00FF22DD; //右轉
- long IRturnleft= 0x00FFC23D; //左轉
- long IRstop=0x00FF02FD; //停止
- long IRAutorun=0x00FF6897; //超音波自走模式
- long IRturnsmallleft= 0x00FFB04F;
- IRrecv irrecv(irReceiverPin); // 定義 IRrecv 物件來接收紅外線訊號
- decode_results results;
- //*************************定義超音波腳位******************************
- int inputPin =A0 ; // 定義超音波信號接收腳位rx
- int outputPin =A1; // 定義超音波信號發射腳位'tx
- int Fspeedd = 0; // 前方距離
- int Rspeedd = 0; // 右方距離
- int Lspeedd = 0; // 左方距離
- int directionn = 0; // 前=8 後=2 左=4 右=6
- Servo myservo; // 設 myservo
- int delay_time = 250; // 伺服馬達轉向後的穩定時間
- int Fgo = 8; // 前進
- int Rgo = 6; // 右轉
- int Lgo = 4; // 左轉
- int Bgo = 2; // 倒車
- //********************************************************************(SETUP)
- LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
- void setup()
- {
- Serial.begin(9600);
- pinMode(MotorRight1, OUTPUT); // 腳位 8 (PWM)
- pinMode(MotorRight2, OUTPUT); // 腳位 9 (PWM)
- pinMode(MotorLeft1, OUTPUT); // 腳位 10 (PWM)
- pinMode(MotorLeft2, OUTPUT); // 腳位 11 (PWM)
- irrecv.enableIRIn(); // 啟動紅外線解碼
- digitalWrite(3,HIGH);
- pinMode(inputPin, INPUT); // 定義超音波輸入腳位
- pinMode(outputPin, OUTPUT); // 定義超音波輸出腳位
- myservo.attach(5); // 定義伺服馬達輸出第5腳位(PWM)
- lcd.init(); // initialize the lcd
- lcd.init();
- // Print a message to the LCD.
- lcd.backlight();
- }
- //******************************************************************(Void)
- void advance(int a) // 前進
- {
- digitalWrite(MotorRight1,LOW);
- digitalWrite(MotorRight2,HIGH);
- digitalWrite(MotorLeft1,LOW);
- digitalWrite(MotorLeft2,HIGH);
- delay(a * 100);
- }
- void right(int b) //右轉(單輪)
- {
- digitalWrite(MotorLeft1,LOW);
- digitalWrite(MotorLeft2,HIGH);
- digitalWrite(MotorRight1,LOW);
- digitalWrite(MotorRight2,LOW);
- delay(b * 100);
- }
- void left(int c) //左轉(單輪)
- {
- digitalWrite(MotorRight1,LOW);
- digitalWrite(MotorRight2,HIGH);
- digitalWrite(MotorLeft1,LOW);
- digitalWrite(MotorLeft2,LOW);
- delay(c * 100);
- }
- void turnR(int d) //右轉(雙輪)
- {
- digitalWrite(MotorRight1,HIGH);
- digitalWrite(MotorRight2,LOW);
- digitalWrite(MotorLeft1,LOW);
- digitalWrite(MotorLeft2,HIGH);
- delay(d * 100);
- }
- void turnL(int e) //左轉(雙輪)
- {
- digitalWrite(MotorRight1,LOW);
- digitalWrite(MotorRight2,HIGH);
- digitalWrite(MotorLeft1,HIGH);
- digitalWrite(MotorLeft2,LOW);
- delay(e * 100);
- }
- void stopp(int f) //停止
- {
- digitalWrite(MotorRight1,LOW);
- digitalWrite(MotorRight2,LOW);
- digitalWrite(MotorLeft1,LOW);
- digitalWrite(MotorLeft2,LOW);
- delay(f * 100);
- }
- void back(int g) //後退
- {
- digitalWrite(MotorRight1,HIGH);
- digitalWrite(MotorRight2,LOW);
- digitalWrite(MotorLeft1,HIGH);
- digitalWrite(MotorLeft2,LOW);;
- delay(g * 100);
- }
- void detection() //測量3個角度(前.左.右)
- {
- int delay_time = 250; // 伺服馬達轉向後的穩定時間
- ask_pin_F(); // 讀取前方距離
- if(Fspeedd < 10) // 假如前方距離小於10公分
- {
- stopp(1); // 清除輸出資料
- back(2); // 後退 0.2秒
-
- }
- if(Fspeedd < 25) // 假如前方距離小於25公分
- {
- stopp(1); // 清除輸出資料
- ask_pin_L(); // 讀取左方距離
- delay(delay_time); // 等待伺服馬達穩定
- ask_pin_R(); // 讀取右方距離
- delay(delay_time); // 等待伺服馬達穩定
- if(Lspeedd > Rspeedd) //假如 左邊距離大於右邊距離
- {
- directionn = Lgo; //向左走
- }
- if(Lspeedd <= Rspeedd) //假如 左邊距離小於或等於右邊距離
- {
- directionn = Rgo; //向右走
- }
- if (Lspeedd < 15 && Rspeedd < 15) //假如 左邊距離和右邊距離皆小於10公分
- {
- directionn = Bgo; //向後走
- }
- }
- else //加如前方大於25公分
- {
- directionn = Fgo; //向前走
- }
- }
- //*********************************************************************************
- void ask_pin_F() // 量出前方距離
- {
- myservo.write(90);
- digitalWrite(outputPin, LOW); // 讓超聲波發射低電壓2μs
- delayMicroseconds(2);
- digitalWrite(outputPin, HIGH); // 讓超聲波發射高電壓10μs,這裡至少是10μs
- delayMicroseconds(10);
- digitalWrite(outputPin, LOW); // 維持超聲波發射低電壓
- float Fdistance = pulseIn(inputPin, HIGH); // 讀差相差時間
- Fdistance= Fdistance/5.8/10; // 將時間轉為距離距離(單位:公分)
- Fspeedd = Fdistance; // 將距離 讀入Fspeedd(前速)
- }
- //********************************************************************************
- void ask_pin_L() // 量出左邊距離
- {
- myservo.write(177);
- delay(delay_time);
- digitalWrite(outputPin, LOW); // 讓超聲波發射低電壓2μs
- delayMicroseconds(2);
- digitalWrite(outputPin, HIGH); // 讓超聲波發射高電壓10μs,這裡至少是10μs
- delayMicroseconds(10);
- digitalWrite(outputPin, LOW); // 維持超聲波發射低電壓
- float Ldistance = pulseIn(inputPin, HIGH); // 讀差相差時間
- Ldistance= Ldistance/5.8/10; // 將時間轉為距離距離(單位:公分)
- Lspeedd = Ldistance; // 將距離 讀入Lspeedd(左速)
- }
- //******************************************************************************
- void ask_pin_R() // 量出右邊距離
- {
- myservo.write(5);
- delay(delay_time);
- digitalWrite(outputPin, LOW); // 讓超聲波發射低電壓2μs
- delayMicroseconds(2);
- digitalWrite(outputPin, HIGH); // 讓超聲波發射高電壓10μs,這裡至少是10μs
- delayMicroseconds(10);
- digitalWrite(outputPin, LOW); // 維持超聲波發射低電壓
- float Rdistance = pulseIn(inputPin, HIGH); // 讀差相差時間
- Rdistance= Rdistance/5.8/10; // 將時間轉為距離距離(單位:公分)
- Rspeedd = Rdistance; // 將距離 讀入Rspeedd(右速)
- }
- //******************************************************************************(LOOP)
- void loop()
- {
-
- //***************************************************************************正常遙控模式
- if (irrecv.decode(&results))
- { // 解碼成功,收到一組紅外線訊號
- /***********************************************************************/
- if (results.value == IRfront)//前進
- {
-
- lcd.setCursor(0,0);
- lcd.print(" IR mode");
- lcd.setCursor(0,1);
- lcd.print(" advance ");
- advance(20);//前進
- }
- /***********************************************************************/
- if (results.value == IRback)//後退
- {
-
- lcd.setCursor(0,0);
- lcd.print(" IR mode");
- lcd.setCursor(0,1);
- lcd.print(" back ");
- back(20);//後退
- }
- /***********************************************************************/
- if (results.value == IRturnright)//右轉
- {
-
- lcd.setCursor(0,0);
- lcd.print(" IR mode");
- lcd.setCursor(0,1);
- lcd.print(" right ");
- right(10); // 右轉
-
- }
- /***********************************************************************/
- if (results.value == IRturnleft)//左轉
- {
-
- lcd.setCursor(0,0);
- lcd.print(" IR mode");
- lcd.setCursor(0,1);
- lcd.print(" left ");
- left(10); // 左轉);
- }
- /***********************************************************************/
- if (results.value == IRstop)//停止
- {
- lcd.setCursor(0,0);
- lcd.print(" IR mode");
- lcd.setCursor(0,1);
- lcd.print(" stop ");
- digitalWrite(MotorRight1,LOW);
- digitalWrite(MotorRight2,LOW);
- digitalWrite(MotorLeft1,LOW);
- digitalWrite(MotorLeft2,LOW);
-
-
- }
- //***********************************************************************超音波自走模式
- if (results.value ==IRAutorun )
- {
- while(IRAutorun)
- {
- myservo.write(90); //讓伺服馬達回歸 預備位置 準備下一次的測量
- detection(); //測量角度 並且判斷要往哪一方向移動
- if(directionn == 8) //假如directionn(方向) = 8(前進)
- {
- if (irrecv.decode(&results))
- {
- irrecv.resume();
- Serial.println(results.value,HEX);
- if(results.value ==IRstop)
- {
- digitalWrite(MotorRight1,LOW);
- digitalWrite(MotorRight2,LOW);
- digitalWrite(MotorLeft1,LOW);
- digitalWrite(MotorLeft2,LOW);
- break;
- }
- }
- results.value=0;
-
-
- lcd.setCursor(0,0);
- lcd.print(" aoto mode");
- lcd.setCursor(0,1);
- lcd.print(" Advance ");
- advance(1); // 正常前進
- }
- if(directionn == 2) //假如directionn(方向) = 2(倒車)
- {
- if (irrecv.decode(&results))
- {
- irrecv.resume();
- Serial.println(results.value,HEX);
- if(results.value ==IRstop)
- {
- digitalWrite(MotorRight1,LOW);
- digitalWrite(MotorRight2,LOW);
- digitalWrite(MotorLeft1,LOW);
- digitalWrite(MotorLeft2,LOW);
- break;
- }
- }
- results.value=0;
-
-
- lcd.setCursor(0,0);
- lcd.print(" aoto mode");
- lcd.setCursor(0,1);
- lcd.print(" Reverse ");
- back(8); // 倒退(車)
- turnL(3); //些微向左方移動(防止卡在死巷裡)
- }
- if(directionn == 6) //假如directionn(方向) = 6(右轉)
- {
- if (irrecv.decode(&results))
- {
- irrecv.resume();
- Serial.println(results.value,HEX);
- if(results.value ==IRstop)
- {
- digitalWrite(MotorRight1,LOW);
- digitalWrite(MotorRight2,LOW);
- digitalWrite(MotorLeft1,LOW);
- digitalWrite(MotorLeft2,LOW);
- break;
- }
- }
- results.value=0;
-
-
- lcd.setCursor(0,0);
- lcd.print(" aoto mode");
- lcd.setCursor(0,1);
- lcd.print(" Right ");
- back(1);
- turnR(3); // 右轉
- }
- if(directionn == 4) //假如directionn(方向) = 4(左轉)
- {
- if (irrecv.decode(&results))
- {
- irrecv.resume();
- Serial.println(results.value,HEX);
- if(results.value ==IRstop)
- {
- digitalWrite(MotorRight1,LOW);
- digitalWrite(MotorRight2,LOW);
- digitalWrite(MotorLeft1,LOW);
- digitalWrite(MotorLeft2,LOW);
- break;
- }
- }
- results.value=0;
-
- lcd.setCursor(0,0);
- lcd.print(" aoto mode");
- lcd.setCursor(0,1);
- lcd.print(" Left ");
- back(1);
- turnL(3); // 左轉
-
- }
-
- if (irrecv.decode(&results))
- {
- irrecv.resume();
- Serial.println(results.value,HEX);
- if(results.value ==IRstop)
- {
- digitalWrite(MotorRight1,LOW);
- digitalWrite(MotorRight2,LOW);
- digitalWrite(MotorLeft1,LOW);
- digitalWrite(MotorLeft2,LOW);
- break;
- }
- }
- }
- results.value=0;
- }
- /***********************************************************************/
- else
- {
- digitalWrite(MotorRight1,LOW);
- digitalWrite(MotorRight2,LOW);
- digitalWrite(MotorLeft1,LOW);
- digitalWrite(MotorLeft2,LOW);
- }
-
- irrecv.resume(); // 繼續收下一組紅外線訊號
- }
- …………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼 所有資料51hei提供下載(完整源碼+手冊-37頁介紹非常詳細):
遙控超聲波測距智能車 (1).rar
(9.79 MB, 下載次數: 36)
2018-1-21 20:24 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|