目录
一、自定义话题信息
1.创建msg文件
2.在 package.xml中添加功能依赖包
3.在CMakeLists.txt添加编译选项
编辑
4.编译生成语言相关文件
二、应用此自定义话题
1.在话题功能包的src目录下创建person_publisher.cpp
2.创建person_subscriber.cpp
3.配置功能包的CMakeLists.txt中的编译规则,build栏中
编辑 4.编译
5.运行
实现内容:自定义话题内容,并且发布和订阅自定义话题中的信息
ROS发布话题与订阅话题https://download.csdn.net/download/m0_56451176/86398720?spm=1001.2014.3001.5503
cd ~/catkin_ws/src/learning_topic
mkdir msg
cd msg
touch Person.msg
gedit Person.msg
Person.msg内容如下:
string name #字符串对象name
uint8 sex
uint8 age
uint8 unknown = 0 #宏定义
uint8 male = 1
uint8 female = 2
注:定义姓名、性别、年龄
宏定义:未知、男、女
在话题功能包的package.xml中添加功能包的编译依赖和运行依赖
message_generation #动态产生message
message_runtime #运行依赖
find_package(....... message_generation)
add_message_files(FILES Person.msg) #将msg文件加入编译
generate_messages(DEPENDENCIES std_msgs)#依赖,在messages,services栏中加
catkin_package(...... message_runtime) #依赖若被注释要取消
cd ~/catkin_ws
catkin_make
cd ~/catkin_ws/src/learning_topic/src
touch person_publisher.cpp
gedit person_publisher.cpp
person_publisher.cpp内容如下
//该例程将发布/person_info话题,自定义消息类型learning_topic::Person
#include
#include "learning_topic/Person.h"
int main(int argc, char **argv){
// ROS节点初始化
ros::init(argc, argv, "person_publisher");
// 创建节点句柄
ros::NodeHandle n;
// 创建一个Publisher,发布名为/person_info的topic,消息类型为learning_topic::Person,队列长度10
ros::Publisher person_info_pub = n.advertise("/person_info", 10);
// 设置循环的频率
ros::Rate loop_rate(1);
int count = 0;
while (ros::ok()){
// 初始化learning_topic::Person类型的消息
learning_topic::Person person_msg; //定义Person对象
person_msg.name = "Tom";
person_msg.age = 18;
person_msg.sex = learning_topic::Person::male;
// 发布消息
person_info_pub.publish(person_msg);
ROS_INFO("Publish Person Info: name:%s age:%d sex:%d",
person_msg.name.c_str(), person_msg.age, person_msg.sex);
// 按照循环频率延时
loop_rate.sleep();
}
return 0;
}
cd ~/catkin_ws/src/learning_topic/src
touch person_subscriber.cpp
gedit person_subscriber.cpp
person_subscriber.cpp内容如下:
//该例程将订阅/person_info话题,自定义消息类型learning_topic::Person
#include
#include "learning_topic/Person.h"
// 接收到订阅的消息后,会进入消息回调函数
void personInfoCallback(const learning_topic::Person::ConstPtr& msg){
// 将接收到的消息打印出来
ROS_INFO("Subcribe Person Info: name:%s age:%d sex:%d", msg->name.c_str(), msg->age, msg->sex);
}
int main(int argc, char **argv){
// 初始化ROS节点
ros::init(argc, argv, "person_subscriber");
// 创建节点句柄
ros::NodeHandle n;
// 创建一个Subscriber,订阅名为/person_info的topic,注册回调函数personInfoCallback
ros::Subscriber person_info_sub = n.subscribe("/person_info", 10, personInfoCallback);
// 循环等待回调函数
ros::spin();
return 0;
}
add_executable(person_publisher src/person_publisher.cpp) #运行
target_link_libraries(person_publisher ${catkin_LIBRARIES}) #库连接
add_dependencies(person_publisher ${PROJECT_NAME}_generate_messages_cpp) #连接依赖,与自定义消息连接
add_executable(person_subscriber src/person_subscriber.cpp)
target_link_libraries(person_subscriber ${catkin_LIBRARIES})
add_dependencies(person_subscriber ${PROJECT_NAME}_generate_messages_cpp)
cd ~/catkin_ws
catkin_make
source ~/catkin_ws/devel/setup.bash
roscore
source ~/catkin_ws/devel/setup.bash
rosrun learning_topic person_publisher
source ~/catkin_ws/devel/setup.bash
rosrun learning_topic person_subscriber
注:1.订阅者可以会有一点延迟
2.当发布者和订阅者已经建立了连接,ros master就不再必要,ros master只负责为两者建立连接,若有其他连接才再需要
详细内容请看:ROS学习(四)发布者与订阅者