当前位置:网站首页>ROS 笔记(06)— 话题消息的定义和使用
ROS 笔记(06)— 话题消息的定义和使用
2022-06-25 11:44:00 【wohu1104】
ROS 中通过 std_msgs 封装了一些原生的数据类型,比如: String、Int32、Int64、Char、Bool、Empty… 但是,这些数据一般只包含一个 data 字段,结构的单一意味着功能上的局限性,当传输一些复杂的数据,比如:激光雷达的信息… std_msgs 由于描述性较差而显得力不从心,这种场景下可以使用自定义的消息类型。
msgs 只是简单的文本文件,每行具有字段类型和字段名称,可以使用的字段类型有:
- int8, int16, int32, int64 (或者无符号类型: uint*)
- float32, float64
- string
- time, duration
- other msg files
- variable-length array[] and fixed-length array[C]
ROS 中还有一种特殊类型:Header ,标头包含时间戳和 ROS 中常用的坐标帧信息。会经常看到msg 文件的第一行具有 Header 标头。
1. 话题模型
我们要实现的话题模型如下:
其中 Message 中的 Person 为我们自定义的消息格式,该消息在订阅者和发布者之间传递。
2. 自定义消息实现
2.1 定义 msg 文件
在功能包 topic_demo 目录下创建一个 msg 文件夹,在该文件夹中创建一个 Person.msg 文件,如下图所示:
Person.msg 中有我们自定义的消息格式
string name
uint8 age
uint8 gender
uint8 unknown = 0
uint8 male = 1
uint8 female = 2
2.2 添加功能包依赖
在 package.xml 中添加以下 exec_depend 运行依赖:
<exec_depend>message_generation</exec_depend>
<exec_depend>message_runtime</exec_depend>
2.3 添加编译选项

find_package(catkin REQUIRED COMPONENTS
geometry_msgs
rospy
std_msgs
turtlesim
message_generation # 新增内容
add_message_files(
FILES
Person.msg
)
generate_messages(
DEPENDENCIES
std_msgs
)
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES topic_demo
CATKIN_DEPENDS geometry_msgs rospy std_msgs turtlesim message_runtime
# DEPENDS system_lib
)
2.4 编译生成语言相关的文件
到项目根目录下执行 catkin_make 命令
$ catkin_make
Base path: /home/wohu/project/ros/ros_demo
Source space: /home/wohu/project/ros/ros_demo/src
Build space: /home/wohu/project/ros/ros_demo/build
Devel space: /home/wohu/project/ros/ros_demo/devel
Install space: /home/wohu/project/ros/ros_demo/install
####
#### Running command: "make cmake_check_build_system" in "/home/wohu/project/ros/ros_demo/build"
####
####
#### Running command: "make -j12 -l12" in "/home/wohu/project/ros/ros_demo/build"
####
[ 0%] Built target std_msgs_generate_messages_py
[ 0%] Built target std_msgs_generate_messages_eus
[ 0%] Built target std_msgs_generate_messages_lisp
[ 0%] Built target std_msgs_generate_messages_nodejs
[ 0%] Built target std_msgs_generate_messages_cpp
[ 0%] Built target _topic_demo_generate_messages_check_deps_Person
[ 57%] Built target topic_demo_generate_messages_py
[ 57%] Built target topic_demo_generate_messages_eus
[ 71%] Built target topic_demo_generate_messages_cpp
[ 85%] Built target topic_demo_generate_messages_nodejs
[100%] Built target topic_demo_generate_messages_lisp
[100%] Built target topic_demo_generate_messages
可以看到会生成各种语言的代码文件。其中 Python 相关的代码文件在
/devel/lib/python2.7/dist-packages
目录下
2.5 运行代码
发布者代码 person_publisher.py :
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 该例程将发布/person_info话题,自定义消息类型learning_topic::Person
import rospy
from topic_demo.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.gender = Person.male;
# 发布消息
person_info_pub.publish(person_msg)
rospy.loginfo("Publsh person message[%s, %d, %d]",
person_msg.name, person_msg.age, person_msg.gender)
# 按照循环频率延时
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 topic_demo.msg import Person
def personInfoCallback(msg):
rospy.loginfo("Subcribe Person Info: name:%s age:%d gender:%d",
msg.name, msg.age, msg.gender)
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()
运行代码命令:
$ cd ~/catkin_ws
$ catkin_make
$ source ./devel/setup.bash
$ rescore
$ rosrun topic_demo person_subscriber.py
$ rosrun topic_demo person_publisher.py
边栏推荐
- Redis6笔记02 配置文件,发布和订阅,新数据类型,Jedis操作
- Arrays. asList()
- Double buffer transparent encryption and decryption driven course paper + project source code based on minifilter framework
- Thirty lines of code prevent VFP forms from running repeatedly, and the function supports parameter transfer
- Sword finger offer II 091 Painting house: application of state machine DP
- Vulnérabilité à l'injection SQL (contournement)
- Arrays.asList()
- Spark runs wordcount (case 1)
- Flink partition policy
- JS indexof() always returns -1
猜你喜欢

基于OpenStreetMap+PostGIS的地理位置系统 论文文档+参考论文文献+项目源码及数据库文件

Develop two modes of BS mode verification code with VFP to make your website more secure

Double buffer transparent encryption and decryption driven course paper + project source code based on minifilter framework

Why distributed IDS? What are the distributed ID generation schemes?

Redis6笔记02 配置文件,发布和订阅,新数据类型,Jedis操作

C disk uses 100% cleaning method

Research on parallel computing architecture of meteorological early warning based on supercomputing platform

SQL injection vulnerability (bypass)

redis的dict的扩容机制(rehash)

SQL注入漏洞(繞過篇)
随机推荐
数据库系列:MySQL索引优化总结(综合版)
Flink batch key points (personal translation)
Shichuang energy rushes to the scientific innovation board: it plans to raise 1.1 billion yuan, with an annual revenue of 700million yuan and a 36% decrease in net profit
揭秘GaussDB(for Redis):全面对比Codis
2022 PMP project management examination agile knowledge points (2)
.Net Core 中使用工厂模式
云原生数据湖以存储、计算、数据管理等能力通过信通院评测认证
现在网上炒股开户身份证信息安全吗?
交易期货沪镍产品网上怎么开户
Deeply understand Flink SQL execution process based on flink1.12
Comment TCP gère - t - il les exceptions lors de trois poignées de main et de quatre vagues?
Course paper + code and executable EXE file of library information management system based on C language
Specific meanings of node and edge in Flink graph
Semaphore source code analysis
Develop two modes of BS mode verification code with VFP to make your website more secure
Eureka accesses the console and reports an error: whitelabel error page
翌圣生物冲刺科创板:25%收入来自新冠产品销售 拟募资11亿
Spark tuning tool -- detailed explanation of sparklens
建造者模式
Flink deeply understands the graph generation process (source code interpretation)