【ROS】消息通讯——发布者/订阅者的来龙去脉

参考:https://www.cnblogs.com/fuzhuoxin/p/12578343.html

1. 创建工作空间和工作环境

mkdir -p ~/learning_ws/src  //工作环境及功能包建立、编译
cd ~/learning_ws/
catkin_make
source devel/setup.bash    //设置环境变量
echo $ROS_PACKAGE_PATH
cd ~/learning_ws/src
catkin_create_pkg ros_tutorials_topic message_generation std_msgs roscpp //创建功能包和依赖

【ROS】消息通讯——发布者/订阅者的来龙去脉_第1张图片

2. 修改配置文件package.xml


<package format="2">
<name>ros_tutorials_topicname>
<version>0.1.0version>
<description>The ros_tutorials_topic packagedescription>
<maintainer email="[email protected]">Fuzhuoxinmaintainer>  
<license>Apache License 2.0license>  
<author email="[email protected]">Fuzhuoxinauthor>  
<buildtool_depend>catkinbuildtool_depend> 
<build_depend>message_generationbuild_depend> 
<build_depend>roscppbuild_depend> 
<build_depend>std_msgsbuild_depend> 
<build_export_depend>roscppbuild_export_depend> 
<build_export_depend>std_msgsbuild_export_depend> 
<exec_depend>roscppexec_depend> 
<exec_depend>std_msgsexec_depend> 
<export>export> 
package>
//注释解释内容如下,在工程中不可直接复制,用上面无注释版 1 
2 
3 
4 <package format="2">
5     
6   <name>ros_tutorials_topicname>
7     
8   <version>0.0.0version>
9     
10   <description>The ros_tutorials_topic packagedescription>
11     
12   <maintainer email="[email protected]">tianbotmaintainer>
13     
14   <license>Apache License 2.0license>
15   
16   
17   
18   
19     
20   <buildtool_depend>catkinbuildtool_depend>
21     
22   <build_depend>message_generationbuild_depend>
23   <build_depend>roscppbuild_depend>
24   <build_depend>std_msgsbuild_depend>
25   <build_export_depend>roscppbuild_export_depend>
26   <build_export_depend>std_msgsbuild_export_depend>
27   <exec_depend>roscppexec_depend>
28   <exec_depend>std_msgsexec_depend>
29   <export>export>
30 package>

3. 配置CMakeLists文件:

#版本要求
cmake_minimum_required(VERSION 2.8.3)

#工程名字
project(ros_tutorials_topic)

#添加依赖包message_generation  roscpp  std_msgs
find_package(catkin REQUIRED COMPONENTS
  message_generation
  roscpp
  std_msgs
)

#声明msg文件夹下的消息文件MsgTutorial.msg
add_message_files(
   FILES
   MsgTutorial.msg
 )

#设置依赖性消息
generate_messages(
   DEPENDENCIES
   std_msgs
 )

#catkin功能包选项,描述库、catkin构建依赖项和系统依赖的功能包
catkin_package(
  LIBRARIES ros_tutorials_topic
  CATKIN_DEPENDS roscpp std_msgs
)

#设置包含目录
include_directories(
  ${catkin_INCLUDE_DIRS}
)

#配置可执行文件、目标链接库和其他依赖性
add_executable(topic_publisher src/topic_publisher.cpp)
add_dependencies(topic_publisher ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(topic_publisher ${catkin_LIBRARIES})

#配置可执行文件、目标链接库和其他依赖性
add_executable(topic_subscriber src/topic_subscriber.cpp)
add_dependencies(topic_subscriber ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(topic_subscriber ${catkin_LIBRARIES})
  • CMakeLists.txt中添加了add_message_files( FILES MsgTutorial.msg),因此要包含MsgTutorial.msg文件:
    【ROS】消息通讯——发布者/订阅者的来龙去脉_第2张图片
    内容为:
1 time stamp
2 int32 data
3 string speak

4. 创建发布者节点:

对应CMakeLists.txt中添加的add_executable(topic_publisher src/topic_publisher.cpp)
【ROS】消息通讯——发布者/订阅者的来龙去脉_第3张图片
内容为:

#include 
#include  //MsgTutorial消息头文件(构建后自动生成)


int main(int argc,char **argv)
{
	ros::init(argc,argv,"topic_publisher"); //初始化发布者节点名称
	ros::NodeHandle nh;//声明节点句柄与ROS系统进行通讯
	//声明发布者,创建一个使用ros_tutorials_topic功能包MsgTutorial消息文件的发布者
	//ros_tutorial_pub,话题名称是ros_tutorial_msg,消息发布者队列设置为100
	ros::Publisher ros_tutorial_pub = nh.advertise<ros_tutorials_topic::MsgTutorial>("/ros_tutorial_msg", 100);
	ros::Rate loop_rate(10);//设定循环周期10HZ,0.1秒
	ros_tutorials_topic::MsgTutorial msg;//声明一个消息
	int count = 0;
	std::stringstream talk;
	while(ros::ok())
	{
		msg.stamp = ros::Time::now();//给MsgTutorial.msg文件中的消息变量赋值
		msg.data = count;
		talk << "How are you"<< count;
		msg.speak = talk.str();
		ROS_INFO("send msg = %d",msg.stamp.sec);//显示消息
		ROS_INFO("send msg = %d",msg.stamp.nsec);
		ROS_INFO("send msg = %d",msg.data);
		ROS_INFO("%s",msg.speak.c_str());
		ros_tutorial_pub.publish(msg);//发布显示的消息
		ros::spinOnce();//循环等待订阅节点的所有回调函数
		loop_rate.sleep();    //按设定值循环
		++count;
	}
	return 0;
}

5. 创建订阅者节点:

对应CMakeLists.txt中添加的add_executable(topic_subscriber src/topic_subscriber.cpp)
【ROS】消息通讯——发布者/订阅者的来龙去脉_第4张图片
内容为:

#include 
#include 

//回调函数
void msgCallback(const ros_tutorials_topic::MsgTutorial::ConstPtr& msg)
{
	ROS_INFO("recieve msg = %d",msg->stamp.sec);
	ROS_INFO("recieve msg = %d",msg->stamp.nsec);
	ROS_INFO("recieve msg = %d",msg->data);
	ROS_INFO("I receive you say:[%s]",msg->speak.c_str());
}

int main(int argc,char **argv)
{
	ros::init(argc,argv,"topic_subscriber");//初始化订阅者节点
	ros::NodeHandle nh;
	//声明订阅者,创建一个使用ros_tutorials_topic功能包,MsgTutorial消息文件的订阅者ros_tutorial_sub,订阅的话题名称是ros_tutorial_msg,消息接收者队列设置为100
	ros::Subscriber ros_tutorial_sub = nh.subscribe<ros_tutorials_topic::MsgTutorial>("/ros_tutorial_msg", 100, msgCallback);
	ros::spin();//调用后台,等待接收消息 
	return 0; 
}

运行效果:
【ROS】消息通讯——发布者/订阅者的来龙去脉_第5张图片
其中:

ros::init(argc,argv,"topic_publisher"); //初始化发布者节点名称
ros::init(argc,argv,"topic_subscriber");//初始化订阅者节点

话题名称是:/ros_tutorial_msg

节点关系图为:
在这里插入图片描述
命令运行:rostopic echo /ros_tutorial_msg,查看话题发布的消息,可以看到消息是连续发布的,只要建立了联系,不会中断,一直发布:
【ROS】消息通讯——发布者/订阅者的来龙去脉_第6张图片
rqt插件同样可以监控话题的消息内容:
【ROS】消息通讯——发布者/订阅者的来龙去脉_第7张图片

你可能感兴趣的:(ROS)