Gazebo自定义机器人仿真中启动Nav2+RViz2时地图未加载问题
系统规格
Ubuntu 22.04 LTS ROS2-HUMBLE Gazebo11
我用Gazebo搭建机器人仿真建图环境,SLAM和Nav2的建图功能完全正常,但启动Nav2和RViz2加载已保存的地图时,地图无法在RViz2中显示。
启动文件如下:
import os from launch import LaunchDescription from launch.actions import DeclareLaunchArgument, IncludeLaunchDescription from launch.substitutions import LaunchConfiguration, PathJoinSubstitution from launch.launch_description_sources import PythonLaunchDescriptionSource from launch.conditions import IfCondition from launch_ros.substitutions import FindPackageShare from launch_ros.actions import Node MAP_NAME='playground' #change to the name of your own map here def generate_launch_description(): depth_sensor = os.getenv('LINOROBOT2_DEPTH_SENSOR', '') nav2_launch_path = PathJoinSubstitution( [FindPackageShare('nav2_bringup'), 'launch', 'bringup_launch.py'] ) rviz_config_path = PathJoinSubstitution( [FindPackageShare('linorobot2_navigation'), 'rviz', 'linorobot2_navigation.rviz'] ) default_map_path = PathJoinSubstitution( [FindPackageShare('linorobot2_navigation'), 'maps', f'{MAP_NAME}.yaml'] ) nav2_config_path = PathJoinSubstitution( [FindPackageShare('linorobot2_navigation'), 'config', 'navigation.yaml'] ) return LaunchDescription([ DeclareLaunchArgument( name='sim', default_value='false', description='Enable use_sime_time to true' ), DeclareLaunchArgument( name='rviz', default_value='false', description='Run rviz' ), DeclareLaunchArgument( name='map', default_value=default_map_path, description='Navigation map path' ), IncludeLaunchDescription( PythonLaunchDescriptionSource(nav2_launch_path), launch_arguments={ 'map': LaunchConfiguration("map"), 'use_sim_time': LaunchConfiguration("sim"), 'params_file': nav2_config_path }.items() ), Node( package='rviz2', executable='rviz2', name='rviz2', output='screen', arguments=['-d', rviz_config_path], condition=IfCondition(LaunchConfiguration("rviz")), parameters=[{'use_sim_time': LaunchConfiguration("sim")}] ) ])
问题复现步骤
mkdir ws cd ws mkdir src git clone -b humble https://github.com/linorobot/linorobot2 src/linorobot2 git clone https://github.com/linorobot/linorobot2_viz src/linorobot2_viz rosdep update && rosdep install --from-path src --ignore-src -y colcon build
在一个终端运行:
source /opt/ros/humble/setup.bash source install/setup.bash ros2 launch linorobot2_gazebo gazebo.launch.py
打开另一个终端运行:
cd ws source /opt/ros/humble/setup.bash source install/setup.bash ros2 launch linorobot2_navigation navigation.launch.py sim:=true rviz:=true
RViz启动后地图无法显示。我试过Nav2 TurtleBot3教程,地图能正常显示,说明问题不在Gazebo或RViz2本身。已确认/map、/map_server/transition_event、/map_updates话题存在,对比过TurtleBot3的启动文件、机器人模型及地图文件,没发现问题。
可能的解决方向
- 检查RViz配置的Map组件:打开RViz后找到Map插件,确认Topic设置为
/map,同时Use Timestamp选项要和use_sim_time保持一致(sim模式下需勾选)。也可以尝试删除现有Map组件,手动重新添加再订阅/map话题测试。 - 验证地图文件的路径与格式:
- 确认
MAP_NAME对应的yaml文件存在于linorobot2_navigation/maps目录下,yaml中image字段的路径是相对yaml文件的正确路径(比如image: playground.pgm,确保pgm文件和yaml在同一目录)。 - 用
ls -l查看地图文件的权限,确保当前用户有可读权限。
- 确认
- 检查use_sim_time参数一致性:确认nav2的map_server、rviz2都正确设置了
use_sim_time:=true,时间不同步会导致RViz无法正确渲染地图。可以用ros2 param get /map_server use_sim_time和ros2 param get /rviz2 use_sim_time验证参数值。 - 查看map_server日志:运行
ros2 run rqt_console rqt_console,过滤map_server的日志信息,看是否有文件加载失败、格式错误等提示。 - 手动启动map_server测试:单独启动map_server排除launch文件的问题:
然后在RViz中手动添加Map组件订阅ros2 run nav2_map_server map_server --ros-args -p yaml_filename:=<你的地图yaml绝对路径> -p use_sim_time:=true/map,看是否能正常显示。
内容的提问来源于stack exchange,提问作者nyongcyongs98
相关产品推荐
相关产品推荐

