隐藏页面特效

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

1|0ROS自定义消息类型


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>

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

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


在其他功能包中使用自定义消息类型方法比较简单,只有在 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下的文件中引用自定义的消息;


__EOF__

本文作者Dyp
本文链接https://www.cnblogs.com/Dyp-/p/18683611.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   逍遥鱼儿叹大海  阅读(40)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示