21 from threading
import Lock
22 from threading
import currentThread
23 from functools
import partial
24 from cv_bridge
import CvBridge, CvBridgeError
32 mandatory_parameters = [
'topic_rate',
'latched',
'lazy']
34 topics_param_name =
'~topics' 35 if not rospy.has_param(topics_param_name):
36 rospy.logerr(
'Parameter ' + topics_param_name +
' not available')
39 topics_list = rospy.get_param(topics_param_name, [])
42 self.
topics = {next(iter(item.keys())): next(iter(item.values()))
for item
in topics_list}
45 mandatory_flag = all(set(mandatory_parameters) <=
46 set(element)
for key, element
in 49 if(not(mandatory_flag)):
50 rospy.logerr(
'Each throttled topic needs 3 parameters ' +
51 str(mandatory_parameters))
59 locking = self.
topics[topic_id][
'lock'].acquire_lock(
False)
62 current_t = currentThread()
63 rospy.logdebug(str(current_t.getName()) +
': cannot lock topic ' 67 publisher = self.
topics[topic_id][
'publisher']
68 subscriber = self.
topics[topic_id][
'subscriber']
73 if None in(publisher, subscriber):
74 topic_info = rostopic.get_topic_class(topic_id)
75 if topic_info[0]
is None:
76 rospy.logwarn(
'Cannot find topic ' + topic_id)
78 self.
topics[topic_id][
'lock'].release_lock()
82 self.
topics[topic_id][
'publisher'] = \
83 rospy.Publisher(topic_id +
'_throttled', topic_info[0],
85 latch = self.
topics[topic_id][
'latched'])
87 rospy.loginfo(
'Created publisher for ' + topic_id)
92 self.
topics[topic_id][
'subscriber'] = \
93 rospy.Subscriber(topic_id, topic_info[0],
95 rospy.loginfo(
'Created subscriber for ' + topic_id)
97 self.
topics[topic_id][
'lock'].release_lock()
100 last_message = self.
topics[topic_id][
'last_message']
102 if last_message
is not None:
104 self.
topics[topic_id][
'publisher'].get_num_connections() == 0 \
105 and self.
topics[topic_id][
'lazy']
106 if not lazy_behavior:
109 resolution = self.
topics[topic_id][
'resolution_factor']
113 if resolution
is not None:
116 self.
topics[topic_id][
'publisher'].publish(last_message)
118 self.
topics[topic_id][
'last_message'] =
None 119 self.
topics[topic_id][
'lock'].release_lock()
122 self.
topics[topic_id][
'lock'].release_lock()
126 locking = self.
topics[topic_id][
'lock'].acquire_lock(
False)
130 current_t = currentThread()
131 rospy.logdebug(str(current_t.getName()) +
': cannot lock topic ' 136 resolution = self.
topics[topic_id][
'resolution_factor']
140 if resolution
is not None:
141 if data._type !=
'sensor_msgs/Image':
142 rospy.logwarn(
'resolution_factor option is not available for ' +
143 data._type +
'. Topic ' + topic_id +
144 ' will not be resolution throttled.')
145 self.
topics[topic_id][
'resolution_factor'] =
None 148 self.
topics[topic_id][
'last_message'] = data
149 self.
topics[topic_id][
'lock'].release_lock()
156 for key, element
in self.topics.items():
157 element[
'lock'] = Lock()
158 element[
'lock'].acquire_lock()
159 element[
'publisher'] =
None 160 element[
'subscriber'] =
None 163 element[
'timer'] = rospy.Timer(
164 rospy.Duration(1.0 / element[
'topic_rate']), personal_callback)
165 element[
'last_message'] =
None 166 element[
'lock'].release_lock()
169 rospy.loginfo(
'Stopping ' + str(rospy.get_name()))
175 old_image = self.bridge.imgmsg_to_cv2(data)
176 except CvBridgeError
as e:
183 new_image = cv2.resize(old_image, (0, 0), fx=resolution,
185 interpolation=cv2.INTER_AREA)
188 new_image = cv2.resize(old_image, (0, 0), fx=resolution,
192 new_image = old_image
193 except cv2.error
as e:
198 new_message = self.bridge.cv2_to_imgmsg(new_image,
199 encoding=data.encoding)
201 except CvBridgeError
as e:
def _resize_image(self, data, resolution)
def subscriber_callback(self, data, topic_id)
def timer_callback(self, event, topic_id)
def _populate_dictionary(self)