小鱼 ROS 2 新书上线!点击链接查看, 新书配套视频点击链接查看。
提问前必看的发帖注意事项—— 提问前必看!不符合要求的问题拒绝回答!!
社区使用指南—如何添加标签修改密码
ROS2书籍7.3.3代码问题navigation.launch.py
-
问题描述:
在运行该节代码时,出现以下问题
[INFO] [launch]: All log files can be found below /home/luohao/.ros/log/2024-09-09-20-04-19-684402-luohao-virtual-machine-9498
[INFO] [launch]: Default logging verbosity is set to INFO
[ERROR] [launch]: Caught exception in launch (see debug for traceback): Caught multiple exceptions when trying to load file of format [py]:- TypeError: Failed to normalize given item of type '<class 'launch.actions.declare_launch_argument.DeclareLaunchArgument'>', when only 'str' or 'launch.Substitution' were expected.
- InvalidFrontendLaunchFileError: The launch file may have a syntax error, or its format is unknown
首先我检查另一个这个关于类的问题,我将书本中部分代码launch的l改为大写,代码如下
#创建launch配置
use_sim_time = launch.substitutions.LaunchConfiguration(
'use_sim_time',default='true'
)
map_yaml_path = launch.substitutions.LaunchConfiguration(
'map', default=os.path.join(fishbot_navigation2_dir, 'maps', 'room.yaml'))
nav2_param_path = launch.substitutions.LaunchConfiguration(
'params_file', default=os.path.join(fishbot_navigation2_dir, 'config', 'nav2_params.yaml'))
return PythonLaunchDescriptionSource([
launch.actions.DeclareLaunchArgument('use_sim_time', default_value=use_sim_time,
description='Use simulation (Gazebo) clock if true'),
launch.actions.DeclareLaunchArgument('map', default_value=map_yaml_path,
description='Full path to map file to load'),
launch.actions.DeclareLaunchArgument('params_file', default_value=nav2_param_path,
description='Full path to param file to load'),重新构建功能包后,重新启动launch,没有上述问题了,但是出现了下面问题
[INFO] [launch]: All log files can be found below /home/luohao/.ros/log/2024-09-09-20-59-13-471894-luohao-virtual-machine-10017
[INFO] [launch]: Default logging verbosity is set to INFO
[ERROR] [launch]: Caught exception in launch (see debug for traceback): Caught multiple exceptions when trying to load file of format [py]:- TypeError: Failed to normalize given item of type '<class 'launch.actions.declare_launch_argument.DeclareLaunchArgument'>', when only 'str' or 'launch.Substitution' were expected.
- InvalidFrontendLaunchFileError: The launch file may have a syntax error, or its format is unknown
不太懂这个原因,求助一下
-
从你提供的错误信息来看,问题似乎仍然与
launch
文件中的DeclareLaunchArgument
使用方式有关。在 ROS 2 的launch
系统中,DeclareLaunchArgument
的使用需要确保参数类型正确,并且通常这些参数在PythonLaunchDescriptionSource
中不应该直接包含复杂的对象,如LaunchConfiguration
。首先,让我们澄清一下
DeclareLaunchArgument
和LaunchConfiguration
的用途:DeclareLaunchArgument
用于在 launch 文件中声明一个参数,这个参数可以在其他地方通过${...}
引用。它通常只接受简单的值(如字符串、布尔值等)作为默认值。LaunchConfiguration
用于在 launch 文件中引用之前声明的参数。
在你的代码中,你试图将
LaunchConfiguration
对象作为DeclareLaunchArgument
的default_value
,这是不正确的。default_value
应该是一个简单的值(如字符串或布尔值),而不是一个LaunchConfiguration
对象。你应该修改你的代码,将
DeclareLaunchArgument
的default_value
设置为简单的字符串或布尔值,然后在需要的地方使用LaunchConfiguration
来引用这些参数。例如:from launch import LaunchDescription from launch_ros.actions import Node from launch.actions import DeclareLaunchArgument from launch.substitutions import LaunchConfiguration import os fishbot_navigation2_dir = os.path.dirname(os.path.abspath(__file__)) def generate_launch_description(): # 创建launch配置 return LaunchDescription([ DeclareLaunchArgument( 'use_sim_time', default_value='true', description='Use simulation (Gazebo) clock if true' ), DeclareLaunchArgument( 'map', default_value=os.path.join(fishbot_navigation2_dir, 'maps', 'room.yaml'), description='Full path to map file to load' ), DeclareLaunchArgument( 'params_file', default_value=os.path.join(fishbot_navigation2_dir, 'config', 'nav2_params.yaml'), description='Full path to param file to load' ), # 其他节点或动作,使用 LaunchConfiguration 引用参数 Node( package='your_package', executable='your_executable', name='your_node', parameters=[ {'use_sim_time': LaunchConfiguration('use_sim_time')}, {'map': LaunchConfiguration('map')}, {'params_file': LaunchConfiguration('params_file')} ] ), # ... 其他节点或动作 ])
注意,在
DeclareLaunchArgument
中,default_value
是一个简单的字符串或布尔值,而在节点或其他需要引用这些参数的地方,我们使用LaunchConfiguration
来引用它们。希望这能帮助你解决问题!如果还有其他问题,请继续提问。
以上为生成回答,仅供参考~
-
@3337489620 哪一个代码清单,尝试直接从配套源码文件中复制:https://gitee.com/ohhuo/ros2bookcode
-
@小鱼 就是7.3.3navigation.launch.py那个代码,你给的这个源码和书上的不一样,就是我在问题里提到的字母大小写
-
@3337489620 我检查下哈
-
@3337489620 确认有这个问题,感谢反馈,我发个勘误