34 from threading
import Lock
35 from rospy
import Subscriber, logerr
36 from rostopic
import get_topic_type
41 from rospy.msg
import AnyMsg
43 """ Manages and interfaces with ROS Subscriber objects. A single subscriber
44 is shared between multiple clients
49 """ Handles multiple clients for a single subscriber.
51 Converts msgs to JSON before handing them to callbacks. Due to subscriber
52 callbacks being called in separate threads, must lock whenever modifying
53 or accessing the subscribed clients. """
56 """ Register a subscriber on the specified topic.
59 topic -- the name of the topic to register the subscriber on
60 msg_type -- (optional) the type to register the subscriber as. If not
61 provided, an attempt will be made to infer the topic type
64 TopicNotEstablishedException -- if no msg_type was specified by the
65 caller and the topic is not yet established, so a topic type cannot
67 TypeConflictException -- if the msg_type was specified by the
68 caller and the topic is established, and the established type is
69 different to the user-specified msg_type
73 topic_type = get_topic_type(topic)[0]
76 if msg_type
is None and topic_type
is None:
83 if msg_type ==
"__AnyMsg":
87 msg_class = ros_loader.get_message_class(msg_type)
90 if topic_type
is not None and topic_type != msg_class._type:
106 """ Verify that the subscriber subscribes to messages of this type.
109 msg_type -- the type to check this subscriber against
112 Exception -- if ros_loader cannot load the specified msg type
113 TypeConflictException -- if the msg_type is different than the type of
117 if msg_type ==
"__AnyMsg":
119 if not ros_loader.get_message_class(msg_type)
is self.
msg_class:
124 """ Subscribe the specified client to this subscriber.
127 client_id -- the ID of the client subscribing
128 callback -- this client's callback, that will be called for incoming
140 """ Unsubscribe the specified client from this subscriber
143 client_id -- the ID of the client to unsubscribe
150 """ Return true if there are subscribers """
155 """ Callback for incoming messages on the rospy.Subscriber
157 Passes the message to registered subscriber callbacks.
160 msg - the ROS message coming from the subscriber
161 callbacks - subscriber callbacks to invoke
172 for callback
in callbacks:
175 except Exception
as exc:
177 logerr(
"Exception calling subscribe callback: %s", exc)
182 Keeps track of client subscriptions
189 def subscribe(self, client_id, topic, callback, msg_type=None):
190 """ Subscribe to a topic
193 client_id -- the ID of the client making this subscribe request
194 topic -- the name of the topic to subscribe to
195 callback -- the callback to call for incoming messages on the topic
196 msg_type -- (optional) the type of the topic
203 if msg_type
is not None:
209 """ Unsubscribe from a topic
212 client_id -- the ID of the client to unsubscribe
213 topic -- the topic to unsubscribe from