Implement Publisher 1. code part (1) msg 1 2 3 4 5 6 7 string name uint8 sex uint8 age uint8 unknown = 0 uint8 male = 1 uint8 female = 2
(2) python person_publisher.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 import rospyfrom learning_topic.msg import Persondef velocity_publisher (): rospy.init_node('person_publisher' , anonymous=True ) person_info_pub = rospy.Publisher('/person_info' , Person, queue_size=10 ) rate = rospy.Rate(10 ) while not rospy.is_shutdown(): person_msg = Person() person_msg.name = "Tom" person_msg.age = 18 person_msg.sex = Person.male person_info_pub.publish(person_msg) rospy.loginfo("Publsh person message[%s, %d, %d]" , person_msg.name, person_msg.age, person_msg.sex) rate.sleep() if __name__ == '__main__' : try : velocity_publisher() except rospy.ROSInterruptException: pass
person_subscriber.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 import rospyfrom learning_topic.msg import Persondef personInfoCallback (msg ): rospy.loginfo("Subcribe Person Info: name:%s age:%d sex:%d" , msg.name, msg.age, msg.sex) def person_subscriber (): rospy.init_node('person_subscriber' , anonymous=True ) rospy.Subscriber("/person_info" , Person, personInfoCallback) rospy.spin() if __name__ == '__main__' : person_subscriber()
(3) C++ person_publisher.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include <ros/ros.h> #include "learning_topic/Person.h" int main (int argc, char **argv) { ros::init (argc, argv, "person_publisher" ); ros::NodeHandle n; ros::Publisher person_info_pub = n.advertise <learning_topic::Person>("/person_info" , 10 ); ros::Rate loop_rate (1 ) ; int count = 0 ; while (ros::ok ()) { learning_topic::Person person_msg; person_msg.name = "Tom" ; person_msg.age = 18 ; person_msg.sex = learning_topic::Person::male; person_info_pub.publish (person_msg); ROS_INFO ("Publish Person Info: name:%s age:%d sex:%d" , person_msg.name.c_str (), person_msg.age, person_msg.sex); loop_rate.sleep (); } return 0 ; }
person_subscriber.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include <ros/ros.h> #include "learning_topic/Person.h" void personInfoCallback (const learning_topic::Person::ConstPtr& msg) { ROS_INFO ("Subcribe Person Info: name:%s age:%d sex:%d" , msg->name.c_str (), msg->age, msg->sex); } int main (int argc, char **argv) { ros::init (argc, argv, "person_subscriber" ); ros::NodeHandle n; ros::Subscriber person_info_sub = n.subscribe ("/person_info" , 10 , personInfoCallback); ros::spin (); return 0 ; }
2. set up env (1) msg part
go to package.xml
, add following
1 2 <build_depend > message_generation</build_depend > <exec_depend > message_runtime</exec_depend >
go to CMakeLists.txt, add following
1 2 3 4 5 6 find_package( …… message_generation) add_message_files(FILES Person.msg) generate_messages(DEPENDENCIES std_msgs) catkin_package(…… message_runtime)
(2) C++ go to the CMakeLists.txt
file, add these two command here
1 2 3 4 5 6 7 add_executable(person_publisher src/person_publisher.cpp) target_link_libraries(person_publisher ${catkin_LIBRARIES}) add_dependencies(person_publisher ${PROJECT_NAME}_generate_messages_cpp) add_executable(person_subscriber src/person_subscriber.cpp) target_link_libraries(person_subscriber ${catkin_LIBRARIES}) add_dependencies(person_subscriber ${PROJECT_NAME}_generate_messages_cpp)