这里写目录标题
一、前言
话题模型如下:
只不过本节所要发送的消息是一个我们自定义的类型,即下图的Person类型。而不是前面章节的Twist类型了。既然我们想发送自定义的类型,那么就需要我们进行一番操作了,本篇笔记就讲这个。
二、自定义话题消息
2.1 思路如下:
2.2具体实施
第一步:定义msg文件
在catkin_ws/src/learning_topic下建立msg文件夹,并建立Person.msg文件。
Person.msg中加入如下代码:
string name
uint8 sex
uint8 age
uint8 unknown = 0
uint8 male = 1
uint8 female = 2
第二步:在package.xml中添加功能包依赖:
第一个语句是编译依赖,第二个语句是运行依赖。
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
操作结果如下:
第三步:在CMakeLists.txt添加编译选项
首先在find_package()中加入message_generation功能包,
然后加入以下代码,其中第一个语句是说编译Person.msg,第二个语句Person.msg是说依赖于std_msgs
add_message_files(FILES Person.msg)
generate_messages(DEPENDENCIES std_msgs)
操作结果如下:
然后,在catkin_package()中加入message_runtime。操作结果如下:
第四步:编译
$ cd ~/catkin_ws
$ catkin_make
经过编译,我们会发现在以下路径中生成一个Person.h文件。
三、创建发布者代码
我们只需要把古月的person_publisher.cpp和person_subscriber.cpp复制到如下路径即可。
3.1cpp版本
person_publisher.cpp代码如下:
/**
* 该例程将发布/person_info话题,自定义消息类型learning_topic::Person
*/
#include <ros/ros.h>
#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<learning_topic::Person>("/person_info", 10);
// 设置循环的频率
ros::Rate loop_rate(1);
int count = 0;
while (ros::ok())
{
// 初始化learning_topic::Person类型的消息
learning_topic::Person person_msg;
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;
}
person_subscriber.cpp代码如下:
/**
* 该例程将订阅/person_info话题,自定义消息类型learning_topic::Person
*/
#include <ros/ros.h>
#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;
}
3.2python版本
person_publisher.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 该例程将发布/person_info话题,自定义消息类型learning_topic::Person
import rospy
from learning_topic.msg import Person
def velocity_publisher():
# ROS节点初始化
rospy.init_node('person_publisher', anonymous=True)
# 创建一个Publisher,发布名为/person_info的topic,消息类型为learning_topic::Person,队列长度10
person_info_pub = rospy.Publisher('/person_info', Person, queue_size=10)
#设置循环的频率
rate = rospy.Rate(10)
while not rospy.is_shutdown():
# 初始化learning_topic::Person类型的消息
person_msg = Person()
person_msg.name = "Tom";
person_msg.age = 18;
person_msg.sex = Person.male;
# 发布消息
person_info_pub.publish(person_msg)
rospy.loginfo("Publsh person message[%s, %d, %d]",
person_msg.name, person_msg.age, person_msg.sex)
# 按照循环频率延时
rate.sleep()
if __name__ == '__main__':
try:
velocity_publisher()
except rospy.ROSInterruptException:
pass
person_subscriber.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 该例程将订阅/person_info话题,自定义消息类型learning_topic::Person
import rospy
from learning_topic.msg import Person
def personInfoCallback(msg):
rospy.loginfo("Subcribe Person Info: name:%s age:%d sex:%d",
msg.name, msg.age, msg.sex)
def person_subscriber():
# ROS节点初始化
rospy.init_node('person_subscriber', anonymous=True)
# 创建一个Subscriber,订阅名为/person_info的topic,注册回调函数personInfoCallback
rospy.Subscriber("/person_info", Person, personInfoCallback)
# 循环等待回调函数
rospy.spin()
if __name__ == '__main__':
person_subscriber()
四、配置CMakeLists.txt中的编译规则
在CMakeLists.txt加入如下代码:
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 devel/setup.bash
$ roscore
$ rosrun learning_topic person_subscriber
$ rosrun learning_topic person_publisher
效果如下: