最近博主在使用幻爾公司 串行總線舵機時,想使用k210控制,由於官方沒有相關例程(樹莓派的版本是python版本代碼,用不了)特此分享一下控制代碼
a.to_bytes(x,'little'/'big')
#將int型變成byte型
#第一個參數是顯示位數,第二個參數為顯示順序:從小到大/從大到小
int.from_bytes(a,'little'/'big')
#將byte型變成int型
#第一個參數是顯示位數,第二個參數為顯示順序:從小到大/從大到小
需要注意波特率為115200
這裡計算校驗和要逐個byte轉成int相加,不是直接把各參數相加
例如servoWriteCmd(1,1,0,1000)
並非是
1+7+1+0+1000
而是化為byte型
0x01+0x07+ 0x01 +0x00 +0x00+ 0xE8 +0x03 = 0xF4 = 244
0xF4取~後超過255因此取最後字節1011即0x0B
故最後的消息為55 55 01 07 01 00 00 E8 03 0B
from machine import UART,Timer
from Maix import GPIO
from fpioa_manager import fm
from ubinascii import *
import time
#映射串口引腳
#初始化串口
uart = UART(UART.UART1, 115200, read_buf_len=4096)
def servoWriteCmd(id, cmd, par1, par2):
fm.register(6, fm.fpioa.UART1_RX, force=True)
fm.register(7, fm.fpioa.UART1_TX, force=True)
begin = 85 #0x55的十進制
buf= begin.to_bytes(1,'little')
buf += buf
try:
len = 7
sum = 0
a = id.to_bytes(1,'little')
b = len.to_bytes(1,'little')
c = cmd.to_bytes(1,'little')
d = par1.to_bytes(1,'little')
dd = par1.to_bytes(1,'big')
f = par2.to_bytes(2,'little')
ff = par2.to_bytes(2,'big')
#print(par2 % 256)
sum = int.from_bytes(a,'big')+ int.from_bytes(b,'big')+ int.from_bytes(c,'big')+(par1 % 256)+(par1 // 256)+(par2 % 256)+(par2 // 256)
print(sum)
sum = ~sum #取反
print(sum.to_bytes(1,'little'))
buf += id.to_bytes(1,'little')+ len.to_bytes(1,'little') + cmd.to_bytes(1,'little')+par1.to_bytes(2,'little')+par2.to_bytes(2,'little')+sum.to_bytes(1,'little')
uart.write(buf)
print(buf)
except Exception as e:
print(e)
def portInit(): #配置用到的IO口
fm.register(6, fm.fpioa.GPIO0, force=True)
RX = GPIO(GPIO.GPIO0,GPIO.OUT)
RX.value(0)
fm.register(7, fm.fpioa.GPIO1, force=True)
TX = GPIO(GPIO.GPIO1,GPIO.OUT)
TX.value(1)
def portWrite(): #配置單線串口為輸出
fm.register(6, fm.fpioa.GPIO0, force=True)
RX = GPIO(GPIO.GPIO0,GPIO.OUT)
RX.value(0)
fm.register(7, fm.fpioa.GPIO1, force=True)
TX = GPIO(GPIO.GPIO1,GPIO.OUT)
TX.value(1)
def portRead(): #配置單線串口為輸入
fm.register(6, fm.fpioa.GPIO0, force=True)
RX = GPIO(GPIO.GPIO0,GPIO.OUT)
RX.value(1)
fm.register(7, fm.fpioa.GPIO1, force=True)
TX = GPIO(GPIO.GPIO1,GPIO.OUT)
TX.value(0)
portInit()
while True:
try:
portWrite() #將單線串口配置為輸出
servoWriteCmd(1,1,0,1000) #發送命令 參數1 舵機id=1, 參數2 命令 = 1, 參數3 位置 = 0, 參數4 時間 = 1000ms 55 55 01 07 01 00 00 E8 03 0B
time.sleep(1.1)
servoWriteCmd(1,1,1000,2000)#55 55 01 07 01 E8 03 D0 07 34
time.sleep(2.1)
except Exception as e:
print(e)
break
需要注意波特率為9600
控制板的代碼相對簡單,沒有校驗和只需要將相應參數轉換為byte型即可,這裡只寫了控制單個舵機的函數,多個舵機控制讀者請酌情自行編寫
# Untitled - By: lenovo - 周一 8月 1 2022
from machine import UART,Timer
from Maix import GPIO
from fpioa_manager import fm
from ubinascii import *
import time
#映射串口引腳
#初始化串口
uart = UART(UART.UART1, 9600, read_buf_len=4096)
def servoWriteCmd(cmd,count,par1,id,par2):
fm.register(6, fm.fpioa.UART1_RX, force=True)
fm.register(7, fm.fpioa.UART1_TX, force=True)
begin = 85
buf= begin.to_bytes(1,'little')
buf += buf
try:
len = count*3+5
buf += len.to_bytes(1,'little')+ cmd.to_bytes(1,'little') + count.to_bytes(1,'little')+par1.to_bytes(2,'little')+id.to_bytes(1,'little')+par2.to_bytes(2,'little')
uart.write(buf)
print(buf)
except Exception as e:
print(e)
#portInit()
while True:
try:
#portWrite() #將單線串口配置為輸出
servoWriteCmd(3,1,1000,2,800) #發送命令 參數1 舵機id=1, 參數2 命令 = 1, 參數3 位置 = 0, 參數4 時間 = 1000ms
time.sleep(1.1)
servoWriteCmd(3,1,1000,2,100)#
time.sleep(2.1)
except Exception as e:
print(e)
break