From e438d6aefd3311d8c27d91e0dd0471373025f6d7 Mon Sep 17 00:00:00 2001 From: marqrazz Date: Wed, 17 Jul 2024 12:59:50 -0600 Subject: [PATCH 1/4] Add ability for param_listener to load values from yaml dictionary --- .../python/parameter_library_header | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/generate_parameter_library_py/generate_parameter_library_py/jinja_templates/python/parameter_library_header b/generate_parameter_library_py/generate_parameter_library_py/jinja_templates/python/parameter_library_header index 0738221..d97de9c 100644 --- a/generate_parameter_library_py/generate_parameter_library_py/jinja_templates/python/parameter_library_header +++ b/generate_parameter_library_py/generate_parameter_library_py/jinja_templates/python/parameter_library_header @@ -10,11 +10,34 @@ from rclpy.exceptions import InvalidParameterValueException from rclpy.time import Time import copy import rclpy +import rclpy.parameter from generate_parameter_library_py.python_validators import ParameterValidators {% if user_validation_file|length -%} import {{user_validation_file}} as custom_validators -{% endif %} +{% endif -%} + +def unpack_parameter_dict(namespace: str, parameter_dict: dict): + """ + Flatten a parameter dictionary recursively. + + :param namespace: The namespace to prepend to the parameter names. + :param parameter_dict: A dictionary of parameters keyed by the parameter names + :return: A list of rclpy Parameter objects + """ + parameters = [] + for param_name, param_value in parameter_dict.items(): + full_param_name = namespace + param_name + # Unroll nested parameters + if type(param_value) == dict: + nested_params = unpack_parameter_dict( + namespace=full_param_name + '.', + parameter_dict=param_value) + parameters.extend(nested_params) + else: + parameters.append(rclpy.parameter.Parameter(full_param_name, value=param_value)) + return parameters + class {{namespace}}: {%- filter indent(width=4) %} @@ -29,7 +52,6 @@ stamp_ = Time() {% endfilter -%} {%- endfilter %} - class ParamListener: def __init__(self, node, prefix=""): self.prefix_ = prefix @@ -53,6 +75,10 @@ stamp_ = Time() def is_old(self, other_param): return self.params_.stamp_ != other_param.stamp_ + def set_params_from_dict(self, param_dict): + params_to_set = unpack_parameter_dict('', param_dict) + self.update(params_to_set) + def refresh_dynamic_parameters(self): updated_params = self.get_params() # TODO remove any destroyed dynamic parameters From 96b4ab2ceef80f9df6bcd3859be9cc207101a1e5 Mon Sep 17 00:00:00 2001 From: marqrazz Date: Wed, 17 Jul 2024 13:50:04 -0600 Subject: [PATCH 2/4] switch to isinstance(dict) --- .../jinja_templates/python/parameter_library_header | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/generate_parameter_library_py/generate_parameter_library_py/jinja_templates/python/parameter_library_header b/generate_parameter_library_py/generate_parameter_library_py/jinja_templates/python/parameter_library_header index d97de9c..afad84a 100644 --- a/generate_parameter_library_py/generate_parameter_library_py/jinja_templates/python/parameter_library_header +++ b/generate_parameter_library_py/generate_parameter_library_py/jinja_templates/python/parameter_library_header @@ -29,7 +29,7 @@ def unpack_parameter_dict(namespace: str, parameter_dict: dict): for param_name, param_value in parameter_dict.items(): full_param_name = namespace + param_name # Unroll nested parameters - if type(param_value) == dict: + if isinstance(param_value, dict): nested_params = unpack_parameter_dict( namespace=full_param_name + '.', parameter_dict=param_value) @@ -52,6 +52,7 @@ stamp_ = Time() {% endfilter -%} {%- endfilter %} + class ParamListener: def __init__(self, node, prefix=""): self.prefix_ = prefix From 6897fd1c8c36aac888354c3f7d73ed3875f2cfdc Mon Sep 17 00:00:00 2001 From: marqrazz Date: Wed, 17 Jul 2024 14:13:41 -0600 Subject: [PATCH 3/4] use PARAMETER_SEPARATOR_STRING constant --- .../jinja_templates/python/parameter_library_header | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generate_parameter_library_py/generate_parameter_library_py/jinja_templates/python/parameter_library_header b/generate_parameter_library_py/generate_parameter_library_py/jinja_templates/python/parameter_library_header index afad84a..3e47db0 100644 --- a/generate_parameter_library_py/generate_parameter_library_py/jinja_templates/python/parameter_library_header +++ b/generate_parameter_library_py/generate_parameter_library_py/jinja_templates/python/parameter_library_header @@ -31,7 +31,7 @@ def unpack_parameter_dict(namespace: str, parameter_dict: dict): # Unroll nested parameters if isinstance(param_value, dict): nested_params = unpack_parameter_dict( - namespace=full_param_name + '.', + namespace=full_param_name + rclpy.parameter.PARAMETER_SEPARATOR_STRING, parameter_dict=param_value) parameters.extend(nested_params) else: From 0a7b708569700bd9eb2137265d1253594320bec3 Mon Sep 17 00:00:00 2001 From: marqrazz Date: Wed, 24 Jul 2024 22:58:54 -0600 Subject: [PATCH 4/4] move to member function --- .../python/parameter_library_header | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/generate_parameter_library_py/generate_parameter_library_py/jinja_templates/python/parameter_library_header b/generate_parameter_library_py/generate_parameter_library_py/jinja_templates/python/parameter_library_header index 3e47db0..2247da3 100644 --- a/generate_parameter_library_py/generate_parameter_library_py/jinja_templates/python/parameter_library_header +++ b/generate_parameter_library_py/generate_parameter_library_py/jinja_templates/python/parameter_library_header @@ -15,29 +15,7 @@ from generate_parameter_library_py.python_validators import ParameterValidators {% if user_validation_file|length -%} import {{user_validation_file}} as custom_validators -{% endif -%} - -def unpack_parameter_dict(namespace: str, parameter_dict: dict): - """ - Flatten a parameter dictionary recursively. - - :param namespace: The namespace to prepend to the parameter names. - :param parameter_dict: A dictionary of parameters keyed by the parameter names - :return: A list of rclpy Parameter objects - """ - parameters = [] - for param_name, param_value in parameter_dict.items(): - full_param_name = namespace + param_name - # Unroll nested parameters - if isinstance(param_value, dict): - nested_params = unpack_parameter_dict( - namespace=full_param_name + rclpy.parameter.PARAMETER_SEPARATOR_STRING, - parameter_dict=param_value) - parameters.extend(nested_params) - else: - parameters.append(rclpy.parameter.Parameter(full_param_name, value=param_value)) - return parameters - +{% endif %} class {{namespace}}: {%- filter indent(width=4) %} @@ -76,6 +54,28 @@ stamp_ = Time() def is_old(self, other_param): return self.params_.stamp_ != other_param.stamp_ + @staticmethod + def unpack_parameter_dict(namespace: str, parameter_dict: dict): + """ + Flatten a parameter dictionary recursively. + + :param namespace: The namespace to prepend to the parameter names. + :param parameter_dict: A dictionary of parameters keyed by the parameter names + :return: A list of rclpy Parameter objects + """ + parameters = [] + for param_name, param_value in parameter_dict.items(): + full_param_name = namespace + param_name + # Unroll nested parameters + if isinstance(param_value, dict): + nested_params = unpack_parameter_dict( + namespace=full_param_name + rclpy.parameter.PARAMETER_SEPARATOR_STRING, + parameter_dict=param_value) + parameters.extend(nested_params) + else: + parameters.append(rclpy.parameter.Parameter(full_param_name, value=param_value)) + return parameters + def set_params_from_dict(self, param_dict): params_to_set = unpack_parameter_dict('', param_dict) self.update(params_to_set)