Exemplo n.º 1
0
 def __init__(self):
     '''
     Creates a new instance. Find the topic of the master_discovery node using
     U{fkie_master_discovery.interface_finder.get_changes_topic()
     <http://docs.ros.org/api/fkie_master_discovery/html/modules.html#interface-finder-module>}.
     Also the parameter C{~ignore_hosts} will be analyzed to exclude hosts from sync.
     '''
     self.masters = {}
     # the connection to the local service master
     self.materuri = masteruri_from_master()
     '''@ivar: the ROS master URI of the C{local} ROS master. '''
     self.__lock = threading.RLock()
     # load interface
     self._load_interface()
     # subscribe to changes notifier topics
     topic_names = interface_finder.get_changes_topic(
         masteruri_from_master())
     self.sub_changes = dict()
     '''@ivar: `dict` with topics {name: U{rospy.Subscriber<http://docs.ros.org/api/rospy/html/rospy.topics.Subscriber-class.html>}} publishes the changes of the discovered ROS masters.'''
     for topic_name in topic_names:
         rospy.loginfo("listen for updates on %s", topic_name)
         self.sub_changes[topic_name] = rospy.Subscriber(
             topic_name, MasterState, self._rosmsg_callback_master_state)
     self.__timestamp_local = None
     self.__own_state = None
     self.update_timer = None
     self.own_state_getter = None
     self._join_threads = dict(
     )  # threads waiting for stopping the sync thread
     # initialize the ROS services
     rospy.Service('~get_sync_info', GetSyncInfo,
                   self._rosservice_get_sync_info)
     rospy.on_shutdown(self.finish)
     self.obtain_masters()
 def registerByROS(self, masteruri, wait=False):
     '''
     This method creates a ROS subscriber to received the notifications of ROS
     master updates. The retrieved messages will be emitted as state_signal.
     @param masteruri: the ROS master URI
     @type masteruri: C{str}
     @param wait: wait for the topic
     @type wait: C{boolean}
     @return: the topic name or an empty string
     '''
     result = ''
     topic_names = interface_finder.get_changes_topic(masteruri, wait)
     self.stop()
     if not hasattr(self, 'sub_changes'):
         self.sub_changes = []
         self.sub_names = []
     if set(self.sub_names) != set(topic_names):
         self.stop()
         self.sub_changes = []
         for topic_name in topic_names:
             rospy.loginfo("listen for updates on %s", topic_name)
             sub_changes = rospy.Subscriber(topic_name, MasterState, self.handlerMasterStateMsg)
             self.sub_changes.append(sub_changes)
             result = topic_name
         self.sub_names = topic_names
     return result