pyserial是一个串口通信驱动库,常用的Windows、Linux、MacOS等都可以安装,这里我使用的是树莓派4B来测试,这块板子还是很强大的,我们可以通过pyserial这个库来操作基于这块板子上的机器狗之类的设备。
1、四足机器狗
本人的是四足机器狗,每条腿有三个舵机,所以是3个自由度,四条腿总共12个舵机也就是12自由度,每个舵机分别控制着对应的关节,比如根关节控制基节,髋关节控制大腿,膝关节控制小腿,如下图:
实物机器人,里面的关节矩阵的顺序是,前面左边为第一组,右边为第二组,后面的右边为第三组,后面的左边为第四组。
2、OS环境
在安装之前我们一般都先熟悉下本地环境,这样方便下载安装对应的版本。
cat /etc/os-releaseNAME="Ubuntu"
VERSION="20.04.4 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.4 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal
查看芯片的类型:uname -a
Linux yahboom 5.4.0-1069-raspi #79-Ubuntu SMP PREEMPT Thu Aug 18 18:15:22 UTC 2022 aarch64 aarch64 aarch64 GNU/Linux
可以看到是64位的架构而且是全新的ARMv8架构,使用的也是全新的A64指令集,其中 raspi 就是Raspberry Pi树莓派的别名,这个就是本人的树莓派在Ubuntu 20版本的环境情况。
3、安装pyserial
安装命令:pip3 install pyserial
安装好了之后,我们来测试下是否成功,安装包是pyserial,导入模块是serial。
import serial
print(serial.VERSION)
#'3.5'
#dir(serial)
['CR', 'EIGHTBITS', 'FIVEBITS', 'LF', 'PARITY_EVEN', 'PARITY_MARK', 'PARITY_NAMES', 'PARITY_NONE', 'PARITY_ODD', 'PARITY_SPACE', 'PortNotOpenError', 'PosixPollSerial', 'SEVENBITS', 'SIXBITS', 'STOPBITS_ONE', 'STOPBITS_ONE_POINT_FIVE', 'STOPBITS_TWO', 'Serial', 'SerialBase', 'SerialException', 'SerialTimeoutException', 'Timeout', 'VERSION', 'VTIMESerial', 'XOFF', 'XON', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', 'absolute_import', 'basestring', 'importlib', 'io', 'iterbytes', 'os', 'protocol_handler_packages', 'serial_for_url', 'serialposix', 'serialutil', 'sys', 'time', 'to_bytes', 'unicode']
4、串口通信接口
4.1、list_ports接口列表
首先我们列出有哪些可用的接口
from serial.tools import list_ports
plist = list_ports.comports()
print(plist[0][0])
#'/dev/ttyAMA0'
这个'/dev/ttyAMA0'就是树莓派4B与上位机的通讯接口,得到了接口名称之后,我们就可以打开这个接口进行通信了。
4.2、打开接口
ser = serial.Serial('/dev/ttyAMA0',115200,timeout=5)
这里的超时时间推荐指定,避免因为读取不到数据,程序会一直等待。
print(ser)
#Serial<id=0xffff87addc40, open=True>(port='/dev/ttyAMA0', baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=False, rtscts=False, dsrdtr=False)
这里在串口设备名称与端口指的是同一个东西:ser.name和ser.port
查看里面包含哪些方法与属性: dir(ser)
['BAUDRATES', 'BAUDRATE_CONSTANTS', 'BYTESIZES', 'PARITIES', 'STOPBITS', '_SAVED_SETTINGS', '__abstractmethods__', '__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_abc_impl', '_baudrate', '_break_state', '_bytesize', '_checkClosed', '_checkReadable', '_checkSeekable', '_checkWritable', '_dsrdtr', '_dtr_state', '_exclusive', '_inter_byte_timeout', '_parity', '_port', '_reconfigure_port', '_rs485_mode', '_rts_state', '_rtscts', '_set_rs485_mode', '_set_special_baudrate', '_stopbits', '_timeout', '_update_break_state', '_update_dtr_state', '_update_rts_state', '_write_timeout', '_xonxoff', 'applySettingsDict', 'apply_settings', 'baudrate', 'break_condition', 'bytesize', 'cancel_read', 'cancel_write', 'cd', 'close', 'closed', 'cts', 'dsr', 'dsrdtr', 'dtr', 'exclusive', 'fd', 'fileno', 'flush', 'flushInput', 'flushOutput', 'getCD', 'getCTS', 'getDSR', 'getRI', 'getSettingsDict', 'get_settings', 'inWaiting', 'in_waiting', 'interCharTimeout', 'inter_byte_timeout', 'iread_until', 'isOpen', 'is_open', 'isatty', 'name', 'nonblocking', 'open', 'out_waiting', 'parity', 'pipe_abort_read_r', 'pipe_abort_read_w', 'pipe_abort_write_r', 'pipe_abort_write_w', 'port', 'portstr', 'read', 'read_all', 'read_until', 'readable', 'readall', 'readinto', 'readline', 'readlines', 'reset_input_buffer', 'reset_output_buffer', 'ri', 'rs485_mode', 'rts', 'rtscts', 'seek', 'seekable', 'sendBreak', 'send_break', 'setDTR', 'setPort', 'setRTS', 'set_input_flow_control', 'set_output_flow_control', 'stopbits', 'tell', 'timeout', 'truncate', 'writable', 'write', 'writeTimeout', 'write_timeout', 'writelines', 'xonxoff']
4.3、字节数组
打开串口接口,我们试着来读取一行:b_data=ser.readline()
#b'U\x00\t\x12\x01\x14\xcf\x00\xaa'
转成列表就是十进制的形式,看起来就比较直观点
list(b'U\x00\t\x12\x01\x14\xcf\x00\xaa')
#[85, 0, 9, 18, 1, 20, 207, 0, 170]
这里可以看到我们实际读取的是长度为9的一个数组,我们思考下,我们操作机器人,应该也是写入长度为9的数组就可以控制机器人的各种状态了,事实证明,想法是正确的。
4.4、serial参数设置
一些常见的重要参数如下:
port:串口的设备名或接口(None)
baudrate:波特率,50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000, 2500000, 3000000, 3500000, 4000000
bytesize:数据位,每个字节的位数,一般是7或8,默认是8
parity:校验位,PARITY_NONE, PARITY_EVEN, PARITY_ODD, PARITY_MARK, PARITY_SPACE
stopbits:停止位,1位停止位、1.5位停止位、2位停止位(STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO)
xonxoff:软件流控(True, False)
rtscts:硬件(RTS/CTS)流控(True, False)
dsr/dtr:硬件(DSR/DTR)流控( True, False)
timeout:读超时,推荐指定,避免堵塞
writeTimeout:写超时
其中波特率需要注意以下三点
传输速度:波特率越高,传输速度越快,但也会增加传输错误的可能性。
传输距离:波特率越高,传输距离越短,因为高速传输会导致信号衰减。
硬件支持:波特率需要与硬件设备匹配,如果设备不支持高速传输,则无法使用高波特率。
5、机器狗
有了上面的知识铺垫,我们来实际操控机器狗,先让机器狗往前跑动起来。源码有比较多的功能,这里我只针对机器狗的向前跑动,,将代码精简出来,让我们对这个串口的通信有一个更加清晰的了解
5.1、代码
gedit MYDOG.py
import serialdef conver2u8(data, limit):return int(128 + 128 * data / limit)class DOG():def __init__(self,port="/dev/ttyAMA0"):self.ser = serial.Serial(port, 115200, timeout=0.5)#往前跑动def moveX(self,step):u8v = conver2u8(step, 25)mode = 0x01order = 0x30value = []value_sum = 0value.append(u8v)value_sum = value_sum + u8vsum_data = ((1 + 0x08) + mode + order + value_sum) % 256sum_data = 255 - sum_datatx = [0x55, 0x00, (1 + 0x08), mode, order]tx.extend(value)tx.extend([sum_data, 0x00, 0xAA])print(tx)self.ser.write(tx)# 停止def stop(self):self.moveX(0)
测试下结果:
import MYDOG
mydog = MYDOG.DOG()
mydog.moveX(5)
这样机器狗就会往前跑动了,step是设置机器狗的步伐宽度,可以看到初始化接口的设备和波特率以及推荐加上超时时间,我们就通过write()方法进行写入即可。mydog.stop()也就是发送的步伐宽度为0就可以让机器狗停止了。
5.2、ser.write写入
我们可以打印tx变量的值,这样就可以查看写入了一些什么内容:
跑动时
[85, 0, 9, 1, 48, 153, 44, 0, 170]
b'U\x00\t\x01\x30\x99\x2c\x00\xaa'
停止时
[85, 0, 9, 1, 48, 128, 69, 0, 170]
b'U\x00\t\x01\x30\x80\x45\x00\xaa'
那么从上面分析来看,我们只需要写入一串列表数值即可,所以精简为如下三条命令就可以让机器狗跑动起来。
精简代码如下:
import serial
ser=serial.Serial("/dev/ttyAMA0", 115200, timeout=0.5)
ser.write([85, 0, 9, 1, 48, 153, 44, 0, 170])
当然这里的十进制也可以使用十六进制的形式代替:
ser.write([0x55, 0x0, 0x9, 0x1, 0x30, 0x99, 0x2c, 0x0, 0xaa])
5.3、ser.readline读取
再次来查看下接收到的信息,这里将超时时间调大点不然获取不到数据:
import serial
ser=serial.Serial("/dev/ttyAMA0", 115200, timeout=5)
ser.readline()
跑动时:b'U\x00\t\x01\x30\x99\x2c\x00\xaa'
停止时:b'U\x00\t\x12\x01d\x7f\x00\xaa'
到这里就完成了,串口通讯的读写操作,这里的值是字节数组、十进制数组、十六进制数组等都是等价的,也就是说我们写入这样的字节数组ser.write(b'U\x00\t\x01\x30\x99\x2c\x00\xaa')同样也是可以让机器狗跑起来,效果是一样的。
6、完整代码与功能
上述是通过写入串口数据来让机器狗跑动起来,熟悉串口库的使用,这里贴出完整代码,里面常见的操作机器狗的功能都有,代码如下:
import serial
import struct
import time__version__ = '2.0.7'
__last_modified__ = '2022/11/18'"""
ORDER 用来存放命令地址和对应数据
ORDER is used to store the command address and corresponding data
"""
ORDER = {"BATTERY": [0x01, 100],"PERFORM": [0x03, 0],"CALIBRATION": [0x04, 0],"UPGRADE": [0x05, 0],"MOVE_TEST": [0x06, 1],"FIRMWARE_VERSION": [0x07],"GAIT_TYPE": [0x09, 0x00],"BT_NAME": [0x13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],"UNLOAD_MOTOR": [0x20, 0],"LOAD_MOTOR": [0x20, 0],"VX": [0x30, 128],"VY": [0x31, 128],"VYAW": [0x32, 128],"TRANSLATION": [0x33, 0, 0, 0],"ATTITUDE": [0x36, 0, 0, 0],"PERIODIC_ROT": [0x39, 0, 0, 0],"MarkTime": [0x3C, 0],"MOVE_MODE": [0x3D, 0],"ACTION": [0x3E, 0],"PERIODIC_TRAN": [0x80, 0, 0, 0],"MOTOR_ANGLE": [0x50, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128],"MOTOR_SPEED": [0x5C, 1],"LEG_POS": [0x40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],"IMU": [0x61, 0],"ROLL": [0x62, 0],"PITCH": [0x63, 0],"YAW": [0x64, 0]
}"""
PARAM 用来存放机器狗的参数限制范围
PARAM is used to store the parameter limit range of the robot dog
"""PARAM = {"TRANSLATION_LIMIT": [35, 18, [75, 115]], # X Y Z 平移范围 Scope of translation"ATTITUDE_LIMIT": [20, 15, 11], # Roll Pitch Yaw 姿态范围 Scope of posture"LEG_LIMIT": [35, 18, [75, 115]], # 腿长范围 Scope of the leg"MOTOR_LIMIT": [[-73, 57], [-66, 93], [-31, 31]], # 下 中 上 舵机范围 Lower, middle and upper steering gear range"PERIOD_LIMIT": [[1.5, 8]],"MARK_TIME_LIMIT": [10, 35], # 原地踏步高度范围 Stationary height range"VX_LIMIT": 25, # X速度范围 X velocity range"VY_LIMIT": 18, # Y速度范围 Y velocity range"VYAW_LIMIT": 100 # 旋转速度范围 Rotation speed range
}def search(data, list):for i in range(len(list)):if data == list[i]:return i + 1return -1def conver2u8(data, limit, mode=0):"""将实际参数转化为0到255的单字节数据Convert the actual parameters to single byte data from 0 to 255"""max = 0xffif mode == 0:min = 0x00else:min = 0x01if not isinstance(limit, list):if data >= limit:return maxelif data <= -limit:return minelse:return int(128 + 128 * data / limit)else:limitmin = limit[0]limitmax = limit[1]if data >= limitmax:return maxelif data <= limitmin:return minelse:return int(255 / (limitmax - limitmin) * (data - limitmin))def conver2float(data, limit):if not isinstance(limit, list):return (data - 128.0) / 255.0 * limitelse:limitmin = limit[0]limitmax = limit[1]return data / 255.0 * (limitmax - limitmin) + limitmindef Byte2Float(rawdata):a = bytearray()a.append(rawdata[3])a.append(rawdata[2])a.append(rawdata[1])a.append(rawdata[0])return struct.unpack("!f", a)[0]class DOGZILLA():"""在实例化DOGZILLA时需要指定上位机与机器狗的串口通讯接口When instantiating DOGZILLA, you need to specify the serialcommunication interface between the upper computer and the machine dog"""def __init__(self, port="/dev/ttyAMA0"):self.ser = serial.Serial(port, 115200, timeout=0.5)self.rx_FLAG = 0self.rx_COUNT = 0self.rx_ADDR = 0self.rx_LEN = 0self.rx_data = bytearray(50)self.__delay = 0.05passdef __send(self, key, index=1, len=1):mode = 0x01order = ORDER[key][0] + index - 1value = []value_sum = 0for i in range(0, len):value.append(ORDER[key][index + i])value_sum = value_sum + ORDER[key][index + i]sum_data = ((len + 0x08) + mode + order + value_sum) % 256sum_data = 255 - sum_datatx = [0x55, 0x00, (len + 0x08), mode, order]tx.extend(value)tx.extend([sum_data, 0x00, 0xAA])self.ser.write(tx)def __read(self, addr, read_len=1):mode = 0x02sum_data = (0x09 + mode + addr + read_len) % 256sum_data = 255 - sum_datatx = [0x55, 0x00, 0x09, mode, addr, read_len, sum_data, 0x00, 0xAA]# time.sleep(0.1)self.ser.flushInput()self.ser.write(tx)def stop(self):self.move_x(0)self.move_y(0)self.mark_time(0)self.turn(0)def move(self, direction, step):if direction in ['x', 'X']:self.move_x(step)elif direction in ['y', 'Y']:self.move_y(step)else:print("ERROR!Invalid direction!")def move_x(self, step):if step > 20:step = 20elif step < -20:step = -20ORDER["VX"][1] = conver2u8(step, PARAM["VX_LIMIT"])self.__send("VX")def move_y(self, step):if step > 18:step = 18elif step < -18:step = -18ORDER["VY"][1] = conver2u8(step, PARAM["VY_LIMIT"])self.__send("VY")def turn(self, step):if step > 70:step = 70elif step < -70:step = -70elif 0 < step < 30:step = 30elif -30 < step < 0:step = -30ORDER["VYAW"][1] = conver2u8(step, PARAM["VYAW_LIMIT"])self.__send("VYAW")def forward(self, step):self.move_x(abs(step))def back(self, step):self.move_x(-abs(step))def left(self, step):self.move_y(abs(step))def right(self, step):self.move_y(-abs(step))def turnleft(self, step):self.turn(abs(step))def turnright(self, step):self.turn(-abs(step))def __translation(self, direction, data):index = search(direction, ['x', 'y', 'z'])if index == -1:print("ERROR!Direction must be 'x', 'y' or 'z'")returnORDER["TRANSLATION"][index] = conver2u8(data, PARAM["TRANSLATION_LIMIT"][index - 1])self.__send("TRANSLATION", index)def translation(self, direction, data):"""使机器狗足端不动,身体进行三轴平动Keep the robot's feet stationary and the body makes three-axis translation"""if (isinstance(direction, list)):if (len(direction) != len(data)):print("ERROR!The length of direction and data don't match!")returnfor i in range(len(data)):self.__translation(direction[i], data[i])else:self.__translation(direction, data)def __attitude(self, direction, data):index = search(direction, ['r', 'p', 'y'])if index == -1:print("ERROR!Direction must be 'r', 'p' or 'y'")returnORDER["ATTITUDE"][index] = conver2u8(data, PARAM["ATTITUDE_LIMIT"][index - 1])self.__send("ATTITUDE", index)def attitude(self, direction, data):"""使机器狗足端不动,身体进行三轴转动Keep the robot's feet stationary and the body makes three-axis rotation"""if (isinstance(direction, list)):if (len(direction) != len(data)):print("ERROR!The length of direction and data don't match!")returnfor i in range(len(data)):self.__attitude(direction[i], data[i])else:self.__attitude(direction, data)def action(self, action_id):"""使机器狗狗指定的预设动作Make the robot do the specified preset action"""if action_id <= 0 or action_id > 255:print("ERROR!Illegal Action ID!")returnORDER["ACTION"][1] = action_idself.__send("ACTION")def reset(self):"""机器狗停止运动,所有参数恢复到初始状态The robot dog stops moving and all parameters return to the initial state"""self.action(255)time.sleep(0.2)def leg(self, leg_id, data):"""控制机器狗的单腿的三轴移动Control the three-axis movement of a single leg of the robot"""value = [0, 0, 0]if leg_id not in [1, 2, 3, 4]:print("Error!Illegal Index!")returnif len(data) != 3:message = "Error!Illegal Value!"returnfor i in range(3):try:value[i] = conver2u8(data[i], PARAM["LEG_LIMIT"][i])except:print("Error!Illegal Value!")for i in range(3):index = 3 * (leg_id - 1) + i + 1ORDER["LEG_POS"][index] = value[i]self.__send("LEG_POS", index)def __motor(self, index, data):ORDER["MOTOR_ANGLE"][index] = conver2u8(data, PARAM["MOTOR_LIMIT"][index % 3 - 1])self.__send("MOTOR_ANGLE", index)def motor(self, motor_id, data):"""控制机器狗单个舵机转动Control the rotation of a single steering gear of the robot"""MOTOR_ID = [11, 12, 13, 21, 22, 23, 31, 32, 33, 41, 42, 43]if isinstance(motor_id, list):if len(motor_id) != len(data):print("Error!Length Mismatching!")returnindex = []for i in range(len(motor_id)):temp_index = search(motor_id[i], MOTOR_ID)if temp_index == -1:print("Error!Illegal Index!")returnindex.append(temp_index)for i in range(len(index)):self.__motor(index[i], data[i])else:index = search(motor_id, MOTOR_ID)self.__motor(index, data)def unload_motor(self, leg_id):if leg_id not in [1, 2, 3, 4]:print('ERROR!leg_id must be 1, 2, 3 or 4')returnORDER["UNLOAD_MOTOR"][1] = 0x10 + leg_idself.__send("UNLOAD_MOTOR")def unload_allmotor(self):ORDER["UNLOAD_MOTOR"][1] = 0x01self.__send("UNLOAD_MOTOR")def load_motor(self, leg_id):if leg_id not in [1, 2, 3, 4]:print('ERROR!leg_id must be 1, 2, 3 or 4')returnORDER["LOAD_MOTOR"][1] = 0x20 + leg_idself.__send("LOAD_MOTOR")def load_allmotor(self):ORDER["LOAD_MOTOR"][1] = 0x00self.__send("LOAD_MOTOR")def __periodic_rot(self, direction, period):index = search(direction, ['r', 'p', 'y'])if index == -1:print("ERROR!Direction must be 'r', 'p' or 'y'")returnif period == 0:ORDER["PERIODIC_ROT"][index] = 0else:ORDER["PERIODIC_ROT"][index] = conver2u8(period, PARAM["PERIOD_LIMIT"][0], mode=1)self.__send("PERIODIC_ROT", index)def periodic_rot(self, direction, period):"""使机器狗周期性转动Make the robot rotate periodically"""if (isinstance(direction, list)):if (len(direction) != len(period)):print("ERROR!The length of direction and data don't match!")returnfor i in range(len(period)):self.__periodic_rot(direction[i], period[i])else:self.__periodic_rot(direction, period)def __periodic_tran(self, direction, period):index = search(direction, ['x', 'y', 'z'])if index == -1:print("ERROR!Direction must be 'x', 'y' or 'z'")returnif period == 0:ORDER["PERIODIC_TRAN"][index] = 0else:ORDER["PERIODIC_TRAN"][index] = conver2u8(period, PARAM["PERIOD_LIMIT"][0], mode=1)self.__send("PERIODIC_TRAN", index)def periodic_tran(self, direction, period):"""使机器狗周期性平动Make the robot translate periodically"""if (isinstance(direction, list)):if (len(direction) != len(period)):print("ERROR!The length of direction and data don't match!")returnfor i in range(len(period)):self.__periodic_tran(direction[i], period[i])else:self.__periodic_tran(direction, period)def mark_time(self, data):"""使机器狗原地踏步Make the robot marks time"""if data == 0:ORDER["MarkTime"][1] = 0else:ORDER["MarkTime"][1] = conver2u8(data, PARAM["MARK_TIME_LIMIT"], mode=1)self.__send("MarkTime")def pace(self, mode):"""改变机器狗的踏步频率Change the step frequency of the robot"""if mode == "normal":value = 0x00elif mode == "slow":value = 0x01elif mode == "high":value = 0x02else:print("ERROR!Illegal Value!")returnORDER["MOVE_MODE"][1] = valueself.__send("MOVE_MODE")def gait_type(self, mode):"""改变机器狗的步态Change the gait of the robot"""if mode == "trot":value = 0x00elif mode == "walk":value = 0x01elif mode == "high_walk":value = 0x02ORDER["GAIT_TYPE"][1] = valueself.__send("GAIT_TYPE")def imu(self, mode):"""开启/关闭机器狗自稳状态Turn on / off the self stable state of the robot dog"""if mode != 0 and mode != 1:print("ERROR!Illegal Value!")returnORDER["IMU"][1] = modeself.__send("IMU")def perform(self, mode):"""开启/关闭机器狗循环做动作状态Turn on / off the action status of the robot dog cycle"""if mode != 0 and mode != 1:print("ERROR!Illegal Value!")returnORDER["PERFORM"][1] = modeself.__send("PERFORM")def motor_speed(self, speed):"""调节舵机转动速度,只在单独控制舵机的情况下有效Adjust the steering gear rotation speed,only effective when control the steering gear separately"""if speed < 0 or speed > 255:print("ERROR!Illegal Value!The speed parameter needs to be between 0 and 255!")returnif speed == 0:speed = 1ORDER["MOTOR_SPEED"][1] = speedself.__send("MOTOR_SPEED")def read_motor(self, out_int=False):"""读取12个舵机的角度 Read the angles of the 12 steering gear"""self.__read(ORDER["MOTOR_ANGLE"][0], 12)time.sleep(self.__delay)angle = []if self.__unpack():for i in range(12):index = round(conver2float(self.rx_data[i], PARAM["MOTOR_LIMIT"][i % 3]), 2)if out_int:if index > 0:angle.append(int(index+0.5))elif index < 0:angle.append(int(index-0.5))else:angle.append(int(index))else:angle.append(index)return angledef read_battery(self):self.__read(ORDER["BATTERY"][0], 1)time.sleep(self.__delay)battery = 0if self.__unpack():battery = int(self.rx_data[0])return batterydef read_version(self):self.__read(ORDER["FIRMWARE_VERSION"][0], 10)time.sleep(self.__delay)firmware_version = 'Null'if self.__unpack():# data = self.rx_data[0:10]data = self.rx_data[2:10]firmware_version = data.decode("utf-8").strip('\0')return firmware_versiondef read_roll(self, out_int=False):self.__read(ORDER["ROLL"][0], 4)time.sleep(self.__delay)roll = 0if self.__unpack():roll = Byte2Float(self.rx_data)if out_int:tmp = int(roll)return tmpreturn round(roll, 2)def read_pitch(self, out_int=False):self.__read(ORDER["PITCH"][0], 4)time.sleep(self.__delay)pitch = 0if self.__unpack():pitch = Byte2Float(self.rx_data)if out_int:tmp = int(pitch)return tmpreturn round(pitch, 2)def read_yaw(self, out_int=False):self.__read(ORDER["YAW"][0], 4)time.sleep(self.__delay)yaw = 0if self.__unpack():yaw = Byte2Float(self.rx_data)if out_int:tmp = int(yaw)return tmpreturn round(yaw, 2)def __unpack(self):n = self.ser.inWaiting()rx_CHECK = 0if n:data = self.ser.read(n)for num in data:if self.rx_FLAG == 0:if num == 0x55:self.rx_FLAG = 1else:self.rx_FLAG = 0elif self.rx_FLAG == 1:if num == 0x00:self.rx_FLAG = 2else:self.rx_FLAG = 0elif self.rx_FLAG == 2:self.rx_LEN = numself.rx_FLAG = 3elif self.rx_FLAG == 3:self.rx_TYPE = numself.rx_FLAG = 4elif self.rx_FLAG == 4:self.rx_ADDR = numself.rx_FLAG = 5elif self.rx_FLAG == 5:if self.rx_COUNT == (self.rx_LEN - 9):self.rx_data[self.rx_COUNT] = numself.rx_COUNT = 0self.rx_FLAG = 6elif self.rx_COUNT < self.rx_LEN - 9:self.rx_data[self.rx_COUNT] = numself.rx_COUNT = self.rx_COUNT + 1elif self.rx_FLAG == 6:for i in self.rx_data[0:(self.rx_LEN - 8)]:rx_CHECK = rx_CHECK + irx_CHECK = 255 - (self.rx_LEN + self.rx_TYPE + self.rx_ADDR + rx_CHECK) % 256if num == rx_CHECK:self.rx_FLAG = 7else:self.rx_FLAG = 0self.rx_COUNT = 0self.rx_ADDR = 0self.rx_LEN = 0elif self.rx_FLAG == 7:if num == 0x00:self.rx_FLAG = 8else:self.rx_FLAG = 0self.rx_COUNT = 0self.rx_ADDR = 0self.rx_LEN = 0elif self.rx_FLAG == 8:if num == 0xAA:self.rx_FLAG = 0self.rx_COUNT = 0return Trueelse:self.rx_FLAG = 0self.rx_COUNT = 0self.rx_ADDR = 0self.rx_LEN = 0return Falsedef calibration(self, state):"""用于软件标定,请谨慎使用!!! For software calibration, please use with caution!!!"""if state:ORDER["CALIBRATION"][1] = 1else:ORDER["CALIBRATION"][1] = 0self.__send("CALIBRATION")if __name__ == '__main__':g_dog = DOGZILLA()version = g_dog.read_version()print("version:", version)
其余的一些功能也都解释如下,先导入DOGZILLA:
from DOGZILLALib import DOGZILLA
mydog = DOGZILLA()
左转弯:mydog.turn(50)
右转弯:mydog.turn(-50)
这个是偏航角VYAW速度的设置
左平移:mydog.left(10)
右平移:mydog.right(10)
这个是Y轴的VY的速度设置
机器狗足端不动,沿着三轴分别平移:mydog.translation(['x','y','z'],[-30,-10,100])
这里的机器狗将分别,往后-30,往右-10,往上100这样的运动
机器狗足端不动,沿着三轴分别转动:mydog.attitude(['r','p','y'],[30,60,90])
这里的RPY分别是ROLL横滚角、PITCH俯仰角、YAW偏航角,对这部分感兴趣的可以查阅:
欧拉角(横滚角、俯仰角、偏航角)、旋转矩阵、四元数的转换与解决万向节死锁
预设的动作:mydog.action,比如撒尿动作:mydog.action(11)
停止运动,所有参数恢复到初始状态:mydog.reset()
单腿运动:mydog.leg(2,[-40,-30,40]),这里是第二条腿(前面右边),沿着X轴后退40,y轴往右30,Z轴往下40
单个舵机转动:mydog.motor(22,60),这里是第二条的髋关节舵机,控制大腿的编号
周期性转动:mydog.periodic_rot(['p','y'],[20,30]),RPY,这里是选P,Y角做转动,也就是俯仰角和偏航角
周期性平动:mydog.periodic_tran(['x','y'],[20,30]),这里是沿着X,Y轴的平移
原地踏步:mydog.mark_time(1)
踏步频率:mydog.pace('slow') # 'normal'、'slow'、'high'
机器狗步态:mydog.gait_type('walk') # 'trot'、'walk'、'high_walk'
开启/关闭机器狗自稳状态:mydog.imu(0)
开启/关闭机器狗循环做动作状态:mydog.perform(1)
舵机转速(只在单独控制舵机的情况下有效):mydog.motor_speed(10)
读取12个舵机的角度:mydog.read_motor()
#[15.2, 40.62, 0.61, 14.18, 41.25, 0.85, 14.69, 51.22, 0.61, 15.2, 50.6, 0.36]
这里会随着机器狗的运动,数据会实时变化。
分别读取机器狗的横滚角、俯仰角、偏航角,同样的舵机的变化,也将导致欧拉角的实时变化。
mydog.read_roll() # 0.37
mydog.read_pitch() # 0.17
mydog.read_yaw() # -21.3
电池电量:mydog.read_battery() # 81
固件版本:mydog.read_version() # '3.1.7-Y'
到这里就结束了,最后总结下,主要是通过机器狗来熟悉这个pyserial串口通讯库,整个代码看下来是比较简洁和高效,只需要连接到指定的串口接口,以及指定波特率与超时时间,就能够轻松建立起通讯连接,然后根据地址写入对应的数据就可以完成对机器狗的操作了。