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

    ROS2自定义接口报错

    已定时 已固定 已锁定 已移动 未解决
    ROS 2相关问题
    自定义接口 ros2 cmake报错
    2
    6
    596
    正在加载更多帖子
    • 从旧到新
    • 从新到旧
    • 最多赞同
    回复
    • 在新帖中回复
    登录后回复
    此主题已被删除。只有拥有主题管理权限的用户可以查看。
    • 夏
      夏雨惊春阑
      最后由 编辑

      报错如下:
      linux@linux-HP-246-14-inch-G10-Notebook-PC:~/status_pose$ colcon build
      Starting >>> status_pose
      --- stderr: status_pose
      CMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required):
      Compatibility with CMake < 3.10 will be removed from a future version of
      CMake.

      Update the VERSION argument <min> value. Or, use the <min>...<max> syntax
      to tell CMake that the project requires at least <min> but has been updated
      to work with policies introduced by <max> or earlier.

      CMake Error at /opt/ros/humble/share/rosidl_generator_py/cmake/rosidl_generator_py_generate_interfaces.cmake:20 (find_package):
      By not providing "FindPythonInterp.cmake" in CMAKE_MODULE_PATH this project
      has asked CMake to find a package configuration file provided by
      "PythonInterp", but CMake did not find one.

      Could not find a package configuration file provided by "PythonInterp"
      (requested version 3.6) with any of the following names:

      PythonInterpConfig.cmake
      pythoninterp-config.cmake
      

      Add the installation prefix of "PythonInterp" to CMAKE_PREFIX_PATH or set
      "PythonInterp_DIR" to a directory containing one of the above files. If
      "PythonInterp" provides a separate development package or SDK, be sure it
      has been installed.
      Call Stack (most recent call first):
      /opt/ros/humble/share/ament_cmake_core/cmake/core/ament_execute_extensions.cmake:48 (include)
      /opt/ros/humble/share/rosidl_cmake/cmake/rosidl_generate_interfaces.cmake:286 (ament_execute_extensions)
      CMakeLists.txt:26 (rosidl_generate_interfaces)


      Failed <<< status_pose [2.17s, exited with code 1]

      Summary: 0 packages finished [2.40s]
      1 package failed: status_pose
      1 package had stderr output: status_pose
      linux@linux-HP-246-14-inch-G10-Notebook-PC:~/status_pose$
      Cmakelist如下:
      cmake_minimum_required(VERSION 3.5) # 使用至少 CMake 3.5 版本
      project(status_pose)

      设置政策以避免使用已弃用的模块

      if(POLICY CMP0094)
      cmake_policy(SET CMP0094 NEW)
      endif()

      if(POLICY CMP0148)
      cmake_policy(SET CMP0148 NEW)
      endif()

      set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/Modules)

      find dependencies

      find_package(ament_cmake REQUIRED)
      find_package(builtin_interfaces REQUIRED)
      find_package(rosidl_default_generators REQUIRED)
      find_package(rclcpp REQUIRED) # 添加对rclcpp的支持
      find_package(Python3 REQUIRED COMPONENTS Interpreter Development) # 使用 FindPython3 模块
      find_package(rosidl_typesupport_cpp REQUIRED) # 添加这一行
      find_package(rosidl_typesupport_fastrtps_cpp REQUIRED) # 添加这一行
      find_package(rosidl_typesupport_introspection_cpp REQUIRED) # 添加这一行

      Generate interfaces

      rosidl_generate_interfaces(${PROJECT_NAME}
      "msg/SystemStatus.msg"
      DEPENDENCIES builtin_interfaces
      )

      Include directories for generated headers

      include_directories(${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_cpp/${PROJECT_NAME})
      include_directories(${CMAKE_CURRENT_BINARY_DIR}/rosidl_typesupport_fastrtps_cpp/${PROJECT_NAME})

      Define executable

      add_executable(SystemStatusPublisher src/SystemStatusPublisher.cpp)

      Ensure the generated message headers are included in the include directories

      target_include_directories(SystemStatusPublisher PRIVATE
      ${rosidl_generator_cpp_INCLUDE_DIRS}
      ${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_cpp/${PROJECT_NAME}
      ${CMAKE_CURRENT_BINARY_DIR}/rosidl_typesupport_fastrtps_cpp/${PROJECT_NAME}
      )

      Link dependencies to the target using ament_target_dependencies

      ament_target_dependencies(SystemStatusPublisher
      rclcpp
      builtin_interfaces
      rosidl_default_runtime
      rosidl_typesupport_cpp
      rosidl_typesupport_fastrtps_cpp
      rosidl_typesupport_introspection_cpp
      )

      Install the executable

      install(TARGETS SystemStatusPublisher
      DESTINATION lib/${PROJECT_NAME})

      Export runtime dependencies

      ament_export_dependencies(rosidl_default_runtime)
      ament_export_dependencies(rosidl_typesupport_cpp)
      ament_export_dependencies(rosidl_typesupport_fastrtps_cpp)
      ament_export_dependencies(rosidl_typesupport_introspection_cpp)

      Install message files

      install(
      DIRECTORY msg/
      DESTINATION share/${PROJECT_NAME}/
      )

      if(BUILD_TESTING)
      find_package(ament_lint_auto REQUIRED)

      the following line skips the linter which checks for copyrights

      comment the line when a copyright and license is added to all source files

      set(ament_cmake_copyright_FOUND TRUE)

      the following line skips cpplint (only works in a git repo)

      comment the line when this package is in a git repo and when

      a copyright and license is added to all source files

      set(ament_cmake_cpplint_FOUND TRUE)
      ament_lint_auto_find_test_dependencies()
      endif()
      ament_package()

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

        根据您提供的错误信息和 CMakeLists.txt 文件,问题主要出在 CMake 试图找到 Python 解释器的配置,但使用了 find_package(Python3 ...),这在 ROS 2 中通常不是正确的方法来查找和配置 Python。ROS 2 使用 find_package 来查找 ROS 2 相关的包,而不是用于配置 Python 解释器。

        在 ROS 2 中,Python 的配置通常通过 setup.py 或 pyproject.toml(对于 Python 包)以及 CMake 的 FindPython3 模块来处理。这里有几个步骤可以帮助您解决问题:

        1. 更新 CMake 最低版本要求:
          ROS 2 Humble 推荐使用更高版本的 CMake。您的 CMakeLists.txt 中设置的是 3.5,建议更新为更高的版本,如 3.10 或更高,以避免将来的兼容性问题。

          cmake_minimum_required(VERSION 3.10)
          
        2. 修改 Python 的查找方式:
          在 ROS 2 中,通常不需要直接调用 find_package 来查找 Python 解释器。相反,您应该使用 CMake 的 FindPython3 模块来配置 Python。在 CMakeLists.txt 中,您可以添加以下内容来查找 Python:

          # 查找 Python3
          find_package(Python3 COMPONENTS Interpreter REQUIRED)
          

          但是,更常见的是使用 ament_python 来处理 Python 包的构建和依赖。如果您正在构建一个 Python 包,您应该考虑使用 setup.py 或 pyproject.toml。

        3. 检查 Python 配置:
          确保您的系统上安装了正确版本的 Python,并且 CMake 能够找到它。您可以通过运行 python3 --version 来检查 Python 版本。

        4. 移除或修改非必要的 find_package 调用:
          如果您的包不是用 Python 编写的,并且不需要 Python3 组件,那么应该移除或修改这些调用。

        5. 清理并重新构建:
          在修改 CMakeLists.txt 后,确保清理旧的构建文件并重新构建您的包:

          colcon build --packages-clear status_pose
          colcon build
          
        6. 检查 ROS 2 环境:
          确保您的 ROS 2 环境已正确设置,包括所有必要的源路径和设置脚本。

        如果您正在构建一个包含 Python 和 C++ 组件的混合包,您可能需要进一步调整您的构建系统,以正确地处理两种语言的依赖和构建。在这种情况下,考虑使用 ament_python 和适当的 CMake 配置来确保一切都能正确构建和链接。


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

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

        夏 1 条回复 最后回复 回复 引用 0
        • 夏
          夏雨惊春阑 @小伊
          最后由 编辑

          @小伊 新问题出现了,我打算构建纯C++功能包,所以C++程序如下:
          #include <memory>
          #include <chrono>
          #include <functional>

          #include "rclcpp/rclcpp.hpp"
          #include "builtin_interfaces/msg/time.hpp"
          #include "status_pose/msg/system_status.hpp"

          class SystemStatusPublisher : public rclcpp::Node
          {
          public:
          SystemStatusPublisher()
          : Node("system_status_publisher"), count_(0)
          {
          publisher_ = this->create_publisher<status_pose::msg::SystemStatus>("system_status", 10);
          timer_ = this->create_wall_timer(
          std::chrono::seconds(1),
          std::bind(&SystemStatusPublisher::timer_callback, this));
          }

          private:
          void timer_callback()
          {
          auto message = status_pose::msg::SystemStatus();
          message.cpu_percent = 10.0;
          message.memory_percent = 20.0;
          message.host_name = "linux-HP-246-14-inch-G10-Notebook-PC";
          message.memory_available = 1024.0;
          message.net_recv = 100.0;
          message.net_sent = 200.0;
          message.timestamp = this->now();

          RCLCPP_INFO(this->get_logger(), "Publishing: 'cpu_percent: %f'", message.cpu_percent);
          publisher_->publish(message);
          

          }
          rclcpp::TimerBase::SharedPtr timer_;
          rclcpp::Publisher<status_pose::msg::SystemStatus>::SharedPtr publisher_;
          size_t count_;
          };

          int main(int argc, char * argv[])
          {
          rclcpp::init(argc, argv);
          rclcpp::spin(std::make_shared<SystemStatusPublisher>());
          rclcpp::shutdown();
          return 0;
          }
          Cmakelist如下:
          cmake_minimum_required(VERSION 3.10) # 使用至少 CMake 3.10 版本
          project(status_pose)

          设置政策以避免使用已弃用的模块

          if(POLICY CMP0094)
          cmake_policy(SET CMP0094 NEW)
          endif()

          if(POLICY CMP0148)
          cmake_policy(SET CMP0148 NEW)
          endif()

          查找依赖项

          find_package(ament_cmake REQUIRED)
          find_package(builtin_interfaces REQUIRED)
          find_package(rosidl_default_generators REQUIRED)
          find_package(rclcpp REQUIRED) # 添加对rclcpp的支持
          find_package(rosidl_typesupport_fastrtps_cpp REQUIRED) # 添加这一行

          生成接口(仅限 C++)

          rosidl_generate_interfaces(${PROJECT_NAME}
          "msg/SystemStatus.msg"
          DEPENDENCIES builtin_interfaces
          LANGUAGE cpp
          )

          包含生成头文件的目录

          include_directories(${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_cpp/${PROJECT_NAME})
          include_directories(${CMAKE_CURRENT_BINARY_DIR}/rosidl_typesupport_fastrtps_cpp/${PROJECT_NAME})

          定义可执行文件

          add_executable(SystemStatusPublisher src/SystemStatusPublisher.cpp)

          确保生成的消息头文件包含在包含目录中

          target_include_directories(SystemStatusPublisher PRIVATE
          ${rosidl_generator_cpp_INCLUDE_DIRS}
          ${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_cpp/${PROJECT_NAME}
          ${CMAKE_CURRENT_BINARY_DIR}/rosidl_typesupport_fastrtps_cpp/${PROJECT_NAME}
          )

          链接依赖项到目标

          ament_target_dependencies(SystemStatusPublisher
          rclcpp
          builtin_interfaces
          rosidl_default_runtime
          rosidl_typesupport_fastrtps_cpp
          )

          安装可执行文件

          install(TARGETS SystemStatusPublisher
          DESTINATION lib/${PROJECT_NAME})

          导出运行时依赖项

          ament_export_dependencies(rosidl_default_runtime)
          ament_export_dependencies(rosidl_typesupport_fastrtps_cpp)

          安装消息文件

          install(
          DIRECTORY msg/
          DESTINATION share/${PROJECT_NAME}/
          )

          if(BUILD_TESTING)
          find_package(ament_lint_auto REQUIRED)

          下面这行跳过了检查版权声明

          在所有源文件添加版权声明和许可证后取消注释

          set(ament_cmake_copyright_FOUND TRUE)

          下面这行跳过了 cpplint(仅适用于 Git 仓库)

          在所有源文件添加版权声明和许可证后取消注释

          set(ament_cmake_cpplint_FOUND TRUE)
          ament_lint_auto_find_test_dependencies()
          endif()

          ament_package()
          package.xml如下:
          <?xml version="1.0"?>
          <?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
          <package format="3">
          <name>status_pose</name>
          <version>0.0.0</version>
          <description>ROS 2 package for publishing system status messages.</description>
          <maintainer email="2968436050@qq.com">linux</maintainer>
          <license>Apache-2.0</license>
          <member_of_group>rosidl_interface_packages</member_of_group>
          <buildtool_depend>ament_cmake</buildtool_depend>
          <build_depend>rclcpp</build_depend>
          <build_depend>builtin_interfaces</build_depend>
          <build_depend>rosidl_default_generators</build_depend>
          <build_depend>rosidl_typesupport_cpp</build_depend>
          <exec_depend>rclcpp</exec_depend>
          <exec_depend>builtin_interfaces</exec_depend>
          <exec_depend>rosidl_default_runtime</exec_depend>
          <exec_depend>rosidl_typesupport_cpp</exec_depend>
          <exec_depend>rosidl_typesupport_fastrtps_cpp</exec_depend>
          <exec_depend>rosidl_typesupport_introspection_cpp</exec_depend>
          <test_depend>ament_lint_auto</test_depend>
          <test_depend>ament_lint_common</test_depend>
          <export>
          <build_type>ament_cmake</build_type>
          </export>
          </package>
          使用了vscode,配置文件如下:
          {
          "configurations": [
          {
          "name": "Linux",
          "includePath": [
          "${workspaceFolder}/**",
          "/opt/ros/humble/include",
          "/usr/include/c++/11",
          "/usr/include/x86_64-linux-gnu/c++/11",
          "/usr/include/c++/11/backward",
          "/usr/lib/gcc/x86_64-linux-gnu/11/include",
          "/usr/local/include",
          "/usr/include/x86_64-linux-gnu",
          "/usr/include",
          "/opt/ros/humble/include/rclcpp",
          "/opt/ros/humble/include/builtin_interfaces",
          "/opt/ros/humble/include/rosidl_typesupport_cpp",
          "/opt/ros/humble/include/rosidl_runtime_cpp",
          "/opt/ros/humble/include/rosidl_typesupport_interface",
          "/opt/ros/humble/include/rcutils",
          "/opt/ros/humble/include/rosidl_typesupport_fastrtps_cpp",
          "/opt/ros/humble/include/rosidl_typesupport_introspection_cpp"
          ],
          "defines": [],
          "compilerPath": "/usr/bin/g++",
          "cStandard": "c11",
          "cppStandard": "c++17",
          "intelliSenseMode": "linux-gcc-x64"
          }
          ],
          "version": 4
          }
          最后报错信息如下:
          linux@linux-HP-246-14-inch-G10-Notebook-PC:~/status_pose$ colcon build --cmake-clean-cache
          Starting >>> status_pose
          --- stderr: status_pose
          CMake Error at /opt/ros/humble/share/rosidl_cmake/cmake/rosidl_generate_interfaces.cmake:162 (message):
          rosidl_generate_interfaces() the passed dependency 'LANGUAGE' has not been
          found before using find_package()
          Call Stack (most recent call first):
          CMakeLists.txt:21 (rosidl_generate_interfaces)


          Failed <<< status_pose [1.59s, exited with code 1]

          Summary: 0 packages finished [1.79s]
          1 package failed: status_pose
          1 package had stderr output: status_pose

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

            从你提供的错误信息和CMakeLists.txt文件来看,问题出在rosidl_generate_interfaces函数的调用上。错误信息指出:“the passed dependency 'LANGUAGE' has not been found before using find_package()”。这通常意味着在调用rosidl_generate_interfaces时,参数的使用方式不正确。

            在ROS 2中,rosidl_generate_interfaces函数用于生成消息、服务和类型的接口代码。这个函数通常的调用格式如下:

            rosidl_generate_interfaces(
              <PROJECT_NAME>
              FILES
                <list_of_interface_files>
              DEPENDENCIES
                <list_of_dependencies>
              TARGET_DEPENDENCIES
                <list_of_target_dependencies>  # (可选)
              LANGUAGES
                <list_of_languages>  # 注意这里是LANGUAGES,不是LANGUAGE
            )
            

            在你的CMakeLists.txt中,你使用了LANGUAGE cpp,但正确的应该是LANGUAGES cpp。修改后的相关部分应该如下:

            rosidl_generate_interfaces(${PROJECT_NAME}
              "msg/SystemStatus.msg"
              DEPENDENCIES builtin_interfaces
              LANGUAGES cpp
            )
            

            此外,还有一些其他的注意事项和可能的改进:

            1. CMake Policy 设置:你的CMakeLists.txt中设置了两个CMake政策,但没有检查这些政策是否适用于你的CMake版本。通常,这些设置是为了解决跨版本兼容性问题,确保你的CMake版本与这些政策兼容。

            2. 安装消息文件:你的install指令应该确保消息文件被正确安装到ROS 2的标准路径下,通常是在share/<package_name>/msg/。你的设置看起来是正确的,但确保msg/目录确实包含了你定义的.msg文件。

            3. 包依赖:在package.xml中,你列出了很多依赖,包括rosidl_typesupport_cpp和rosidl_typesupport_introspection_cpp,但在CMakeLists.txt中并没有看到对这些类型的支持进行明确的配置。如果你不需要这些类型的支持,可以从package.xml中移除它们。如果你需要,确保在CMakeLists.txt中添加了相应的配置。

            4. VSCode 配置:你的VSCode配置看起来相当完整,确保所有路径都是正确的,特别是ROS 2的安装路径和相关的include路径。

            在修改CMakeLists.txt后,重新运行colcon build --cmake-clean-cache来清理旧的构建缓存并重新构建你的包。这应该解决你遇到的问题。如果还有其他问题,可能需要检查其他配置或依赖是否正确。


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

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

            夏 1 条回复 最后回复 回复 引用 0
            • 夏
              夏雨惊春阑 @小伊
              最后由 编辑

              @小伊linux@linux-HP-246-14-inch-G10-Notebook-PC:~/status_pose$ colcon build --cmake-clean-cache
              Starting >>> status_pose
              --- stderr: status_pose
              CMake Error at /opt/ros/humble/share/rosidl_cmake/cmake/rosidl_generate_interfaces.cmake:162 (message):
              rosidl_generate_interfaces() the passed dependency 'LANGUAGES' has not been
              found before using find_package()
              Call Stack (most recent call first):
              CMakeLists.txt:12 (rosidl_generate_interfaces)


              Failed <<< status_pose [1.57s, exited with code 1]

              Summary: 0 packages finished [1.73s]
              1 package failed: status_pose
              1 package had stderr output: status_pose

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

                这个错误信息表明在使用 colcon build 命令构建 status_pose 包时遇到了问题。错误发生在 CMake 的配置阶段,具体是在 rosidl_generate_interfaces.cmake 文件中。错误消息指出 rosidl_generate_interfaces() 函数中传递的依赖 LANGUAGES 在调用 find_package() 之前没有被找到。

                这里有几个可能的解决步骤:

                1. 检查 CMakeLists.txt 文件:
                  打开 status_pose 包的 CMakeLists.txt 文件,检查第 12 行附近的 rosidl_generate_interfaces 调用。确保在调用这个函数之前已经正确设置了所有必要的依赖和变量,特别是 LANGUAGES。通常,LANGUAGES 应该是一个列表,比如 set(LANGUAGES "cpp" "py"),用来指定需要生成的接口语言。

                2. 确保所有依赖都已正确安装:
                  确保你的环境中已经安装了所有必要的 ROS 2 组件和依赖。你可以检查你的 ROS 2 安装是否完整,特别是与 rosidl 相关的部分。

                3. 清理并重新构建:
                  使用 --cmake-clean-cache 选项已经是一个很好的开始,因为它会清理旧的 CMake 缓存。但有时候,完全删除构建目录(通常是 build 或 install 目录)并重新运行 colcon build 可以解决问题。

                4. 检查 ROS 2 版本兼容性:
                  确保你的 status_pose 包是为当前安装的 ROS 2 版本(在这个案例中是 Humble)开发的。有时候,包的某些部分可能依赖于特定版本的 ROS 2,而在其他版本中可能不兼容。

                5. 查看文档和社区资源:
                  如果上述步骤都无法解决问题,查看 ROS 2 的官方文档或搜索相关的社区论坛和问答网站可能会有帮助。可能有其他开发者遇到过类似的问题,并分享了解决方案。

                通过上述步骤,你应该能够定位问题的原因,并找到解决这个问题的方法。如果问题仍然存在,可能需要更详细地检查你的项目配置或寻求来自 ROS 2 社区的帮助。


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

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

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