Esempio n. 1
0
def _get_param_names(node_name):
    # This method is called in a service callback; calling a service of the same node
    # will cause a deadlock.
    global _parent_node_name
    if node_name == _parent_node_name:
        return []

    client = _node.create_client(ListParameters,
                                 '{}/list_parameters'.format(node_name))

    ready = client.wait_for_service(timeout_sec=5.0)
    if not ready:
        raise RuntimeError('Wait for list_parameters service timed out')

    request = ListParameters.Request()
    future = client.call_async(request)
    rclpy.spin_until_future_complete(_node, future)
    response = future.result()

    if response is not None:
        return [
            '{}:{}'.format(node_name, param_name)
            for param_name in response.result.names
        ]
    else:
        return []
Esempio n. 2
0
def _has_params(node, node_name):
    client = node.create_client(
        ListParameters, '{node_name}/list_parameters'.format_map(locals()))
    if not client.service_is_ready():
        client.wait_for_service()
    ret = len(client.call(ListParameters.Request()).result.names) > 0
    node.destroy_client(client)
    return ret
Esempio n. 3
0
    def main(self, *, args):  # noqa: D102
        with NodeStrategy(args) as node:
            node_names = get_node_names(
                node=node, include_hidden_nodes=args.include_hidden_nodes)

        if args.node_name:
            if args.node_name not in [n.full_name for n in node_names]:
                return 'Node not found'
            node_names = [
                n for n in node_names if args.node_name == n.full_name
            ]

        with DirectNode(args) as node:
            clients = {}
            futures = {}
            # create clients
            for node_name in node_names:
                client = node.create_client(
                    ListParameters,
                    '{node_name.full_name}/list_parameters'.format_map(
                        locals()))
                clients[node_name] = client

            # wait until all clients have been called
            while True:
                for node_name in [n for n in node_names if n not in futures]:
                    # call as soon as ready
                    client = clients[node_name]
                    if client.service_is_ready():
                        request = ListParameters.Request()
                        for prefix in args.param_prefixes:
                            request.prefixes.append(prefix)
                        future = client.call_async(request)
                        futures[node_name] = future

                if len(futures) == len(clients):
                    break
                rclpy.spin_once(node, timeout_sec=1.0)

            # wait for all responses
            for node_name in node_names:
                rclpy.spin_until_future_complete(node, futures[node_name])

            # print responses
            for node_name in sorted(futures.keys()):
                future = futures[node_name]
                if future.result() is not None:
                    if not args.node_name:
                        print('{node_name.full_name}:'.format_map(locals()))
                    response = future.result()
                    for name in sorted(response.result.names):
                        print('  {name}'.format_map(locals()))
                else:
                    e = future.exception()
                    print('Exception while calling service of node '
                          "'{node_name.full_name}': {e}".format_map(locals()),
                          file=sys.stderr)
    def wait_for_parameter_availability(
        self, parameter_names: set, timeout=TIMEOUT_DURATION
    ) -> bool:
        client = self.node.create_client(
            srv_type=ListParameters, srv_name="/march/safety_node/list_parameters"
        )
        client.wait_for_service(TIMEOUT_DURATION)
        start_time = datetime.now()
        while datetime.now() - start_time < timedelta(seconds=timeout):
            future = client.call_async(ListParameters.Request())
            rclpy.spin_until_future_complete(self.node, future)

            if set(future.result().result.names) == parameter_names:
                return True
        return False
Esempio n. 5
0
def call_list_parameters(*, node, node_name, prefix=None):
    # create client
    client = node.create_client(ListParameters, f'{node_name}/list_parameters')

    # call as soon as ready
    ready = client.wait_for_service(timeout_sec=5.0)
    if not ready:
        raise RuntimeError('Wait for service timed out')

    request = ListParameters.Request()
    future = client.call_async(request)
    rclpy.spin_until_future_complete(node, future)

    # handle response
    response = future.result()
    return response.result.names
Esempio n. 6
0
def list_parameters(node, timeout_sec=10.0):
    # create client
    client = node.create_client(ListParameters,
                                'consai2r2_description/list_parameters')

    # call as soon as ready
    ready = client.wait_for_service(timeout_sec)
    if not ready:
        raise RuntimeError('Wait for service timed out')

    request = ListParameters.Request()
    future = client.call_async(request)
    rclpy.spin_until_future_complete(node, future)

    # handle response
    response = future.result()
    if response is None:
        raise RuntimeError("Failed to get the list of parameters'")

    return response.result.names
Esempio n. 7
0
def call_list_parameters(*, node, node_name, prefix=None):
    # create client
    client = node.create_client(
        ListParameters, '{node_name}/list_parameters'.format_map(locals()))

    # call as soon as ready
    ready = client.wait_for_service(timeout_sec=5.0)
    if not ready:
        raise RuntimeError('Wait for service timed out')

    request = ListParameters.Request()
    future = client.call_async(request)
    rclpy.spin_until_future_complete(node, future)

    # handle response
    response = future.result()
    if response is None:
        e = future.exception()
        raise RuntimeError(
            "Exception while calling service of node '{node_name}': {e}".
            format_map(locals()))
    return response.result.names
    def main(self, *, args):  # noqa: D102

        with NodeStrategy(args) as node:
            node_names = get_node_names(
                node=node, include_hidden_nodes=args.include_hidden_nodes)

        absolute_node_name = get_absolute_node_name(args.node_name)
        node_name = parse_node_name(absolute_node_name)
        if absolute_node_name:
            if absolute_node_name not in [n.full_name for n in node_names]:
                return 'Node not found'

        if not os.path.isdir(args.output_dir):
            raise RuntimeError(
                f"'{args.output_dir}' is not a valid directory.")

        with DirectNode(args) as node:
            # create client
            service_name = f'{absolute_node_name}/list_parameters'
            client = node.create_client(ListParameters, service_name)

            client.wait_for_service()

            if not client.service_is_ready():
                raise RuntimeError(f"Could not reach service '{service_name}'")

            request = ListParameters.Request()
            future = client.call_async(request)

            # wait for response
            rclpy.spin_until_future_complete(node, future)

            yaml_output = {node_name.name: {'ros__parameters': {}}}

            # retrieve values
            if future.result() is not None:
                response = future.result()
                for param_name in sorted(response.result.names):
                    pval = self.get_parameter_value(node, absolute_node_name,
                                                    param_name)
                    self.insert_dict(
                        yaml_output[node_name.name]['ros__parameters'],
                        param_name, pval)
            else:
                e = future.exception()
                raise RuntimeError('Exception while calling service of node '
                                   f"'{node_name.full_name}': {e}")

            if args.print:
                print(yaml.dump(yaml_output, default_flow_style=False))
                return

            if absolute_node_name[0] == '/':
                file_name = absolute_node_name[1:].replace('/', '__')
            else:
                file_name = absolute_node_name.replace('/', '__')

            print('Saving to: ',
                  os.path.join(args.output_dir, file_name + '.yaml'))
            with open(os.path.join(args.output_dir, file_name + '.yaml'),
                      'w') as yaml_file:
                yaml.dump(yaml_output, yaml_file, default_flow_style=False)
Esempio n. 9
0
    def main(self, *, args):  # noqa: D102
        with NodeStrategy(args) as node:
            node_names = get_node_names(
                node=node, include_hidden_nodes=args.include_hidden_nodes)

        node_name = get_absolute_node_name(args.node_name)
        if node_name:
            if node_name not in [n.full_name for n in node_names]:
                return 'Node not found'
            node_names = [n for n in node_names if node_name == n.full_name]

        regex_filter = getattr(args, 'filter')
        if regex_filter is not None:
            regex_filter = re.compile(regex_filter[0])

        with DirectNode(args) as node:
            service_names = get_service_names(
                node=node, include_hidden_services=args.include_hidden_nodes)

            clients = {}
            futures = {}
            # create clients for nodes which have the service
            for node_name in node_names:
                service_name = f'{node_name.full_name}/list_parameters'
                if service_name in service_names:
                    client = node.create_client(ListParameters, service_name)
                    clients[node_name] = client

            # wait until all clients have been called
            while True:
                for node_name in [
                        n for n in clients.keys() if n not in futures
                ]:
                    # call as soon as ready
                    client = clients[node_name]
                    if client.service_is_ready():
                        request = ListParameters.Request()
                        for prefix in args.param_prefixes:
                            request.prefixes.append(prefix)
                        future = client.call_async(request)
                        futures[node_name] = future

                if len(futures) == len(clients):
                    break
                rclpy.spin_once(node, timeout_sec=1.0)

            # wait for all responses
            for future in futures.values():
                rclpy.spin_until_future_complete(node, future, timeout_sec=1.0)

            # print responses
            for node_name in sorted(futures.keys()):
                future = futures[node_name]
                if future.result() is None:
                    e = future.exception()
                    print(
                        'Exception while calling service of node '
                        f"'{node_name.full_name}': {e}",
                        file=sys.stderr)
                response = future.result()
                sorted_names = sorted(response.result.names)
                if regex_filter is not None:
                    sorted_names = [
                        name for name in sorted_names
                        if regex_filter.match(name)
                    ]
                if not args.node_name and sorted_names:
                    print(f'{node_name.full_name}:')
                # get descriptors for the node if needs to print parameter type
                name_to_type_map = {}
                if args.param_type is True:
                    resp = call_describe_parameters(
                        node=node,
                        node_name=node_name.full_name,
                        parameter_names=sorted_names)
                    for descriptor in resp.descriptors:
                        name_to_type_map[
                            descriptor.name] = get_parameter_type_string(
                                descriptor.type)

                for name in sorted_names:
                    if args.param_type is True:
                        param_type_str = name_to_type_map[name]
                        print(f'  {name} (type: {param_type_str})')
                    else:
                        print(f'  {name}')
Esempio n. 10
0
 def list_parameters(self):
     list_params_request = ListParameters.Request()
     list_params_response = self._list_params_client.call(
         list_params_request)
     return list_params_response.result.names
Esempio n. 11
0
        print(
            f"Service {list_params_client.srv_name} is not ready, waiting a second"
        )
        time.sleep(1)
    if not get_params_client.service_is_ready():
        print(
            f"Service {get_params_client.srv_name} is not ready, waiting a second"
        )
        time.sleep(1)
    if not set_params_client.service_is_ready():
        print(
            f"Service {set_params_client.srv_name} is not ready, waiting a second"
        )
        time.sleep(1)
    print(f"Requesting parameter list for {req_node}")
    f = list_params_client.call_async(ListParameters.Request())
    req_time = time.time()
    while not f.done():
        rclpy.spin_once(node, timeout_sec=0.1)
    all_param_names = f.result().result.names
    print("Parameters received")

    # result_params = result_params.result
    params_found = False
    while not params_found:
        query_param_name = get_string(
            "Enter a parameter name (or part of it): ")
        if query_param_name:
            filtered_params = [
                p for p in all_param_names if query_param_name in p
            ]