gazebo无法加载带有STL资源的urdf
-
环境:
虚拟机Ubuntu22.04,ros2 humble
问题描述:
参照《动手学ROS2》的“第三章建模仿真篇”搭建的小车模型可以顺利地在gazebo仿真。自己从solidworks上导出来的URDF(包含STL格式的meshes资源),rviz可以正常加载模型,但gazebo加载失败,软件直接卡死。
具体细节和上下文:
URDF含STL的对应脚本为:
<mesh filename="package://XBot/meshes/base_link.STL"/>
如果把以上屏蔽,将mesh改成其他geometry简单几何体,gazebo能正常加载。尝试过的解决方法:
- 参照https://robotics.stackexchange.com/questions/25224/gazebo-not-running-when-i-try-to-load-stl-files
将package改成file,也无效。
<mesh filename="file:///$(find XBot)/XBot/meshes/base_link.STL"/> - 参照https://ros-users.narkive.com/zdLA8RCV/error-loading-stl-mesh-into-gazebo-ogre
设置GAZEBO_RESOURCE_PATH环境变量,也不生效
- 参照https://robotics.stackexchange.com/questions/25224/gazebo-not-running-when-i-try-to-load-stl-files
-
@250893029 package://XBot/meshes/base_link.STL 是找在install/share下的文件,你可以去路径里看一下,colcon build & source install setup.bash 后在install/XBot/share/XBot/meshes里有没有对应文件。
没有的话,需要去 XBot/setup.py 里把meshes 什么的加进去
data_files=[..., (os.path.join('share', package_name, 'meshes'), glob(os.path.join('meshes, '*.*'))), ... ]
-
@372365381 谢谢,我在setup.py里面加了meshes资源,加了后,rviz才能成功显示模型,但gazebo还是无效
-
后来自己摸索着解决了。
URDF里面依然采用 <mesh filename="package://.... />
但在launch文件里要配置GAZEBO_MODEL_PATH环境变量
pkg_share = os.pathsep + os.path.join(get_package_prefix(package_name), 'share')if 'GAZEBO_MODEL_PATH' in os.environ:
os.environ['GAZEBO_MODEL_PATH'] += pkg_share
else:
os.environ['GAZEBO_MODEL_PATH'] = pkg_share -
@250893029 你好,我也遇到了相同的问题,复制您的代码时显示函数get_package_prefix报错,请问这个函数是那里引入的
-
@250893029 可以把完整的launch文件发出来吗?
-
@250893029 你好,可以把launch文件的具体内容分享一下吗,加了语句之后还是各种报错
-
@3164066018
要在package中加入这个,在YT上看一个俄罗斯大哥提到的 -
@GHFG-98 你好 我也是遇到了这样的问题,ros2 humble加载gazebo11,但无法识别urdf或者xacro文件中mesh的模型资源
我在package.xml中加入了
但是依然无法加载
我在launch.py中有如下配置,但也无效。
git@github.com:QILIANGDELEISHUI/ROS2-sw_urdf_GAZEBO.git这是源文件的仓库地址。如果您能回复,万分感激。 -
@zzpcp3 @3164066018 @捶你胸口 @凄凉的泪水
我在gazebo.launch.py
中加入的代码是这样的from ament_index_python.packages import get_package_prefix package_name = 'cubot_v2_sw2urdf' pkg_share = os.pathsep + os.path.join(get_package_prefix(package_name), 'share') if 'GAZEBO_MODEL_PATH' in os.environ: # 如果你修改了~/.bashrc, 就会执行这个 os.environ['GAZEBO_MODEL_PATH'] += pkg_share else: # 注意此处gazebo-11修改为你的gazebo版本 os.environ['GAZEBO_MODEL_PATH'] = "/usr/share/gazebo-11/models" + pkg_share
注意把
gazebo-11
替换为你的gazebo版本,可以先去找找对应的路径是否存在全部的sw转urdf流程可以在我的blog里面看看
完整launch文件如下
import launch import launch_ros from ament_index_python.packages import get_package_share_directory, get_package_prefix from launch.launch_description_sources import PythonLaunchDescriptionSource import os def generate_launch_description(): # Get default path robot_name_in_model = "cubot_v2_sw2urdf" urdf_tutorial_path = get_package_share_directory('cubot_v2_sw2urdf') default_model_path = os.path.join( urdf_tutorial_path, 'urdf', 'cubot_v2_sw2urdf.urdf') # Read URDF file content with open(default_model_path, 'r') as urdf_file: robot_description = urdf_file.read() robot_state_publisher_node = launch_ros.actions.Node( package='robot_state_publisher', executable='robot_state_publisher', parameters=[{'robot_description': robot_description}] ) # Include another launch file for Gazebo launch_gazebo = launch.actions.IncludeLaunchDescription( PythonLaunchDescriptionSource([get_package_share_directory( 'gazebo_ros'), '/launch', '/gazebo.launch.py']), ) # Request Gazebo to spawn the robot spawn_entity_node = launch_ros.actions.Node( package='gazebo_ros', executable='spawn_entity.py', arguments=['-topic', '/robot_description', '-entity', robot_name_in_model]) package_name = 'cubot_v2_sw2urdf' pkg_share = os.pathsep + os.path.join(get_package_prefix(package_name), 'share') if 'GAZEBO_MODEL_PATH' in os.environ: os.environ['GAZEBO_MODEL_PATH'] += pkg_share else: os.environ['GAZEBO_MODEL_PATH'] = "/usr/share/gazebo-11/models" + pkg_share return launch.LaunchDescription([ robot_state_publisher_node, launch_gazebo, spawn_entity_node ])