ROS2自定义消息类型并在同一功能包中使用

ROS自定义消息类型

msg 文件夹中编写自定义消息,在 srv 文件夹中编写自定义服务,然后修改 CMakeLists.txt 文件:

    cmake_minimum_required(VERSION 3.8)
    project(my_msgs)

    if (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
        add_compile_options(-Wall -Wextra -Wpedantic)
    endif ()

    # find dependencies
    find_package(ament_cmake REQUIRED)
    find_package(rosidl_default_generators REQUIRED)
    find_package(other_msgs REQUIRED)   # 依赖其他的自定义消息

    set(msg_files
    )
    set(srv_files
        srv/my_test.srv
    )
    rosidl_generate_interfaces(${PROJECT_NAME}
        ${msg_files}
        ${srv_files}
        DEPENDENCIES other_msgs
        ADD_LINTER_TESTS
    )

    ament_export_dependencies(rosidl_default_runtime)


    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()

修改后的 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>my_msgs</name>
      <version>0.0.0</version>
      <description>TODO: Package description</description>
      <maintainer email="">my</maintainer>
      <license>TODO: License declaration</license>

      <buildtool_depend>ament_cmake</buildtool_depend>
      <buildtool_depend>rosidl_default_generators</buildtool_depend>
      <exec_depend>rosidl_default_runtime</exec_depend>

      <depend>other_msgs</depend>

      <member_of_group>rosidl_interface_packages</member_of_group>

      <test_depend>ament_lint_auto</test_depend>
      <test_depend>ament_lint_common</test_depend>

      <export>
        <build_type>ament_cmake</build_type>
      </export>
    </package>

然后编译就可以生成自定义消息类型。

在同一功能包中使用自定义消息类型

在其他功能包中使用自定义消息类型方法比较简单,只有在 CMakeLists.txt 中添加

    # 省略部分
    find_package(my_msgs REQUIRED)
    # 省略部分
    ament_target_dependencies(${PROJECT_NAME}_node my_msgs)
    # 省略部分

然后在 package.xml 文件中增加 <depend>my_msgs</depend> 即可。

但是在自定义的功能包中使用的话,需要添加命令

    rosidl_target_interfaces(node_name
        ${PROJECT_NAME} "rosidl_typesupport_cpp"
    )

需要在上述的 CMakeLists.txt 中增加:

    add_executable(node_name
        src/main.cpp
    )
    target_link_libraries(node_name

    )
    rosidl_target_interfaces(node_name
        ${PROJECT_NAME} "rosidl_typesupport_cpp"
    )
    install(TARGETS node_name
        DESTINATION lib/${PROJECT_NAME}
    )

便可以在src下的文件中引用自定义的消息;

posted @ 2025-01-21 17:08  逍遥鱼儿叹大海  阅读(441)  评论(0)    收藏  举报