本次更新增加了前后傾斜(每次動(dòng)作交換前部和后部高度)、蹲起與抬腳動(dòng)作,均位于用于連續(xù)執(zhí)行動(dòng)作的function函數(shù)中,但實(shí)測(cè)抬腳動(dòng)作需要先啟動(dòng)function函數(shù)的另一項(xiàng)功能才能正常開啟,代碼檢查無(wú)誤,應(yīng)該是MicroPython固件的bug。另外,對(duì)于判斷功能,增加了elif語(yǔ)句的使用,避免重復(fù)使用if語(yǔ)句以提高運(yùn)行效率。
程序如下
import time
from machine import SoftI2C,Pin
from servo import Servos
import _thread
i2c=SoftI2C(sda=Pin(9),scl=Pin(8),freq=10000)
servos=Servos(i2c,address=0x40)
?
servos.position(0,90)
servos.position(1,90)
servos.position(2,90)
servos.position(3,90)
servos.position(4,90)
servos.position(5,90)
servos.position(6,90)
servos.position(7,90)
servos.position(8,120)
servos.position(9,120)
servos.position(10,120)
servos.position(11,120)
time.sleep(1)
?
Height1=0 #前部高度,0為最高
Height2=0 #后部高度,0為最高
mode=0
delay=300
?
def straight(angle): #直行,給所有轉(zhuǎn)向舵機(jī)寫入相應(yīng)角度以調(diào)整方向
? ? servos.position(0,angle)
? ? servos.position(1,angle)
? ? servos.position(2,angle)
? ? servos.position(3,angle)
?
def turn(angle): #傾斜
? ? servos.position(0,angle)
? ? servos.position(1,angle)
? ? servos.position(2,180-angle)
? ? servos.position(3,180-angle)
? ??
def tilt(height1,height2): #前后傾斜,局部變量與全局變量通過(guò)大小寫區(qū)分
? ? servos.position(4,90-height1)
? ? servos.position(5,90-height1)
? ? servos.position(6,90-height2)
? ? servos.position(7,90-height2)
? ? servos.position(8,120-height1-height1) #底部舵機(jī)使用兩倍偏置以確保平衡
? ? servos.position(9,120-height1-height1)
? ? servos.position(10,120-height2-height2)
? ? servos.position(11,120-height2-height2)
? ??
def crawl(step,angle): #爬行,第一個(gè)參數(shù)為步驟數(shù),第二個(gè)參數(shù)為相比直立狀態(tài)降低的角度,步長(zhǎng)隨第二個(gè)參數(shù)的增大而增大
? ? if step==1:
? ? ? ? servos.position(4,90-angle-angle)
? ? ? ? servos.position(7,90-angle)
? ? if step==2:
? ? ? ? servos.position(5,90-angle-angle)
? ? if step==3:
? ? ? ? servos.position(6,90-angle-angle)
? ? if step==4:
? ? ? ? servos.position(4,90-angle)
? ? ? ? servos.position(5,90-angle)
? ? ? ? servos.position(6,90-angle)
? ? ? ? servos.position(7,90-angle-angle)? ?
def walk(step,angle): #步行,第一個(gè)參數(shù)為步驟數(shù),第二個(gè)參數(shù)為相比直立狀態(tài)降低的角度,步長(zhǎng)隨第二個(gè)參數(shù)的增大而增大
? ? if step==1:
? ? ? ? servos.position(4,90-angle-angle)
? ? ? ? servos.position(5,90-angle)
? ? ? ? servos.position(6,90-angle)
? ? ? ? servos.position(7,90-angle-angle)
? ? if step==2:
? ? ? ? servos.position(4,90-angle)
? ? ? ? servos.position(5,90-angle-angle)
? ? ? ? servos.position(6,90-angle-angle)
? ? ? ? servos.position(7,90-angle)
?
i1=0
i2=0
def function(i1, i2): #功能函數(shù),執(zhí)行連續(xù)進(jìn)行的動(dòng)作
? ? global mode
? ? global delay
? ? global Height1
? ? global Height2
? ? while True:
? ? ? ? if Height1>45: #參數(shù)超出范圍時(shí)設(shè)為最大值
? ? ? ? ? ? Height1=45
? ? ? ? if Height2>45: #參數(shù)超出范圍時(shí)設(shè)為最大值
? ? ? ? ? ? Height2=45? ??
? ? ? ? if mode==0 or mode>6: #不執(zhí)行功能
? ? ? ? ? ? Step=0 #步數(shù)復(fù)位? ? ? ? ? ??
? ? ? ? elif mode==1: #爬行功能
? ? ? ? ? ? if Step==0:
? ? ? ? ? ? ? ? tilt(Height1,Height1) #先降低高度
? ? ? ? ? ? ? ? Step=1 #改變變量的值以進(jìn)入下一步驟
? ? ? ? ? ? elif Step==1:? ??
? ? ? ? ? ? ? ? crawl(1,Height1)
? ? ? ? ? ? ? ? Step=2
? ? ? ? ? ? elif Step==2:? ??
? ? ? ? ? ? ? ? crawl(2,Height1)
? ? ? ? ? ? ? ? Step=3
? ? ? ? ? ? elif Step==3:? ??
? ? ? ? ? ? ? ? crawl(3,Height1)
? ? ? ? ? ? ? ? Step=4
? ? ? ? ? ? elif Step==4:? ??
? ? ? ? ? ? ? ? crawl(4,Height1)
? ? ? ? ? ? ? ? Step=1
? ? ? ? elif mode==2: #爬行功能(反向)
? ? ? ? ? ? if Step==0:
? ? ? ? ? ? ? ? tilt(Height1,Height1) #先降低高度
? ? ? ? ? ? ? ? Step=4 #改變變量的值以進(jìn)入下一步驟
? ? ? ? ? ? elif Step==4:? ??
? ? ? ? ? ? ? ? crawl(4,Height1)
? ? ? ? ? ? ? ? Step=3
? ? ? ? ? ? elif Step==3:? ??
? ? ? ? ? ? ? ? crawl(3,Height1)
? ? ? ? ? ? ? ? Step=2
? ? ? ? ? ? elif Step==2:? ??
? ? ? ? ? ? ? ? crawl(2,Height1)
? ? ? ? ? ? ? ? Step=1
? ? ? ? ? ? elif Step==1:? ??
? ? ? ? ? ? ? ? crawl(1,Height1)
? ? ? ? ? ? ? ? Step=4??
? ? ? ? elif mode==3: #步行功能(由于只有兩個(gè)步驟,無(wú)正反向調(diào)節(jié),可能不能正常使用,后續(xù)視實(shí)際測(cè)試情況進(jìn)行調(diào)整)
? ? ? ? ? ? if Step>2:
? ? ? ? ? ? ? ? Step=0
? ? ? ? ? ? if Step==0:
? ? ? ? ? ? ? ? tilt(Height1,Height1) #先降低高度
? ? ? ? ? ? ? ? Step=1 #改變變量的值以進(jìn)入下一步驟
? ? ? ? ? ? elif Step==1:? ??
? ? ? ? ? ? ? ? walk(1,Height1)
? ? ? ? ? ? ? ? Step=2
? ? ? ? ? ? elif Step==2:? ??
? ? ? ? ? ? ? ? walk(2,Height1)
? ? ? ? ? ? ? ? Step=1
? ? ? ? elif mode==4: #前后傾斜
? ? ? ? ? ? if Step>2:
? ? ? ? ? ? ? ? Step=0
? ? ? ? ? ? if Step==0:
? ? ? ? ? ? ? ? tilt(0,0) #回到直立狀態(tài)
? ? ? ? ? ? ? ? Step=1 #改變變量的值以進(jìn)入下一步驟
? ? ? ? ? ? elif Step==1:? ??
? ? ? ? ? ? ? ? tilt(Height1,Height2)
? ? ? ? ? ? ? ? Step=2
? ? ? ? ? ? elif Step==2:
? ? ? ? ? ? ? ? tilt(Height1,Height2) #向反方向傾斜
? ? ? ? ? ? ? ? Step=0
? ? ? ? elif mode==5: #蹲起動(dòng)作
? ? ? ? ? ? if Step>1:
? ? ? ? ? ? ? ? Step=0
? ? ? ? ? ? if Step==0:
? ? ? ? ? ? ? ? tilt(0,0) #回到直立狀態(tài)
? ? ? ? ? ? ? ? Step=1 #改變變量的值以進(jìn)入下一步驟
? ? ? ? ? ? elif Step==1:? ??
? ? ? ? ? ? ? ? tilt(Height1,Height2)
? ? ? ? ? ? ? ? Step=0
? ? ? ? elif mode==6: #抬腳動(dòng)作
? ? ? ? ? ? tilt(0,0) #回到直立狀態(tài)
? ? ? ? ? ? if Step==1:
? ? ? ? ? ? ? ? servos.position(4,20)
? ? ? ? ? ? ? ? Step=2 #改變變量的值以進(jìn)入下一步驟
? ? ? ? ? ? elif Step==2:? ??
? ? ? ? ? ? ? ? servos.position(4,90)
? ? ? ? ? ? ? ? Step=3
? ? ? ? ? ? elif Step==3:? ??
? ? ? ? ? ? ? ? servos.position(5,20)
? ? ? ? ? ? ? ? Step=4
? ? ? ? ? ? elif Step==4:? ??
? ? ? ? ? ? ? ? servos.position(5,90)
? ? ? ? ? ? ? ? Step=1? ??
? ? ? ? time.sleep_ms(delay)
?
#開啟線程
_thread.start_new_thread(function, (i1, i2))
? ??
while True:
? ? try:? ??
? ? ? ? data=int(input('input')) #輸入9位數(shù)字
? ? ? ? mode=data//100000000 #第一位為模式
? ? ? ? Height1=data%100000000 #第二、三位為前部高度或步長(zhǎng)
? ? ? ? Height1=Height1//1000000
? ? ? ? Height2=data%1000000 #第四、五、六位為后部高度或旋轉(zhuǎn)角度
? ? ? ? Height2=Height2//1000
? ? ? ? delay=data%1000 #最后三位為延時(shí)
? ? ? ? #非連續(xù)執(zhí)行的功能在此處執(zhí)行文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-618678.html
?if mode==7: #直行功能
? ? ? ? ? ? if Height2<=180: #參數(shù)超出范圍則不執(zhí)行,下同
? ? ? ? ? ? ? ? straight(Height2) ?
? ? ? ? elif mode==8: #轉(zhuǎn)向功能
? ? ? ? ? ? if Height2<=180:
? ? ? ? ? ? ? ? turn(Height2)
? ? ? ? elif mode==9: #傾斜功能
? ? ? ? ? ? if Height1<=60 and Height2<=60:
? ? ? ? ? ? ? ? tilt(Height1,Height2) ? ? ? ?
? ? except:
? ? ? ? pass
? ??文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-618678.html
到了這里,關(guān)于ESP32(MicroPython) 四足機(jī)器人(五)功能補(bǔ)充的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!