Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability for param_listener to load values from yaml dictionary #211

Merged
merged 4 commits into from
Aug 1, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 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
MarqRazz marked this conversation as resolved.
Show resolved Hide resolved


class {{namespace}}:
{%- filter indent(width=4) %}
Expand Down Expand Up @@ -53,6 +76,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
Expand Down