效果预览第一步建立工作空间与目录结构# 1. 创建工作空间 (如果已有 workspace 可跳过) mkdir -p ~/g1_ws/src cd ~/g1_ws/src # 2. 创建包目录 (必须叫 g1_description) mkdir -p g1_description/urdf mkdir -p g1_description/meshes mkdir -p g1_description/launch将 g1_29dof.urdf (以及 .xml) 放入 ~/g1_ws/src/g1_description/urdf/将所有 .STL 文件放入 ~/g1_ws/src/g1_description/meshes/为 URDF 文件添加 Gazebo 标签添加颜色~/g1_ws/src/g1_description/urdf/g1_29dof.urdf粘贴到 g1_29dof.urdf 文件末尾在最后一行的 标签之前gazebo referencepelvis materialGazebo/DarkGrey/material /gazebo gazebo referenceleft_hip_pitch_link materialGazebo/DarkGrey/material /gazebo gazebo referenceleft_ankle_roll_link materialGazebo/DarkGrey/material /gazebo gazebo referenceright_hip_pitch_link materialGazebo/DarkGrey/material /gazebo gazebo referenceright_ankle_roll_link materialGazebo/DarkGrey/material /gazebo gazebo referencelogo_link materialGazebo/DarkGrey/material /gazebo gazebo referencehead_link materialGazebo/DarkGrey/material /gazebo gazebo referencepelvis_contour_link materialGazebo/Grey/material /gazebo gazebo referenceleft_hip_roll_link materialGazebo/Grey/material /gazebo gazebo referenceleft_hip_yaw_link materialGazebo/Grey/material /gazebo gazebo referenceleft_knee_link materialGazebo/Grey/material /gazebo gazebo referenceleft_ankle_pitch_link materialGazebo/Grey/material /gazebo gazebo referenceright_hip_roll_link materialGazebo/Grey/material /gazebo gazebo referenceright_hip_yaw_link materialGazebo/Grey/material /gazebo gazebo referenceright_knee_link materialGazebo/Grey/material /gazebo gazebo referenceright_ankle_pitch_link materialGazebo/Grey/material /gazebo gazebo referencewaist_yaw_link materialGazebo/Grey/material /gazebo gazebo referencewaist_roll_link materialGazebo/Grey/material /gazebo gazebo referencetorso_link materialGazebo/Grey/material /gazebo gazebo referencewaist_support_link materialGazebo/Grey/material /gazebo gazebo referenceleft_shoulder_pitch_link materialGazebo/Grey/material /gazebo gazebo referenceleft_shoulder_roll_link materialGazebo/Grey/material /gazebo gazebo referenceleft_shoulder_yaw_link materialGazebo/Grey/material /gazebo gazebo referenceleft_elbow_link materialGazebo/Grey/material /gazebo gazebo referenceleft_wrist_roll_link materialGazebo/Grey/material /gazebo gazebo referenceleft_wrist_pitch_link materialGazebo/Grey/material /gazebo gazebo referenceleft_wrist_yaw_link materialGazebo/Grey/material /gazebo gazebo referenceleft_rubber_hand materialGazebo/Grey/material /gazebo gazebo referenceright_shoulder_pitch_link materialGazebo/Grey/material /gazebo gazebo referenceright_shoulder_roll_link materialGazebo/Grey/material /gazebo gazebo referenceright_shoulder_yaw_link materialGazebo/Grey/material /gazebo gazebo referenceright_elbow_link materialGazebo/Grey/material /gazebo gazebo referenceright_wrist_roll_link materialGazebo/Grey/material /gazebo gazebo referenceright_wrist_pitch_link materialGazebo/Grey/material /gazebo gazebo referenceright_wrist_yaw_link materialGazebo/Grey/material /gazebo gazebo referenceright_rubber_hand materialGazebo/Grey/material /gazebo第二步补充缺失的系统文件~/g1_ws/src/g1_description/package.xml?xml version1.0? package format2 nameg1_description/name version0.0.1/version descriptionThe g1_description package/description maintainer emailusertodo.todoUser/maintainer licenseTODO/license buildtool_dependcatkin/buildtool_depend dependroscpp/depend dependrospy/depend dependstd_msgs/depend dependurdf/depend dependxacro/depend exec_dependgazebo_ros/exec_depend exec_dependjoint_state_publisher/exec_depend exec_dependrobot_state_publisher/exec_depend exec_dependrviz/exec_depend export gazebo_ros gazebo_model_path${prefix}/../ /export /package~/g1_ws/src/g1_description/CMakeLists.txtcmake_minimum_required(VERSION 3.0.2) project(g1_description) find_package(catkin REQUIRED COMPONENTS urdf xacro ) catkin_package() # 安装路径设置确保launch和meshes能被找到 install(DIRECTORY launch meshes urdf DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION} )第三步解决网格路径问题RDF 文件通常写死了网格路径。如果您的 URDF 是从其他地方导出的它可能写的是 file:///home/xxx/... 或者 package://g1_description/...为了确保不报错执行以下检查打开 urdf/g1_29dof.urdf。搜索 mesh filename...。修改路径确保所有路径都以 package://g1_description/meshes/ 开头。错误示例: filenameD:/downloads/meshes/pelvis.STL正确示例: filenamepackage://g1_description/meshes/pelvis.STL(可以使用 VS Code 的“全部替换”功能)第四步编写 RViz 启动文件~/g1_ws/src/g1_description/launch/display.launchlaunch param namerobot_description textfile$(find g1_description)/urdf/g1_29dof.urdf / node namejoint_state_publisher_gui pkgjoint_state_publisher_gui typejoint_state_publisher_gui / node namerobot_state_publisher pkgrobot_state_publisher typerobot_state_publisher / node namerviz pkgrviz typerviz args-d $(find g1_description)/launch/g1.rviz / /launch第一次运行时 RViz 可能会报错找不到配置这是正常的需要手动添加 RobotModel第五步编写 Gazebo 启动文件~/g1_ws/src/g1_description/launch/gazebo.launchlaunch include file$(find gazebo_ros)/launch/empty_world.launch arg namepaused valuetrue/ arg nameuse_sim_time valuetrue/ arg namegui valuetrue/ /include param namerobot_description textfile$(find g1_description)/urdf/g1_29dof.urdf / node namespawn_urdf pkggazebo_ros typespawn_model args-file $(find g1_description)/urdf/g1_29dof.urdf -urdf -z 0.79 -model g1_robot outputscreen / node namerobot_state_publisher pkgrobot_state_publisher typerobot_state_publisher / /launch第六步编译并运行cd ~/g1_ws catkin_make source devel/setup.bash运行 RViz 可视化roslaunch g1_description display.launch在 RViz 左侧点击 Add - RobotModel。将 Fixed Frame 修改为 URDF 中的根链接名称通常是 base_link 或 pelvis。现在应该能看到机器人并可以通过小窗口滑动条动它的关节。运行 Gazebo 仿真roslaunch g1_description gazebo.launch