一:前期準(zhǔn)備工作: 硬件部分: (1)材料: 樹莓派3b+,超聲波測距模塊,手機(提供熱點),towerpro sg 5100電機三個,towerpro sg 90電機一個,杜邦線若干,面包板,電阻,四個五號電池連起來的電池盒兩個,五號電池八節(jié),鋁板,亞克力板,螺絲螺帽若干, (2)圖紙: 機器人本體部分: 
各條腿: 

制備安裝好之后是這個樣子的: 
軟件部分: (1)先配置樹莓派,開啟遠(yuǎn)程連接,由于步驟過于簡單,在此省略,請參照2制作日志或登陸樹莓派實驗室之初級教程。 詳見樹莓派實驗室初級教程, 1定義:伺服馬達(dá)受不同長度的脈沖控制。基本上可以這樣理解,伺服電機接收到1個脈沖,就會旋轉(zhuǎn)1個脈沖對應(yīng)的角度,從而實現(xiàn)位移。詳細(xì)定義請自行百度, 2樹莓派如何操作: 在這個網(wǎng)址下,有關(guān)于樹莓派上操作gpio端口的全部詳細(xì)解釋,https://www.cnblogs.com/dongxiaodong/p/9877734.html 簡單概括為:樹莓派的管腳有兩種命名方式,分別為wpi和bcm碼,需要在代碼中說明, 以下是一個實例,示范調(diào)用代碼的格式 1、首先對 RPi.GPIO 進行設(shè)置 | import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) #物理引腳編碼 GPIO.setup(12, GPIO.OUT) |
2、設(shè)置某個輸出針腳狀態(tài)為高電平: | GPIO.output(12, GPIO.HIGH) # 或者 GPIO.output(12, 1) # 或者 GPIO.output(12, True) |
3、設(shè)置某個輸出針腳狀態(tài)為低電平: | GPIO.output(12, GPIO.LOW) # 或者 GPIO.output(12, 0) # 或者 GPIO.output(12, False) |
4、程序結(jié)束后進行清理 二:開發(fā)程序: 了解了對于gpio接口的控制之后我們開始寫程序, 源程序在附于最后,寫完后使用樹莓派連接,進入目標(biāo)文件夾 即: cd Desktop Cd pythfiles 執(zhí)行命令:python hexapod1.py m f 即可使機器人前進,連線圖如下圖所示 實際的布線圖如下圖所示 
最終成品視頻附于附件一 源代碼: - <font style="font-size: 15pt">import RPi.GPIO as GPIO
- import pigpio
- import time
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- tilt = 4
- br = 21
- bl = 6
- trig = 23
- echo = 24
- GPIO.setup(trig, GPIO.OUT)
- GPIO.setup(echo, GPIO.IN)
- pi = pigpio.pi()
- def backward():
- pi.set_servo_pulsewidth(tilt, 800)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(bl, 800)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(tilt, 2000)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(br, 1800)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(tilt, 1500)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(bl, 1500)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(br, 1500)
- time.sleep(0.15)
- return;
- def forward():
- pi.set_servo_pulsewidth(tilt, 800)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(bl, 1800)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(tilt, 2000)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(br, 800)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(tilt, 1500)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(bl, 1500)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(br, 1500)
- time.sleep(0.15)
- return;
- def left():
- pi.set_servo_pulsewidth(tilt, 800)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(bl, 1800)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(tilt, 2000)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(br, 1800)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(tilt, 1500)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(bl, 1500)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(br, 1500)
- time.sleep(0.15)
- return;
- def right():
- pi.set_servo_pulsewidth(tilt, 800)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(bl, 800)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(tilt, 2000)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(br, 800)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(tilt, 1500)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(bl, 1500)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(br, 1500)
- time.sleep(0.15)
- return;
-
- def stop():
- pi.set_servo_pulsewidth(tilt, 0)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(bl, 0)
- time.sleep(0.15)
- pi.set_servo_pulsewidth(br, 0)
- time.sleep(0.15)
-
- return
- def obstacleDetected():
- backward()
- backward()
- backward()
- backward()
- backward()
- right()
- right()
- right()
-
- return
- def turnHead():
- pi.set_servo_pulsewidth(head, 700)
- time.sleep(0.5)
- pi.set_servo_pulsewidth(head, 2100)
- time.sleep(0.5)
- pi.set_servo_pulsewidth(head, 1500)
- time.sleep(0.5)
- return
- def autoMode():
- print ("Running in auto mode!")
- turnHead()
-
- time.sleep(0.5)
- GPIO.output(trig, 0)
- time.sleep(0.5)
-
- GPIO.output(trig,1)
- time.sleep(0.00001)
- GPIO.output(trig,0)
-
- while GPIO.input(echo) == 0:
- pulse_start = time.time()
-
- while GPIO.input(echo) == 1:
- pulse_end = time.time()
- pulse_duration = pulse_end - pulse_start
-
- distance = pulse_duration * 17150
-
- distance = round(distance, 2)
-
- if distance > 1 and distance < 35:
- obstacleDetected()
- else:
- forward()
- forward()
- forward()
-
- pi.set_servo_pulsewidth(head, 2100)
- time.sleep(0.5)
- return
-
- def manualMode():
-
- move = str(sys.argv[2])
- if move == "F" or move == "f":
- print("Moving forward!")
- forward()
- elif move == "B" or move == "b":
- print("Moving backward!")
- backward()
- elif move == "L" or move == "l":
- print("Moving left!")
- left()
- elif move == "R" or move == "r":
- print("Moving right!")
- right()
- else:
- print("Invalid argument!")
-
- return
-
- def main():
- opt = str(sys.argv[1])
-
- if opt == "A" or opt == "a":
- autoMode()
- elif opt == "M" or opt == "m":
- manualMode()
-
- return
-
- while True:
- main()
- GPIO.cleanup()
- pi.stop()
- </font>
復(fù)制代碼
以上的Word格式文檔51黑下載地址:
3六足機器人制作文檔.docx
(1.8 MB, 下載次數(shù): 29)
2019-7-14 10:04 上傳
點擊文件名下載附件
|