一、Publisher节点 “节点” 是一个连接着ROS框架的可执行文件,这里我们用C++写一个publisher节点 (命名为”talker”) ,不断地发布一条信息。
首先进入包目录的src文件夹:
1 2 roscd beginner_tutorials cd src
然后创建一个名为'talker.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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 # include "ros/ros.h" # include "std_msgs/String.h" # include <sstream> int main (int argc, char **argv) { ros::init(argc, argv, "talker" ); ros::NodeHandle n; ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter" , 1000 ); ros::Rate loop_rate (10 ) ; int count = 0 ; while (ros::ok()) { std_msgs::String msg; std ::stringstream ss; ss << "hello world " << count; msg.data = ss.str(); ROS_INFO("%s" , msg.data.c_str()); chatter_pub.publish(msg); ros::spinOnce(); loop_rate.sleep(); ++count; } return 0 ; }
各语句的功能如注释所示,不再讲解。
二、Subscriber节点 Publisher节点写完,我们有了信息的发布者,接下来我们写一个Subcriber节点来接收信息。
同样在beginner_tutorials包目录的src文件夹下,建立一个名为'listener.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 # include "ros/ros.h" # include "std_msgs/String.h" 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, "listener" ); ros::NodeHandle n; ros::Subscriber sub = n.subscribe("chatter" , 1000 , chatterCallback); ros::spin(); return 0 ; }
代码比较简单,功能如注释所示。
三、构建项目 1、修改CMakeList.txt 继续使用我们上一节使用的自己创建的beginner_tutorial包。
此包的CMakeLists.txt文件应该如下所示(移除不必要的注释之后):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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()
我们需要添加以下内容:
1 2 3 4 5 6 7 8 9 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)
最终的CMakeLists.txt应该像这样:
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 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
和listenner
,默认存放于~/catkin_ws/devel/lib/<package name>
。
2、解释 1 2 3 add_dependencies(talker beginner_tutorials_generate_messages_cpp )
add_dependencies声明某CMake target对另外的CMake target的依赖。
这确保了在我们使用某个包里定义的信息类型之前生成它们的头文件。在我们使用其他包的信息类型的时候,也要添加类似这样的依赖。例如再添加一条:
1 2 3 4 add_dependencies(talker beginner_tutorials_generate_messages_cpp roscpp_generate_messages_cpp )
3、构建 进入我们工作空间的顶层目录执行:
等待一会儿项目即可构建成功。
四、运行节点 首先要启动ROS:
然后进入工作目录将包导出到全局:
1 2 cd ~/catkin_ws source ./devel/setup.bash
运行talker节点:
1 rosrun beginner_tutorials talker
将看到类似于:
1 2 3 4 5 6 [ INFO] [1461049103.046522470] : hello world 0 [ INFO] [1461049103.146694944] : hello world 1 [ INFO] [1461049103.246582083] : hello world 2 [ INFO] [1461049103.346603146] : hello world 3 [ INFO] [1461049103.446525742] : hello world 4 [ INFO] [1461049103.546518768] : hello world 5
运行listener节点:
1 rosrun beginner_tutorials listener
将看到类似于:
1 2 3 4 5 6 [ INFO] [1461049221.914537834] : I heard: [hello world 3] [ INFO] [1461049222.014228659] : I heard: [hello world 4] [ INFO] [1461049222.114193464] : I heard: [hello world 5] [ INFO] [1461049222.214291045] : I heard: [hello world 6] [ INFO] [1461049222.314210593] : I heard: [hello world 7] [ INFO] [1461049222.414272158] : I heard: [hello world 8]
节点运行成功。
为正常使用来必力评论功能请激活JavaScript