ROS2 Interface 开发No.9 附录

ROS2 接口开发

更新于:2026年8月17日

1.1 常见问题

Q1:为什么消息发送后没有效果?

  • 检查 QoS 配置是否匹配(Reliable vs Best Effort)
  • 确认 topic 名称是否正确
  • 检查机器人是否处于正确的运动状态(如 joint_bridge 模式下关节命令才生效)

Q2:如何确定关节索引?

  • 参考机器人配置文档
  • 使用 ros2 topic echo /hardware/joint_state 查看关节数量
  • 通常从 0 开始顺序编号

Q3:控制不稳定怎么办?

  • 降低刚度(stiffness)参数
  • 增加阻尼(damping)参数
  • 提高控制频率
  • 检查目标位置是否在合理范围内

Q4:request_id 错误如何处理?

  • 订阅状态 topic 获取当前 request_id
  • 确保发送的 request_id = 当前 ID + 1
  • 如果状态不同步,等待下一个状态消息

1.2 单位换算参考

换算公式
角度转弧度rad = deg × π / 180
弧度转角度deg = rad × 180 / π
度/秒转弧度/秒rad/s = deg/s × π / 180

1.3 开发建议

  1. 先使用示例程序测试接口是否正常
  2. 逐步修改参数观察效果
  3. 使用 ros2 topic echo 监控消息
  4. 使用 ros2 topic hz 检查发布频率
  5. 实现安全保护机制(速度限制、超时检测等)

1.4 消息定义文件原始内容

以下为 interface_protocol 包中所有消息定义文件的原始内容,来自 GitHub 仓库

4.1 JointCommand.msg

PLAIN
std_msgs/Header header
float64[] position
float64[] velocity
float64[] feed_forward_torque
float64[] torque
float64[] stiffness
float64[] damping
uint8 parallel_parser_type

4.2 JointState.msg

PLAIN
std_msgs/Header header
float64[] position
float64[] velocity
float64[] torque

4.3 MotionState.msg

PLAIN
string current_motion_task
string[] available_transition_motions

4.4 MotionStateRequest.msg

PLAIN
string target_motion_name

4.5 BodyVelCmd.msg

PLAIN
std_msgs/Header header
float64[] linear_velocity
float64 yaw_velocity

4.6 LedControl.msg

PLAIN
uint8 BLINK_RED = 0x1
uint8 BLINK_GREEN = 0x2
uint8 BLINK_BLUE = 0x3
uint8 BLINK_WHITE = 0x4
uint8 CONSTANT_ON_WHITE = 0x5
uint8 CONSTANT_ON_GREEN = 0x6
uint8 BREATHE_WHITE = 0x7
uint8 WATER_WHITE = 0x8
uint8 BREATHE_RED = 0x9
uint8 BLINK_ORANGE = 0xa
uint8 CONSTANT_ON_ORANGE = 0xb

uint8 color

4.7 JointMotionPlanRequest.msg

PLAIN
## Motion plan request message (via topic)
## request_type constants
uint8 REQUEST_PLAN_EXECUTE=0    # Plan and execute
uint8 REQUEST_CANCEL=1          # Cancel specified request_id
uint8 REQUEST_RESET=2           # Reset to default pose

int32 request_id
uint8  request_type

## Motion target (consistent with JointMotion.action)
bool      use_gravity_compensation
int32[]   joint_indices           # Empty array means reset to default
float64[] target_positions        # Target joint positions (rad)
float64[] target_velocities       # Target joint velocities (empty for auto-planning)
float64   execution_time          # Expected execution time (s)
float64[] stiffness               # Optional, empty array for default
float64[] damping                 # Optional, empty array for default

4.8 JointMotionPlanState.msg

PLAIN
uint8 STATUS_DISABLED = 0
uint8 IDLE = 1
uint8 EXECUTING = 2
uint8 EXITING = 3

int32 request_id
uint8  status
float64 progress          # 0.0~1.0

4.9 JointOverrideCommand.msg

PLAIN
std_msgs/Header header
float64 weight
int32[] joint_indices
float64[] position
float64[] velocity
float64[] feed_forward_torque
float64[] torque
float64[] stiffness
float64[] damping

📎 本章涉及的开源仓库源码文件(点击文件名跳转 GitHub):BodyVelCmd.msg · JointCommand.msg · JointMotionPlanRequest.msg · JointMotionPlanState.msg · JointOverrideCommand.msg · JointState.msg · LedControl.msg · MotionState.msg · MotionStateRequest.msg