[Reserved] write simple message publisher and subscriber

By writing on a ROS first program hello_world , we ROS have a basic understanding of the entire program development process, and now we will be written in the true sense of ROS program to communicate between nodes. Because before has been built catkin_ws such a workspace, later developed Feature Pack will be on the inside, to where the new feature pack named topic_example , respectively, in the preparation of this two-node Feature Pack publish_node.cpp and subscribe_node.cpp , publishing node ( publish_node ) the topic ( Chatter release) std_msgs :: String type of news, subscribe to the node ( subscribe_node ) from topic ( Chatter subscription) std_msgs :: String type of message, the specific content of the message delivered here is a greeting " How are you ... ", the communication network structure shown in Figure 16 .

( FIG. 16 ) message publish and subscribe ROS communications network architecture of FIG.

1. Create a Feature Pack

In catkin_ws / src / directory new feature pack topic_example , and when creating the specified explicitly dependent roscpp and std_msgs . Open command line terminal, enter the command:

~ CD / catkin_ws / the src / 

# when creating the function package topic_example, explicit dependency specified roscpp and std_msgs, 
# CMakeLists.txt dependent and will be written to the default function package.xml packet 
catkin_create_pkg topic_example roscpp std_msgs

2. write the source code of the function package

Feature Pack need to write two separate executable node, a node to publish news, news subscription to another node, it is necessary to package the new features topic_example / src / New directory two files publish_node.cpp and subscribe_node .cpp , populate the following code, respectively.

Firstly, the publishing node publish_node.cpp , the code reads as follows:

 

Copy the code
 1 #include "ros/ros.h" 
 2 #include "std_msgs/String.h" 
 3 
 4 #include <sstream>
 5 
 6 int main(int argc, char **argv) 
 7 {
 8   ros::init(argc, argv, "publish_node");
 9   ros::NodeHandle nh;
10 
11   ros::Publisher chatter_pub = nh.advertise<std_msgs::String>("chatter", 1000);
12   ros::Rate loop_rate(10);
13   int count = 0;
14 
15   while (ros::ok()) 
16   {
17     std_msgs::String msg;
18 
19     std::stringstream ss; 
20     ss << "how are you " << count; 
21     msg.data = ss.str();
22     ROS_INFO("%s", msg.data.c_str());
23   
24     chatter_pub.publish(msg);
25   
26     ros::spinOnce();
27     loop_rate.sleep();
28     ++count;
29   }
30 
31   return 0;
32 }
Copy the code

To release the code to parse node.

#include "ros/ros.h"

This is a header file that contains ROS / ros.h , which is ROS provide a C ++ client library, header file must be included.

 

#include "std_msgs/String.h"

Because the code requires ROS standard message type provided std_msgs :: String , it is necessary to include this header.

 

ros::init(argc, argv, "publish_node");

This is an initialization ros node and indicate the name of the node, where a node named publish_node , once the program can be run after the ros registered as the calculated figure publish_node node name identified.

 

praise :: Node Handle NH;

This sentence is a statement ros node handles initialization ros node necessary.

 

ros::Publisher chatter_pub = nh.advertise<std_msgs::String>("chatter", 1000);

This tells ros Node Manager we will chatter published on this topic std_msgs :: String type of message. Here the parameter 1000 is released in size sequence, if released too quickly the message, the message is larger than the buffer 1000 will start to discard message if a previously issued.

 

ros::Rate loop_rate(10);

This phrase is used to specify the frequency of self-circulation, where the parameter 10 indicates 10Hz frequency, with the object needs to sleep () method is used.

 

while (ros::ok()) {...}

roscpp default mounted SIGINT handle, which is used to process a sentence ctrl + c keyboard operation, the node is kicked out of another node of the same name ROS network, ros :: the shutdown () is invoked at a place, all ros :: NodeHandle handles have been destroyed and so trigger the ros :: ok () returns false case value.

 

std_msgs::String msg;

Defines a std_msgs :: String message type of object that has a data member of the data used to store the data of our upcoming release. To post will be filled out data to the object data members.

 

chatter_pub.publish(msg);

Publisher using the defined data objects will be announced out, after the execution of this sentence, ROS other nodes in the network will be able to receive data in this message.

 

praise :: spin once ();

This sentence is an opportunity to make a callback function to be executed statement, this program, there are no callback function, so this one can not, here just for a complete specification of the program was not impress.

 

loop_rate.sleep();

Recall that this is a self-loop to control the frequency of sleep through.

 

Next, description subscription node subscribe_node.cpp , code is as follows:

 

Copy the code
 1 #include "ros/ros.h" 
 2 #include "std_msgs/String.h" 
 3 
 4 void chatterCallback(const std_msgs::String::ConstPtr& msg)
 5 {
 6   ROS_INFO("I heard: [%s]",msg->data.c_str());
 7 }
 8 
 9 int main(int argc, char **argv) 
10 {
11   ros::init(argc, argv, "subscribe_node");
12   ros::NodeHandle nh;
13 
14   ros::Subscriber chatter_sub = nh.subscribe("chatter", 1000,chatterCallback);
15 
16   ros::spin();
17 
18   return 0;
19 }
Copy the code

Subscribe to parse the code of the node.

Explained before had similar code does not do too much to explain, explain the emphasis here earlier did not come across code.

void chatterCallback(const std_msgs::String::ConstPtr& msg)

{

  ROS_INFO("I heard: [%s]",msg->data.c_str());

}

This is a callback function, when a message arrives chatter when the subject is automatically called once the callback function which is a word used to print the message data subscription from the topic.

 

ros::Subscriber chatter_sub = nh.subscribe("chatter", 1000,chatterCallback);

This tells ros Node Manager we will from chatter Subscribe to this topic in the news, when a message arrives here will automatically call the designated chatterCallback callback function. Here the parameter 1000 is a subscription sequence size, if the speed is fast enough to process the message, the message is larger than the buffer 1000 th word will start to discard previously received messages.

 

ros::spin();

This sentence from the program to enter a suspended state of the cycle, so that the program receives a message with the best efficiency and calls the callback function. If there is no message arrives, this sentence does not take up a lot of CPU resources, so this sentence is safe to use. Once ros :: ok () is triggered to return false , ROS :: Spin () pending state will stop and pop. Simply put, the program execution to this one, right here continuously from the circulation, at the same time check that a message arrives and decide whether to invoke the callback function.

3. Compile function package configuration and compilation

Creating Feature Pack topic_example when explicitly specified dependent roscpp and std_msgs , the default will be written to depend on the function package CMakeLists.txt and package.xml in, so only need CMakeLists.txt a few words for the end of the line in the file add the following statement executable files on it:

add_executable(publish_node src/publish_node.cpp)
target_link_libraries(publish_node ${catkin_LIBRARIES})

add_executable(subscribe_node src/subscribe_node.cpp)
target_link_libraries(subscribe_node ${catkin_LIBRARIES})

Then, you can use the following command to compile the package features:

cd ~/catkin_ws/
catkin_make -DCATKIN_WHITELIST_PACKAGES="topic_example"

4. Start running function package

First, we need to use roscore command to start the ROS Node Manager, ROS Node Manager is the foundation of all nodes running.

Open command line terminal, enter the command:

roscore

Then, the source devel / setup.bash activation catkin_ws workspace, with rosrun <package_name> <node_name> publishing node start function package.

Then open a command line terminal, respectively, enter the command:

cd ~/catkin_ws/
source devel/setup.bash
rosrun topic_example publish_node 

See output " How are you ... ", it means that the publishing node has started properly and began to chatter announced the topic of data as 17 .

(Figure 17 ) Published node has a normal start

Finally, the source devel / setup.bash activation catkin_ws workspace, with rosrun <package_name> <node_name> start function package subscription node.

Then open a command line terminal, respectively, enter the command:

cd ~/catkin_ws/
source devel/setup.bash
rosrun topic_example subscribe_node 

See output " the I the Heard: [How are you ...] ", it means that the subscription node is up from normal and began to chatter receiving message data subject, as 18 .

 

(Figure 18 ) Subscribe node has a normal start

Here, we write the news publisher and subscriber and you're done, in order to deepen the understanding of the whole process workflow, I then publish and subscribe messaging ROS communication network structure out to deepen understanding about.

 

( FIG. 19 ) message publish and subscribe ROS communications network architecture of FIG.

Guess you like

Origin www.cnblogs.com/letisl/p/11938023.html