当前位置: 首页>>代码示例>>Python>>正文


Python rclpy.create_node方法代码示例

本文整理汇总了Python中rclpy.create_node方法的典型用法代码示例。如果您正苦于以下问题:Python rclpy.create_node方法的具体用法?Python rclpy.create_node怎么用?Python rclpy.create_node使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在rclpy的用法示例。


在下文中一共展示了rclpy.create_node方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import create_node [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() 
开发者ID:ros2,项目名称:examples,代码行数:18,代码来源:subscriber_lambda.py

示例2: main

# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import create_node [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() 
开发者ID:ros2,项目名称:examples,代码行数:19,代码来源:subscriber_old_school.py

示例3: main

# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import create_node [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() 
开发者ID:ros2,项目名称:examples,代码行数:24,代码来源:publisher_old_school.py

示例4: main

# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import create_node [as 别名]
def main(args=None):
    rclpy.init(args=args)
    node = rclpy.create_node('minimal_client')
    cli = node.create_client(AddTwoInts, 'add_two_ints')

    req = AddTwoInts.Request()
    req.a = 41
    req.b = 1
    while not cli.wait_for_service(timeout_sec=1.0):
        node.get_logger().info('service not available, waiting again...')

    future = cli.call_async(req)
    rclpy.spin_until_future_complete(node, future)

    try:
        result = future.result()
    except Exception as e:
        node.get_logger().info('Service call failed %r' % (e,))
    else:
        node.get_logger().info(
            'Result of add_two_ints: for %d + %d = %d' %
            (req.a, req.b, result.sum))

    node.destroy_node()
    rclpy.shutdown() 
开发者ID:ros2,项目名称:examples,代码行数:27,代码来源:client.py

示例5: setUp

# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import create_node [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,
        ) 
开发者ID:ros2,项目名称:rclpy,代码行数:18,代码来源:test_publisher.py

示例6: setUp

# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import create_node [as 别名]
def setUp(self):
        self.context = rclpy.context.Context()
        rclpy.init(context=self.context)
        self.node = rclpy.create_node(
            TEST_NODE,
            namespace=TEST_NAMESPACE,
            context=self.context,
            parameter_overrides=[
                Parameter('initial_foo', Parameter.Type.INTEGER, 4321),
                Parameter('initial_bar', Parameter.Type.STRING, 'init_param'),
                Parameter('initial_baz', Parameter.Type.DOUBLE, 3.14)
            ],
            cli_args=[
                '--ros-args', '-p', 'initial_fizz:=buzz',
                '--params-file', str(TEST_RESOURCES_DIR / 'test_parameters.yaml'),
                '-p', 'initial_buzz:=1.'
            ],
            automatically_declare_parameters_from_overrides=False
        ) 
开发者ID:ros2,项目名称:rclpy,代码行数:21,代码来源:test_node.py

示例7: test_use_sim_time

# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import create_node [as 别名]
def test_use_sim_time(self):
        self.assertTrue(self.node.has_parameter(USE_SIM_TIME_NAME))
        self.assertFalse(self.node.get_parameter(USE_SIM_TIME_NAME).value)

        temp_node = rclpy.create_node(
            TEST_NODE + '2',
            namespace=TEST_NAMESPACE,
            context=self.context,
            parameter_overrides=[
                Parameter(USE_SIM_TIME_NAME, value=True),
            ],
            automatically_declare_parameters_from_overrides=False
        )
        # use_sim_time is declared automatically anyways; in this case using override value.
        self.assertTrue(temp_node.has_parameter(USE_SIM_TIME_NAME))
        self.assertTrue(temp_node.get_parameter(USE_SIM_TIME_NAME).value)
        temp_node.destroy_node() 
开发者ID:ros2,项目名称:rclpy,代码行数:19,代码来源:test_node.py

示例8: test_use_global_arguments

# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import create_node [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) 
开发者ID:ros2,项目名称:rclpy,代码行数:19,代码来源:test_node.py

示例9: test_bad_node_arguments

# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import create_node [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) 
开发者ID:ros2,项目名称:rclpy,代码行数:25,代码来源:test_node.py

示例10: test_destroy_timers

# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import create_node [as 别名]
def test_destroy_timers():
    context = rclpy.context.Context()
    rclpy.init(context=context)
    try:
        node = rclpy.create_node('test_node3', context=context)
        try:
            timer1 = node.create_timer(0.1, None)
            timer2 = node.create_timer(1, None)
            timer2  # noqa

            assert 2 == len(tuple(node.timers))
            assert node.destroy_timer(timer1)

            assert 1 == len(tuple(node.timers))
        finally:
            node.destroy_node()
        assert 0 == len(tuple(node.timers))
    finally:
        rclpy.shutdown(context=context) 
开发者ID:ros2,项目名称:rclpy,代码行数:21,代码来源:test_destruction.py

示例11: test_destroy_node_asap

# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import create_node [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) 
开发者ID:ros2,项目名称:rclpy,代码行数:20,代码来源:test_destruction.py

示例12: setUpClass

# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import create_node [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) 
开发者ID:ros2,项目名称:rclpy,代码行数:20,代码来源:test_action_graph.py

示例13: test_handle_destroyed_when_not_used

# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import create_node [as 别名]
def test_handle_destroyed_when_not_used():
    context = rclpy.context.Context()
    rclpy.init(context=context)

    try:
        node = rclpy.create_node('test_handle_destroyed_when_not_used', context=context)
        with node.handle:
            node.handle.destroy()
            with node.handle:
                pass

        with pytest.raises(InvalidHandle):
            with node.handle:
                pass
    finally:
        rclpy.shutdown(context=context) 
开发者ID:ros2,项目名称:rclpy,代码行数:18,代码来源:test_handle.py

示例14: main

# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import create_node [as 别名]
def main(args=None):
    rclpy.init(args=args)

    node = rclpy.create_node('parameter_node')
    node.declare_parameter('bool_param', True)
    node.declare_parameter('int_param', 42)
    node.declare_parameter('double_param', 1.23)
    node.declare_parameter('str_param', 'Hello World')
    node.declare_parameter('bool_array_param', [False, False, True])
    node.declare_parameter('int_array_param', [1, 2, 3])
    node.declare_parameter('str_array_param', ['foo', 'bar', 'baz'])
    node.declare_parameter('double_array_param', [3.125, 6.25, 12.5])
    node.declare_parameter('foo' + PARAMETER_SEPARATOR_STRING + 'str_param', 'foo')
    node.declare_parameter('foo' + PARAMETER_SEPARATOR_STRING +
                           'bar' + PARAMETER_SEPARATOR_STRING +
                           'str_param', 'foobar')

    try:
        rclpy.spin(node)
    except KeyboardInterrupt:
        print('parameter node stopped cleanly')
    except BaseException:
        print('exception in parameter node:', file=sys.stderr)
        raise
    finally:
        node.destroy_node()
        rclpy.shutdown() 
开发者ID:ros2,项目名称:ros2cli,代码行数:29,代码来源:parameter_node.py

示例15: __init__

# 需要导入模块: import rclpy [as 别名]
# 或者: from rclpy import create_node [as 别名]
def __init__(self, args, *, node_name=None):
        timeout_reached = False

        def timer_callback():
            nonlocal timeout_reached
            timeout_reached = True

        argv = getattr(args, 'argv', [])

        rclpy.init(args=argv)

        node_name_suffix = getattr(
            args, 'node_name_suffix', '_%d' % os.getpid())
        start_parameter_services = getattr(
            args, 'start_parameter_services', False)

        if node_name is None:
            node_name = NODE_NAME_PREFIX + node_name_suffix

        self.node = rclpy.create_node(
            node_name,
            start_parameter_services=start_parameter_services)
        timeout = getattr(args, 'spin_time', DEFAULT_TIMEOUT)
        timer = self.node.create_timer(timeout, timer_callback)

        while not timeout_reached:
            rclpy.spin_once(self.node)

        self.node.destroy_timer(timer) 
开发者ID:ros2,项目名称:ros2cli,代码行数:31,代码来源:direct.py


注:本文中的rclpy.create_node方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。