Esempio n. 1
0
def subscriber(node: Node, topic_name: str, message_type: MsgType,
               callback: Callable[[MsgType], Any],
               qos_profile: QoSProfile) -> Optional[str]:
    """Initialize a node with a single subscription and spin."""
    if message_type is None:
        topic_names_and_types = get_topic_names_and_types(
            node=node, include_hidden_topics=True)
        try:
            expanded_name = expand_topic_name(topic_name, node.get_name(),
                                              node.get_namespace())
        except ValueError as e:
            raise RuntimeError(e)
        try:
            validate_full_topic_name(expanded_name)
        except rclpy.exceptions.InvalidTopicNameException as e:
            raise RuntimeError(e)
        for n, t in topic_names_and_types:
            if n == expanded_name:
                if len(t) > 1:
                    raise RuntimeError(
                        "Cannot echo topic '%s', as it contains more than one type: [%s]"
                        % (topic_name, ', '.join(t)))
                message_type = t[0]
                break
        else:
            raise RuntimeError(
                'Could not determine the type for the passed topic')

    msg_module = get_message(message_type)

    node.create_subscription(msg_module, topic_name, callback, qos_profile)

    rclpy.spin(node)
Esempio n. 2
0
    elif p_type == ParameterType.PARAMETER_STRING:
        p_value.string_value = value
    return p_value


rclpy.init(args=sys.argv)
node = Node("param_cli_setter")
time.sleep(1)

done = False
while not done:
    nodes = [((ns + "/") if ns != "/" else "/") + name
             for name, ns in node.get_node_names_and_namespaces()]
    nodes = [
        fqn for fqn in nodes
        if "ros2cli" not in fqn and node.get_name() not in fqn
    ]
    if not nodes:
        print("No nodes found, trying again", end="\r")
        time.sleep(1)
        continue
    req_node = select_bullet("Select a node to examine parameters", nodes)
    list_params_client = node.create_client(ListParameters,
                                            req_node + "/list_parameters")
    get_params_client = node.create_client(GetParameters,
                                           req_node + "/get_parameters")
    set_params_client = node.create_client(SetParameters,
                                           req_node + "/set_parameters")
    time.sleep(0.2)
    if not list_params_client.service_is_ready():
        print(