ROS2 Humble中加载SDF至Gazebo时出现RcutilsLogger调用错误
ROS2 Humble中Gazebo加载SDF模型报错问题排查与解决
问题描述
通过Python包launch_py内的launch/car_launch2.launch.py启动文件,加载ROS2 C++包models_pkg中路径models/rcCar_assembly/models.sdf下的SDF文件时,Gazebo加载出现RcutilsLogger错误;单独启动Gazebo也存在异常。
环境信息
- Gazebo版本:11.10.2
- ROS2发行版:Humble
报错详情
启动模型时的报错
执行命令 ros2 launch launch_py car_launch2.launch.py 后输出:
hp@hp-HP-Pavilion-Gaming-Laptop-15-dk2xxx:~/rosws/ros2/bfmc_ws$ ros2 launch launch_py car_launch2.launch.py [INFO] [launch]: All log files can be found below /home/hp/.ros/log/2022-09-25-10-53-59-112288-hp-HP-Pavilion-Gaming-Laptop-15-dk2xxx-20694 [INFO] [launch]: Default logging verbosity is set to INFO urdf_file_name : rcCar_assembly/model.sdf [INFO] [spawn_entity.py-1]: process started with pid [20695] [spawn_entity.py-1] [INFO] [1664083439.508709594] [spawn_entity]: Spawn Entity started [spawn_entity.py-1] [INFO] [1664083439.509012197] [spawn_entity]: Loading entity XML from file /home/hp/rosws/ros2/bfmc_ws/install/models_pkg/share/models_pkg/rcCar_assembly/model.sdf [spawn_entity.py-1] Traceback (most recent call last): [spawn_entity.py-1] File "/opt/ros/humble/lib/gazebo_ros/spawn_entity.py", line 370, in <module> [spawn_entity.py-1] main() [spawn_entity.py-1] File "/opt/ros/humble/lib/gazebo_ros/spawn_entity.py", line 365, in main [spawn_entity.py-1] exit_code = spawn_entity_node.run() [spawn_entity.py-1] File "/opt/ros/humble/lib/gazebo_ros/spawn_entity.py", line 143, in run [spawn_entity.py-1] self.get_logger().error('Error: specified file %s does not exist', self.args.file) [spawn_entity.py-1] TypeError: RcutilsLogger.error() takes 2 positional arguments but 3 were given [ERROR] [spawn_entity.py-1]: process has died [pid 20695, exit code 1, cmd '/opt/ros/humble/lib/gazebo_ros/spawn_entity.py -entity automobile -file /home/hp/rosws/ros2/bfmc_ws/install/models_pkg/share/models_pkg/rcCar_assembly/model.sdf -x 0.82 -y -14.91 -z 0.032939 -Y 1.5707 --ros-args']. hp@hp-HP-Pavilion-Gaming-Laptop-15-dk2xxx:~/rosws/ros2/bfmc_ws$
单独启动Gazebo的报错
执行命令 ros2 launch gazebo_ros gazebo.launch.py 后输出:
~/rosws/ros2/bfmc_ws$ ros2 launch gazebo_ros gazebo.launch.py [INFO] [launch]: All log files can be found below /home/hp/.ros/log/2022-09-25-11-06-53-410707-hp-HP-Pavilion-Gaming-Laptop-15-dk2xxx-21804 [INFO] [launch]: Default logging verbosity is set to INFO [INFO] [gzserver-1]: process started with pid [21805] [INFO] [gzclient-2]: process started with pid [21807] [ERROR] [gzserver-1]: process has died [pid 21805, exit code 255, cmd 'gzserver -slibgazebo_ros_init.so -slibgazebo_ros_factory.so -slibgazebo_ros_force_system.so']. [gzclient-2] ../src/intel/isl/isl.c:2220: FINISHME: ../src/intel/isl/isl.c:isl_surf_supports_ccs: CCS for 3D textures is disabled, but a workaround is available.
启动文件代码
car_launch2.launch.py 内容:
import os from ament_index_python.packages import get_package_share_directory from launch import LaunchDescription from launch.actions import DeclareLaunchArgument from launch.substitutions import LaunchConfiguration from launch_ros.actions import Node def generate_launch_description(): use_sim_time = LaunchConfiguration('use_sim_time', default='false') urdf_file_name = 'rcCar_assembly/model.sdf' args_ = " -model automobile -sdf -x 0.82 -y -14.91 -z 0.032939 -Y 1.570796" print("urdf_file_name : {}".format(urdf_file_name)) urdf = os.path.join( get_package_share_directory('models_pkg'), urdf_file_name) return LaunchDescription([ DeclareLaunchArgument( 'use_sim_time', default_value='false', description='Use simulation (Gazebo) clock if true'), Node( package="gazebo_ros", executable = "spawn_entity.py", output='screen', arguments=["-entity", "automobile", "-file", urdf, "-x","0.82", "-y","-14.91", "-z","0.032939", "-Y","1.5707"]) ])
解决方案
1. 修复RcutilsLogger语法错误
报错提示 RcutilsLogger.error() takes 2 positional arguments but 3 were given,原因是ROS2 Humble中get_logger().error()不支持多参数格式化,需提前完成字符串格式化:
- 打开
/opt/ros/humble/lib/gazebo_ros/spawn_entity.py文件 - 定位到第143行:
self.get_logger().error('Error: specified file %s does not exist', self.args.file) - 修改为以下两种格式之一:
或# f-string格式化 self.get_logger().error(f'Error: specified file {self.args.file} does not exist')# str.format()格式化 self.get_logger().error('Error: specified file {} does not exist'.format(self.args.file))
2. 解决Gazebo启动失败问题
针对gzserver退出码255
- 清理Gazebo缓存:删除
~/.gazebo/cache目录 - 处理Intel显卡驱动警告:
isl.c的提示属于显卡驱动特性限制,可尝试更新Intel显卡驱动,或在启动前添加环境变量启用软件渲染:export LIBGL_ALWAYS_SOFTWARE=1 - 检查环境变量配置:确保正确source ROS2和工作空间的setup文件:
source /opt/ros/humble/setup.bash source ~/rosws/ros2/bfmc_ws/install/setup.bash
额外检查模型路径
启动日志显示加载路径为 /home/hp/rosws/ros2/bfmc_ws/install/models_pkg/share/models_pkg/rcCar_assembly/model.sdf,需确认该文件真实存在,同时注意用户描述的路径是models/rcCar_assembly/models.sdf,检查是否存在文件名拼写差异(model.sdf vs models.sdf)。
内容的提问来源于stack exchange,提问作者Mohanaharish Vasudevan
相关产品推荐
相关产品推荐

