ROS2- 构建-topic发布订阅-07
1.订阅发布模型
✅ 1 对 1 单一发布者 → 单一订阅者
✅ 1 对 N 一发布,多订阅
✅ N 对 1 多发布,单订阅
✅ N 对 N 多发布,多订阅
2.消息
为了方便发送者和接收者进行数据的交换,ROS2帮我们在数据传递时做好了消息的序列化和反序列化。
ROS2的消息序列化与反序列化通信是可以做到跨编程语言、跨平台和跨设备之间。
当定义好消息接口后,ROS2会根据消息接口内容生成不同语言的接口类,在不同编程语言中调用相同的类,可实现无感的消息序列化和反序列化。
同一个话题,所有的发布者和接收者必须使用相同消息接口。
3. rqt_graph
rqt_graph 可以实时可视化节点与话题连接,是调试话题通信首选工具
依次使用三个窗口打开:
ros2 run demo_nodes_py listener
ros2 run demo_nodes_cpp talker
rqt_graph
4. ros2 topic
ros2 topic list/echo/info 可用于查看、调试话题内容
ros2 topic list
ros2 topic list -t # -t 是消息的类型
ros2 topic echo /chatter # 打印实时话题内容
ros2 topic info /chatter # 查看主题信息
ros2 interface show std_msgs/msg/String # 查看消息类型
ros2 topic pub /chatter std_msgs/msg/String 'data: "123"' # 手动发布命令
root@ubuntu-16:~/stephen/chapt2_ws# ros2 run demo_nodes_py listener
[INFO] [1749460845.622703658] [listener]: I heard: [Hello World: 1]
[INFO] [1749460846.585035350] [listener]: I heard: [Hello World: 2]
[INFO] [1749460847.585155507] [listener]: I heard: [Hello World: 3]
[INFO] [1749460848.585182717] [listener]: I heard: [Hello World: 4]
[INFO] [1749460849.584898102] [listener]: I heard: [Hello World: 5]
[INFO] [1749460850.585010729] [listener]: I heard: [Hello World: 6]
[INFO] [1749460851.585083046] [listener]: I heard: [Hello World: 7]
[INFO] [1749460852.585105640] [listener]: I heard: [Hello World: 8]
[INFO] [1749460845.583254254] [talker]: Publishing: 'Hello World: 1'
[INFO] [1749460846.583215888] [talker]: Publishing: 'Hello World: 2'
[INFO] [1749460847.583208081] [talker]: Publishing: 'Hello World: 3'
[INFO] [1749460848.583186474] [talker]: Publishing: 'Hello World: 4'
[INFO] [1749460849.583175535] [talker]: Publishing: 'Hello World: 5'
[INFO] [1749460850.583180991] [talker]: Publishing: 'Hello World: 6'
[INFO] [1749460851.583178698] [talker]: Publishing: 'Hello World: 7'
[INFO] [1749460852.583163828] [talker]: Publishing: 'Hello World: 8'
[INFO] [1749460853.583111802] [talker]: Publishing: 'Hello World: 9'
root@ubuntu-16:/embodied_ai_framework# ros2 topic list
/chatter
/parameter_events
/rosout
root@ubuntu-16:/embodied_ai_framework#
root@ubuntu-16:/embodied_ai_framework#
root@ubuntu-16:/embodied_ai_framework#
root@ubuntu-16:/embodied_ai_framework# rqt_graph
could not connect to display
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
Available platform plugins are: eglfs, linuxfb, minimal, minimalegl, offscreen, vnc, xcb.
root@ubuntu-16:/embodied_ai_framework# ros2 topic list -t
/chatter [std_msgs/msg/String]
/parameter_events [rcl_interfaces/msg/ParameterEvent]
/rosout [rcl_interfaces/msg/Log]
root@ubuntu-16:/embodied_ai_framework# ros2 topic echo /chatter
data: 'Hello World: 91'
---
data: 'Hello World: 92'
---
data: 'Hello World: 93'
---
data: 'Hello World: 94'
---
data: 'Hello World: 95'
---
data: 'Hello World: 96'
---
^Croot@ubuntu-16:/embodied_ai_framework# ros2 topic info /chatter
Type: std_msgs/msg/String
Publisher count: 1
Subscription count: 1
root@ubuntu-16:/embodied_ai_framework# ros2 interface show std_msgs/msg/String
# This was originally provided as an example message.
# It is deprecated as of Foxy
# It is recommended to create your own semantically meaningful message.
# However if you would like to continue using this please use the equivalent in example_msgs.
string data
root@ubuntu-16:/embodied_ai_framework#
root@ubuntu-16:/embodied_ai_framework#
root@ubuntu-16:/embodied_ai_framework#
root@ubuntu-16:/embodied_ai_framework# ros2 topic pub /chatter std_msgs/msg/String 'data: "123"'
publisher: beginning loop
publishing #1: std_msgs.msg.String(data='123')
publishing #2: std_msgs.msg.String(data='123')
publishing #3: std_msgs.msg.String(data='123')
publishing #4: std_msgs.msg.String(data='123')

浙公网安备 33010602011771号