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

制備安裝好之后是這個(gè)樣子的: 
軟件部分: (1)先配置樹(shù)莓派,開(kāi)啟遠(yuǎn)程連接,由于步驟過(guò)于簡(jiǎn)單,在此省略,請(qǐng)參照2制作日志或登陸樹(shù)莓派實(shí)驗(yàn)室之初級(jí)教程。 詳見(jiàn)樹(shù)莓派實(shí)驗(yàn)室初級(jí)教程, 1定義:伺服馬達(dá)受不同長(zhǎng)度的脈沖控制;旧峡梢赃@樣理解,伺服電機(jī)接收到1個(gè)脈沖,就會(huì)旋轉(zhuǎn)1個(gè)脈沖對(duì)應(yīng)的角度,從而實(shí)現(xiàn)位移。詳細(xì)定義請(qǐng)自行百度, 2樹(shù)莓派如何操作: 在這個(gè)網(wǎng)址下,有關(guān)于樹(shù)莓派上操作gpio端口的全部詳細(xì)解釋,https://www.cnblogs.com/dongxiaodong/p/9877734.html 簡(jiǎn)單概括為:樹(shù)莓派的管腳有兩種命名方式,分別為wpi和bcm碼,需要在代碼中說(shuō)明, 以下是一個(gè)實(shí)例,示范調(diào)用代碼的格式 1、首先對(duì) RPi.GPIO 進(jìn)行設(shè)置 | import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) #物理引腳編碼 GPIO.setup(12, GPIO.OUT) |
2、設(shè)置某個(gè)輸出針腳狀態(tài)為高電平: | GPIO.output(12, GPIO.HIGH) # 或者 GPIO.output(12, 1) # 或者 GPIO.output(12, True) |
3、設(shè)置某個(gè)輸出針腳狀態(tài)為低電平: | GPIO.output(12, GPIO.LOW) # 或者 GPIO.output(12, 0) # 或者 GPIO.output(12, False) |
4、程序結(jié)束后進(jìn)行清理 二:開(kāi)發(fā)程序: 了解了對(duì)于gpio接口的控制之后我們開(kāi)始寫程序, 源程序在附于最后,寫完后使用樹(shù)莓派連接,進(jìn)入目標(biāo)文件夾 即: cd Desktop Cd pythfiles 執(zhí)行命令:python hexapod1.py m f 即可使機(jī)器人前進(jìn),連線圖如下圖所示 實(shí)際的布線圖如下圖所示 
最終成品視頻附于附件一 源代碼: - <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六足機(jī)器人制作文檔.docx
(1.8 MB, 下載次數(shù): 29)
2019-7-14 10:04 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|