1#!/usr/bin/env python
2# license removed for brevity
3import rospy
4from std_msgs.msg import String
5
6def talker():
7 pub = rospy.Publisher('chatter', String, queue_size=10)
8 rospy.init_node('talker', anonymous=True)
9 rate = rospy.Rate(10) # 10hz
10 while not rospy.is_shutdown():
11 hello_str = "hello world %s" % rospy.get_time()
12 rospy.loginfo(hello_str)
13 pub.publish(hello_str)
14 rate.sleep()
15
16if __name__ == '__main__':
17 try:
18 talker()
19 except rospy.ROSInterruptException:
20 pass
21
1#!/usr/bin/env python
2import rospy
3from std_msgs.msg import String
4
5def callback(data):
6 rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)
7
8def listener():
9
10 rospy.init_node('listener', anonymous=True)
11
12 rospy.Subscriber("chatter", String, callback)
13
14 rospy.spin()
15
16if __name__ == '__main__':
17 listener()
18