1.创建功能包,参考ros官方方式Writing a simple publisher and subscriber (C++) — ROS 2 Documentation: Humble documentation
ros2 pkg create --build-type ament_cmake --license Apache-2.0 cpp_pubsub
2.创建msg
在cpp_pubsub功能包下创建msg文件夹 ros2_ws/cpp_pubsub/msg
mkdir msg
在msg文件夹下创建.msg文件 ros2_ws/cpp_pubsub/msg/Demo.msg
touch Demo.msg
Demo.msg定义如下
int64 num std_msgs/String str
3.修改cpp_pubsub功能包下的package.xml文件
xml文件内添加
<buildtool_depend>rosidl_default_generators</buildtool_depend> <exec_depend>rosidl_default_runtime</exec_depend> <member_of_group>rosidl_interface_packages</member_of_group>
4.CmakeLists.txt添加
find_package(rosidl_default_generators REQUIRED) rosidl_generate_interfaces(${PROJECT_NAME} "msg/Demo.msg" DEPENDENCIES std_msgs )
#导出运行时依赖
ament_export_dependencies(rosidl_default_runtime)
5.修改功能CmakeLists.txt 要编译的 talker listener node节点的依赖
add_executable(talker src/publisher_member_function.cpp) ament_target_dependencies(talker rclcpp) add_executable(listener src/subscriber_member_function.cpp) ament_target_dependencies(listener rclcpp) install(TARGETS talker listener DESTINATION lib/${PROJECT_NAME}) rosidl_get_typesupport_target(cpp_typesupport_target ${PROJECT_NAME} rosidl_typesupport_cpp) target_link_libraries(talker "${cpp_typesupport_target}") target_link_libraries(listener "${cpp_typesupport_target}") ament_export_dependencies(rosidl_default_runtime)
6.完整的package.xml和CmakeLists.txt如下
#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>cpp_pubsub</name> <version>0.0.0</version> <description>TODO: Package description</description> <maintainer email="">nvidia</maintainer> <license>TODO: License declaration</license> <buildtool_depend>ament_cmake</buildtool_depend> <depend>rclcpp</depend> <depend>std_msgs</depend> <test_depend>ament_lint_auto</test_depend> <test_depend>ament_lint_common</test_depend> <buildtool_depend>rosidl_default_generators</buildtool_depend> <exec_depend>rosidl_default_runtime</exec_depend> <member_of_group>rosidl_interface_packages</member_of_group> <export> <build_type>ament_cmake</build_type> </export> </package>
#CMakeLists.txt文件
cmake_minimum_required(VERSION 3.8) project(cpp_pubsub) if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") add_compile_options(-Wall -Wextra -Wpedantic) endif() set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # find dependencies find_package(ament_cmake REQUIRED) find_package(rclcpp REQUIRED) find_package(std_msgs REQUIRED) find_package(rosidl_default_generators REQUIRED) rosidl_generate_interfaces(${PROJECT_NAME} "msg/Num.msg" DEPENDENCIES std_msgs ) add_executable(talker src/publisher_member_function.cpp) ament_target_dependencies(talker rclcpp) add_executable(listener src/subscriber_member_function.cpp) ament_target_dependencies(listener rclcpp) install(TARGETS talker listener DESTINATION lib/${PROJECT_NAME}) rosidl_get_typesupport_target(cpp_typesupport_target ${PROJECT_NAME} rosidl_typesupport_cpp) target_link_libraries(talker "${cpp_typesupport_target}") target_link_libraries(listener "${cpp_typesupport_target}") ament_export_dependencies(rosidl_default_runtime) ament_package()
7.publisher_member_function.cpp文件代码修改
void timer_callback() { auto message = cpp_pubsub::msg::Num(); message.num = (int64_t)count_++; message.str.data = "Hello, world!"; RCLCPP_INFO(this->get_logger(), "Publishing: %s %ld", message.str.data.c_str(), message.num); publisher_->publish(message); }
8.subscriber_member_function.cpp文件修改
void topic_callback(const cpp_pubsub::msg::Num & msg) const { RCLCPP_INFO(this->get_logger(), "I heard: %s %ld", msg.str.data.c_str(), msg.num); }
标签:文件,target,自定义,typesupport,cpp,ament,msg,rosidl From: https://www.cnblogs.com/duzw/p/18303331