cd ~/catkin_ws/src/beginner_tutorials
編輯文件src/listener.cpp:
vim src/listener.cpp
將下面的代碼複製到文件中:
#include "ros/ros.h"
#include "std_msgs/String.h"
/**
* This tutorial demonstrates simple receipt of messages over the ROS system.
*/
void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
ROS_INFO("I heard: [%s]", msg->data.c_str());
}
int main(int argc, char **argv)
{
/**
* The ros::init() function needs to see argc and argv so that it can perform
* any ROS arguments and name remapping that were provided at the command line. For programmatic
* remappings you can use a different version of init() which takes remappings
* directly, but for most command-line programs, passing argc and argv is the easiest
* way to do it. The third argument to init() is the name of the node.
*
* You must call one of the versions of ros::init() before using any other
* part of the ROS system.
*/
ros::init(argc, argv, "listener");
/**
* NodeHandle is the main access point to communications with the ROS system.
* The first NodeHandle constructed will fully initialize this node, and the last
* NodeHandle destructed will close down the node.
*/
ros::NodeHandle n;
/**
* The subscribe() call is how you tell ROS that you want to receive messages
* on a given topic. This invokes a call to the ROS
* master node, which keeps a registry of who is publishing and who
* is subscribing. Messages are passed to a callback function, here
* called chatterCallback. subscribe() returns a Subscriber object that you
* must hold on to until you want to unsubscribe. When all copies of the Subscriber
* object go out of scope, this callback will automatically be unsubscribed from
* this topic.
*
* The second parameter to the subscribe() function is the size of the message
* queue. If messages are arriving faster than they are being processed, this
* is the number of messages that will be buffered up before beginning to throw
* away the oldest ones.
*/
ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
/**
* ros::spin() will enter a loop, pumping callbacks. With this version, all
* callbacks will be called from within this thread (the main one). ros::spin()
* will exit when Ctrl-C is pressed, or the node is shutdown by the master.
*/
ros::spin();
return 0;
}
保存退出。下面看一下代碼的解釋,
void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
ROS_INFO("I heard: [%s]", msg->data.c_str());
}
當一個消息到達chatter話題時,這個回調函數將會被調用。
ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
訂閱chatter話題,當一個新的消息到達時,ROS將會調用chatterCallback()函數。第二個參數是對列的長度,如果我們處理消息的速度不夠快,會將收到的消息緩衝下來,一共可以緩衝1000條消息,滿1000之後,後面的到達的消息將會覆蓋前面的消息。
NodeHandle::subscribe()將會返回一個ros::Subscriber類型的對象,當訂閱對象被銷燬以後,它將會自動從chatter話題上撤銷。
ros::spin();
ros::spin()進入了一個循環,可以儘快的調用消息的回調函數。不要擔心,如果它沒有什麼事情可做時,它也不會浪費太多的CPU。當ros::ok()返回false時,ros::spin()將會退出。這就意味着,當ros::shutdown()被調用,或按下CTRL+C等情況,都可以退出。下面總結一下寫一個訂閱者的步驟:(1)初始化ROS系統(2)訂閱chatter話題(3)Spin,等待消息的到來(4)當一個消息到達時,chatterCallback()函數被調用。
下面看一下如何構建節點。這時候你的CMakeLists.txt看起來應該是下面這個樣子,包括前面所做的修改,註釋部分可以除去:
cmake_minimum_required(VERSION 2.8.3)
project(beginner_tutorials)
## Find catkin and any catkin packages
find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs genmsg)
## Declare ROS messages and services
add_message_files(DIRECTORY msg FILES Num.msg)
add_service_files(DIRECTORY srv FILES AddTwoInts.srv)
## Generate added messages and services
generate_messages(DEPENDENCIES std_msgs)
## Declare a catkin package
catkin_package()
將下面幾行代碼添加到CMakeLists.txt的最後。最終你的CMakeLists.txt文件看起來樣該是下面這個樣子:
cmake_minimum_required(VERSION 2.8.3)
project(beginner_tutorials)
## Find catkin and any catkin packages
find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs genmsg)
## Declare ROS messages and services
add_message_files(FILES Num.msg)
add_service_files(FILES AddTwoInts.srv)
## Generate added messages and services
generate_messages(DEPENDENCIES std_msgs)
## Declare a catkin package
catkin_package()
## Build talker and listener
include_directories(include ${catkin_INCLUDE_DIRS})
add_executable(talker src/talker.cpp)
target_link_libraries(talker ${catkin_LIBRARIES})
add_dependencies(talker beginner_tutorials_generate_messages_cpp)
add_executable(listener src/listener.cpp)
target_link_libraries(listener ${catkin_LIBRARIES})
add_dependencies(listener beginner_tutorials_generate_messages_cpp)
這將會創建兩個可執行文件,talker和listener。它們將會產生在~/catkin_ws/devel/lib/share/<package name>目錄下,下面開始構建,在你的工作空間根目錄下輸入:
catkin_make