开发工具链
机器人开发涉及模型描述、可视化调试、行为编排、构建系统等多个环节。本文梳理 ROS2 生态及周边常用的开发工具。
本文重点是“格式与工具”。如果你想看机器人、物体、传感器、材质如何做成可用仿真资产,见 仿真资产;如果你想看这些资产如何组成世界、如何配置物理规则、随机化和评测,见 仿真世界构建与物理规则。
graph LR
subgraph 建模["建模阶段"]
URDF[URDF / Xacro]
MJCF[MJCF]
SDF[SDF]
end
subgraph 开发["开发阶段"]
BUILD[colcon + CMake<br/>构建系统]
BT[行为树<br/>BT.CPP / py_trees]
end
subgraph 调试["调试 & 可视化"]
RVIZ[RViz2]
FOX[Foxglove]
RERUN[rerun.io]
BAG[rosbag2 / MCAP]
end
URDF --> BUILD
BUILD --> BT
BT --> RVIZ
BAG --> FOX
BAG --> RERUN
style 建模 fill:#e8eaf6
style 开发 fill:#e8f5e9
style 调试 fill:#fff3e0
机器人描述格式
URDF (Unified Robot Description Format)
URDF 是 ROS 生态中最广泛使用的机器人描述格式,基于 XML。
核心元素:
| 元素 | 说明 | 关键属性 |
|---|---|---|
<link> |
刚体连杆 | visual, collision, inertial |
<joint> |
关节 | type (revolute/prismatic/fixed), limits, axis |
<transmission> |
传动 | 连接关节和执行器 |
<gazebo> |
仿真插件 | 物理参数、传感器插件 |
<robot name="my_arm">
<link name="base_link">
<visual>
<geometry><mesh filename="package://my_robot/meshes/base.stl"/></geometry>
<material><color rgba="0.8 0.8 0.8 1.0"/></material>
</visual>
<collision>
<geometry><mesh filename="package://my_robot/meshes/base_collision.stl"/></geometry>
</collision>
<inertial>
<mass value="2.5"/>
<inertia ixx="0.01" iyy="0.01" izz="0.01" ixy="0" ixz="0" iyz="0"/>
</inertial>
</link>
<joint name="joint1" type="revolute">
<parent link="base_link"/>
<child link="link1"/>
<origin xyz="0 0 0.1" rpy="0 0 0"/>
<axis xyz="0 0 1"/>
<limit lower="-3.14" upper="3.14" effort="10.0" velocity="1.0"/>
<dynamics damping="0.5" friction="0.1"/>
</joint>
</robot>
URDF 局限性:
- 不支持闭链 (closed-loop kinematic chain)
- 不支持可变拓扑(动态连接/断开)
- 惯性参数必须手动填写
- 不支持软体
Xacro 宏:使用 xacro 定义可参数化的宏,避免重复定义。通过 xacro my_robot.urdf.xacro > my_robot.urdf 生成最终 URDF。
MJCF (MuJoCo Format)
MJCF 是 MuJoCo 的原生描述格式,比 URDF 更强大。
| 对比维度 | URDF | MJCF |
|---|---|---|
| 闭链 | 不支持 | 支持(equality constraint) |
| 接触参数 | 有限 | 丰富(condim, solref, solimp) |
| 肌腱/弹簧 | 不支持 | 原生支持(tendon) |
| 软体 | 不支持 | composite body |
| 执行器 | 简单 | 多种类型(motor, position, velocity) |
| 传感器 | 插件 | 原生支持 |
<mujoco model="arm">
<worldbody>
<body name="link1" pos="0 0 0.1">
<joint name="joint1" type="hinge" axis="0 0 1" range="-180 180"/>
<geom type="capsule" size="0.02" fromto="0 0 0 0 0 0.2"/>
<body name="link2" pos="0 0 0.2">
<joint name="joint2" type="hinge" axis="0 1 0" range="-120 120"/>
<geom type="capsule" size="0.015" fromto="0 0 0 0 0 0.15"/>
</body>
</body>
</worldbody>
<actuator>
<motor joint="joint1" ctrlrange="-1 1" gear="50"/>
<position joint="joint2" kp="100" ctrlrange="-2.09 2.09"/>
</actuator>
<sensor>
<jointpos joint="joint1"/>
<jointvel joint="joint2"/>
<touch site="fingertip_site"/>
</sensor>
</mujoco>
SDF (Simulation Description Format)
SDF 是 Gazebo 使用的场景描述格式,比 URDF 更通用。
| 特性 | 说明 |
|---|---|
| 场景支持 | 可描述完整场景(多个模型、光源、物理属性) |
| 闭链 | 支持 |
| 传感器 | 原生支持 Camera, LiDAR, IMU 等 |
| 嵌套模型 | 支持模型组合 |
格式转换
# URDF → SDF (Gazebo 工具)
gz sdf -p my_robot.urdf > my_robot.sdf
# URDF → MJCF (MuJoCo 工具)
python -c "import mujoco; m = mujoco.MjModel.from_xml_path('my_robot.urdf')"
# OnShape/SolidWorks → URDF
# 使用 onshape-to-robot 或 sw_urdf_exporter 插件
这些格式的“语法层面”在本文已经够用,但它们如何落到真实资产生产流中,需要结合 仿真资产 一起看;它们如何进入完整世界并影响接触、时间系统和任务逻辑,则应继续看 仿真世界构建与物理规则。
可视化工具
RViz2
RViz2 是 ROS2 的标准 3D 可视化工具。
常用 Display 类型:
| Display | 话题类型 | 用途 |
|---|---|---|
| RobotModel | robot_description |
显示机器人模型 |
| TF | tf2_msgs/TFMessage |
坐标变换可视化 |
| PointCloud2 | sensor_msgs/PointCloud2 |
点云显示 |
| Image | sensor_msgs/Image |
相机图像 |
| LaserScan | sensor_msgs/LaserScan |
激光扫描 |
| MarkerArray | visualization_msgs/MarkerArray |
自定义标记 |
| Path | nav_msgs/Path |
规划路径 |
| Map | nav_msgs/OccupancyGrid |
栅格地图 |
| Wrench | geometry_msgs/WrenchStamped |
力/力矩 |
# 启动 RViz2
rviz2
# 加载配置文件
rviz2 -d my_config.rviz
Foxglove Studio
Foxglove 是新一代的机器人数据可视化平台,支持 Web 和桌面端。
| 对比 | RViz2 | Foxglove |
|---|---|---|
| 平台 | Linux (X11/Wayland) | Web/Desktop (跨平台) |
| 数据源 | 实时 ROS2 话题 | ROS2 实时 + rosbag 回放 + MCAP |
| 3D 渲染 | 中等 | 较好 (WebGL) |
| 自定义面板 | 有限 | 丰富(扩展系统) |
| 远程访问 | 需要 VNC/SSH | 原生 WebSocket |
| 协作 | 无 | 云端共享 |
# 安装 Foxglove Bridge (ROS2 → Foxglove)
sudo apt install ros-humble-foxglove-bridge
ros2 launch foxglove_bridge foxglove_bridge_launch.xml
# 浏览器访问 https://app.foxglove.dev
rerun.io
rerun 是面向多模态数据的可视化框架,尤其适合 AI/ML 流水线调试。支持图像、点云、3D 网格、时间序列等多模态数据,Python 优先 (pip install rerun-sdk),内置时间轴回放功能。
import rerun as rr
rr.init("robot_debug", spawn=True)
rr.log("camera/rgb", rr.Image(image_array))
rr.log("lidar/points", rr.Points3D(point_cloud, colors=colors))
行为树 (Behavior Trees)
行为树是机器人任务编排的主流方法,Nav2 等框架基于行为树管理导航流程。
核心概念
| 节点类型 | 符号 | 行为 |
|---|---|---|
| Sequence | → | 依次执行子节点,遇到 FAILURE 即停 |
| Fallback | ? | 依次尝试子节点,遇到 SUCCESS 即停 |
| Action | ▢ | 执行具体动作 |
| Condition | ◯ | 检查条件 |
| Decorator | ◇ | 修饰子节点(重试、反转、超时等) |
BT.CPP / py_trees
BT.CPP 是 C++ 行为树库,Nav2 的底层依赖。通过 XML 文件定义行为树结构,C++ 实现具体 Action/Condition 节点。
<!-- BT.CPP 行为树 XML -->
<root BTCPP_format="4">
<BehaviorTree ID="PickAndPlace">
<Sequence>
<DetectObject object_name="cup" pose="{target_pose}"/>
<PlanMotion goal="{target_pose}" trajectory="{traj}"/>
<ExecuteTrajectory trajectory="{traj}"/>
<CloseGripper/>
</Sequence>
</BehaviorTree>
</root>
py_trees 是 Python 行为树库,适合快速原型开发,纯 Python API 构建行为树。
构建系统
colcon
colcon 是 ROS2 的标准构建工具。
# 基本构建
colcon build
# 常用选项
colcon build --symlink-install # Python 包符号链接(免重复编译)
colcon build --packages-select my_pkg # 只编译指定包
colcon build --packages-up-to my_pkg # 编译指定包及其依赖
colcon build --cmake-args -DCMAKE_BUILD_TYPE=Release # Release 模式
colcon build --parallel-workers 4 # 并行编译数
# 测试
colcon test
colcon test-result --verbose
CMake / ament 构建
ROS2 C++ 包使用 ament_cmake,Python 包使用 ament_python。关键文件:
- C++ 包:
CMakeLists.txt+package.xml,使用ament_target_dependencies()声明依赖 - Python 包:
setup.py+package.xml,在entry_points中注册节点
调试工具
rosbag2 录制与回放
# 录制所有话题
ros2 bag record -a -o experiment_001
# 录制指定话题
ros2 bag record /camera/image_raw /joint_states /tf -o experiment_001
# 录制到 MCAP 格式(推荐,Foxglove 兼容)
ros2 bag record -a -s mcap -o experiment_001
# 回放
ros2 bag play experiment_001
ros2 bag play experiment_001 --rate 0.5 # 半速回放
ros2 bag play experiment_001 --loop # 循环回放
# 查看 bag 信息
ros2 bag info experiment_001
TF2 调试
TF (Transform) 是 ROS 中坐标变换的核心,TF 问题是最常见的调试场景之一。
# 查看 TF 树
ros2 run tf2_tools view_frames
# 生成 frames.pdf
# 查看两个坐标系间的变换
ros2 run tf2_ros tf2_echo base_link camera_link
# 监控 TF 发布频率
ros2 run tf2_ros tf2_monitor
# 常见问题排查
# 1. "Could not transform..." → 检查 TF 树是否连通
# 2. TF 延迟 → 检查时间戳同步
# 3. TF 跳变 → 检查传感器标定
其他调试命令
ros2 topic hz /camera/image_raw # 发布频率
ros2 topic bw /pointcloud # 带宽
rqt_graph # 节点图可视化
相关链接
- RViz2 文档
- Foxglove 文档
- rerun.io 文档
- BT.CPP 文档
- 相关笔记:仿真平台 | 仿真资产 | 仿真世界构建与物理规则 | ROS2 架构 | NVIDIA 生态