cmvr-es/docs/ethercat_motor_tutorial.md
lgv 1e3466c1e6 feat(motor): add TCHL EtherCAT support and separate vendor operations
Add the TC600E TR motor, PDO mapping, configuration, vendor documents, and
real-motor examples for profile and cyclic position and velocity control.
Share EtherCAT motor conversion, cyclic batch commands, and feedback with
EYOU while supporting configurable CSP velocity feedforward objects and
rated-torque per-mille commands and feedback.

Keep common CiA402 state transitions, mode selection, motion commands, and
profile parameter SDO writes in Cia402Protocol. Move complete vendor limit,
zero calibration, brake, and TCHL enable timing workflows into independent
adapters, with Motor delegating each operation to its owner. Update TI5,
MuJoCo, the motor manager, and arm initialization for the revised interfaces.

Preserve the configured mode during torqueOn and use only the standard
6/7/F enable sequence. Keep PP set-point acknowledgement in position
commands and initialize EtherCAT arm modes before enabling axes.

Validation: built cmvr_es and all three affected real-motor test targets;
listed their tests without running hardware cases. Offline checks covered
CiA402 and vendor enable, calibration, failure cleanup, and PP handshakes.
Code whitespace checks passed; the vendor XML is preserved verbatim.
No real motors were operated during verification.
2026-10-19 15:53:25 +08:00

18 KiB
Raw Blame History

EtherCAT 电机接入教程

当前 EtherCAT CoE + CiA402 驱动包含意优和同川两种厂商适配:

厂商 配置枚举 ESI 资料 RxPDO / TxPDO
意优 MOTOR_VENDOR_EYOU docs/EYOU_ServoModule_ECAT_V145_no_slot.xml 0x1600 / 0x1A00
同川 TCHL MOTOR_VENDOR_TCHL docs/TC-TECH_TC600E_TR_1.5.xml 0x1601 / 0x1A01

ESI XML 作为厂商通信说明和对照资料保存,运行时不直接解析 XML。实际 PDO 映射写在:

cmvr-es/devices/motor/drivers/ethercat_motor/include/vendor/eyou/eyou_cia402_pdo_mapping.h
cmvr-es/devices/motor/drivers/ethercat_motor/include/vendor/tchl/tchl_cia402_pdo_mapping.h

1. 系统依赖

IgH EtherCAT userspace 已安装在:

dependency/x86/third_party/ethercat/v1.7.0

真机运行前,系统里还需要安装/加载 IgH master 内核模块。使用仓库脚本启动 EtherCAT master:

sudo script/ethercat/start_ethercat.sh eno1
script/ethercat/status_ethercat.sh

其中 eno1 是连接 EtherCAT 从站的网卡。脚本会读取该网卡 MAC,写入:

dependency/x86/third_party/ethercat/v1.7.0/etc/ethercat.conf

并通过 bundled IgH 的 ethercatctl -c 启动 master。确认目标从站的身份和位置后再启动程序。

停止 EtherCAT:

sudo script/ethercat/stop_ethercat.sh eno1

如果这张网卡要恢复给普通网络使用:

sudo script/ethercat/stop_ethercat.sh eno1 --restore-network

2. 电机配置

新增或修改:

cmvr-es/config/devices/motor/ethercat_motors.pb.txt

示例:

motor {
  id: "ethercat_motors"

  motor_groups {
    id: "right_arm_ethercat"
    bus_type: MOTOR_BUS_ETHERCAT
    vendor: MOTOR_VENDOR_EYOU
    protocol: MOTOR_PROTOCOL_ETHERCAT_CIA402

    ethercat {
      master_index: 0
      cycle_us: 1000
      slave_op_timeout_ms: 12000
      slave_state_poll_period_ms: 10

      cia402 {
        state_transition_timeout_ms: 1200
        velocity_stop_timeout_ms: 2000
        status_poll_period_ms: 10
        stopped_velocity_tolerance_rad_s: 0.001
      }

      zero_calibration {
        timeout_ms: 2000
        poll_period_ms: 10
        stable_sample_count: 5
        position_tolerance_counts: 10000
        stable_delta_counts: 1000
      }

      dc { enable: false }

      slaves { motor_id: 1 alias: 0 position: 0 }
      slaves { motor_id: 2 alias: 0 position: 1 }
      slaves { motor_id: 3 alias: 0 position: 2 }
      slaves { motor_id: 4 alias: 0 position: 3 }
      slaves { motor_id: 5 alias: 0 position: 4 }
      slaves { motor_id: 6 alias: 0 position: 5 }
      slaves { motor_id: 7 alias: 0 position: 6 }
    }

    joint_limits {
      enable: true
      source: JOINT_LIMIT_SOURCE_CUSTOM
      joints { joint_name: "R_SHOULDER_P" q_lb: -3.14 q_ub: 3.14 qd: 5.0 qdd: 10.0 }
      joints { joint_name: "R_SHOULDER_R" q_lb: -0.78 q_ub: 1.57 qd: 5.0 qdd: 10.0 }
      joints { joint_name: "R_SHOULDER_Y" q_lb: -3.14 q_ub: 3.14 qd: 5.0 qdd: 10.0 }
      joints { joint_name: "R_ELBOW_R" q_lb: 0 q_ub: 2.05 qd: 5.0 qdd: 10.0 }
      joints { joint_name: "R_WRIST_P" q_lb: -3.14 q_ub: 3.14 qd: 5.0 qdd: 10.0 }
      joints { joint_name: "R_WRIST_Y" q_lb: -0.78 q_ub: 0.78 qd: 5.0 qdd: 10.0 }
      joints { joint_name: "R_WRIST_R" q_lb: -0.57 q_ub: 1.57 qd: 5.0 qdd: 10.0 }
    }

    motors {
      motors { id: 1 joint_name: "R_SHOULDER_P" encoder_counts_per_rev: 65536 gear_ratio: 101.0 }
      motors { id: 2 joint_name: "R_SHOULDER_R" encoder_counts_per_rev: 65536 gear_ratio: 101.0 }
      motors { id: 3 joint_name: "R_SHOULDER_Y" encoder_counts_per_rev: 65536 gear_ratio: 101.0 }
      motors { id: 4 joint_name: "R_ELBOW_R" encoder_counts_per_rev: 65536 gear_ratio: 101.0 }
      motors { id: 5 joint_name: "R_WRIST_P" encoder_counts_per_rev: 65536 gear_ratio: 101.0 }
      motors { id: 6 joint_name: "R_WRIST_Y" encoder_counts_per_rev: 65536 gear_ratio: 101.0 }
      motors { id: 7 joint_name: "R_WRIST_R" encoder_counts_per_rev: 65536 gear_ratio: 101.0 }
    }
  }
}

字段说明:

  • master_index:IgH master 编号,第一张 EtherCAT master 是 0。
  • slave_op_timeout_ms、slave_state_poll_period_ms:进入 OP 的等待超时和状态查询周期,均需显式配置正值。
  • dc:必须显式配置是否启用;启用时还需提供参考从站、Sync0 周期和偏移、同步周期及 assign_activate 等字段。
  • alias:EtherCAT alias,一般为 0。
  • position:slave 在 EtherCAT 链路上的顺序,从 0 开始。
  • motor_id:系统内电机 id,必须和 motors.motors.id 对应。
  • encoder_counts_per_rev、gear_ratio:两者乘积表示关节输出轴每转对应的驱动器位置单位数。驱动器使用电机端原始计数时,分别填写电机编码器每转计数和机械减速比;其他单位设置见下方同川说明。

3. 注册设备

修改:

cmvr-es/config/manager/device_manager.pb.txt

加入:

devices {
  id: "ethercat_motors"
  type: MOTOR_SYSTEM
  enable: true
  config_file: "devices/motor/ethercat_motors.pb.txt"
}

4. 机械臂使用 EtherCAT 电机

修改机械臂配置中的 motor backend:

motor {
  motor_system_id: "ethercat_motors"
  motor_group_ids: "right_arm_ethercat"
  joint_names: "R_SHOULDER_P"
  joint_names: "R_SHOULDER_R"
  joint_names: "R_SHOULDER_Y"
  joint_names: "R_ELBOW_R"
  joint_names: "R_WRIST_P"
  joint_names: "R_WRIST_Y"
  joint_names: "R_WRIST_R"
}

5. 代码结构

EtherCAT 总线运行时:

cmvr-es/devices/motor/bus_runtime/ethercat/include/ethercat_motor_bus_runtime.h
cmvr-es/devices/motor/bus_runtime/ethercat/src/ethercat_motor_bus_runtime.cpp

它负责:

  • 打开 IgH master
  • 创建 domain
  • 使用 MotorManager 按 vendor + protocol 选定的 PDO mapping 配置从站
  • 按 mapping 配置对应的 RxPDO / TxPDO
  • 注册每个非 padding PDO entry 的 offset
  • 启动 cyclic loop
  • 按 motor_id + index + subindex 提供通用 PDO 读写接口

公共电机层和 CiA402 协议层:

cmvr-es/devices/motor/drivers/ethercat_motor/include/ethercat_motor.h
cmvr-es/devices/motor/drivers/ethercat_motor/src/ethercat_motor.cpp
cmvr-es/devices/motor/drivers/ethercat_motor/include/cia402/cia402_protocol.h
cmvr-es/devices/motor/drivers/ethercat_motor/include/cia402/cia402_objects.h
cmvr-es/devices/motor/drivers/ethercat_motor/include/vendor/motor_vendor_adapter.h
cmvr-es/devices/motor/drivers/ethercat_motor/src/cia402/cia402_protocol.cpp

继承与调用关系:

AbstractMotor
└── EthercatMotor:公共配置、单位换算、多轴批量命令和反馈
    ├── EyouMotor:组合 EyouMotorAdapter
    └── TchlMotor:组合 TchlMotorAdapter

厂家 Motor → 共享 Cia402Protocol → EthercatMotorBusRuntime
厂家 Motor → 厂家 Adapter       → EthercatMotorBusRuntime

Cia402Protocol 负责:

  • CiA402 6040/6041 状态机
  • 设置 6060 运行模式
  • PP、PV、PT、CSP、CSV、CST 的目标值和模式切换
  • 根据映射选择 CSP 速度前馈对象;PP/PV 轮廓参数通过 SDO 下发
  • 读 6064/606C/6077/603F
  • rad 和 encoder count 的换算

各厂家文件分别位于 include/vendor/eyou、src/vendor/eyou 和 include/vendor/tchl、src/vendor/tchl。按完整操作划分职责:通用控制由 Protocol 完成;厂家限位、抱闸、校准及同川使能/失能由 Adapter 完成。Adapter 内部可以访问标准对象,以保留厂家要求的步骤和顺序;Protocol 与 Adapter 都只依赖 Runtime,互不持有或调用。

Motor 负责加锁、参数换算和分发,同一个功能只调用其中一层。例如位置限位直接委托 Adapter,避免 Motor 先通过 Protocol 写一遍 607D。校准后的目标同步、软限位恢复和失败处理也由 Adapter 完成。Protocol 在后续标准控制入口根据实际模式和状态校验指令;周期控制只读取 PDO,不增加周期 SDO 访问。

setLimitQ、setLimitQd、calibrateZeroQ 和 brakeRelease 保留为 Motor 层业务接口,不再属于 MotorProtocolInterface 或 Cia402Protocol。意优和同川通过各自 Adapter 实现;TI5 保留具体驱动的转发实现。

torqueOn() 按驱动当前已配置的模式使能,不再强制切换到 PP,也不写 6060。驱动已有有效模式时,可先 torqueOn() 再 setMode(...);模式尚未配置时,需要先 setMode(...) 再使能。单轴测试显式采用后一种顺序,确保初始模式明确。使能前准备安全目标,随后按 0006→0007→000F 等待 OperationEnabled;模式确认由 setMode() 完成,PP 的新目标及应答握手由 commandProfilePosition() 完成。同川额外完成厂家要求的等待。机械臂初始化时,仅为未配置模式的 EtherCAT 轴选择初始 CSP,已有模式保留。

6. 同川 TCHL 配置与联调

使用单轴模板 cmvr-es/config/devices/motor/tchl_motors.pb.txt。模板尚未注册为活动设备;按当前实机 PrD.00=0、电子齿轮 1:1,依据手册印刷第 2 页参数表及第 20 页换算,encoder_counts_per_rev 配置为 131072,当前 gear_ratio 配置为 101。接入其他模块时应读取 PrD.06 / 3D06:00 UINT16 确认减速比,并核对机械轴实际位置、速度和加速度限位,再按第 3、4 节关联设备与使用方;设备 id、组 id 和关节名需使用模板中的对应值。

映射按提供的 TC600E TR ESI 编写,Vendor ID 为 0x00075500,Product Code 为 0x00000002;实机身份仍需核对。默认 PDO 如下,所有子索引均为 0:

方向 PDO / SM 条目顺序(位宽)
主站发送 0x1601 / SM2,17 字节 6040(16), 607A(32), 60FF(32), 6071(16), 60B1(32), 6060(8)
主站接收 0x1A01 / SM3,19 字节 6041(16), 6064(32), 606C(32), 6077(16), 60F4(32), 603F(16), 6061(8)

CSP 写目标位置 607A 和速度前馈 60B1;CSV 的目标速度写 60FF。6081/6083/6084 不放入此默认 PDO,PP/PV 参数使用 SDO。模板当前关闭 DC,通信周期为 1 ms;保留的 DC 配置为 Sync0 周期 1 ms、assign_activate: 768(0x0300)、偏移 0,开启前需核对实际同步要求。手册列出的 Sync0 周期为 1、2、3 ms。

目标力矩和反馈力矩均以额定力矩的千分比(‰)表示:1000 为额定力矩,100 为额定力矩的 10%。PT 的 torque_slope 按已确认的 0.1%/s(‰/s) 直接写入 6087;例如 1000 表示每秒变化一个额定力矩,不做额外的 1000 倍换算。

单位配置必须与驱动器当前设置一致,程序不自动修改全闭环、电子齿轮或绝对位置参数:

  • PrD.00=0 且电子齿轮为 1:1 时,使用电机端每转计数和机械减速比。
  • 手册第 2 页列出的 TCHL_11/14/17/20/25/32 电机端均为 17 bit,即每转 131072,输出端为 19 bit,即每转 524288。第 65 页说明 PrD.04 是输出端一圈计数除以 4,因此 3D04=131072 对应输出端 524288,不能将该寄存器当作电机端分辨率来源。
  • PrD.00=1 时位置采用输出端编码器尺度,应填写输出端每转的驱动器位置单位数,并将换算用 gear_ratio 设为 1,避免重复乘机械减速比。
  • 若设置了电子齿轮,应使用换算后的用户单位数;还需核对 PrD.07 对 606C 速度反馈来源的选择。当前协议按同一尺度处理位置和每秒速度,联调时通过小幅运动核对位置增量与速度反馈。

同川厂家操作的具体行为:

  • 位置限位完整流程在 Adapter 中:写入 607D:01/:02,再设置 3809:00 UINT16=1 开启软限位,并读回验证。速度限位也由 Adapter 写入、读回 607F 和 6081;意优另外处理厂家限速对象 2024。
  • 初始化读取 3136:00(Pr1.54)和 3137:00(Pr1.55),均为 UINT16 毫秒值,范围 0–500。同川 Adapter 完整执行使能/失能:使能后等待固定 PrF.44 的 200 ms 加 Pr1.54;失能后等待 Pr1.55,再允许后续使能。
  • 强制释放抱闸先确认 6041 不是 Operation Enabled,再写 381E:00 UINT16=128 并读回。该读回只确认命令值,不能作为物理抱闸状态反馈。
  • 零位校准沿用 motor->calibrateZeroQ(),具体流程在 TchlMotorAdapter::calibrateZero() 中实现:停止并失能后,将 6098:00 INT8 设为 35、607C:00 INT32 设为 0,切换 6060=6,按 0006→0007→000F→001F 将当前位置定义为零。Adapter 内部处理使能/失能延迟、完成检查、最终失能、目标 PDO 同步和软限位恢复;TchlMotor 只调用 Adapter。意优仍由自己的 Adapter 实现完整校准,Cia402Protocol 不提供厂家校准流程。
  • 同川手册还给出了写 3801:00 UINT16=5 后断电重启的置零方式;上述接口使用方法 35,其掉电保持尚未实机验证。

单轴 PP 实机测试

测试入口为 tchl_motor_real_test 的 TchlMotorRealTest.ProfilePosition。默认读取同川单轴模板,可用环境变量 TCHL_MOTOR_CONFIG 指定配置文件。

测试使用 MotorManager 完成总线、电机及限位初始化,随后设置 PP 模式、使能、下发一次目标、打印反馈。目标是绝对输出轴位置 1.57 rad,速度 0.5 rad/s,加减速度 5.0 rad/s²;修改测试文件顶部三个常量即可。每 100 ms 打印目标、实际位置、速度和额定力矩千分比,共打印 10 秒,不自动返回起点,也不进行到位或轮廓参数读回校验。

正常结束、接口失败或收到 Ctrl+C 后,依次尝试 quickStop、torqueOff,最后停止总线。Ctrl+C 在当前阻塞接口返回后处理。

构建和列举用例:

cmake --build cmake-build-debug --target tchl_motor_real_test --parallel 4
LD_LIBRARY_PATH="$PWD/dependency/x86/third_party/grpc/v1.76.0/lib" \
  cmake-build-debug/cmvr-es/devices/motor/drivers/ethercat_motor/tchl_motor_real_test --gtest_list_tests

以下命令会使能并移动电机,需使用实际换算参数和机械限位:

LD_LIBRARY_PATH="$PWD/dependency/x86/third_party/grpc/v1.76.0/lib" \
  cmake-build-debug/cmvr-es/devices/motor/drivers/ethercat_motor/tchl_motor_real_test \
  --gtest_filter=TchlMotorRealTest.ProfilePosition

单轴 CSP 正弦往返测试

入口为同一测试程序的 TchlMotorRealTest.CyclicPositionSin,使用同一份同川配置。参考意优 CSP 用例,采用升余弦位置和正弦速度前馈:

q_target  = q0 + Δq / 2 × (1 − cos(ωt))
qd_target = Δq / 2 × ω × sin(ωt)
ω = 2π / T

q0 取设置 CSP 并使能后的实际位置。当前 Δq = 1.57 rad、T = 6 s、运行两周期(12 秒),范围为 q0 到 q0 + 1.57 rad;可修改测试顶部的 kCspExcursionRad、kCspPeriodS、kCspCycles。开始下发前检查整个轨迹在配置的位置限位内。

下发间隔使用 ethercat.cycle_us(当前为 1 ms),位置经 PDO 写入 607A,速度前馈写入同川的 60B1。轨迹按实际经过时间计算,延迟后跳过错过的时刻;每 100 ms 打印目标位置、目标速度及实际位置、速度、力矩。正常结束下发起点位置和零前馈,再停止、失能;中途 Ctrl+C 直接停止、失能。

以下命令会使能并移动电机:

LD_LIBRARY_PATH="$PWD/dependency/x86/third_party/grpc/v1.76.0/lib" \
  cmake-build-debug/cmvr-es/devices/motor/drivers/ethercat_motor/tchl_motor_real_test \
  --gtest_filter=TchlMotorRealTest.CyclicPositionSin

单轴 PV / CSV 速度测试

同一测试文件增加两个速度用例,继续使用 TCHL_MOTOR_CONFIG 指定配置:

用例 目标及持续时间 下发方式
TchlMotorRealTest.ProfileVelocity 0.3 rad/s,加减速度 1 rad/s²,运行 2 秒;随后下发零速度并打印 1 秒 每个阶段调用一次 commandProfileVelocity;6083/6084 通过 SDO 配置,60FF 通过 PDO 下发
TchlMotorRealTest.CyclicVelocitySin qd = 0.2 × sin(2πt/6) rad/s,运行两周期(12 秒),最后下发零速度 按 ethercat.cycle_us(当前 1 ms)调用 commandCyclicVelocity,写入 PDO 60FF

可修改测试顶部的 kPvVelocityRadS、kPvAccelerationRadS2、kPvDuration 和 kCsvVelocityAmplitudeRadS、kCsvPeriodS、kCsvCycles。两个用例每 100 ms 打印目标速度、实际位置、实际速度和力矩;开始前检查预计行程及速度限位,打印反馈时检查实际位置是否越界。正常结束、接口失败或 Ctrl+C 都会执行停止和失能。

默认 PV 预计前进约 0.6 rad,开始前按 0.645 rad 预留运行及减速空间;CSV 的理想最大位移约为 0.382 rad,整周期积分回到起点,实际位置以反馈为准。

分别运行以下用例会使能并移动电机:

LD_LIBRARY_PATH="$PWD/dependency/x86/third_party/grpc/v1.76.0/lib" \
  cmake-build-debug/cmvr-es/devices/motor/drivers/ethercat_motor/tchl_motor_real_test \
  --gtest_filter=TchlMotorRealTest.ProfileVelocity

LD_LIBRARY_PATH="$PWD/dependency/x86/third_party/grpc/v1.76.0/lib" \
  cmake-build-debug/cmvr-es/devices/motor/drivers/ethercat_motor/tchl_motor_real_test \
  --gtest_filter=TchlMotorRealTest.CyclicVelocitySin

单轴当前位置置零测试

入口为 TchlMotorRealTest.CalibrateZeroQ。将机械轴放在希望定义为零点的位置后单独运行。测试调用一次 motor->calibrateZeroQ(),打印校准前后的实际位置、速度、力矩和模式。方法 35 内部会暂时使能,成功后保持失能、模式为 Homing;后续运动需先设置所需模式,再重新使能。

Adapter 检查启动命令已经到达驱动、状态字 bit12/bit10 已置位、bit13 无错误且实际位置接近 0。等待默认超时 2000 ms、轮询 10 ms、位置容差 100 个计数,可通过已有 ethercat.zero_calibration 配置覆盖。配置中的软限位会按新零点重新应用。

LD_LIBRARY_PATH="$PWD/output/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" \
  ./cmake-build-debug/cmvr-es/devices/motor/drivers/ethercat_motor/tchl_motor_real_test \
  --gtest_filter=TchlMotorRealTest.CalibrateZeroQ

7. 编译验证

cmake -S . -B cmake-build-debug
cmake --build cmake-build-debug --target motor_manager

真机验证顺序:

sudo script/ethercat/start_ethercat.sh eno1
script/ethercat/status_ethercat.sh
cmake --build cmake-build-debug --target cmvr_es

第一次联调先不要大幅度运动。先看 master 是否打开、slave 是否进入 OP、状态字是否更新,再给单个电机小角度目标。