rosros 0.2.5
Simple unified interface to ROS1 / ROS2 Python API
Loading...
Searching...
No Matches
executors.py
Go to the documentation of this file.
1"""
2Partial stand-in for `rclpy.executors` in ROS1, providing `Executor` classes.
3
4------------------------------------------------------------------------------
5This file is part of rosros - simple unified interface to ROS1 / ROS2.
6Released under the BSD License.
7
8@author Erki Suurjaak
9@created 12.06.2022
10@modified 12.06.2022
11------------------------------------------------------------------------------
12"""
13## @namespace rosros.rclify.executors
14import threading
15
16from .. import ros1
17from . task import Task
18
19
20
21class TimeoutException(Exception):
22 """Signal that a timeout occurred."""
23
25 """Signal that executor was shut down."""
26
28 """Context has been shutdown."""
29
30class ConditionReachedException(Exception):
31 """Future has been completed."""
32
33
34
36 """API stand-in for ROS2 compatibility, all spinning is performed by sleep."""
37
38 def __init__(self, *, context=None):
39 """
40 @param context ignored (ROS2 compatibility stand-in)
41 """
42 super().__init__()
43 self._context = context
44 self._nodes = set()
45 self._nodes_lock = threading.RLock()
46 self._is_shutdown = False
47
48 @property
49 def context(self):
50 """Returns context given in constructor, or None ((ROS2 compatibility stand-in)."""
51 return self._context
52
53 def create_task(self, callback, *args, **kwargs):
54 """
55 Adds a callback or coroutine to be executed during spin, returns a Future.
56
57 Arguments to this function are passed to the callback.
58
59 @param callback a callback to be run in the executor
60 """
61 return Task(callback, args, kwargs, executor=self)
63 def shutdown(self, timeout_sec=None):
64 """
65 Shuts down ROS.
66
67 @param timeout_sec ignored (ROS2 compatibility stand-in)
68 @return True
69 """
70 self._is_shutdown = True
71 with self._nodes_lock:
72 self._nodes = set()
73 ros1.shutdown()
74 return True
75
76 def add_node(self, node):
77 """
78 Adds a node to this executor.
79
80 @param node the node to add to the executor
81 @return True if the node was added, False if it already existed
82 """
83 with self._nodes_lock:
84 node.executor = self
85 result = node in self._nodes
86 self._nodes.add(node)
87 return result
88
89 def remove_node(self, node):
90 """
91 Stop managing this node's callbacks.
92
93 @param node the node to remove from the executor
94 """
95 with self._nodes_lock:
96 self._nodes.discard(node)
97
98 def wake(self):
99 """Does nothing (ROS2 compatibility stand-in)."""
100
101 def get_nodes(self):
102 """Return nodes that have been added to this executor."""
103 with self._nodes_lock:
104 return list(self._nodes)
105
106 def spin(self):
107 """Waits until ROS shut down."""
108 ros1.spin()
109
110 def spin_until_future_complete(self, future, timeout_sec=None):
111 """
112 Waits until future complete or timeout reached or ROS shut down.
113
114 @param future the future to wait on until done.
115 @param timeout_sec maximum seconds to wait. Block forever if None or negative.
116 Don't wait if 0.
117 """
118 ros1.spin_until_future_complete(future, timeout_sec)
119
120 def spin_once(self, timeout_sec=None):
121 """
122 Waits until timeout, or forever if timeout None or negative, or until ROS shutdown.
123
124 @param timeout_sec seconds to wait. Block forever if None or negative. Don't wait if 0.
125 """
126 ros1.spin_once(timeout_sec)
127
128 def spin_once_until_future_complete(self, future, timeout_sec=None):
129 """
130 Waits until future complete or timeout reached or ROS shut down.
131
132 @param future the future to wait on until done.
133 @param timeout_sec maximum seconds to wait. Block forever if None or negative.
134 Don't wait if 0.
135 """
136 ros1.spin_until_future_complete(future, timeout_sec)
137
139
141 """API stand-in for ROS2 compatibility."""
143
144class MultiThreadedExecutor(Executor):
145 """API stand-in for ROS2 compatibility."""
146
147
148__all__ = [
149 "ConditionReachedException", "Executor", "ExternalShutdownException",
150 "MultiThreadedExecutor", "ShutdownException", "SingleThreadedExecutor", "TimeoutException"
151]
add_node(self, node)
Adds a node to this executor.
Definition executors.py:82
remove_node(self, node)
Stop managing this node's callbacks.
Definition executors.py:94
shutdown(self, timeout_sec=None)
Shuts down ROS.
Definition executors.py:70
spin_once(self, timeout_sec=None)
Waits until timeout, or forever if timeout None or negative, or until ROS shutdown.
Definition executors.py:124
spin_until_future_complete(self, future, timeout_sec=None)
Waits until future complete or timeout reached or ROS shut down.
Definition executors.py:116
__init__(self, *context=None)
Definition executors.py:41
wake(self)
Does nothing (ROS2 compatibility stand-in).
Definition executors.py:98
context
Returns context given in constructor, or None ((ROS2 compatibility stand-in).
Definition executors.py:50
spin_once_until_future_complete(self, future, timeout_sec=None)
Waits until future complete or timeout reached or ROS shut down.
Definition executors.py:133
create_task(self, callback, *args, **kwargs)
Adds a callback or coroutine to be executed during spin, returns a Future.
Definition executors.py:62
API stand-in for ROS2 compatibility.
Definition executors.py:138
Executes a function or coroutine.
Definition task.py:187
spin()
Spins ROS node forever.
Definition core.py:133
get_nodes()
Returns all ROS nodes, as `[node full name, ]`.
Definition core.py:326