找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 4856|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

已制成樹(shù)莓派控制六足機(jī)器人,參考樹(shù)莓派實(shí)驗(yàn)室,具體學(xué)習(xí)及制作過(guò)程見(jiàn)附件

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:583802 發(fā)表于 2019-7-14 10:04 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式

一:前期準(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í)教程,

  • 理解伺服電機(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)行清理

GPIO.cleanup()

二:開(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í)際的布線圖如下圖所示

最終成品視頻附于附件一

源代碼:

  1. <font style="font-size: 15pt">import RPi.GPIO as GPIO
  2. import pigpio
  3. import time
  4. GPIO.setmode(GPIO.BCM)
  5. GPIO.setwarnings(False)
  6. tilt = 4
  7. br = 21
  8. bl = 6
  9. trig = 23
  10. echo = 24
  11. GPIO.setup(trig, GPIO.OUT)
  12. GPIO.setup(echo, GPIO.IN)
  13. pi = pigpio.pi()
  14. def backward():
  15.     pi.set_servo_pulsewidth(tilt, 800)
  16.     time.sleep(0.15)
  17.     pi.set_servo_pulsewidth(bl, 800)   
  18.     time.sleep(0.15)
  19.     pi.set_servo_pulsewidth(tilt, 2000)
  20.     time.sleep(0.15)
  21.     pi.set_servo_pulsewidth(br, 1800)
  22.     time.sleep(0.15)
  23.     pi.set_servo_pulsewidth(tilt, 1500)
  24.     time.sleep(0.15)
  25.     pi.set_servo_pulsewidth(bl, 1500)  
  26.     time.sleep(0.15)
  27.     pi.set_servo_pulsewidth(br, 1500)  
  28.     time.sleep(0.15)
  29.     return;
  30. def forward():
  31.     pi.set_servo_pulsewidth(tilt, 800)
  32.     time.sleep(0.15)
  33.     pi.set_servo_pulsewidth(bl, 1800)      
  34.     time.sleep(0.15)
  35.     pi.set_servo_pulsewidth(tilt, 2000)
  36.     time.sleep(0.15)
  37.     pi.set_servo_pulsewidth(br, 800)
  38.     time.sleep(0.15)
  39.     pi.set_servo_pulsewidth(tilt, 1500)
  40.     time.sleep(0.15)
  41.     pi.set_servo_pulsewidth(bl, 1500)  
  42.     time.sleep(0.15)
  43.     pi.set_servo_pulsewidth(br, 1500)  
  44.     time.sleep(0.15)
  45.     return;
  46. def left():
  47.     pi.set_servo_pulsewidth(tilt, 800)
  48.     time.sleep(0.15)
  49.     pi.set_servo_pulsewidth(bl, 1800)      
  50.     time.sleep(0.15)
  51.     pi.set_servo_pulsewidth(tilt, 2000)
  52.     time.sleep(0.15)
  53.     pi.set_servo_pulsewidth(br, 1800)
  54.     time.sleep(0.15)
  55.     pi.set_servo_pulsewidth(tilt, 1500)
  56.     time.sleep(0.15)
  57.     pi.set_servo_pulsewidth(bl, 1500)  
  58.     time.sleep(0.15)
  59.     pi.set_servo_pulsewidth(br, 1500)  
  60.     time.sleep(0.15)
  61.     return;
  62. def right():
  63.     pi.set_servo_pulsewidth(tilt, 800)
  64.     time.sleep(0.15)
  65.     pi.set_servo_pulsewidth(bl, 800)   
  66.     time.sleep(0.15)
  67.     pi.set_servo_pulsewidth(tilt, 2000)
  68.     time.sleep(0.15)
  69.     pi.set_servo_pulsewidth(br, 800)
  70.     time.sleep(0.15)
  71.     pi.set_servo_pulsewidth(tilt, 1500)
  72.     time.sleep(0.15)
  73.     pi.set_servo_pulsewidth(bl, 1500)  
  74.     time.sleep(0.15)
  75.     pi.set_servo_pulsewidth(br, 1500)  
  76.     time.sleep(0.15)
  77.     return;
  78.      
  79. def stop():
  80.     pi.set_servo_pulsewidth(tilt, 0)
  81.     time.sleep(0.15)
  82.     pi.set_servo_pulsewidth(bl, 0)  
  83.     time.sleep(0.15)
  84.     pi.set_servo_pulsewidth(br, 0)  
  85.     time.sleep(0.15)
  86.      
  87.     return
  88. def obstacleDetected():
  89.     backward()
  90.     backward()
  91.     backward()
  92.     backward()
  93.     backward()
  94.     right()
  95.     right()
  96.     right()
  97.      
  98.     return
  99. def turnHead():
  100.     pi.set_servo_pulsewidth(head, 700)
  101.     time.sleep(0.5)
  102.     pi.set_servo_pulsewidth(head, 2100)
  103.     time.sleep(0.5)
  104.     pi.set_servo_pulsewidth(head, 1500)
  105.     time.sleep(0.5)
  106.     return
  107. def autoMode():
  108.     print ("Running in auto mode!")
  109.     turnHead()
  110.      
  111.     time.sleep(0.5)
  112.     GPIO.output(trig, 0)
  113.     time.sleep(0.5)
  114.      
  115.     GPIO.output(trig,1)
  116.     time.sleep(0.00001)
  117.     GPIO.output(trig,0)
  118.      
  119.     while GPIO.input(echo) == 0:
  120.         pulse_start = time.time()
  121.      
  122.     while GPIO.input(echo) == 1:
  123.         pulse_end = time.time()
  124.     pulse_duration = pulse_end - pulse_start
  125.      
  126.     distance = pulse_duration * 17150
  127.      
  128.     distance = round(distance, 2)
  129.      
  130.     if distance > 1 and distance < 35:
  131.         obstacleDetected()
  132.     else:
  133.         forward()
  134.         forward()
  135.         forward()
  136.      
  137.     pi.set_servo_pulsewidth(head, 2100)
  138.     time.sleep(0.5)
  139.     return
  140.      
  141. def manualMode():
  142.      
  143.         move = str(sys.argv[2])
  144.         if move == "F" or move == "f":
  145.         print("Moving forward!")
  146.         forward()
  147.     elif move == "B" or move == "b":
  148.         print("Moving backward!")
  149.         backward()
  150.     elif move == "L" or move == "l":
  151.         print("Moving left!")
  152.         left()
  153.     elif move == "R" or move == "r":
  154.         print("Moving right!")
  155.         right()
  156.     else:
  157.         print("Invalid argument!")
  158.      
  159.     return
  160.      
  161. def main():
  162.     opt = str(sys.argv[1])
  163.      
  164.     if opt == "A" or opt == "a":
  165.         autoMode()
  166.     elif opt == "M" or opt == "m":
  167.         manualMode()
  168.      
  169.     return
  170.          
  171. while True:
  172.     main()
  173. GPIO.cleanup()
  174. pi.stop()
  175. </font>
復(fù)制代碼

以上的Word格式文檔51黑下載地址:

3六足機(jī)器人制作文檔.docx (1.8 MB, 下載次數(shù): 29)


評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表