1. 功能說(shuō)明
在小型平行履帶底盤(pán)樣機(jī)上安裝2個(gè) 灰度傳感器 ,實(shí)現(xiàn)機(jī)器人沿指定路線進(jìn)行運(yùn)動(dòng)的效果。
2. 使用樣機(jī)
本實(shí)驗(yàn)使用的樣機(jī)為R026a樣機(jī)。
3. 功能實(shí)現(xiàn)
3.1 電子硬件
在這個(gè)示例中,我們采用了以下硬件,請(qǐng)大家參考:
主控板 | Basra(兼容Arduino Uno) |
擴(kuò)展板 | Bigfish2.1 |
傳感器 | 灰度傳感器 |
電池 | 7.4V鋰電池 |
3.2 編寫(xiě)程序
編程環(huán)境:Arduino 1.8.19
編寫(xiě)并燒錄以下程序(2gray_blackline.ino),該程序?qū)?shí)現(xiàn)演示動(dòng)圖中的動(dòng)作【例程源代碼資料下載詳見(jiàn) https://www.robotway.com/h-col-114.html】
/*------------------------------------------------------------------------------------ 版權(quán)說(shuō)明:Copyright 2023 Robottime(Beijing) Technology Co., Ltd. All Rights Reserved. Distributed under MIT license.See file LICENSE for detail or copy at https://opensource.org/licenses/MIT by 機(jī)器譜 2023-02-09 https://www.robotway.com/ ------------------------------ 實(shí)驗(yàn)接線:兩個(gè)直流電機(jī)接口的針腳號(hào)分別為D5,D6以及D9,D10;灰度傳感器連在A0、A4接口上 ------------------------------------------------------------------------------------*/ int i=0; void Forward(); void Back(); void Turn_left(); void Turn_right(); void Speed_up(); void Slow_down(); void setup() { // put your setup code here, to run once: pinMode(A0,INPUT); pinMode(A4,INPUT); pinMode(9,OUTPUT); pinMode(10,OUTPUT); pinMode(5,OUTPUT); pinMode(6,OUTPUT); Serial.begin(9600); } void loop() { // put your main code here, to run repeatedly: int a=digitalRead(A0); int b=digitalRead(A4); Serial.println(a); Serial.println(b);//Here is serial monitor,you will kown in later learning if(a==0&&b==0) Forward(); if(a==1&&b==0) Turn_left(); if(a==0&&b==1) Turn_right(); } void Forward()// { analogWrite(9,120); analogWrite(10,0); analogWrite(5,120); analogWrite(6,0); delay(50); } void Back() { digitalWrite(9,LOW); digitalWrite(10,HIGH); digitalWrite(5,LOW); digitalWrite(6,HIGH); delay(4000); } void Turn_left() { digitalWrite(9,0); digitalWrite(10,0); digitalWrite(5,100); digitalWrite(6,0); delay(50); } void Turn_right() { digitalWrite(9,100); digitalWrite(10,0); digitalWrite(5,0); digitalWrite(6,0); delay(50); } void Speed_up() { for(i=0;i<=255;i+=5) { analogWrite(9,i); analogWrite(10,0); analogWrite(5,i); analogWrite(6,0); delay(50); }//speed up } void Slow_down() { for(i=255;i>=0;i-=5) { analogWrite(9,i); analogWrite(10,0); analogWrite(5,i); analogWrite(6,0); delay(50); } }
審核編輯黃宇
-
傳感器
+關(guān)注
關(guān)注
2552文章
51359瀏覽量
755681 -
機(jī)器人
+關(guān)注
關(guān)注
211文章
28618瀏覽量
207927
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論