rosmasterless.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 import roslib; roslib.load_manifest('test_ros') # FIXME
00003 import rospy
00004 import std_msgs.msg
00005 import rosgraph.masterapi
00006 
00007 try:
00008     import rospy.masterslave as masterslave
00009 except:
00010     import rospy.impl.masterslave as masterslave
00011 try:
00012     import rospy.registration as registration
00013 except:
00014     import rospy.impl.registration as registration
00015 
00016 master_check_interval = 2
00017 
00018 def new_publish(*args, **nargs):
00019     check_master()
00020     original_Publisher_publish(*args, **nargs)
00021 
00022 def new_init_node(*args, **nargs):
00023     global check_interval
00024     global our_uri
00025     original_rospy_init_node(*args, **nargs)
00026     our_uri =  rospy.get_node_uri()
00027     check_interval = rospy.Duration(master_check_interval)
00028     check_master()
00029 
00030 next_check_time = None
00031 master_pid = None
00032 pub_list = {}
00033 def check_master():
00034     global next_check_time
00035     global master
00036     global our_uri
00037     global master_pid
00038     now = rospy.Time.now()
00039     if next_check_time == None or now > next_check_time:
00040         next_check_time = now + check_interval
00041         try:
00042             new_pid = master.getPid()
00043             if new_pid != master_pid:
00044                 raise Exception
00045         except:
00046             try:
00047                 master = rosgraph.masterapi.Master(rospy.get_name())
00048                 for (name, type) in pub_list.iteritems():
00049                     master.registerPublisher(name, type, our_uri)
00050                 master_pid = master.getPid()
00051                 #print "Registered with master."
00052             except Exception, e:
00053                 master = None
00054                 master_pid = None
00055 
00056 def new_registration_reg_added(self, resolved_name, data_type_or_uri, reg_type):
00057     if reg_type == registration.Registration.PUB:
00058         pub_list[resolved_name] = data_type_or_uri
00059         try:
00060             master.registerPublisher(resolved_name, data_type_or_uri, our_uri)
00061         except:
00062             pass
00063     return True
00064 
00065 # Store original functions
00066 original_Publisher_publish = rospy.Publisher.publish
00067 original_rospy_init_node = rospy.init_node
00068 
00069 # hot patch
00070 masterslave.ROSHandler._is_registered = lambda x: True
00071 registration.RegManager.reg_added = new_registration_reg_added
00072 rospy.Publisher.publish = new_publish
00073 rospy.init_node = new_init_node
00074 
00075 ## init with default topics disabled
00076 #import rosmasterless
00077 #
00078 #rospy.init_node('ronin', disable_rosout=True, disable_rostime=True)
00079 #
00080 #pub = rospy.Publisher('chatter', std_msgs.msg.String)
00081 #
00082 #while not rospy.is_shutdown():
00083 #    pub.publish('testing')
00084 #    rospy.sleep(rospy.Duration(1))


multi_interface_roam
Author(s): Blaise Gassend
autogenerated on Wed Sep 16 2015 04:38:27