本案例在之前的基础上进行扩展,传递一个自定义的复数类消息。


本系列用时7天,博主也是从零开始,尽力去写的,如果发现了错误一定要私信告诉我呀。这么努力的博主,关注一下吧。

作者:杨丝儿
座右铭:始于兴趣,源于热爱,成于投入。
介绍:爱丁堡大学 人工智能专业。技术兴趣点集中在机器人、人工智能可解释性、数学、物理等等。
聊天吹水QQ群:兔叽的魔术工房 (942848525)
个人博客discover304.top
个人B站账号杨丝儿今天也在科学修仙(UP主跨站求个三连加关注)


✨注意


✨定义消息

我们设计一个传递复数类型,复数类。

在包下新建msg文件夹,并在文件夹内新建Complex.msg文件

1
2
mkdir msg
touch Complex.msg

编辑Complex.msg文件

1
2
int32 real_part
int32 imaginary_part

✨添加依赖

编辑包内的package.xml文件添加依赖

1
2
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>

小贴士:package.xml内已经有大量的待选依赖,直接去掉注释就好
2021-12-30 10-19-27 的屏幕截图


✨添加配置信息

在包内CMakeLists.txt中修改

1
2
3
4
5
6
7
8
9
find_package(catkin REQUIRED COMPONENTS
rospy
std_msgs # 新增行
message_generation # 新增行
)

catkin_package(
CATKIN_DEPENDS message_runtime # 新增行
)

将下面面代码去掉注释

1
2
3
4
5
6
7
8
9
add_message_files(
FILEs
Complex.msg # 修改为类型文件名
)

generate_messages(
DEPENDENCIES
std_msgs # Or other packages containing msgs
)

小贴士CMakeLists.txt也有大量的注释以及说明内容,可以参考。


✨重编译

在工作区执行下面的命令。

1
catkin_make

如果出现红色报错,需要回溯,看看哪里出现了问题。


✨使用新类型

修改包内topic_publisher.py代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python3

import rospy
# from std_msgs.msg import Int32
from communicate_bot.msg import Complex

class Publisher():
# count = 0
count = Complex()

def Publisher(self):
# pass
self.count.real_part = 0
self.count.imaginary_part = 0

def publish(self):
rospy.init_node('topic_publisher')
# pub = rospy.Publisher('counter', Int32, queue_size=10)
pub = rospy.Publisher('counter', Complex, queue_size=10)
rate = rospy.Rate(2)
while not rospy.is_shutdown():
pub.publish(self.count)
# self.count += 1
self.count.real_part += 1
self.count.imaginary_part += 1
# print(self.count)
print(f"{self.count.real_part}+{self.count.imaginary_part}i")
rate.sleep()


if __name__ == '__main__':
publisher = Publisher()
publisher.publish()

修改包内topic_subscriber.py代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/env python3

import rospy
# from std_msgs.msg import Int32
from communicate_bot.msg import Complex

class Subscriber():
def Subscriber():
pass

def listen(self):
rospy.init_node('topic_subscriber')
# sub = rospy.Subscriber('counter', Int32, lambda msg : print(msg.data))
sub = rospy.Subscriber('counter', Complex, lambda msg : print(f"{msg.real_part}+{msg.imaginary_part}i"))
rospy.spin()


if __name__ == "__main__":
subscriber = Subscriber()
subscriber.listen()

小贴士:自定义的类型有的时候会很大,所以我们可以采用锁存话题(latched)的形式。上传一个消息后,会一直保留到下一个新的同类消息上传。

1
pub = rospy.Publisher(<消息名>, <消息类型>, latched=True)

✨运行效果


✨附加内容

CMakeLists.txt中的说明/教程:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
################################################
## Declare ROS messages, services and actions ##
################################################

## To declare and build messages, services or actions from within this
## package, follow these steps:
## * Let MSG_DEP_SET be the set of packages whose message types you use in
## your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).
## * In the file package.xml:
## * add a build_depend tag for "message_generation"
## * add a build_depend and a exec_depend tag for each package in MSG_DEP_SET
## * If MSG_DEP_SET isn't empty the following dependency has been pulled in
## but can be declared for certainty nonetheless:
## * add a exec_depend tag for "message_runtime"
## * In this file (CMakeLists.txt):
## * add "message_generation" and every package in MSG_DEP_SET to
## find_package(catkin REQUIRED COMPONENTS ...)
## * add "message_runtime" and every package in MSG_DEP_SET to
## catkin_package(CATKIN_DEPENDS ...)
## * uncomment the add_*_files sections below as needed
## and list every .msg/.srv/.action file to be processed
## * uncomment the generate_messages entry below
## * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)

下一篇会涉及运行阶段的重命名,链接在这里:TODO正在施工中