本文主要内容:
- ROS topics
- publisher
- topic messages
Part 1: 发布(Publisher)
创建一个Publisher 的Python 代码示例 [1]:
#! /usr/bin/env python
import rospy
from std_msgs.msg import Int32
rospy.init_node('topic_publisher')
pub = rospy.Publisher('/counter', Int32, queue_size=1)
rate = rospy.Rate(2)
count = Int32()
count.data = 0
while not rospy.is_shutdown():
pub.publish(count)
count.data += 1
rate.sleep()
一个topic就像一个管道(pipe),节点(Node)用话题(topic)给其他节点发布信息。
一个发布器(Publisher)是一个一直发布消息(Message)的节点。(图片来源:[2])
Nothing happens? Well... that's not actually true! You have just created a topic named /counter, and published through it as an integer that increases indefinitely. Let's check some things.
A topic is like a pipe. Nodes use topics to publish information for other nodes so that they can communicate.
You can find out, at any time, the number of topics in the system by doing a rostopic list. You can also check for a specific topic.
On your webshell, type rostopic list and check for a topic named '/counter'.
常见的topic命令行:
rostopic list # 显示所有运行的topic
rostopic list | grep '/counter' # 找出叫 '/counter' 的topic
rostopic info /counter # 得到 有关 '/counter' topic 的信息
rostopic pub <topic_name> <message_type> <value> # 在<topic_name> 发布 <message_type>格式 的消息
rostopic echo <topic_name> - # 读取发布到topic的消息
rostopic echo <topic_name> -n1 # 读取一个发布到topic的最后一个消息
Here, you have just listed all of the topics running right now and filtered with the grep command the ones that contain the word /counter. If it appears, then the topic is running as it should.
You can request information about a topic by doing rostopic info <name_of_topic>.