调用使用Protobuf定义号的ROS消息[C++Python]

it2024-12-03  2

接上篇,我们这里来调用定义好的protobuf消息,这里依然参考apollo的代码,https://github.com/ApolloAuto/apollo-platform仓库里面的ros下面的pb_msgs_example里面的代码 下面的内容也都是里面的代码粘贴复制,感兴趣的直接看源代码就可以

2.调用自定义的proto_msg

https://github.com/ApolloAuto/apollo-platform/blob/master/ros/pb_msgs_example

2.1 代码结构

2.2 代码

2.2.1 proto
chatter.proto syntax = "proto2"; package pb_msgs; import "time.proto"; message ShortMessage { required Time stamp = 1; required string content = 2; } message Counter { required uint64 count = 1 [default = 0]; }
2.2.1 scripts
listener.py #!/usr/bin/env python import rospy import pb_msgs.msg def callback(data): rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.content) def listener(): rospy.init_node("listener", anonymous = True) rospy.Subscriber("pb_chatter", pb_msgs.msg.ShortMessage, callback) rospy.spin() if __name__ == "__main__": listener() talker.py #!/usr/bin/env python import rospy import pb_msgs.msg def talker(): pub = rospy.Publisher('pb_chatter', pb_msgs.msg.ShortMessage, queue_size = 10) rospy.init_node('talker', anonymous = True) rate = rospy.Rate(10) while not rospy.is_shutdown(): msg = pb_msgs.msg.ShortMessage() now = rospy.get_rostime() msg.stamp.sec = now.secs msg.stamp.nsec = now.nsecs msg.content = "Hello world!" pub.publish(msg) rate.sleep() if __name__ == '__main__': try: talker() except rospy.ROSInterruptException: pass
2.2.1 src
listener.cpp #include "ros/ros.h" #include "chatter.pb.h" void pbChatterCallback(const boost::shared_ptr<pb_msgs::ShortMessage>& msg) { ROS_INFO_STREAM("Time: " << msg->stamp().sec() << "." << msg->stamp().nsec()); ROS_INFO("I heard pb short message: [%s]", msg->content().c_str()); } void counterCallback(const boost::shared_ptr<pb_msgs::Counter>& msg) { ROS_INFO("I get counter message: [%d]", (int)msg->count()); } int main(int argc, char** argv) { ros::init(argc, argv, "listener"); ros::NodeHandle n; ros::Subscriber pb_sub = n.subscribe("pb_chatter", 1000, pbChatterCallback); ros::Subscriber sub = n.subscribe("counter", 1000, counterCallback); ros::spin(); return 0; } talker.cpp #include "ros/ros.h" #include "chatter.pb.h" #include <sstream> int main(int argc, char** argv) { ros::init(argc, argv, "talker"); ros::NodeHandle n; ros::Publisher pb_chatter_pub = n.advertise<pb_msgs::ShortMessage>("pb_chatter", 1000); ros::Publisher counter_pub = n.advertise<pb_msgs::Counter>("counter", 1000); ros::Rate loop_rate(10); int count = 0; while (ros::ok()) { pb_msgs::ShortMessage pb_msg; ros::Time now = ros::Time::now(); pb_msg.mutable_stamp()->set_sec(now.sec); pb_msg.mutable_stamp()->set_nsec(now.nsec); std::stringstream ss; ss << "Hello world " << count; pb_msg.set_content(ss.str()); ROS_INFO("%s", pb_msg.content().c_str()); pb_chatter_pub.publish(pb_msg); pb_msgs::Counter counter; counter.set_count(count); counter_pub.publish(counter); ros::spinOnce(); loop_rate.sleep(); ++count; } return 0; }
2.2.1 CMakeLists.txt
cmake_minimum_required(VERSION 2.8.3) project(pb_msgs_example) ## Add support for C++11, supported in ROS Kinetic and newer # add_definitions(-std=c++11) ## Find catkin macros and libraries ## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz) ## is used, also find other catkin packages find_package(catkin REQUIRED COMPONENTS roscpp genmsg std_msgs pb_msgs) ## Generate messages in the 'msg' folder # add_message_files( # FILES # Message1.msg # Message2.msg # ) add_proto_files( DIRECTORY proto FILES chatter.proto ) ## Generate added messages and services with any dependencies listed here generate_messages(DEPENDENCIES pb_msgs std_msgs) ################################### ## catkin specific configuration ## ################################### ## The catkin_package macro generates cmake config files for your package ## Declare things to be passed to dependent projects ## INCLUDE_DIRS: uncomment this if you package contains header files ## LIBRARIES: libraries you create in this project that dependent projects also need ## CATKIN_DEPENDS: catkin_packages dependent projects also need ## DEPENDS: system dependencies of this project that dependent projects also need catkin_package( # INCLUDE_DIRS include LIBRARIES pb_msgs_example_proto CATKIN_DEPENDS roscpp DEPENDS roscpp genmsg gencpp genpy pb_msgs std_msgs ) ########### ## Build ## ########### ## Specify additional locations of header files ## Your package locations should be listed before other locations # include_directories(include) # TODO: Check names of system library include directories (genmsg, gencpp, genpy) include_directories(${CATKIN_INCLUDE_DIRS}) ## Declare a C++ library # add_library(${PROJECT_NAME} # src/${PROJECT_NAME}/pb_msgs_example.cpp # ) ## Add cmake target dependencies of the library ## as an example, code may need to be generated before libraries ## either from message generation or dynamic reconfigure # add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS}) ## Declare a C++ executable ## With catkin_make all packages are built within a single CMake context ## The recommended prefix ensures that target names across packages don't collide add_executable(pb_talker src/talker.cpp) target_link_libraries(pb_talker ${catkin_LIBRARIES} pb_msgs_example_proto) add_executable(pb_listener src/listener.cpp) target_link_libraries(pb_listener ${catkin_LIBRARIES} pb_msgs_example_proto) ## Rename C++ executable without prefix ## The above recommended prefix causes long target names, the following renames the ## target back to the shorter version for ease of user use ## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node" # set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "") ## Add cmake target dependencies of the executable ## same as for the library above # add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS}) ## Specify libraries to link a library or executable target against # target_link_libraries(${PROJECT_NAME}_node # ${catkin_LIBRARIES} # ${genmsg_LIBRARIES} # ${gencpp_LIBRARIES} # ${genpy_LIBRARIES} # ) ############# ## Install ## ############# # all install targets should use catkin DESTINATION variables # See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html ## Mark executable scripts (Python etc.) for installation ## in contrast to setup.py, you can choose the destination # install(PROGRAMS # scripts/my_python_script # DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} # ) catkin_install_python(PROGRAMS scripts/talker.py scripts/listener.py DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} ) ## Mark executables and/or libraries for installation install(TARGETS pb_talker pb_listener ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} ) ## Mark cpp header files for installation # install(DIRECTORY include/${PROJECT_NAME}/ # DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION} # FILES_MATCHING PATTERN "*.h" # PATTERN ".svn" EXCLUDE # ) ############# ## Testing ## ############# ## Add gtest based cpp test target and link libraries # catkin_add_gtest(${PROJECT_NAME}-test test/test_pb_msgs_example.cpp) # if(TARGET ${PROJECT_NAME}-test) # target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME}) # endif() ## Add folders to be run by python nosetests # catkin_add_nosetests(test)
2.2.1 package.xml
<package> <name>pb_msgs_example</name> <version>1.0.0</version> <description>The pb_msgs_example package</description> <maintainer email="info@apollo.auto">Apollo Authors</maintainer> <license>BSD</license> <buildtool_depend>catkin</buildtool_depend> <build_depend>roscpp</build_depend> <build_depend>gencpp</build_depend> <build_depend>genmsg</build_depend> <build_depend>genpy</build_depend> <build_depend>std_msgs</build_depend> <build_depend>pb_msgs</build_depend> <run_depend>roscpp</run_depend> <run_depend>gencpp</run_depend> <run_depend>genmsg</run_depend> <run_depend>genpy</run_depend> <run_depend>std_msgs</run_depend> <run_depend>pb_msgs</run_depend> </package>
最新回复(0)