节点ROS通信系统中就是一个可执行的程序,这边博客主要记录如何在Python代码节点中定义发布器和订阅器。
发布器:
import rospy
from std_msgs.msg import String#载入String的msg类型
def talker():
#定义一个发布器,话题为'chatter'
pub = rospy.Publisher('chatter', String, queue_size=10)
#声明一个名为'talker'的节点
rospy.init_node('talker', anonymous=True)
rate = rospy.Rate(10) # 10hz的发布频率
while not rospy.is_shutdown():
hello_str = "hello world %s" % rospy.get_time()
rospy.loginfo(hello_str)
pub.publish(hello_str)
rate.sleep()#休眠一段时间来保证rate的频率
if __name__ == '__main__':
try:
talker()
except rospy.ROSInterruptException:
pass
from std_msgs.msg import String 载入String的消息类型
rospy.Publisher()定义了一个话题,名为"chatter",消息类型为String,消息队列的长度为10
rospy.init_node()初始化一个名为"take