Damiao 电机与 CAN 总线

本指南介绍如何通过 CAN 总线通信在 LeRobot 中设置和使用 Damiao 电机。

目前仅支持 Linux,因为 OpenArms 的 CAN 适配器只有 Linux 驱动。

Linux CAN 设置

在使用 Damiao 电机之前,你需要在 Linux 系统上设置好 CAN 接口。

安装 CAN 工具

sudo apt-get install can-utils

配置 CAN 接口(手动)

对于标准 CAN FD(OpenArms 推荐):

sudo ip link set can0 down
sudo ip link set can0 type can bitrate 1000000 dbitrate 5000000 fd on
sudo ip link set can0 up

对于标准 CAN(不带 FD):

sudo ip link set can0 down
sudo ip link set can0 type can bitrate 1000000
sudo ip link set can0 up

配置 CAN 接口(使用 LeRobot)

LeRobot 提供了一个实用脚本,用于设置和测试 CAN 接口:

# Setup multiple interfaces (e.g., OpenArms Followers with 2 CAN buses)
lerobot-setup-can --mode=setup --interfaces=can0,can1

调试 CAN 通信

使用内置的调试工具测试电机通信:

# Test motors on all interfaces
lerobot-setup-can --mode=test --interfaces=can0,can1

# Run speed/latency test
lerobot-setup-can --mode=speed --interfaces=can0

测试模式会扫描电机(ID 0x01-0x08),并报告哪些有响应。示例输出:

can0: UP (CAN FD)
  Motor 0x01 (joint_1): ✓ FOUND
    → Response 0x11 [FD]: 00112233...
  Motor 0x02 (joint_2): ✓ FOUND
  Motor 0x03 (joint_3): ✗ No response
  ...
  Summary: 2/8 motors found

用法

基本设置

from lerobot.motors import Motor
from lerobot.motors.damiao import DamiaoMotorsBus

# Define your motors with send/receive CAN IDs
motors = {
    "joint_1": Motor(id=0x01, motor_type_str="dm8009", recv_id=0x11),
    "joint_2": Motor(id=0x02, motor_type_str="dm4340", recv_id=0x12),
    "joint_3": Motor(id=0x03, motor_type_str="dm4310", recv_id=0x13),
}

# Create the bus
bus = DamiaoMotorsBus(
    port="can0",  # Linux socketcan interface
    motors=motors,
)

# Connect
bus.connect()

读取电机 state

# Read single motor position (degrees)
position = bus.read("Present_Position", "joint_1")

# Read from multiple motors
positions = bus.sync_read("Present_Position")  # All motors
positions = bus.sync_read("Present_Position", ["joint_1", "joint_2"])

# Read all states at once (position, velocity, torque)
states = bus.sync_read_all_states()
# Returns: {'joint_1': {'position': 45.2, 'velocity': 1.3, 'torque': 0.5}, ...}

写入电机命令

# Enable torque
bus.enable_torque()

# Set goal position (degrees)
bus.write("Goal_Position", "joint_1", 45.0)

# Set positions for multiple motors
bus.sync_write("Goal_Position", {
    "joint_1": 45.0,
    "joint_2": -30.0,
    "joint_3": 90.0,
})

# Disable torque
bus.disable_torque()

配置选项

参数默认值描述
port-CAN 接口(can0)或串口(/dev/cu.usbmodem*
use_can_fdTrue启用 CAN FD 以获得更高数据速率
bitrate1000000标称比特率(1 Mbps)
data_bitrate5000000CAN FD 数据比特率(5 Mbps)

电机配置

每个电机都需要:

  • id:用于发送命令的 CAN ID
  • motor_type:受支持的电机类型之一(例如 "dm8009""dm4340"
  • recv_id:用于接收响应的 CAN ID

OpenArms 的默认 ID 遵循这样的模式:发送 ID 0x0N,接收 ID 0x1N,其中 N 是关节编号。

故障排查

电机无响应

  1. 检查电源
  2. 检查 CAN 接线:检查 CAN-H、CAN-L 和 GND 连接
  3. 检查电机 ID:使用 Damiao 调试工具验证/配置 ID
  4. 测试 CAN 接口:运行 candump can0 查看是否收到消息
  5. 运行诊断lerobot-setup-can --mode=test --interfaces=can0

电机超时参数

如果电机被配置为 timeout=0,它们将不会响应命令。请使用 Damiao 调试工具设置一个非零的超时值。

验证 CAN FD state

ip -d link show can0 | grep fd
在 GitHub 上更新