Mac平台RoboStack环境下gazebo_ros包缺失,如何运行ROS2-Humble+Gazebo?
RoboStack环境下ROS2 Humble与Gazebo集成问题解决及示例
环境信息
- 操作系统:Mac
- ROS安装方式:遵循RoboStack官方指南
- ROS版本:Humble
问题描述
在RoboStack环境中尝试查找gazebo_ros包时,执行mamba search gazebo_ros -c robostack返回No match found for: gazebo_ros. Search: *gazebo_ros*。使用调用gazebo_ros包的launch.py启动时,报错package 'gazebo_ros' not found。
原launch.py代码:
import os from ament_index_python.packages import get_package_share_directory from launch import LaunchDescription from launch_ros.actions import Node from launch.actions import ExecuteProcess def generate_launch_description(): # Paths to the URDF file pkg_share = get_package_share_directory('hrssd') default_model_path = os.path.join(pkg_share, 'urdf', 'hrssd.urdf') # Launch Gazebo with an empty world, replace this with your world file if needed gazebo = ExecuteProcess( cmd=['gazebo', '--verbose', '-s', 'libgazebo_ros_factory.so'], output='screen' ) # Node to spawn the robot in Gazebo spawn_entity = Node(package='gazebo_ros', executable='spawn_entity.py', arguments=['-entity', 'hrssd', '-file', default_model_path], output='screen') return LaunchDescription([ gazebo, spawn_entity, ])
启动报错信息:
[INFO] [launch]: All log files can be found below /Users/X/.ros/log/2024-02-04-00-15-30-935223-MSK-C02DW5J1MD6R.local-1565 [INFO] [launch]: Default logging verbosity is set to INFO [ERROR] [launch]: Caught exception in launch (see debug for traceback): "package 'gazebo_ros' not found, searching: ['/Users/X/Development/ros_ws/install/hrssd', '/usr/local/Caskroom/miniforge/base/envs/ros_env']"
解决步骤及适配后的launch.py示例
1. 安装正确的Gazebo-ROS集成包
RoboStack中,ROS2与Gazebo的集成功能被打包在ros-humble-gazebo-ros及相关包中,执行以下命令安装:
mamba install ros-humble-gazebo-ros ros-humble-gazebo-ros-pkgs -c robostack -c conda-forge
2. 适配RoboStack的launch.py示例
使用gazebo_ros官方提供的launch文件启动Gazebo,确保通信插件正确加载,同时保留模型生成逻辑:
import os from ament_index_python.packages import get_package_share_directory from launch import LaunchDescription from launch_ros.actions import Node from launch.actions import IncludeLaunchDescription from launch.launch_description_sources import PythonLaunchDescriptionSource def generate_launch_description(): # 获取自定义包的URDF路径 pkg_hrssd = get_package_share_directory('hrssd') urdf_path = os.path.join(pkg_hrssd, 'urdf', 'hrssd.urdf') # 调用gazebo_ros包的官方启动文件 pkg_gazebo_ros = get_package_share_directory('gazebo_ros') gazebo_launch = IncludeLaunchDescription( PythonLaunchDescriptionSource( os.path.join(pkg_gazebo_ros, 'launch', 'gazebo.launch.py') ), # 指定启动空世界,可替换为自定义world文件路径 launch_arguments={'world': 'empty.world'}.items() ) # 生成机器人模型到Gazebo中 spawn_robot = Node( package='gazebo_ros', executable='spawn_entity.py', arguments=['-entity', 'hrssd', '-file', urdf_path], output='screen' ) return LaunchDescription([ gazebo_launch, spawn_robot ])
替代启动方式(直接调用gzserver/gzclient)
如果偏好手动启动Gazebo服务和客户端,可使用以下代码:
import os from ament_index_python.packages import get_package_share_directory from launch import LaunchDescription from launch_ros.actions import Node from launch.actions import ExecuteProcess def generate_launch_description(): pkg_hrssd = get_package_share_directory('hrssd') urdf_path = os.path.join(pkg_hrssd, 'urdf', 'hrssd.urdf') # 启动Gazebo服务端(加载ROS通信插件) gz_server = ExecuteProcess( cmd=['gzserver', '--verbose', '-s', 'libgazebo_ros_init.so', '-s', 'libgazebo_ros_factory.so'], output='screen' ) # 启动Gazebo客户端 gz_client = ExecuteProcess( cmd=['gzclient'], output='screen' ) spawn_robot = Node( package='gazebo_ros', executable='spawn_entity.py', arguments=['-entity', 'hrssd', '-file', urdf_path], output='screen' ) return LaunchDescription([ gz_server, gz_client, spawn_robot ])
内容的提问来源于stack exchange,提问作者Виталий Аллахвердянц
相关产品推荐
相关产品推荐

