import time import struct import can def arb_id_single(motor_id: int) -> int: # 单电机命令:0x140 + ID(1~32) return 0x140 + motor_id def send_cmd(bus, motor_id: int, data8: bytes): msg = can.Message( arbitration_id=arb_id_single(motor_id), data=data8, is_extended_id=False, ) bus.send(msg) def motor_run(bus, motor_id: int): # 电机运行命令:DATA[0]=0x88,其余 0 send_cmd(bus, motor_id, bytes([0x88, 0, 0, 0, 0, 0, 0, 0])) def motor_stop(bus, motor_id: int): # 电机停止命令:DATA[0]=0x81,其余 0 send_cmd(bus, motor_id, bytes([0x81, 0, 0, 0, 0, 0, 0, 0])) def torque_iq_cmd_a1(bus, motor_id: int, iq_control: int): """ 转矩闭环控制命令 0xA1: DATA[0] = 0xA1 DATA[4..5] = iqControl (int16, little-endian), 范围 -2048~2048 其余字节 0 【协议:0xA1,iqControl:int16,DATA[4]=低字节,DATA[5]=高字节】 """ if not (-2048 <= iq_control <= 2048): raise ValueError("iq_control must be in [-2048, 2048]") data = bytearray(8) data[0] = 0xA1 data[4:6] = struct.pack("