Soup's Blog

Back

ROS2 手眼相机 6D 位姿调试方案总结Blur image

方案概述#

本方案通过 xacro 参数 + launch 文件 的方式,实现了无需修改 URDF 文件即可实时调整相机 6D 位姿(位置 + 姿态)的便捷调试方法。

核心文件结构#

ur5e_gripper_moveit_config/
├── urdf/
│   ├── single_ur5e_gripper_handeye.urdf.xacro    # 宏定义(定义相机)
│   └── ur5e_gripper_handeye.urdf.xacro            # 实例文件(声明参数)
└── launch/
    └── view_handeye_robot_adjustable.launch.py    # 可调参数 launch 文件
plaintext

实现步骤#

步骤 1: 在宏定义中添加相机位姿参数#

文件: single_ur5e_gripper_handeye.urdf.xacro

步骤 2: 在实例文件中声明 xacro 参数#

文件: ur5e_gripper_handeye.urdf.xacro

步骤 3: 创建可调参数的 launch 文件#

文件: view_handeye_robot_adjustable.launch.py

使用方法#

基础使用(默认参数)#

ros2 launch ur5e_gripper_moveit_config view_handeye_robot_adjustable.launch.py
bash

调整位置#

# 调整 X 方向偏移 5cm
ros2 launch ur5e_gripper_moveit_config view_handeye_robot_adjustable.launch.py \
    camera_x_offset:=0.05

# 同时调整多个方向
ros2 launch ur5e_gripper_moveit_config view_handeye_robot_adjustable.launch.py \
    camera_x_offset:=0.05 \
    camera_y_offset:=-0.04 \
    camera_z_offset:=-0.03
bash

调整姿态#

# 调整俯仰角(向下倾斜 30°)
ros2 launch ur5e_gripper_moveit_config view_handeye_robot_adjustable.launch.py \
    camera_pitch:=-1.05

# 同时调整姿态
ros2 launch ur5e_gripper_moveit_config view_handeye_robot_adjustable.launch.py \
    camera_roll:=0.0 \
    camera_pitch:=-1.57 \
    camera_yaw:=1.57
bash

完整 6D 位姿调整#

ros2 launch ur5e_gripper_moveit_config view_handeye_robot_adjustable.launch.py \
    camera_x_offset:=0.05 \
    camera_y_offset:=-0.04 \
    camera_z_offset:=-0.03 \
    camera_roll:=0.0 \
    camera_pitch:=-1.5708 \
    camera_yaw:=1.5708
bash

参数说明#

位置参数(单位:米)#

参数说明正方向
camera_x_offsetX轴偏移(机械臂前侧)前方
camera_y_offsetY轴偏移(机械臂左侧)左侧
camera_z_offsetZ轴偏移(垂直向上)上方

姿态参数(单位:弧度)#

参数说明旋转轴
camera_roll翻滚角X轴
camera_pitch俯仰角Y轴
camera_yaw偏航角Z轴

常用角度对照表#

角度弧度值用途
0.0水平
30°0.52轻微倾斜
45°0.79中等倾斜
90°1.57垂直
-90°-1.57垂直(反向)
180°3.14反向

技术原理#

Xacro 参数传递流程#

命令行参数 (Launch)

launch 文件 (DeclareLaunchArgument)

xacro 命令 (camera_x_offset:=0.05)

URDF 实例文件 (<xacro:arg>)

宏定义 (<xacro:macro params>)

相机位姿 (<origin xyz="..." rpy="..."/>)
plaintext

优势对比#

方案传统方法本方案
调整速度慢(修改文件→编译→启动)快(命令行参数实时生效)
便利性需要编辑 XML 文件一行命令搞定
可逆性需要手动记录旧值默认值始终保留
适用场景固定位姿快速迭代调试

调试技巧#

1. 使用 TF 树验证位姿#

# 启动后查看 TF 树
ros2 run tf2_tools view_frames

# 检查相机相对于 tool0 的变换
ros2 run tf2_ros tf2_echo tool0 hand_eye_camera_color_optical_frame
bash

2. 在 RViz 中可视化#

  • 添加 TF 显示
  • 设置 Fixed Frame 为 tool0
  • 观察 hand_eye_camera_color_optical_frame 位置

3. 增量式调整#

# 先粗调(大步长)
camera_y_offset:=-0.05

# 再精调(小步长 0.01)
camera_y_offset:=-0.04
camera_y_offset:=-0.045
bash

注意事项#

  1. 参数单位: 位置用米,姿态用弧度
  2. 坐标系: 相对于 tool0(末端法兰)
  3. 重新编译: 修改 URDF 参数后需要 colcon build
  4. 默认值: 在 ur5e_gripper_handeye.urdf.xacro 中设置

扩展应用#

此方案同样适用于:

  • 其他传感器位姿调试(激光雷达、IMU 等)
  • 末端执行器位姿调整
  • 夹爪位置微调
  • 任何需要固定安装在机器人上的组件

总结#

通过 xacro 参数化 + launch 文件传递 的组合,我们实现了一个灵活、高效的相机位姿调试方案。开发者可以在不修改源代码的情况下,通过命令行参数实时调整相机的 6D 位姿,大大提高了调试效率。

ROS2 手眼相机 6D 位姿调试方案总结
http://www.soupcola.top/blog/ros2_blogs/ros2_blogs-11
Author Soup Cola
Published at 2026年2月9日