编写一个发布器和接收器(python)

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
#!/usr/bin/env python
import rospy
from std_msgs.msg import String

def talker():
rospy.init_node('talker',anonymous=True)

pub = rospy.Publisher('chatter', String, queue_size=10)

rate = rospy.Rate(10)

while not rospy.is_shutdown():
hello_str ="hello world %s" % rospy.get_time()

rospy.loginfo(hello_str)

pub.publish(hello_str)

rate.sleep()

if __name__ == '__main__ ':
try:
talker()
except rospy.ROSInterruptException:
pass