AS: adding first phase of orientation
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
#!/usr/bin/python3
|
||||
# EASY-INSTALL-ENTRY-SCRIPT: 'examples-rclpy-executors==0.15.3','console_scripts','callback_group'
|
||||
import re
|
||||
import sys
|
||||
|
||||
# for compatibility with easy_install; see #2198
|
||||
__requires__ = 'examples-rclpy-executors==0.15.3'
|
||||
|
||||
try:
|
||||
from importlib.metadata import distribution
|
||||
except ImportError:
|
||||
try:
|
||||
from importlib_metadata import distribution
|
||||
except ImportError:
|
||||
from pkg_resources import load_entry_point
|
||||
|
||||
|
||||
def importlib_load_entry_point(spec, group, name):
|
||||
dist_name, _, _ = spec.partition('==')
|
||||
matches = (
|
||||
entry_point
|
||||
for entry_point in distribution(dist_name).entry_points
|
||||
if entry_point.group == group and entry_point.name == name
|
||||
)
|
||||
return next(matches).load()
|
||||
|
||||
|
||||
globals().setdefault('load_entry_point', importlib_load_entry_point)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(load_entry_point('examples-rclpy-executors==0.15.3', 'console_scripts', 'callback_group')())
|
||||
@@ -0,0 +1,33 @@
|
||||
#!/usr/bin/python3
|
||||
# EASY-INSTALL-ENTRY-SCRIPT: 'examples-rclpy-executors==0.15.3','console_scripts','composed'
|
||||
import re
|
||||
import sys
|
||||
|
||||
# for compatibility with easy_install; see #2198
|
||||
__requires__ = 'examples-rclpy-executors==0.15.3'
|
||||
|
||||
try:
|
||||
from importlib.metadata import distribution
|
||||
except ImportError:
|
||||
try:
|
||||
from importlib_metadata import distribution
|
||||
except ImportError:
|
||||
from pkg_resources import load_entry_point
|
||||
|
||||
|
||||
def importlib_load_entry_point(spec, group, name):
|
||||
dist_name, _, _ = spec.partition('==')
|
||||
matches = (
|
||||
entry_point
|
||||
for entry_point in distribution(dist_name).entry_points
|
||||
if entry_point.group == group and entry_point.name == name
|
||||
)
|
||||
return next(matches).load()
|
||||
|
||||
|
||||
globals().setdefault('load_entry_point', importlib_load_entry_point)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(load_entry_point('examples-rclpy-executors==0.15.3', 'console_scripts', 'composed')())
|
||||
@@ -0,0 +1,33 @@
|
||||
#!/usr/bin/python3
|
||||
# EASY-INSTALL-ENTRY-SCRIPT: 'examples-rclpy-executors==0.15.3','console_scripts','custom_callback_group'
|
||||
import re
|
||||
import sys
|
||||
|
||||
# for compatibility with easy_install; see #2198
|
||||
__requires__ = 'examples-rclpy-executors==0.15.3'
|
||||
|
||||
try:
|
||||
from importlib.metadata import distribution
|
||||
except ImportError:
|
||||
try:
|
||||
from importlib_metadata import distribution
|
||||
except ImportError:
|
||||
from pkg_resources import load_entry_point
|
||||
|
||||
|
||||
def importlib_load_entry_point(spec, group, name):
|
||||
dist_name, _, _ = spec.partition('==')
|
||||
matches = (
|
||||
entry_point
|
||||
for entry_point in distribution(dist_name).entry_points
|
||||
if entry_point.group == group and entry_point.name == name
|
||||
)
|
||||
return next(matches).load()
|
||||
|
||||
|
||||
globals().setdefault('load_entry_point', importlib_load_entry_point)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(load_entry_point('examples-rclpy-executors==0.15.3', 'console_scripts', 'custom_callback_group')())
|
||||
@@ -0,0 +1,33 @@
|
||||
#!/usr/bin/python3
|
||||
# EASY-INSTALL-ENTRY-SCRIPT: 'examples-rclpy-executors==0.15.3','console_scripts','custom_executor'
|
||||
import re
|
||||
import sys
|
||||
|
||||
# for compatibility with easy_install; see #2198
|
||||
__requires__ = 'examples-rclpy-executors==0.15.3'
|
||||
|
||||
try:
|
||||
from importlib.metadata import distribution
|
||||
except ImportError:
|
||||
try:
|
||||
from importlib_metadata import distribution
|
||||
except ImportError:
|
||||
from pkg_resources import load_entry_point
|
||||
|
||||
|
||||
def importlib_load_entry_point(spec, group, name):
|
||||
dist_name, _, _ = spec.partition('==')
|
||||
matches = (
|
||||
entry_point
|
||||
for entry_point in distribution(dist_name).entry_points
|
||||
if entry_point.group == group and entry_point.name == name
|
||||
)
|
||||
return next(matches).load()
|
||||
|
||||
|
||||
globals().setdefault('load_entry_point', importlib_load_entry_point)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(load_entry_point('examples-rclpy-executors==0.15.3', 'console_scripts', 'custom_executor')())
|
||||
@@ -0,0 +1,33 @@
|
||||
#!/usr/bin/python3
|
||||
# EASY-INSTALL-ENTRY-SCRIPT: 'examples-rclpy-executors==0.15.3','console_scripts','listener'
|
||||
import re
|
||||
import sys
|
||||
|
||||
# for compatibility with easy_install; see #2198
|
||||
__requires__ = 'examples-rclpy-executors==0.15.3'
|
||||
|
||||
try:
|
||||
from importlib.metadata import distribution
|
||||
except ImportError:
|
||||
try:
|
||||
from importlib_metadata import distribution
|
||||
except ImportError:
|
||||
from pkg_resources import load_entry_point
|
||||
|
||||
|
||||
def importlib_load_entry_point(spec, group, name):
|
||||
dist_name, _, _ = spec.partition('==')
|
||||
matches = (
|
||||
entry_point
|
||||
for entry_point in distribution(dist_name).entry_points
|
||||
if entry_point.group == group and entry_point.name == name
|
||||
)
|
||||
return next(matches).load()
|
||||
|
||||
|
||||
globals().setdefault('load_entry_point', importlib_load_entry_point)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(load_entry_point('examples-rclpy-executors==0.15.3', 'console_scripts', 'listener')())
|
||||
33
ros2_ws/install/examples_rclpy_executors/lib/examples_rclpy_executors/talker
Executable file
33
ros2_ws/install/examples_rclpy_executors/lib/examples_rclpy_executors/talker
Executable file
@@ -0,0 +1,33 @@
|
||||
#!/usr/bin/python3
|
||||
# EASY-INSTALL-ENTRY-SCRIPT: 'examples-rclpy-executors==0.15.3','console_scripts','talker'
|
||||
import re
|
||||
import sys
|
||||
|
||||
# for compatibility with easy_install; see #2198
|
||||
__requires__ = 'examples-rclpy-executors==0.15.3'
|
||||
|
||||
try:
|
||||
from importlib.metadata import distribution
|
||||
except ImportError:
|
||||
try:
|
||||
from importlib_metadata import distribution
|
||||
except ImportError:
|
||||
from pkg_resources import load_entry_point
|
||||
|
||||
|
||||
def importlib_load_entry_point(spec, group, name):
|
||||
dist_name, _, _ = spec.partition('==')
|
||||
matches = (
|
||||
entry_point
|
||||
for entry_point in distribution(dist_name).entry_points
|
||||
if entry_point.group == group and entry_point.name == name
|
||||
)
|
||||
return next(matches).load()
|
||||
|
||||
|
||||
globals().setdefault('load_entry_point', importlib_load_entry_point)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(load_entry_point('examples-rclpy-executors==0.15.3', 'console_scripts', 'talker')())
|
||||
@@ -0,0 +1,19 @@
|
||||
Metadata-Version: 2.1
|
||||
Name: examples-rclpy-executors
|
||||
Version: 0.15.3
|
||||
Summary: Examples of creating and using exectors to run multiple nodes in rclpy.
|
||||
Home-page: UNKNOWN
|
||||
Author: Shane Loretz
|
||||
Author-email: sloretz@openrobotics.org
|
||||
Maintainer: Aditya Pande, Shane Loretz
|
||||
Maintainer-email: aditya.pande@openrobotics.org, shane@openrobotics.org
|
||||
License: Apache License, Version 2.0
|
||||
Keywords: ROS
|
||||
Platform: UNKNOWN
|
||||
Classifier: Intended Audience :: Developers
|
||||
Classifier: License :: OSI Approved :: Apache Software License
|
||||
Classifier: Programming Language :: Python
|
||||
Classifier: Topic :: Software Development
|
||||
|
||||
UNKNOWN
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package.xml
|
||||
setup.cfg
|
||||
setup.py
|
||||
../../../../build/examples_rclpy_executors/examples_rclpy_executors.egg-info/PKG-INFO
|
||||
../../../../build/examples_rclpy_executors/examples_rclpy_executors.egg-info/SOURCES.txt
|
||||
../../../../build/examples_rclpy_executors/examples_rclpy_executors.egg-info/dependency_links.txt
|
||||
../../../../build/examples_rclpy_executors/examples_rclpy_executors.egg-info/entry_points.txt
|
||||
../../../../build/examples_rclpy_executors/examples_rclpy_executors.egg-info/requires.txt
|
||||
../../../../build/examples_rclpy_executors/examples_rclpy_executors.egg-info/top_level.txt
|
||||
../../../../build/examples_rclpy_executors/examples_rclpy_executors.egg-info/zip-safe
|
||||
examples_rclpy_executors/__init__.py
|
||||
examples_rclpy_executors/callback_group.py
|
||||
examples_rclpy_executors/composed.py
|
||||
examples_rclpy_executors/custom_callback_group.py
|
||||
examples_rclpy_executors/custom_executor.py
|
||||
examples_rclpy_executors/listener.py
|
||||
examples_rclpy_executors/talker.py
|
||||
resource/examples_rclpy_executors
|
||||
test/test_copyright.py
|
||||
test/test_flake8.py
|
||||
test/test_pep257.py
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
[console_scripts]
|
||||
callback_group = examples_rclpy_executors.callback_group:main
|
||||
composed = examples_rclpy_executors.composed:main
|
||||
custom_callback_group = examples_rclpy_executors.custom_callback_group:main
|
||||
custom_executor = examples_rclpy_executors.custom_executor:main
|
||||
listener = examples_rclpy_executors.listener:main
|
||||
talker = examples_rclpy_executors.talker:main
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
setuptools
|
||||
@@ -0,0 +1 @@
|
||||
examples_rclpy_executors
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,72 @@
|
||||
# Copyright 2017 Open Source Robotics Foundation, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from examples_rclpy_executors.listener import Listener
|
||||
import rclpy
|
||||
from rclpy.callback_groups import MutuallyExclusiveCallbackGroup
|
||||
from rclpy.executors import MultiThreadedExecutor
|
||||
from rclpy.node import Node
|
||||
from std_msgs.msg import String
|
||||
|
||||
|
||||
class DoubleTalker(Node):
|
||||
"""Publish messages to a topic using two publishers at different rates."""
|
||||
|
||||
def __init__(self):
|
||||
super().__init__('double_talker')
|
||||
|
||||
self.i = 0
|
||||
self.pub = self.create_publisher(String, 'chatter', 10)
|
||||
|
||||
# This type of callback group only allows one callback to be executed at a time
|
||||
self.group = MutuallyExclusiveCallbackGroup()
|
||||
# Pass the group as a parameter to give it control over the execution of the timer callback
|
||||
self.timer = self.create_timer(1.0, self.timer_callback, callback_group=self.group)
|
||||
self.timer2 = self.create_timer(0.5, self.timer_callback, callback_group=self.group)
|
||||
|
||||
def timer_callback(self):
|
||||
msg = String()
|
||||
msg.data = 'Hello World: {0}'.format(self.i)
|
||||
self.i += 1
|
||||
self.get_logger().info('Publishing: "{0}"'.format(msg.data))
|
||||
self.pub.publish(msg)
|
||||
|
||||
|
||||
def main(args=None):
|
||||
rclpy.init(args=args)
|
||||
try:
|
||||
talker = DoubleTalker()
|
||||
listener = Listener()
|
||||
# MultiThreadedExecutor executes callbacks with a thread pool. If num_threads is not
|
||||
# specified then num_threads will be multiprocessing.cpu_count() if it is implemented.
|
||||
# Otherwise it will use a single thread. This executor will allow callbacks to happen in
|
||||
# parallel, however the MutuallyExclusiveCallbackGroup in DoubleTalker will only allow its
|
||||
# callbacks to be executed one at a time. The callbacks in Listener are free to execute in
|
||||
# parallel to the ones in DoubleTalker however.
|
||||
executor = MultiThreadedExecutor(num_threads=4)
|
||||
executor.add_node(talker)
|
||||
executor.add_node(listener)
|
||||
|
||||
try:
|
||||
executor.spin()
|
||||
finally:
|
||||
executor.shutdown()
|
||||
listener.destroy_node()
|
||||
talker.destroy_node()
|
||||
finally:
|
||||
rclpy.shutdown()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,52 @@
|
||||
# Copyright 2017 Open Source Robotics Foundation, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import sys
|
||||
|
||||
from examples_rclpy_executors.listener import Listener
|
||||
from examples_rclpy_executors.talker import Talker
|
||||
import rclpy
|
||||
from rclpy.executors import ExternalShutdownException
|
||||
from rclpy.executors import SingleThreadedExecutor
|
||||
|
||||
|
||||
def main(args=None):
|
||||
rclpy.init(args=args)
|
||||
try:
|
||||
talker = Talker()
|
||||
listener = Listener()
|
||||
|
||||
# Runs all callbacks in the main thread
|
||||
executor = SingleThreadedExecutor()
|
||||
# Add imported nodes to this executor
|
||||
executor.add_node(talker)
|
||||
executor.add_node(listener)
|
||||
|
||||
try:
|
||||
# Execute callbacks for both nodes as they become ready
|
||||
executor.spin()
|
||||
finally:
|
||||
executor.shutdown()
|
||||
listener.destroy_node()
|
||||
talker.destroy_node()
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
except ExternalShutdownException:
|
||||
sys.exit(1)
|
||||
finally:
|
||||
rclpy.try_shutdown()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,116 @@
|
||||
# Copyright 2017 Open Source Robotics Foundation, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import sys
|
||||
import threading
|
||||
|
||||
import rclpy
|
||||
from rclpy.callback_groups import CallbackGroup
|
||||
from rclpy.executors import ExternalShutdownException
|
||||
from rclpy.node import Node
|
||||
from std_msgs.msg import String
|
||||
|
||||
|
||||
class ThrottledCallbackGroup(CallbackGroup):
|
||||
"""
|
||||
Throttle callbacks using a token bucket.
|
||||
|
||||
Callback groups are responsible for controlling when callbacks are allowed to be executed.
|
||||
rclpy provides two groups: one which always allows a callback to be executed, and another which
|
||||
allows only one callback to be executed at a time. If neither of these are sufficient then a
|
||||
custom callback group should be used instead.
|
||||
"""
|
||||
|
||||
def __init__(self, node):
|
||||
super().__init__()
|
||||
self.timer = node.create_timer(0.5, self.timer_callback)
|
||||
self.bucket = 10
|
||||
self.bucket_max = 10
|
||||
self.lock = threading.Lock()
|
||||
|
||||
def can_execute(self, entity):
|
||||
"""
|
||||
Ask group if this entity could be executed.
|
||||
|
||||
:param entity: A timer, subscriber, client, or service instance
|
||||
:rtype bool: true if a callback can be executed
|
||||
"""
|
||||
return self.bucket > 0
|
||||
|
||||
def beginning_execution(self, entity):
|
||||
"""
|
||||
Get permission from the group to execute a callback for an entity.
|
||||
|
||||
:param entity: A timer, subscriber, client, or service instance
|
||||
:rtype bool: true if the executor has permission to execute it
|
||||
"""
|
||||
with self.lock:
|
||||
if self.bucket > 0:
|
||||
# Take a token
|
||||
self.bucket -= 1
|
||||
return True
|
||||
# The bucket has no tokens
|
||||
return False
|
||||
|
||||
def ending_execution(self, entity):
|
||||
"""
|
||||
Notify group that a callback finished executing.
|
||||
|
||||
:param entity: A timer, subscriber, client, or service instance
|
||||
"""
|
||||
pass
|
||||
|
||||
def timer_callback(self):
|
||||
"""Replenish the tokens in the bucket at a steady rate."""
|
||||
with self.lock:
|
||||
# If there is room in the bucket, add a token to it.
|
||||
if self.bucket < self.bucket_max:
|
||||
self.bucket += 1
|
||||
|
||||
|
||||
class ThrottledTalker(Node):
|
||||
"""A Node which uses a custom callback group."""
|
||||
|
||||
def __init__(self):
|
||||
super().__init__('intermittent_talker')
|
||||
self.i = 0
|
||||
self.pub = self.create_publisher(String, 'chatter', 10)
|
||||
self.group = ThrottledCallbackGroup(self)
|
||||
# Timer triggers very quickly, but is part of a throttled group
|
||||
self.timer = self.create_timer(0.1, self.timer_callback, callback_group=self.group)
|
||||
|
||||
def timer_callback(self):
|
||||
msg = String()
|
||||
msg.data = 'Hello World: {0}'.format(self.i)
|
||||
self.i += 1
|
||||
self.get_logger().info('Publishing: "{0}"'.format(msg.data))
|
||||
self.pub.publish(msg)
|
||||
|
||||
|
||||
def main(args=None):
|
||||
rclpy.init(args=args)
|
||||
try:
|
||||
talker = ThrottledTalker()
|
||||
rclpy.spin(talker)
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
except ExternalShutdownException:
|
||||
sys.exit(1)
|
||||
finally:
|
||||
rclpy.try_shutdown()
|
||||
talker.destroy_node()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,103 @@
|
||||
# Copyright 2017 Open Source Robotics Foundation, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import os
|
||||
|
||||
from examples_rclpy_executors.listener import Listener
|
||||
from examples_rclpy_executors.talker import Talker
|
||||
import rclpy
|
||||
from rclpy.executors import Executor
|
||||
from rclpy.node import Node
|
||||
from std_msgs.msg import String
|
||||
|
||||
|
||||
class Estopper(Node):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__('estopper')
|
||||
self.sub = self.create_subscription(String, 'estop', self.estop_callback, 10)
|
||||
|
||||
def estop_callback(self, msg):
|
||||
self.get_logger().info('I heard: "%s"' % msg.data)
|
||||
|
||||
|
||||
class PriorityExecutor(Executor):
|
||||
"""
|
||||
Execute high priority callbacks in multiple threads, all others in a single thread.
|
||||
|
||||
This is an example of a custom exectuor in python. Executors are responsible for managing
|
||||
how callbacks get mapped to threads. Rclpy provides two executors: one which runs all callbacks
|
||||
in the main thread, and another which runs callbacks in a pool of threads. A custom executor
|
||||
should be written if neither are appropriate for your application.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.high_priority_nodes = set()
|
||||
self.hp_executor = ThreadPoolExecutor(max_workers=os.cpu_count() or 4)
|
||||
self.lp_executor = ThreadPoolExecutor(max_workers=1)
|
||||
|
||||
def add_high_priority_node(self, node):
|
||||
self.high_priority_nodes.add(node)
|
||||
# add_node inherited
|
||||
self.add_node(node)
|
||||
|
||||
def spin_once(self, timeout_sec=None):
|
||||
"""
|
||||
Execute a single callback, then return.
|
||||
|
||||
This is the only function which must be overridden by a custom executor. Its job is to
|
||||
start executing one callback, then return. It uses the method `wait_for_ready_callbacks`
|
||||
to get work to execute.
|
||||
|
||||
:param timeout_sec: Seconds to wait. Block forever if None. Don't wait if <= 0
|
||||
:type timeout_sec: float or None
|
||||
"""
|
||||
# wait_for_ready_callbacks yields callbacks that are ready to be executed
|
||||
try:
|
||||
handler, group, node = self.wait_for_ready_callbacks(timeout_sec=timeout_sec)
|
||||
except StopIteration:
|
||||
pass
|
||||
else:
|
||||
if node in self.high_priority_nodes:
|
||||
self.hp_executor.submit(handler)
|
||||
else:
|
||||
self.lp_executor.submit(handler)
|
||||
|
||||
|
||||
def main(args=None):
|
||||
rclpy.init(args=args)
|
||||
try:
|
||||
listener = Listener()
|
||||
talker = Talker()
|
||||
estopper = Estopper()
|
||||
|
||||
executor = PriorityExecutor()
|
||||
executor.add_high_priority_node(estopper)
|
||||
executor.add_node(listener)
|
||||
executor.add_node(talker)
|
||||
try:
|
||||
executor.spin()
|
||||
finally:
|
||||
executor.shutdown()
|
||||
estopper.destroy_node()
|
||||
talker.destroy_node()
|
||||
listener.destroy_node()
|
||||
finally:
|
||||
rclpy.shutdown()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,66 @@
|
||||
# Copyright 2017 Open Source Robotics Foundation, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import sys
|
||||
|
||||
import rclpy
|
||||
from rclpy.executors import ExternalShutdownException
|
||||
from rclpy.node import Node
|
||||
from std_msgs.msg import String
|
||||
|
||||
|
||||
class Listener(Node):
|
||||
"""
|
||||
A node with a single subscriber.
|
||||
|
||||
This class creates a node which prints messages it receives on a topic. Creating a node by
|
||||
inheriting from Node is recommended because it allows it to be imported and used by
|
||||
other scripts.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
# Calls Node.__init__('listener')
|
||||
super().__init__('listener')
|
||||
self.sub = self.create_subscription(String, 'chatter', self.chatter_callback, 10)
|
||||
|
||||
def chatter_callback(self, msg):
|
||||
self.get_logger().info('I heard: "%s"' % msg.data)
|
||||
|
||||
|
||||
def main(args=None):
|
||||
"""
|
||||
Run a Listener node standalone.
|
||||
|
||||
This function is called directly when using an entrypoint. Entrypoints are configured in
|
||||
setup.py. This along with the script installation in setup.cfg allows a listener node to be run
|
||||
with the command `ros2 run examples_rclpy_executors listener`.
|
||||
|
||||
:param args: Arguments passed in from the command line.
|
||||
"""
|
||||
rclpy.init(args=args)
|
||||
try:
|
||||
listener = Listener()
|
||||
rclpy.spin(listener)
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
except ExternalShutdownException:
|
||||
sys.exit(1)
|
||||
finally:
|
||||
rclpy.try_shutdown()
|
||||
listener.destroy_node()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Runs a listener node when this script is run directly (not through an entrypoint)
|
||||
main()
|
||||
@@ -0,0 +1,76 @@
|
||||
# Copyright 2017 Open Source Robotics Foundation, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import sys
|
||||
|
||||
import rclpy
|
||||
from rclpy.executors import ExternalShutdownException
|
||||
from rclpy.node import Node
|
||||
from std_msgs.msg import String
|
||||
|
||||
|
||||
class Talker(Node):
|
||||
"""
|
||||
A node with a single publisher.
|
||||
|
||||
This class creates a node which regularly publishes messages on a topic. Creating a node by
|
||||
inheriting from Node is recommended because it allows it to be imported and used by
|
||||
other scripts.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
# Calls Node.__init__('talker')
|
||||
super().__init__('talker')
|
||||
self.i = 0
|
||||
self.pub = self.create_publisher(String, 'chatter', 10)
|
||||
# Create a timer that calls a callback every second. A timer is recommended for executing
|
||||
# periodic tasks because it does not block the main thread while it's waiting. This allows
|
||||
# an executor to do other work when mutliple nodes are run in the same process.
|
||||
self.timer = self.create_timer(1.0, self.timer_callback)
|
||||
|
||||
def timer_callback(self):
|
||||
msg = String()
|
||||
msg.data = 'Hello World: {0}'.format(self.i)
|
||||
self.i += 1
|
||||
self.get_logger().info('Publishing: "{0}"'.format(msg.data))
|
||||
self.pub.publish(msg)
|
||||
|
||||
|
||||
def main(args=None):
|
||||
"""
|
||||
Run a Talker node standalone.
|
||||
|
||||
This function is called directly when using an entrypoint. Entrypoints are configured in
|
||||
setup.py. This along with the script installation in setup.cfg allows a talker node to be run
|
||||
with the command `ros2 run examples_rclpy_executors talker`.
|
||||
|
||||
:param args: Arguments passed in from the command line.
|
||||
"""
|
||||
# Run standalone
|
||||
rclpy.init(args=args)
|
||||
try:
|
||||
talker = Talker()
|
||||
rclpy.spin(talker)
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
except ExternalShutdownException:
|
||||
sys.exit(1)
|
||||
finally:
|
||||
rclpy.try_shutdown()
|
||||
talker.destroy_node()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Runs a talker node when this script is run directly (not through an entrypoint)
|
||||
main()
|
||||
@@ -0,0 +1 @@
|
||||
rclpy:std_msgs
|
||||
@@ -0,0 +1 @@
|
||||
prepend-non-duplicate;AMENT_PREFIX_PATH;
|
||||
@@ -0,0 +1,3 @@
|
||||
# generated from colcon_powershell/shell/template/hook_prepend_value.ps1.em
|
||||
|
||||
colcon_prepend_unique_value AMENT_PREFIX_PATH "$env:COLCON_CURRENT_PREFIX"
|
||||
@@ -0,0 +1,3 @@
|
||||
# generated from colcon_core/shell/template/hook_prepend_value.sh.em
|
||||
|
||||
_colcon_prepend_unique_value AMENT_PREFIX_PATH "$COLCON_CURRENT_PREFIX"
|
||||
@@ -0,0 +1 @@
|
||||
prepend-non-duplicate;PYTHONPATH;lib/python3.10/site-packages
|
||||
@@ -0,0 +1,3 @@
|
||||
# generated from colcon_powershell/shell/template/hook_prepend_value.ps1.em
|
||||
|
||||
colcon_prepend_unique_value PYTHONPATH "$env:COLCON_CURRENT_PREFIX\lib/python3.10/site-packages"
|
||||
@@ -0,0 +1,3 @@
|
||||
# generated from colcon_core/shell/template/hook_prepend_value.sh.em
|
||||
|
||||
_colcon_prepend_unique_value PYTHONPATH "$COLCON_CURRENT_PREFIX/lib/python3.10/site-packages"
|
||||
@@ -0,0 +1,31 @@
|
||||
# generated from colcon_bash/shell/template/package.bash.em
|
||||
|
||||
# This script extends the environment for this package.
|
||||
|
||||
# a bash script is able to determine its own path if necessary
|
||||
if [ -z "$COLCON_CURRENT_PREFIX" ]; then
|
||||
# the prefix is two levels up from the package specific share directory
|
||||
_colcon_package_bash_COLCON_CURRENT_PREFIX="$(builtin cd "`dirname "${BASH_SOURCE[0]}"`/../.." > /dev/null && pwd)"
|
||||
else
|
||||
_colcon_package_bash_COLCON_CURRENT_PREFIX="$COLCON_CURRENT_PREFIX"
|
||||
fi
|
||||
|
||||
# function to source another script with conditional trace output
|
||||
# first argument: the path of the script
|
||||
# additional arguments: arguments to the script
|
||||
_colcon_package_bash_source_script() {
|
||||
if [ -f "$1" ]; then
|
||||
if [ -n "$COLCON_TRACE" ]; then
|
||||
echo "# . \"$1\""
|
||||
fi
|
||||
. "$@"
|
||||
else
|
||||
echo "not found: \"$1\"" 1>&2
|
||||
fi
|
||||
}
|
||||
|
||||
# source sh script of this package
|
||||
_colcon_package_bash_source_script "$_colcon_package_bash_COLCON_CURRENT_PREFIX/share/examples_rclpy_executors/package.sh"
|
||||
|
||||
unset _colcon_package_bash_source_script
|
||||
unset _colcon_package_bash_COLCON_CURRENT_PREFIX
|
||||
@@ -0,0 +1,6 @@
|
||||
source;share/examples_rclpy_executors/hook/pythonpath.ps1
|
||||
source;share/examples_rclpy_executors/hook/pythonpath.dsv
|
||||
source;share/examples_rclpy_executors/hook/pythonpath.sh
|
||||
source;share/examples_rclpy_executors/hook/ament_prefix_path.ps1
|
||||
source;share/examples_rclpy_executors/hook/ament_prefix_path.dsv
|
||||
source;share/examples_rclpy_executors/hook/ament_prefix_path.sh
|
||||
@@ -0,0 +1,116 @@
|
||||
# generated from colcon_powershell/shell/template/package.ps1.em
|
||||
|
||||
# function to append a value to a variable
|
||||
# which uses colons as separators
|
||||
# duplicates as well as leading separators are avoided
|
||||
# first argument: the name of the result variable
|
||||
# second argument: the value to be prepended
|
||||
function colcon_append_unique_value {
|
||||
param (
|
||||
$_listname,
|
||||
$_value
|
||||
)
|
||||
|
||||
# get values from variable
|
||||
if (Test-Path Env:$_listname) {
|
||||
$_values=(Get-Item env:$_listname).Value
|
||||
} else {
|
||||
$_values=""
|
||||
}
|
||||
$_duplicate=""
|
||||
# start with no values
|
||||
$_all_values=""
|
||||
# iterate over existing values in the variable
|
||||
if ($_values) {
|
||||
$_values.Split(";") | ForEach {
|
||||
# not an empty string
|
||||
if ($_) {
|
||||
# not a duplicate of _value
|
||||
if ($_ -eq $_value) {
|
||||
$_duplicate="1"
|
||||
}
|
||||
if ($_all_values) {
|
||||
$_all_values="${_all_values};$_"
|
||||
} else {
|
||||
$_all_values="$_"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
# append only non-duplicates
|
||||
if (!$_duplicate) {
|
||||
# avoid leading separator
|
||||
if ($_all_values) {
|
||||
$_all_values="${_all_values};${_value}"
|
||||
} else {
|
||||
$_all_values="${_value}"
|
||||
}
|
||||
}
|
||||
|
||||
# export the updated variable
|
||||
Set-Item env:\$_listname -Value "$_all_values"
|
||||
}
|
||||
|
||||
# function to prepend a value to a variable
|
||||
# which uses colons as separators
|
||||
# duplicates as well as trailing separators are avoided
|
||||
# first argument: the name of the result variable
|
||||
# second argument: the value to be prepended
|
||||
function colcon_prepend_unique_value {
|
||||
param (
|
||||
$_listname,
|
||||
$_value
|
||||
)
|
||||
|
||||
# get values from variable
|
||||
if (Test-Path Env:$_listname) {
|
||||
$_values=(Get-Item env:$_listname).Value
|
||||
} else {
|
||||
$_values=""
|
||||
}
|
||||
# start with the new value
|
||||
$_all_values="$_value"
|
||||
# iterate over existing values in the variable
|
||||
if ($_values) {
|
||||
$_values.Split(";") | ForEach {
|
||||
# not an empty string
|
||||
if ($_) {
|
||||
# not a duplicate of _value
|
||||
if ($_ -ne $_value) {
|
||||
# keep non-duplicate values
|
||||
$_all_values="${_all_values};$_"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
# export the updated variable
|
||||
Set-Item env:\$_listname -Value "$_all_values"
|
||||
}
|
||||
|
||||
# function to source another script with conditional trace output
|
||||
# first argument: the path of the script
|
||||
# additional arguments: arguments to the script
|
||||
function colcon_package_source_powershell_script {
|
||||
param (
|
||||
$_colcon_package_source_powershell_script
|
||||
)
|
||||
# source script with conditional trace output
|
||||
if (Test-Path $_colcon_package_source_powershell_script) {
|
||||
if ($env:COLCON_TRACE) {
|
||||
echo ". '$_colcon_package_source_powershell_script'"
|
||||
}
|
||||
. "$_colcon_package_source_powershell_script"
|
||||
} else {
|
||||
Write-Error "not found: '$_colcon_package_source_powershell_script'"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# a powershell script is able to determine its own path
|
||||
# the prefix is two levels up from the package specific share directory
|
||||
$env:COLCON_CURRENT_PREFIX=(Get-Item $PSCommandPath).Directory.Parent.Parent.FullName
|
||||
|
||||
colcon_package_source_powershell_script "$env:COLCON_CURRENT_PREFIX\share/examples_rclpy_executors/hook/pythonpath.ps1"
|
||||
colcon_package_source_powershell_script "$env:COLCON_CURRENT_PREFIX\share/examples_rclpy_executors/hook/ament_prefix_path.ps1"
|
||||
|
||||
Remove-Item Env:\COLCON_CURRENT_PREFIX
|
||||
@@ -0,0 +1,87 @@
|
||||
# generated from colcon_core/shell/template/package.sh.em
|
||||
|
||||
# This script extends the environment for this package.
|
||||
|
||||
# function to prepend a value to a variable
|
||||
# which uses colons as separators
|
||||
# duplicates as well as trailing separators are avoided
|
||||
# first argument: the name of the result variable
|
||||
# second argument: the value to be prepended
|
||||
_colcon_prepend_unique_value() {
|
||||
# arguments
|
||||
_listname="$1"
|
||||
_value="$2"
|
||||
|
||||
# get values from variable
|
||||
eval _values=\"\$$_listname\"
|
||||
# backup the field separator
|
||||
_colcon_prepend_unique_value_IFS=$IFS
|
||||
IFS=":"
|
||||
# start with the new value
|
||||
_all_values="$_value"
|
||||
# workaround SH_WORD_SPLIT not being set in zsh
|
||||
if [ "$(command -v colcon_zsh_convert_to_array)" ]; then
|
||||
colcon_zsh_convert_to_array _values
|
||||
fi
|
||||
# iterate over existing values in the variable
|
||||
for _item in $_values; do
|
||||
# ignore empty strings
|
||||
if [ -z "$_item" ]; then
|
||||
continue
|
||||
fi
|
||||
# ignore duplicates of _value
|
||||
if [ "$_item" = "$_value" ]; then
|
||||
continue
|
||||
fi
|
||||
# keep non-duplicate values
|
||||
_all_values="$_all_values:$_item"
|
||||
done
|
||||
unset _item
|
||||
# restore the field separator
|
||||
IFS=$_colcon_prepend_unique_value_IFS
|
||||
unset _colcon_prepend_unique_value_IFS
|
||||
# export the updated variable
|
||||
eval export $_listname=\"$_all_values\"
|
||||
unset _all_values
|
||||
unset _values
|
||||
|
||||
unset _value
|
||||
unset _listname
|
||||
}
|
||||
|
||||
# since a plain shell script can't determine its own path when being sourced
|
||||
# either use the provided COLCON_CURRENT_PREFIX
|
||||
# or fall back to the build time prefix (if it exists)
|
||||
_colcon_package_sh_COLCON_CURRENT_PREFIX="/root/ros2_ws/install/examples_rclpy_executors"
|
||||
if [ -z "$COLCON_CURRENT_PREFIX" ]; then
|
||||
if [ ! -d "$_colcon_package_sh_COLCON_CURRENT_PREFIX" ]; then
|
||||
echo "The build time path \"$_colcon_package_sh_COLCON_CURRENT_PREFIX\" doesn't exist. Either source a script for a different shell or set the environment variable \"COLCON_CURRENT_PREFIX\" explicitly." 1>&2
|
||||
unset _colcon_package_sh_COLCON_CURRENT_PREFIX
|
||||
return 1
|
||||
fi
|
||||
COLCON_CURRENT_PREFIX="$_colcon_package_sh_COLCON_CURRENT_PREFIX"
|
||||
fi
|
||||
unset _colcon_package_sh_COLCON_CURRENT_PREFIX
|
||||
|
||||
# function to source another script with conditional trace output
|
||||
# first argument: the path of the script
|
||||
# additional arguments: arguments to the script
|
||||
_colcon_package_sh_source_script() {
|
||||
if [ -f "$1" ]; then
|
||||
if [ -n "$COLCON_TRACE" ]; then
|
||||
echo "# . \"$1\""
|
||||
fi
|
||||
. "$@"
|
||||
else
|
||||
echo "not found: \"$1\"" 1>&2
|
||||
fi
|
||||
}
|
||||
|
||||
# source sh hooks
|
||||
_colcon_package_sh_source_script "$COLCON_CURRENT_PREFIX/share/examples_rclpy_executors/hook/pythonpath.sh"
|
||||
_colcon_package_sh_source_script "$COLCON_CURRENT_PREFIX/share/examples_rclpy_executors/hook/ament_prefix_path.sh"
|
||||
|
||||
unset _colcon_package_sh_source_script
|
||||
unset COLCON_CURRENT_PREFIX
|
||||
|
||||
# do not unset _colcon_prepend_unique_value since it might be used by non-primary shell hooks
|
||||
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0"?>
|
||||
<?xml-model href="http://download.ros.org/schema/package_format2.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
|
||||
<package format="2">
|
||||
<name>examples_rclpy_executors</name>
|
||||
<version>0.15.3</version>
|
||||
<description>Examples of creating and using exectors to run multiple nodes in the same process</description>
|
||||
<maintainer email="sloretz@openrobotics.org">Shane Loretz</maintainer>
|
||||
<maintainer email="aditya.pande@openrobotics.org">Aditya Pande</maintainer>
|
||||
<license>Apache License 2.0</license>
|
||||
|
||||
|
||||
<exec_depend>rclpy</exec_depend>
|
||||
<exec_depend>std_msgs</exec_depend>
|
||||
|
||||
<test_depend>ament_copyright</test_depend>
|
||||
<test_depend>ament_flake8</test_depend>
|
||||
<test_depend>ament_pep257</test_depend>
|
||||
<test_depend>python3-pytest</test_depend>
|
||||
|
||||
<export>
|
||||
<build_type>ament_python</build_type>
|
||||
</export>
|
||||
</package>
|
||||
@@ -0,0 +1,42 @@
|
||||
# generated from colcon_zsh/shell/template/package.zsh.em
|
||||
|
||||
# This script extends the environment for this package.
|
||||
|
||||
# a zsh script is able to determine its own path if necessary
|
||||
if [ -z "$COLCON_CURRENT_PREFIX" ]; then
|
||||
# the prefix is two levels up from the package specific share directory
|
||||
_colcon_package_zsh_COLCON_CURRENT_PREFIX="$(builtin cd -q "`dirname "${(%):-%N}"`/../.." > /dev/null && pwd)"
|
||||
else
|
||||
_colcon_package_zsh_COLCON_CURRENT_PREFIX="$COLCON_CURRENT_PREFIX"
|
||||
fi
|
||||
|
||||
# function to source another script with conditional trace output
|
||||
# first argument: the path of the script
|
||||
# additional arguments: arguments to the script
|
||||
_colcon_package_zsh_source_script() {
|
||||
if [ -f "$1" ]; then
|
||||
if [ -n "$COLCON_TRACE" ]; then
|
||||
echo "# . \"$1\""
|
||||
fi
|
||||
. "$@"
|
||||
else
|
||||
echo "not found: \"$1\"" 1>&2
|
||||
fi
|
||||
}
|
||||
|
||||
# function to convert array-like strings into arrays
|
||||
# to workaround SH_WORD_SPLIT not being set
|
||||
colcon_zsh_convert_to_array() {
|
||||
local _listname=$1
|
||||
local _dollar="$"
|
||||
local _split="{="
|
||||
local _to_array="(\"$_dollar$_split$_listname}\")"
|
||||
eval $_listname=$_to_array
|
||||
}
|
||||
|
||||
# source sh script of this package
|
||||
_colcon_package_zsh_source_script "$_colcon_package_zsh_COLCON_CURRENT_PREFIX/share/examples_rclpy_executors/package.sh"
|
||||
unset convert_zsh_to_array
|
||||
|
||||
unset _colcon_package_zsh_source_script
|
||||
unset _colcon_package_zsh_COLCON_CURRENT_PREFIX
|
||||
Reference in New Issue
Block a user