Advertisement
Guest User

Node A

a guest
May 19th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.00 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import rospy
  3. import random
  4. from std_msgs.msg import String
  5.  
  6. hash = None
  7. pub = rospy.Publisher('AToB', String, queue_size=10) #publisher
  8.  
  9. def callback(data):
  10.     rospy.loginfo("A heard %s",data.data)
  11.     if hash == data.data[0:32]:
  12.         sendMsg()
  13.  
  14. def talker():
  15.     rospy.Subscriber("CToA", String, callback) #listening
  16.     rospy.init_node('node_A', anonymous=True)
  17.     sendMsg()
  18.     rospy.spin()#looping
  19.  
  20. def sendMsg():
  21.     rospy.set_param('global_timer',rospy.get_time())#restart global timer
  22.     rospy.Timer(rospy.Duration(10),timeout,oneshot=True)#set timeout function
  23.     global hash
  24.     hash = "%032x" % random.getrandbits(128)
  25.     msg = "%s:%s" % (hash,(rospy.get_time() - rospy.get_param('global_timer')))
  26.     rospy.loginfo("A Sent " + msg)
  27.     pub.publish(msg)
  28.  
  29. def timeout(event):#10s timeout function
  30.     global_timer = rospy.get_param('global_timer')
  31.     if (rospy.get_time() - global_timer) > 10:
  32.         sendMsg()
  33.  
  34. if __name__ == '__main__':
  35.     talker()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement