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..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 @@ -10,6 +10,7 @@ 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 -%} @@ -53,6 +54,32 @@ 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) + def refresh_dynamic_parameters(self): updated_params = self.get_params() # TODO remove any destroyed dynamic parameters