本文整理汇总了Python中rclpy.init方法的典型用法代码示例。如果您正苦于以下问题:Python rclpy.init方法的具体用法?Python rclpy.init怎么用?Python rclpy.init使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rclpy
的用法示例。
在下文中一共展示了rclpy.init方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import init [as 别名]
def main(args=None):
rclpy.init(args=args)
node = rclpy.create_node('minimal_subscriber')
subscription = node.create_subscription(
String, 'topic', lambda msg: node.get_logger().info('I heard: "%s"' % msg.data), 10)
subscription # prevent unused variable warning
rclpy.spin(node)
# Destroy the node explicitly
# (optional - otherwise it will be done automatically
# when the garbage collector destroys the node object)
node.destroy_node()
rclpy.shutdown()
示例2: main
# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import init [as 别名]
def main(args=None):
rclpy.init(args=args)
node = EchoServer()
try:
rclpy.spin(node)
except KeyboardInterrupt:
print('server stopped cleanly')
except BaseException:
print('exception in server:', file=sys.stderr)
raise
finally:
# Destroy the node explicitly
# (optional - Done automatically when node is garbage collected)
node.destroy_node()
rclpy.shutdown()
示例3: main
# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import init [as 别名]
def main(args=None):
parsed_args = parse_arguments(args=args)
rclpy.init(args=args)
node = RepeaterNode(message_type=parsed_args.message_type)
try:
rclpy.spin(node)
except KeyboardInterrupt:
print('repeater stopped cleanly')
except BaseException:
print('exception in repeater:', file=sys.stderr)
raise
finally:
node.destroy_node()
rclpy.shutdown()
示例4: main
# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import init [as 别名]
def main(args=None):
global g_node
rclpy.init(args=args)
g_node = rclpy.create_node('minimal_subscriber')
subscription = g_node.create_subscription(String, 'topic', chatter_callback, 10)
subscription # prevent unused variable warning
while rclpy.ok():
rclpy.spin_once(g_node)
# Destroy the node explicitly
# (optional - otherwise it will be done automatically
# when the garbage collector destroys the node object)
g_node.destroy_node()
rclpy.shutdown()
示例5: main
# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import init [as 别名]
def main(args=None):
rclpy.init(args=args)
node = rclpy.create_node('minimal_publisher')
publisher = node.create_publisher(String, 'topic', 10)
msg = String()
i = 0
while rclpy.ok():
msg.data = 'Hello World: %d' % i
i += 1
node.get_logger().info('Publishing: "%s"' % msg.data)
publisher.publish(msg)
sleep(0.5) # seconds
# Destroy the node explicitly
# (optional - otherwise it will be done automatically
# when the garbage collector destroys the node object)
node.destroy_node()
rclpy.shutdown()
示例6: main
# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import init [as 别名]
def main(args=None):
rclpy.init(args=args)
minimal_client = MinimalClientAsync()
minimal_client.send_request()
while rclpy.ok():
rclpy.spin_once(minimal_client)
if minimal_client.future.done():
try:
response = minimal_client.future.result()
except Exception as e:
minimal_client.get_logger().info(
'Service call failed %r' % (e,))
else:
minimal_client.get_logger().info(
'Result of add_two_ints: for %d + %d = %d' %
(minimal_client.req.a, minimal_client.req.b, response.sum))
break
minimal_client.destroy_node()
rclpy.shutdown()
示例7: main
# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import init [as 别名]
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()
示例8: main
# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import init [as 别名]
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)
finally:
listener.destroy_node()
rclpy.shutdown()
示例9: main
# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import init [as 别名]
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)
finally:
talker.destroy_node()
rclpy.shutdown()
示例10: main
# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import init [as 别名]
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()
示例11: setUp
# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import init [as 别名]
def setUp(cls):
cls.context = rclpy.context.Context()
rclpy.init(context=cls.context)
cls.node = rclpy.create_node(
'node',
context=cls.context,
cli_args=[
'--ros-args', '-r', '{}:={}'.format(TEST_TOPIC_FROM, TEST_TOPIC_TO),
'--ros-args', '-r', '{}:={}'.format(TEST_FQN_TOPIC_FROM, TEST_FQN_TOPIC_TO)
],
)
cls.node_with_ns = rclpy.create_node(
'node_withns',
context=cls.context,
namespace=TEST_NODE_NAMESPACE,
)
示例12: test_use_global_arguments
# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import init [as 别名]
def test_use_global_arguments(self):
context = rclpy.context.Context()
rclpy.init(
args=['process_name', '--ros-args', '-r', '__node:=global_node_name'],
context=context
)
try:
node1 = rclpy.create_node(
'my_node', namespace='/my_ns', use_global_arguments=True, context=context)
node2 = rclpy.create_node(
'my_node', namespace='/my_ns', use_global_arguments=False, context=context)
self.assertEqual('global_node_name', node1.get_name())
self.assertEqual('my_node', node2.get_name())
node1.destroy_node()
node2.destroy_node()
finally:
rclpy.shutdown(context=context)
示例13: test_bad_node_arguments
# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import init [as 别名]
def test_bad_node_arguments(self):
context = rclpy.context.Context()
rclpy.init(context=context)
from rclpy.impl.implementation_singleton import rclpy_implementation as _rclpy
invalid_ros_args_error_pattern = r'Failed to parse ROS arguments:.*not-a-remap.*'
with self.assertRaisesRegex(_rclpy.RCLInvalidROSArgsError, invalid_ros_args_error_pattern):
rclpy.create_node(
'my_node',
namespace='/my_ns',
cli_args=['--ros-args', '-r', 'not-a-remap'],
context=context)
unknown_ros_args_error_pattern = r'Found unknown ROS arguments:.*\[\'--my-custom-flag\'\]'
with self.assertRaisesRegex(_rclpy.UnknownROSArgsError, unknown_ros_args_error_pattern):
rclpy.create_node(
'my_node',
namespace='/my_ns',
cli_args=['--ros-args', '--my-custom-flag'],
context=context)
rclpy.shutdown(context=context)
示例14: test_destroy_node_asap
# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import init [as 别名]
def test_destroy_node_asap():
context = rclpy.context.Context()
rclpy.init(context=context)
try:
node = rclpy.create_node('test_destroy_subscription_asap', context=context)
with node.handle:
node.destroy_node()
# handle valid because it's still being used
with node.handle:
pass
with pytest.raises(InvalidHandle):
# handle invalid because it was destroyed when no one was using it
with node.handle:
pass
finally:
rclpy.shutdown(context=context)
示例15: setUpClass
# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import init [as 别名]
def setUpClass(cls):
cls.context = rclpy.context.Context()
rclpy.init(context=cls.context)
cls.node0 = rclpy.create_node(TEST_NODE0, namespace=TEST_NAMESPACE0, context=cls.context)
cls.node1 = rclpy.create_node(TEST_NODE1, namespace=TEST_NAMESPACE1, context=cls.context)
cls.node2 = rclpy.create_node(TEST_NODE2, namespace=TEST_NAMESPACE2, context=cls.context)
cls.action_client10 = ActionClient(cls.node1, Fibonacci, TEST_ACTION0)
cls.action_server10 = ActionServer(cls.node1, Fibonacci, TEST_ACTION0, lambda: None)
cls.action_client20 = ActionClient(cls.node2, Fibonacci, TEST_ACTION0)
cls.action_client21 = ActionClient(cls.node2, Fibonacci, TEST_ACTION1)
cls.action_server20 = ActionServer(cls.node2, Fibonacci, TEST_ACTION0, lambda: None)
cls.action_server21 = ActionServer(cls.node2, Fibonacci, TEST_ACTION1, lambda: None)
assert cls.wait_for_node(node=cls.node1, remote_node=cls.node0, timeout=2)
assert cls.wait_for_node(node=cls.node1, remote_node=cls.node2, timeout=2)
assert cls.wait_for_node(node=cls.node2, remote_node=cls.node0, timeout=2)
assert cls.wait_for_node(node=cls.node2, remote_node=cls.node1, timeout=2)