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 not in node_names: return 'Node not found' with DirectNode(args) as node: parameter = Parameter() Parameter.name = args.name parameter.value = get_parameter_value(string_value=args.value) response = call_set_parameters(node=node, node_name=args.node_name, parameters=[parameter]) # output response assert len(response.results) == 1 result = response.results[0] if result.successful: msg = 'Set parameter successful' if result.reason: msg += ': ' + result.reason print(msg) else: msg = 'Setting parameter failed' if result.reason: msg += ': ' + result.reason print(msg, file=sys.stderr)
def _set_param(node_name, name, value, parameter_type=None): """ Internal helper function for set_param. Attempts to set the given parameter in the target node with the desired value, deducing the parameter type if it's not specified. parameter_type allows forcing a type for the given value; this is useful to delete parameters. """ parameter = Parameter() parameter.name = name if parameter_type is None: parameter.value = get_parameter_value(string_value=value) else: parameter.value = ParameterValue() parameter.value.type = parameter_type if parameter_type != ParameterType.PARAMETER_NOT_SET: setattr(parameter.value, _parameter_type_mapping[parameter_type]) try: # call_get_parameters will fail if node does not exist. call_set_parameters(node=_node, node_name=node_name, parameters=[parameter]) except: pass
def load_component_into_container(*, node, remote_container_node_name, package_name, plugin_name, node_name=None, node_namespace=None, log_level=None, remap_rules=None, parameters=None, extra_arguments=None): """ Load component into a running container synchronously. :param node: an `rclpy.Node` instance :param remote_container_node_name: of the container node to load the component into :param package_name: where the component node plugin is to be found :param plugin_name: of the component plugin to load :param node_name: name for the component node :param node_namespace: namespace for the component node :param log_level: log level for the component node :param remap_rules: remapping rules for the component node, in the 'from:=to' form :param parameters: optional parameters for the component node, in the 'name:=value' form :param extra_arguments: arguments specific to the container node in the 'name:=value' form """ load_node_client = node.create_client( composition_interfaces.srv.LoadNode, '{}/_container/load_node'.format(remote_container_node_name)) try: if not load_node_client.wait_for_service(timeout_sec=5.0): raise RuntimeError( f"No 'load_node' service found for '{remote_container_node_name}' container" ) request = composition_interfaces.srv.LoadNode.Request() request.package_name = package_name request.plugin_name = plugin_name if node_name is not None: request.node_name = node_name if node_namespace is not None: request.node_namespace = node_namespace if log_level is not None: request.log_level = log_level if remap_rules is not None: request.remap_rules = remap_rules if parameters is not None: for param in parameters: name, _, value = param.partition(':=') param_msg = rcl_interfaces.msg.Parameter() param_msg.value = get_parameter_value(string_value=value) param_msg.name = name request.parameters.append(param_msg) if extra_arguments is not None: for arg in extra_arguments: name, _, value = arg.partition(':=') arg_msg = rcl_interfaces.msg.Parameter() arg_msg.value = get_parameter_value(string_value=value) arg_msg.name = name request.extra_arguments.append(arg_msg) future = load_node_client.call_async(request) rclpy.spin_until_future_complete(node, future) response = future.result() if not response.success: raise RuntimeError('Failed to load component: ' + response.error_message.capitalize()) return response.unique_id, response.full_node_name finally: node.destroy_client(load_node_client)
def test_get_parameter_value(string_value, expected_type, value_attribute, expected_value): value = get_parameter_value(string_value=string_value) assert value.type == expected_type assert getattr(value, value_attribute) == expected_value