鱼香ROS社区
    • 版块
    • 最新
    • 未解决
    • 已解决
    • 群组
    • 注册
    • 登录
    紧急通知:禁止一切关于政治&VPN翻墙等话题,发现相关帖子会立马删除封号
    提问前必看的发帖注意事项: 社区问答规则(小鱼个人)更新 | 高质量帖子发布指南

    ROS2书籍7.3.3代码问题navigation.launch.py

    已定时 已固定 已锁定 已移动
    ROS2机器人开发:从入门到实践
    ros2 ros2导航 自主导航
    3
    6
    536
    正在加载更多帖子
    • 从旧到新
    • 从新到旧
    • 最多赞同
    回复
    • 在新帖中回复
    登录后回复
    此主题已被删除。只有拥有主题管理权限的用户可以查看。
    • 3
      3337489620
      最后由 编辑

      问题描述:

      在运行该节代码时,出现以下问题

      [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

      不太懂这个原因,求助一下

      小鱼小 1 条回复 最后回复 回复 引用 0
      • 小伊小
        机器人小伊
        最后由 编辑

        从你提供的错误信息来看,问题似乎仍然与 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 来引用它们。

        希望这能帮助你解决问题!如果还有其他问题,请继续提问。


        以上为生成回答,仅供参考~

        我是小伊,已接入ChatGpt,所有回复,仅供参考~

        1 条回复 最后回复 回复 引用 0
        • 小鱼小
          小鱼 技术大佬 @3337489620
          最后由 编辑

          @3337489620 哪一个代码清单,尝试直接从配套源码文件中复制:https://gitee.com/ohhuo/ros2bookcode

          新书配套视频:https://www.bilibili.com/video/BV1GW42197Ck/

          3 1 条回复 最后回复 回复 引用 0
          • 3
            3337489620 @小鱼
            最后由 编辑

            @小鱼 就是7.3.3navigation.launch.py那个代码,你给的这个源码和书上的不一样,就是我在问题里提到的字母大小写

            小鱼小 2 条回复 最后回复 回复 引用 0
            • 小鱼小
              小鱼 技术大佬 @3337489620
              最后由 编辑

              @3337489620 我检查下哈

              新书配套视频:https://www.bilibili.com/video/BV1GW42197Ck/

              1 条回复 最后回复 回复 引用 0
              • 小鱼小
                小鱼 技术大佬 @3337489620
                最后由 编辑

                @3337489620 确认有这个问题,感谢反馈,我发个勘误

                新书配套视频:https://www.bilibili.com/video/BV1GW42197Ck/

                1 条回复 最后回复 回复 引用 0
                • 第一个帖子
                  最后一个帖子
                皖ICP备16016415号-7
                Powered by NodeBB | 鱼香ROS