- 创建工作空间和工作环境
1 mkdir -p ~/learning_ws/src //工作环境及功能包建立、编译 2 cd ~/learning_ws/ 3 catkin_make 4 source devel/setup.bash //设置环境变量 5 echo $ROS_PACKAGE_PATH 6 cd ~/learning_ws/src
7 catkin_create_pkg ros_tutorials_topic message_generation std_msgs roscpp //创建功能包和依赖
- 修改配置文件package.xml
1 <?xml version="1.0"?> 2 <package format="2"> 3 <name>ros_tutorials_topic</name> 4 <version>0.1.0</version> 5 <description>The ros_tutorials_topic package</description> 6 <maintainer email="tianbot@todo.todo">Fuzhuoxin</maintainer>
7 <license>Apache License 2.0</license>
8 <author email="tianbot@todo.todo">Fuzhuoxin</author>
9 <buildtool_depend>catkin</buildtool_depend>
10 <build_depend>message_generation</build_depend>
11 <build_depend>roscpp</build_depend>
12 <build_depend>std_msgs</build_depend>
13 <build_export_depend>roscpp</build_export_depend>
14 <build_export_depend>std_msgs</build_export_depend>
15 <exec_depend>roscpp</exec_depend>
16 <exec_depend>std_msgs</exec_depend>
17 <export></export>
18 </package>
//注释解释内容如下,在工程中不可直接复制,用上面无注释版
1 <!-- xml版本声明 --> 2 <?xml version="1.0"?> 3 <!-- 功能包标签 --> 4 <package format="2"> 5 <!-- 功能包名称 --> 6 <name>ros_tutorials_topic</name> 7 <!-- 功能包版本 --> 8 <version>0.0.0</version> 9 <!-- 功能包简要说明 --> 10 <description>The ros_tutorials_topic package</description> 11 <!-- 功能包管理者信息 --> 12 <maintainer email="tianbot@todo.todo">tianbot</maintainer> 13 <!-- 记录版本许可 --> 14 <license>Apache License 2.0</license> 15 <!-- 功能包说明的详细地址、网页、博客等 --> 16 <!-- <url type="website">https://www.cnblogs.com/fuzhuoxin/</url> --> 17 <!-- 开发人员信息 --> 18 <!-- <author email="jane.doe@example.com">Jane Doe</author> --> 19 <!-- 构建系统依赖关系,正在使用catkin构建 --> 20 <buildtool_depend>catkin</buildtool_depend> 21 <!-- 编写功能包时所依赖的功能包名称 --> 22 <build_depend>message_generation</build_depend> 23 <build_depend>roscpp</build_depend> 24 <build_depend>std_msgs</build_depend> 25 <build_export_depend>roscpp</build_export_depend> 26 <build_export_depend>std_msgs</build_export_depend> 27 <exec_depend>roscpp</exec_depend> 28 <exec_depend>std_msgs</exec_depend> 29 <export></export> 30 </package>
配置CMake文件:
1 #版本要求 2 cmake_minimum_required(VERSION 2.8.3) 3 #工程名字 4 project(ros_tutorials_topic) 5 #添加依赖包message_generation roscpp std_msgs 6 find_package(catkin REQUIRED COMPONENTS 7 message_generation 8 roscpp 9 std_msgs 10 ) 11 #声明msg文件夹下的消息文件MsgTutorial.msg 12 add_message_files( 13 FILES 14 MsgTutorial.msg 15 ) 16 #设置依赖性消息 17 generate_messages( 18 DEPENDENCIES 19 std_msgs 20 ) 21 #catkin功能包选项,描述库、catkin构建依赖项和系统依赖的功能包 22 catkin_package( 23 24 LIBRARIES ros_tutorials_topic 25 CATKIN_DEPENDS roscpp std_msgs 26 ) 27 #设置包含目录 28 include_directories( 29 ${catkin_INCLUDE_DIRS} 30 ) 31 #配置可执行文件、目标链接库和其他依赖性 32 add_executable(topic_publisher src/topic_publisher.cpp) 33 add_dependencies(topic_publisher ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS}) 34 target_link_libraries(topic_publisher ${catkin_LIBRARIES}) 35 36 #配置可执行文件、目标链接库和其他依赖性 37 add_executable(topic_subscriber src/topic_subscriber.cpp) 38 add_dependencies(topic_subscriber ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS}) 39 target_link_libraries(topic_subscriber ${catkin_LIBRARIES})
- 在CMakeLists.txt中添加了add_message_files( FILES MsgTutorial.msg)因此要包含MsgTutorial.msg文件
内容为
1 time stamp 2 int32 data 3 string speak
- 创建发布者节点:对应CMakeLists.txt中添加的add_executable(topic_publisher src/topic_publisher.cpp)
内容为:
1 #include <ros/ros.h> 2 #include <ros_tutorials_topic/MsgTutorial.h> //MsgTutorial消息头文件(构建后自动生成) 3 4 5 int main(int argc,char **argv) 6 { 7 ros::init(argc,argv,"topic_publisher"); //初始化发布者节点名称 8 ros::NodeHandle nh;//声明节点句柄与ROS系统进行通讯 9 //声明发布者,创建一个使用ros_tutorials_topic功能包MsgTutorial消息文件的发布者 10 //ros_tutorial_pub,话题名称是ros_tutorial_msg,消息发布者队列设置为100 11 ros::Publisher ros_tutorial_pub = nh.advertise<ros_tutorials_topic::MsgTutorial>("ros_tutorial_msg", 100); 12 ros::Rate loop_rate(10);//设定循环周期10HZ,0.1秒 13 ros_tutorials_topic::MsgTutorial msg;//声明一个消息 14 int count = 0; 15 std::stringstream talk; 16 while(ros::ok()) 17 { 18 msg.stamp = ros::Time::now();//给MsgTutorial.msg文件中的消息变量赋值 19 msg.data = count; 20 talk << "How are you"<< count; 21 msg.speak = talk.str(); 22 ROS_INFO("send msg = %d",msg.stamp.sec);//显示消息 23 ROS_INFO("send msg = %d",msg.stamp.nsec); 24 ROS_INFO("send msg = %d",msg.data); 25 ROS_INFO("%s",msg.speak.c_str()); 26 ros_tutorial_pub.publish(msg);//发布显示的消息 27 ros::spinOnce();//循环等待订阅节点的所有回调函数 28 loop_rate.sleep(); //按设定值循环 29 ++count; 30 } 31 return 0; 32 }
- 创建发布者节点:对应CMakeLists.txt中添加的add_executable(topic_publisher src/topic_publisher.cpp)
内容为:
1 #include <ros/ros.h> 2 #include <ros_tutorials_topic/MsgTutorial.h> 3 4 //回调函数 5 void msgCallback(const ros_tutorials_topic::MsgTutorial::ConstPtr& msg) 6 { 7 ROS_INFO("recieve msg = %d",msg->stamp.sec); 8 ROS_INFO("recieve msg = %d",msg->stamp.nsec); 9 ROS_INFO("recieve msg = %d",msg->data); 10 ROS_INFO("I receive you say:[%s]",msg->speak.c_str()); 11 } 12 int main(int argc,char **argv) 13 { 14 ros::init(argc,argv,"topic_subscriber");//初始化订阅者节点 15 ros::NodeHandle nh; 16 //声明订阅者,创建一个使用ros_tutorials_topic功能包MsgTutorial消息文件的订阅者
17 //ros_tutorial_sub,订阅的话题名称是ros_tutorial_msg,消息接收者队列设置为100
18 ros::Subscriber ros_tutorial_sub = nh.subscribe<ros_tutorials_topic::MsgTutorial>("ros_tutorial_msg", 100, msgCallback);
19 ros::spin();//调用后台,等待接收消息
20 return 0; 21 }
运行效果:
命令查看节点关系如图所示:与程序的对应关系为
ros::init(argc,argv,"topic_publisher"); //初始化发布者节点名称
话题名称是ros_tutorial_msg
ros::init(argc,argv,"topic_subscriber");//初始化订阅者节点
命令运行rostopic echo /ros_tutorial_msg,查看话题发布的消息,可以看到消息是连续发布的,只要建立了联系,不会中断,一直发布。
用rqt插件同样可以监控话题的消息内容