如何将ROS1 noetic中Unitree G1 URDF导入GazeboRViz进行仿真与可视化?

摘要:博客地址:https:www.cnblogs.comzylyehuo Unitree G1 模型文件下载地址(挑选自己需要的部分,本教程基于 g1_29dof.urdf (以及 .xml 和 meshes 文件夹)) 有核心的 UR
博客地址:https://www.cnblogs.com/zylyehuo/ Unitree G1 模型文件下载地址(挑选自己需要的部分,本教程基于 g1_29dof.urdf (以及 .xml 和 meshes 文件夹)) 有核心的 URDF 文件和 Meshes (STL 网格文件) 效果预览 第一步:建立工作空间与目录结构 # 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 reference="pelvis"> <material>Gazebo/DarkGrey</material> </gazebo> <gazebo reference="left_hip_pitch_link"> <material>Gazebo/DarkGrey</material> </gazebo> <gazebo reference="left_ankle_roll_link"> <material>Gazebo/DarkGrey</material> </gazebo> <gazebo reference="right_hip_pitch_link"> <material>Gazebo/DarkGrey</material> </gazebo> <gazebo reference="right_ankle_roll_link"> <material>Gazebo/DarkGrey</material> </gazebo> <gazebo reference="logo_link"> <material>Gazebo/DarkGrey</material> </gazebo> <gazebo reference="head_link"> <material>Gazebo/DarkGrey</material> </gazebo> <gazebo reference="pelvis_contour_link"> <material>Gazebo/Grey</material> </gazebo> <gazebo reference="left_hip_roll_link"> <material>Gazebo/Grey</material> </gazebo> <gazebo reference="left_hip_yaw_link"> <material>Gazebo/Grey</material> </gazebo> <gazebo reference="left_knee_link"> <material>Gazebo/Grey</material> </gazebo> <gazebo reference="left_ankle_pitch_link"> <material>Gazebo/Grey</material> </gazebo> <gazebo reference="right_hip_roll_link"> <material>Gazebo/Grey</material> </gazebo> <gazebo reference="right_hip_yaw_link"> <material>Gazebo/Grey</material> </gazebo> <gazebo reference="right_knee_link"> <material>Gazebo/Grey</material> </gazebo> <gazebo reference="right_ankle_pitch_link"> <material>Gazebo/Grey</material> </gazebo> <gazebo reference="waist_yaw_link"> <material>Gazebo/Grey</material> </gazebo> <gazebo reference="waist_roll_link"> <material>Gazebo/Grey</material> </gazebo> <gazebo reference="torso_link"> <material>Gazebo/Grey</material> </gazebo> <gazebo reference="waist_support_link"> <material>Gazebo/Grey</material> </gazebo> <gazebo reference="left_shoulder_pitch_link"> <material>Gazebo/Grey</material> </gazebo> <gazebo reference="left_shoulder_roll_link"> <material>Gazebo/Grey</material> </gazebo> <gazebo reference="left_shoulder_yaw_link"> <material>Gazebo/Grey</material> </gazebo> <gazebo reference="left_elbow_link"> <material>Gazebo/Grey</material> </gazebo> <gazebo reference="left_wrist_roll_link"> <material>Gazebo/Grey</material> </gazebo> <gazebo reference="left_wrist_pitch_link"> <material>Gazebo/Grey</material> </gazebo> <gazebo reference="left_wrist_yaw_link"> <material>Gazebo/Grey</material> </gazebo> <gazebo reference="left_rubber_hand"> <material>Gazebo/Grey</material> </gazebo> <gazebo reference="right_shoulder_pitch_link"> <material>Gazebo/Grey</material> </gazebo> <gazebo reference="right_shoulder_roll_link"> <material>Gazebo/Grey</material> </gazebo> <gazebo reference="right_shoulder_yaw_link"> <material>Gazebo/Grey</material> </gazebo> <gazebo reference="right_elbow_link"> <material>Gazebo/Grey</material> </gazebo> <gazebo reference="right_wrist_roll_link"> <material>Gazebo/Grey</material> </gazebo> <gazebo reference="right_wrist_pitch_link"> <material>Gazebo/Grey</material> </gazebo> <gazebo reference="right_wrist_yaw_link"> <material>Gazebo/Grey</material> </gazebo> <gazebo reference="right_rubber_hand"> <material>Gazebo/Grey</material> </gazebo> 第二步:补充缺失的系统文件 ~/g1_ws/src/g1_description/package.xml <?xml version="1.0"?> <package format="2"> <name>g1_description</name> <version>0.0.1</version> <description>The g1_description package</description> <maintainer email="user@todo.todo">User</maintainer> <license>TODO</license> <buildtool_depend>catkin</buildtool_depend> <depend>roscpp</depend> <depend>rospy</depend> <depend>std_msgs</depend> <depend>urdf</depend> <depend>xacro</depend> <exec_depend>gazebo_ros</exec_depend> <exec_depend>joint_state_publisher</exec_depend> <exec_depend>robot_state_publisher</exec_depend> <exec_depend>rviz</exec_depend> <export> <gazebo_ros gazebo_model_path="${prefix}/.."/> </export> </package> ~/g1_ws/src/g1_description/CMakeLists.txt cmake_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/ 开头。 错误示例: filename="D:/downloads/meshes/pelvis.STL" 正确示例: filename="package://g1_description/meshes/pelvis.STL" (可以使用 VS Code 的“全部替换”功能) 第四步:编写 RViz 启动文件 ~/g1_ws/src/g1_description/launch/display.launch <launch> <param name="robot_description" textfile="$(find g1_description)/urdf/g1_29dof.urdf" /> <node name="joint_state_publisher_gui" pkg="joint_state_publisher_gui" type="joint_state_publisher_gui" /> <node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher" /> <node name="rviz" pkg="rviz" type="rviz" args="-d $(find g1_description)/launch/g1.rviz" /> </launch> 第一次运行时 RViz 可能会报错找不到配置,这是正常的,需要手动添加 RobotModel 第五步:编写 Gazebo 启动文件 ~/g1_ws/src/g1_description/launch/gazebo.launch <launch> <include file="$(find gazebo_ros)/launch/empty_world.launch"> <arg name="paused" value="true"/> <arg name="use_sim_time" value="true"/> <arg name="gui" value="true"/> </include> <param name="robot_description" textfile="$(find g1_description)/urdf/g1_29dof.urdf" /> <node name="spawn_urdf" pkg="gazebo_ros" type="spawn_model" args="-file $(find g1_description)/urdf/g1_29dof.urdf -urdf -z 0.79 -model g1_robot" output="screen" /> <node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_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 Gazebo 打开后,点击底部的 "Play" 按钮。 预期结果:机器人会出现在世界中。因为没有写控制算法,受重力影响,它可能会直接跪下或倒下。