Autopilot (sixty-seven) --------- ROS study notes (3)

       Introduced in front of the principle of ROS, simple compiler, how to create a node, and other nodes send and receive information it? This part describes this part.

1. Write publisher node

       The new features in the package catkin_ws named topic_example, write two-node publish_node.cpp and subscribe_node.cpp in this feature pack, respectively, the publishing node (publish_node) released std_msgs :: String type of message to the topic (chatter), Feed node (subscribe_node) from a topic subscription std_msgs (chatter) :: String type message.
       a. in catkin_ws / src / directory new function package 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 /      
         $ catkin_create_pkg topic_example roscpp std_msgs      
           explicitly specified when creating dependency roscpp ### and std_msgs. Dependence will be written CMakeLists.txt and default feature pack in package.xml

       B prepared. Source code, the new function package and create two files publish_node.cpp subscribe_node.cpp lower topic_example / src / directory, the following code are filled.  

///////publish_node.cpp

#include "ros/ros.h" 
#include "std_msgs/String.h" 
#include <sstream>

int main(int argc, char **argv) {
  ros::init(argc, argv, "publish_node");//初始化,节点取名为publish_node
  ros::NodeHandle nh;//创建节点句柄
  ros::Publisher chatter_pub = nh.advertise<std_msgs::String>("chatter", 1000);
  ////这句话告诉ros节点管理器我们将会在chatter这个话题上发布std_msgs::String类型的消息。
  ros::Rate loop_rate(10);//自循环的频率,10Hz频率
  int count = 0;
  while (ros::ok()) {
    std_msgs::String msg;
    std::stringstream ss; 
    ss << "how are you " << count; 
    msg.data = ss.str();
    ROS_INFO("%s", msg.data.c_str());
    chatter_pub.publish(msg);//广播信息
    ros::spinOnce();
    loop_rate.sleep();//通过休眠来控制自循环的频率
    ++count;
  }
  return 0;
}
////subscribe_node.cpp

#include "ros/ros.h" 
#include "std_msgs/String.h" 

///这是一个回调函数,当有消息到达chatter话题时会自动被调用一次,用来打印从话题中订阅的消息数据。
void chatterCallback(const std_msgs::String::ConstPtr& msg){
  ROS_INFO("I heard: [%s]",msg->data.c_str());
}

int main(int argc, char **argv) {
  ros::init(argc, argv, "subscribe_node");
  ros::NodeHandle nh;
  ros::Subscriber chatter_sub = nh.subscribe("chatter", 1000,chatterCallback);
  //告诉ros节点管理器我们将会从chatter这个话题中订阅消息,当有消息到达时会自动调用这里指定的chatterCallback回调函数。这里的参数1000是表示订阅序列的大小,如果消息处理的速度不够快,缓冲区中的消息大于1000个的话就会开始丢弃先前接收的消息。
  ros::spin();//这一句话让程序进入自循环的挂起状态,从而让程序以最好的效率接收消息并调用回调函数。
  return 0;
}

2. Compile configuration and compile      
      when you create a feature pack topic_example, explicitly specified dependent roscpp and std_msgs, will be dependent on the default feature pack CMakeLists.txt written and package.xml in, you only need to file at the end of the line CMakeLists.txt the following statement is added for a few 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})

       You can use the following command to compile the package features:

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

3. Feature Pack up and running

       Roscore need to use commands to start Node Manager ROS, ROS Node Manager is the foundation of all nodes run.
       Reboot a terminal, with a source devel / setup.bash activate catkin_ws workspace, zsh: source devel / setup.zsh, with rosrun . <package_name> <node_name> start function package and publishing node has been sending information see: 

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

                                    

        In particular, if there is:

        In CMakeList.txt was simply find_package () followed by catkin_package () recompilation.      

        Restart a terminal, with source devel / setup.bash activation catkin_ws workspace, zsh: source devel / setup.zsh, with rosrun <package_name> <node_name> start function package subscription node. At this point we write and publish news feed reader and you're done.

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

                           
      
      

Published 68 original articles · won praise 115 · views 830 000 +

Guess you like

Origin blog.csdn.net/zhouyy858/article/details/104549991