3 from threading
import Timer
10 A proxy for calling services.
14 def __init__(self, topics={}, persistent=False, wait_duration=10):
16 Initializes the proxy with optionally a given set of clients.
18 @type topics: dictionary string - message class
19 @param topics: A dictionay containing a collection of topic - message type pairs.
21 @type persistent: bool
22 @param persistent: Defines if the service callers are persistent.
24 @type wait_duration: int
25 @param wait_duration: Defines how long to wait for the given services if not available right now.
27 for topic, msg_type
in topics.items():
28 self.
setupService(topic, msg_type, persistent, wait_duration)
30 def setupService(self, topic, msg_type, persistent=False, wait_duration=10):
32 Tries to set up a service caller for calling it later.
35 @param topic: The topic of the service to call.
37 @type msg_type: service class
38 @param msg_type: The type of messages of this service.
40 @type persistent: bool
41 @param persistent: Defines if this service caller is persistent.
43 @type wait_duration: int
44 @param wait_duration: Defines how long to wait for the given service if it is not available right now.
46 if topic
not in ProxyServiceCaller._services:
47 ProxyServiceCaller._services[topic] = rospy.ServiceProxy(topic, msg_type, persistent)
52 Checks if the service on the given topic is available.
55 @param topic: The topic of interest.
59 def call(self, topic, request):
61 Performs a service call on the given topic.
64 @param topic: The topic to call.
66 @type request: service
67 @param request: The request to send to this service.
70 raise ValueError(
'Cannot call service client %s: Topic not available.' % topic)
72 return ProxyServiceCaller._services[topic].
call(request)
76 Checks whether a service is available.
79 @param topic: The topic of the service.
81 @type wait_duration: int
82 @param wait_duration: Defines how long to wait for the given service if it is not available right now.
84 client = ProxyServiceCaller._services.get(topic)
86 Logger.logerr(
"Service client %s not yet registered, need to add it first!" % topic)
93 rospy.wait_for_service(topic, wait_duration)
95 except rospy.exceptions.ROSException:
105 Logger.logerr(
"Service client %s timed out!" % topic)
109 Logger.loginfo(
"Finally found service %s..." % (topic))
113 Logger.logwarn(
"Waiting for service %s..." % (topic))