1. Define msg file:
Create a new MSG folder under the catkin_ws/src/learning_topic file and a new Person.msg file under the folder.
The code in the msg file is as follows:
string name uint8 sex uint8 age uint8 unknow = 0 uint8 male = 1 uint8 female = 2
2. Add functional package dependencies in package.xml:
Add in catkin_ws/src/learning_topic/package.xml
<build_depend>message_generation</build_depend> <exec_depend>message_runtime</exec_depend>
3. Add compilation options to CMakeLists.txt:
Add message_generation to find_package, and add it as follows:
find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs turtlesim message_generation )
Add message_runtime to catkin_package, and add it as follows:
catkin_package( # INCLUDE_DIRS include # LIBRARIES learning_topic CATKIN_DEPENDS roscpp rospy std_msgs turtlesim message_runtime # DEPENDS system_lib )
And add:
add_message_files(FILES Person.msg) generate_messages(DEPENDENCIES std_msgs) 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)
4. The code in src:
person_publisher.cpp
/** * This routine will publish / person_info topics, customize the message type learning_topic::Person */ #include <ros/ros.h> #include "learning_topic/Person.h" int main(int argc, char **argv) { // ROS Node Initialization ros::init(argc, argv, "person_publisher"); // Create node handles ros::NodeHandle n; // Create a Publisher that publishes a topic named / person_info with a message type of learning_topic::Person and a queue length of 10 ros::Publisher person_info_pub = n.advertise<learning_topic::Person>("/person_info", 10); // Set the frequency of the cycle ros::Rate loop_rate(1); int count = 0; while (ros::ok()) { // Initialize learning_topic::Person-type messages learning_topic::Person person_msg; person_msg.name = "Tom"; person_msg.age = 18; person_msg.sex = learning_topic::Person::male; // Publish news 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); // Delay in accordance with cyclic frequency loop_rate.sleep(); } return 0; }
person_subscriber.cpp
/** * This routine subscribes to / person_info topics, customizing the message type learning_topic::Person */ #include <ros/ros.h> #include "learning_topic/Person.h" // After receiving the subscribed message, it enters the message callback function void personInfoCallback(const learning_topic::Person::ConstPtr& msg) { // Print out the received message 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) { // Initialize ROS nodes ros::init(argc, argv, "person_subscriber"); // Create node handles ros::NodeHandle n; // Create a Subscriber, subscribe to a topic named / person_info, and register the callback function personInfoCallback ros::Subscriber person_info_sub = n.subscribe("/person_info", 10, personInfoCallback); // Loop waiting callback function ros::spin(); return 0; }
5. Compile and run:
Running catkin_make in catkin_ws directory requires running the following two commands in two different terminals after compilation is completed:
Step 1 Open Receive rosrun learning_topic person_subscriber Part 2 Open Send rosrun learning_topic person_publisher