当前位置:网站首页>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
边栏推荐
- Detailed explanation of Flink checkpoint specific operation process and summary of error reporting and debugging methods
- GC
- 剑指 Offer II 091. 粉刷房子 : 状态机 DP 运用题
- Flink partition policy
- Subclass a inherits from parent class B, a a = new a(); Then the execution sequence of the constructor of parent class B, the static code block of parent class B, the non static code block of parent c
- cnds
- Research on parallel computing architecture of meteorological early warning based on supercomputing platform
- ThingsPanel 發布物聯網手機客戶端(多圖)
- Hangzhou / Beijing neitui Ali Dharma academy recruits academic interns in visual generation (talent plan)
- Comparator (for arrays.sort)
猜你喜欢

The service layer reports an error. The XXX method invalid bound statement (not found) cannot be found

基於Minifilter框架的雙緩沖透明加解密驅動 課程論文+項目源碼

SQL injection vulnerability (bypass)

Sentinel integrated Nacos data source

CFCA安心签接入

Leetcode 1249. Remove invalid brackets (awesome, finally made)

Niuke.com: Candy distribution

TCP如何处理三次握手和四次挥手期间的异常

Idea local launch Flink task

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
随机推荐
客户经理的开户二维码开户买股票安全吗?有谁知道啊
Detailed explanation of Spark's support source code for Yan priority
杭州/北京内推 | 阿里达摩院招聘视觉生成方向学术实习生(人才计划)
Multiple clicks of the button result in results
翌圣生物冲刺科创板:25%收入来自新冠产品销售 拟募资11亿
Caused by: org. xml. sax. SAXParseException; lineNumber: 1; columnNumber: 10; Processing matching '[xx][mm][ll]' is not allowed
Deeply understand Flink SQL execution process based on flink1.12
Eureka accesses the console and reports an error: whitelabel error page
Geographic location system based on openstreetmap+postgis paper documents + reference papers + project source code and database files
Idea local launch Flink task
数据库系列:MySQL索引优化总结(综合版)
Spark tuning tool -- detailed explanation of sparklens
Idea uses the fast request interface for debugging
Niuke.com: host scheduling
Double tampon transparent cryptage et décryptage basé sur le cadre minifilter
SQL注入漏洞(绕过篇)
4 life distributions
時創能源沖刺科創板:擬募資11億 年營收7億淨利反降36%
Explain websocket protocol in detail
Bayes