1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 """
35 Master-less rospy node api.
36
37 The ronin API is more handle-based than the rospy API. This is in
38 order to provide better forwards compatibility for ronin as changes to
39 rospy are made to enable this functionality more gracefully.
40
41 Here is an example ronin-based talker:::
42
43 n = ronin.init_node('talker', anonymous=True)
44 pub = n.Publisher('chatter', String)
45 r = n.Rate(10) # 10hz
46 while not n.is_shutdown():
47 str = "hello world %s"%n.get_time()
48 n.loginfo(str)
49 pub.publish(str)
50 r.sleep()
51
52 Authors: Ken Conley and Blaise Gassend
53 """
54
55 import roslib; roslib.load_manifest('ronin')
56
57 import rospy
58
59 import roslib.masterapi
60
61 import rospy.masterslave
62 import rospy.registration
63
64
65 rospy.masterslave.ROSHandler._is_registered = lambda x: True
66
67 _MASTER_CHECK_INTERVAL = 10
68
70 kwds['disable_rosout'] = True
71 kwds['disable_rostime'] = True
72
73 rospy.init_node(name, **kwds)
74 handle = RoninHandle(name, rospy.get_node_uri(), rospy.Duration(_MASTER_CHECK_INTERVAL))
75
76
77 rospy.registration.RegManager.reg_added = handle._reg_added
78
79 handle._check_master()
80 return handle
81
82
84 self.ronin._check_master()
85 self.ronin_publish(*args[1:], **kwds)
86
89 self.pub = pub
90 self.handle = handle
91
93 self.handle._check_master()
94 self.pub.ronin_publish(*args, **kwds)
95
97
98 - def __init__(self, name, uri, check_interval):
99 self._uri = uri
100 self._name = name
101 self._check_interval = check_interval
102
103 self._master = roslib.masterapi.Master(name)
104
105 self._pub_list = []
106 self._sub_list = []
107
108 self._next_check_time = None
109 self._master_pid = None
110
111 self.Rate = rospy.Rate
112 self.Subscriber = rospy.Subscriber
113
114 self.is_shutdown = rospy.is_shutdown
115 self.get_time = rospy.get_time
116 self.get_rostime = rospy.get_rostime
117 self.sleep = rospy.sleep
118
119 self.loginfo = rospy.loginfo
120 self.logerr = rospy.logerr
121 self.logdebug = rospy.logdebug
122 self.logwarn = rospy.logwarn
123
124 - def _reg_added(self, resolved_name, data_type_or_uri, reg_type):
125 try:
126 if reg_type == rospy.registration.Registration.PUB:
127 self._pub_list.append((resolved_name, data_type_or_uri))
128 if self._master_pid is not None:
129 master.registerPublisher(resolved_name, data_type_or_uri, our_uri)
130 elif reg_type == rospy.registration.Registration.SUB:
131 self._sub_list.append((resolved_name, data_type_or_uri))
132 if self._master_pid is not None:
133 master.registerSubscriber(resolved_name, data_type_or_uri, our_uri)
134 except:
135 pass
136 return True
137
139 self.ronin._check_master()
140 original_Publisher_publish(*args, **nargs)
141
143
144 pub = rospy.Publisher(*args, **kwds)
145 pub.ronin_publish = pub.publish
146 pub.publish = _PubHandle(pub, self)
147 return pub
148
150
151
152 now = rospy.Time.now()
153 next_check_time = self._next_check_time
154 if next_check_time == None or now > next_check_time:
155 self._next_check_time = now + self._check_interval
156 if self._master_pid != None:
157 try:
158 new_pid = master.getPid()
159 if new_pid != self._master_pid:
160 self._master_pid = None
161 except:
162 self._master_pid = None
163 if self._master_pid == None:
164 try:
165 for name, type in self._pub_list:
166 self._master.registerPublisher(name, type, self._uri)
167 for name, type in self._sub_list:
168 self._master.registerSubscriber(name, type, self._uri)
169
170 self._master_pid = self._master.getPid()
171 except Exception, e:
172 pass
173 if self._master_pid:
174 return True
175