SHARE
TWEET

Node A

a guest May 19th, 2017 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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()
RAW Paste Data
Want to get better at Python?
Learn to code Python in 2017
Top