34 from threading
import Lock
35 from rospy
import Subscriber, logerr
36 from rostopic
import get_topic_type
42 """ Manages and interfaces with ROS Subscriber objects. A single subscriber 43 is shared between multiple clients 48 """ Handles multiple clients for a single subscriber. 50 Converts msgs to JSON before handing them to callbacks. Due to subscriber 51 callbacks being called in separate threads, must lock whenever modifying 52 or accessing the subscribed clients. """ 55 """ Register a subscriber on the specified topic. 58 topic -- the name of the topic to register the subscriber on 59 msg_type -- (optional) the type to register the subscriber as. If not 60 provided, an attempt will be made to infer the topic type 63 TopicNotEstablishedException -- if no msg_type was specified by the 64 caller and the topic is not yet established, so a topic type cannot 66 TypeConflictException -- if the msg_type was specified by the 67 caller and the topic is established, and the established type is 68 different to the user-specified msg_type 72 topic_type = get_topic_type(topic)[0]
75 if msg_type
is None and topic_type
is None:
83 msg_class = ros_loader.get_message_class(msg_type)
86 if topic_type
is not None and topic_type != msg_class._type:
97 self.subscriber.unregister()
99 self.subscriptions.clear()
102 """ Verify that the subscriber subscribes to messages of this type. 105 msg_type -- the type to check this subscriber against 108 Exception -- if ros_loader cannot load the specified msg type 109 TypeConflictException -- if the msg_type is different than the type of 113 if not ros_loader.get_message_class(msg_type)
is self.
msg_class:
115 self.msg_class._type, msg_type)
119 """ Subscribe the specified client to this subscriber. 122 client_id -- the ID of the client subscribing 123 callback -- this client's callback, that will be called for incoming 131 self.subscriber.impl.add_callback(self.
callback, [callback])
132 self.subscriber.impl.remove_callback(self.
callback, [callback])
135 """ Unsubscribe the specified client from this subscriber 138 client_id -- the ID of the client to unsubscribe 145 """ Return true if there are subscribers """ 151 """ Callback for incoming messages on the rospy.Subscriber 153 Passes the message to registered subscriber callbacks. 156 msg - the ROS message coming from the subscriber 157 callbacks - subscriber callbacks to invoke 165 callbacks = self.subscriptions.values()
168 for callback
in callbacks:
171 except Exception
as exc:
173 logerr(
"Exception calling subscribe callback: %s", exc)
179 Keeps track of client subscriptions 185 def subscribe(self, client_id, topic, callback, msg_type=None):
186 """ Subscribe to a topic 189 client_id -- the ID of the client making this subscribe request 190 topic -- the name of the topic to subscribe to 191 callback -- the callback to call for incoming messages on the topic 192 msg_type -- (optional) the type of the topic 198 if msg_type
is not None:
204 """ Unsubscribe from a topic 207 client_id -- the ID of the client to unsubscribe 208 topic -- the topic to unsubscribe from
def __init__(self, topic, msg_type=None)
def callback(self, msg, callbacks=None)
def subscribe(self, client_id, topic, callback, msg_type=None)
def verify_type(self, msg_type)
def unsubscribe(self, client_id, topic)
def has_subscribers(self)
def unsubscribe(self, client_id)
def subscribe(self, client_id, callback)