i am new to ros and i want to know how to write a publisher subscriber code in the same node in python (can i get a basic hello world code)
!/usr/bin/env python
import rospy from std_msgs.msg import String
pub = rospy.Publisher('chatter', String, queue_size=10) rospy.init_node('chatternode', anonymous=False)
def callback(data): rospy.loginfo(rospy.get_caller_id() + 'I heard %s', data.data)
def chatternode(): rospy.Subscriber('chatter', String, callback) 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() pub.publish(hello_str) rospy.spin()
if __name__ == '__main__': try: chatternode() except rospy.ROSInterruptException: pass