ros发布节点信息python_ROS 入门 (2) | 话题 (Topics)

本文介绍了ROS中的发布(Publisher)和订阅器(Subscriber)概念,通过Python展示了如何创建Publisher节点并发布消息到话题(Topic)。文中详细解释了如何检查系统中的topics,以及如何使用消息(Messages)。此外,还讲解了如何创建自定义消息类型,包括创建消息文件、修改CMakeLists.txt和package.xml文件,并强调了编译新消息后需要执行source devel/setup.bash的重要性。
摘要由CSDN通过智能技术生成

本文主要内容:

  • 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]

380c540d9c8d0028e6d12bf2414d234f.png


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>.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值