__init__.py
Go to the documentation of this file.
1 import rospy
2 from rospy.impl.tcpros_base import DEFAULT_BUFF_SIZE
3 from Queue import Queue, Empty as EmptyQueueException
4 from threading import Condition
5 
6 # The global callback queue
7 callback_queue = Queue()
8 
9 def spin():
10  '''
11  This spinner is used in place of the rospy.spin() method. Whereas
12  rospy.spin() simply waits for rospy.is_shutdown(), this spinner processes
13  the callback queue until rospy.is_shutdown() is true. The callback
14  queue is thread-safe, so for a multi-threaded queue processor, call this
15  spin method in multiple threads.
16  '''
17  while not rospy.is_shutdown():
18  try:
19  condition = callback_queue.get(block=True, timeout=0.1)
20  except EmptyQueueException:
21  pass
22  else:
23  with condition:
24  condition.notify()
25  condition.wait()
26 
27 # Override the default rospy spin() with swri_rospy.spin()
28 rospy.spin = spin
29 
30 def single_threaded(callback):
31  '''
32  This decorator can be used on service or subscriber callbacks. A callback
33  wrapped with this decorator will put a condition on the callback_queue and
34  then wait its turn before executing the decorated callback.
35  '''
36  # Prevent recursive decoration, which would deadlock
37  if hasattr(callback, 'single_threaded') and callback.single_threaded:
38  return callback
39 
40  def wrapped_callback(*args, **kwds):
41  # Put condition on queue
42  condition = Condition()
43  with condition:
44  callback_queue.put(condition)
45  # Wait for condition
46  condition.wait()
47  # Do the callback
48  try:
49  ret = callback(*args, **kwds)
50  finally:
51  condition.notify()
52  return ret
53  wrapped_callback.single_threaded = True
54  return wrapped_callback
55 
56 def service_wrapper(func):
57  '''
58  This is a decorator for functions that handle ROS service calls.
59  It catches unhandled exceptions and reports them cleanly as errors
60  on the ROS console.
61  '''
62  def wrapper(*args, **kwds):
63  try:
64  return func(*args, **kwds)
65  except:
66  rospy.logerr(traceback.format_exc())
67  return None
68  return wrapper
69 
70 class Service(rospy.Service):
71  def __init__(self, name, service_class, handler,
72  buff_size=DEFAULT_BUFF_SIZE, error_handler=None,
73  asynchronous=False):
74  if not asynchronous:
75  handler = single_threaded(handler)
76  super(Service, self).__init__(name, service_class, handler, buff_size,
77  error_handler)
78 
79 class Subscriber(rospy.Subscriber):
80  def __init__(self, name, data_class, callback=None, callback_args=None,
81  queue_size=None, buff_size=DEFAULT_BUFF_SIZE,
82  tcp_nodelay=False, asynchronous=False):
83  if not asynchronous:
84  callback = single_threaded(callback)
85  super(Subscriber, self).__init__(name, data_class, callback,
86  callback_args, queue_size, buff_size, tcp_nodelay)
87 
88 class Timer(rospy.Timer):
89  def __init__(self, period, callback, oneshot=False, asynchronous=False):
90  if not asynchronous:
91  callback = single_threaded(callback)
92  super(Timer, self).__init__(period, callback, oneshot)
def spin()
Definition: __init__.py:9
def single_threaded(callback)
Definition: __init__.py:30
def service_wrapper(func)
Definition: __init__.py:56
def __init__(self, name, service_class, handler, buff_size=DEFAULT_BUFF_SIZE, error_handler=None, asynchronous=False)
Definition: __init__.py:73
def __init__(self, period, callback, oneshot=False, asynchronous=False)
Definition: __init__.py:89
def __init__(self, name, data_class, callback=None, callback_args=None, queue_size=None, buff_size=DEFAULT_BUFF_SIZE, tcp_nodelay=False, asynchronous=False)
Definition: __init__.py:82


swri_rospy
Author(s):
autogenerated on Fri Jun 7 2019 22:06:01