Skip to content
New issue

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

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

Already on GitHub? # to your account

Support nested mapped parameters #166

Merged
Merged
Show file tree
Hide file tree
Changes from 7 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
22 changes: 22 additions & 0 deletions example/src/parameters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,28 @@ admittance_controller:
description: "specifies which joints will be used by the controller",
}

nested_dynamic:
__map_joints:
__map_dof_names:
nested: {
type: double,
default_value: 1.0,
description: "test nested map params",
validation: {
gt_eq<>: [ 0.0001 ],
}
}
__map_joints:
__map_dof_names:
nested_deep: {
type: double,
default_value: 1.0,
description: "test deep nested map params",
validation: {
gt_eq<>: [ 0.0001 ],
}
}

pid:
rate: {
type: double,
Expand Down
21 changes: 21 additions & 0 deletions example_python/generate_parameter_module_example/parameters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,27 @@ admittance_controller:
description: "specifies which joints will be used by the controller",
}

nested_dynamic:
__map_joints:
__map_dof_names:
nested: {
type: double,
default_value: 1.0,
description: "test nested map params",
validation: {
gt_eq<>: [ 0.0001 ],
}
}
__map_joints:
__map_dof_names:
nested_deep: {
type: double,
default_value: 1.0,
description: "test deep nested map params",
validation: {
gt_eq<>: [ 0.0001 ],
}
}
pid:
rate: {
type: double,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@


def run(output_file, yaml_file, validation_module=''):
print(f'Running {__file__} {output_file} {yaml_file} {validation_module}')
gen_param_struct = GenerateCode('python')
output_dir = os.path.dirname(output_file)
if not os.path.isdir(output_dir):
Expand All @@ -62,7 +63,6 @@ def main():
output_file = args.output_python_module_file
yaml_file = args.input_yaml_file
validate_file = args.validate_file

run(output_file, yaml_file, validate_file)


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
for (const auto & value : {{param_struct_instance}}.{{mapped_param}}){
{%- filter indent(width=4) %}
auto& entry = {{param_struct_instance}}.{{parameter_map}}[value];
{% for mapped_param in mapped_params -%}
for (const auto & value_{{loop.index}} : updated_params.{{mapped_param}}) {
{% endfor -%}
{%- filter indent(width=4) -%}
auto& entry = {{param_struct_instance}}.{{struct_name}}{% for map in parameter_map%}.{{map}}[value_{{loop.index}}]{% endfor %};
std::string value = fmt::format("{%- for mapped_param in mapped_params -%}{% if loop.index == 1 %}{}{% else %}.{}{% endif -%} {%- endfor -%}",
{%- for mapped_param in mapped_params -%}{% if loop.index == 1 %} value_{{loop.index}}{% else %}, value_{{loop.index}}{% endif -%} {%- endfor %});
auto param_name = fmt::format("{}{}.{}.{}", prefix_, "{{struct_name}}", value, "{{parameter_field}}");
if (!parameters_interface_->has_parameter(param_name)) {
{%- filter indent(width=4) %}
Expand Down Expand Up @@ -49,4 +53,6 @@ parameters_interface_->declare_parameter(param_name, parameter, descriptor);
}
{{set_runtime_parameter-}}
{% endfilter -%}
{%- for mapped_param in mapped_params -%}
}
{% endfor -%}
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
for (const auto & value : updated_params.{{mapped_param}}){
{%- filter indent(width=4) %}
{% for mapped_param in mapped_params -%}
for (const auto & value_{{loop.index}} : updated_params.{{mapped_param}}) {
{% endfor -%}
{%- filter indent(width=4) -%}
std::string value = fmt::format("{%- for mapped_param in mapped_params -%}{% if loop.index == 1 %}{}{% else %}.{}{% endif -%} {%- endfor -%}",
{%- for mapped_param in mapped_params -%}{% if loop.index == 1 %} value_{{loop.index}}{% else %}, value_{{loop.index}}{% endif -%} {%- endfor %});
auto param_name = fmt::format("{}{}.{}.{}", prefix_, "{{struct_name}}", value, "{{parameter_field}}");
if (param.get_name() == param_name) {
{%- filter indent(width=4) %}
{% if parameter_validations|length -%}
{{parameter_validations-}}
{% endif -%}
updated_params.{{parameter_map}}[value].{{parameter_field}} = param.{{parameter_as_function}};
updated_params.{{struct_name}}{% for map in parameter_map%}.{{map}}[value_{{loop.index}}]{% endfor %}.{{parameter_field}} = param.{{parameter_as_function}};
RCLCPP_DEBUG_STREAM(logger_, param.get_name() << ": " << param.get_type_name() << " = " << param.value_to_string());
{% endfilter -%}
}
{% endfilter -%}
{%- for mapped_param in mapped_params -%}
}
{% endfor -%}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
for value in {{param_struct_instance}}.{{mapped_param}}:
{%- filter indent(width=4) %}
{{param_struct_instance}}.{{struct_name}}.add_entry(value)
entry = {{param_struct_instance}}.{{struct_name}}.get_entry(value)
param_name = f"{self.prefix_}{{struct_name}}.{value}.{{parameter_field}}"
{% for mapped_param in mapped_params -%}
{%- filter indent(width=4*loop.index0) %}
for value_{{loop.index}} in updated_params.{{mapped_param}}:
{%- endfilter -%}
{% endfor -%}
{%- filter indent(width=4*(mapped_params|length)) %}
{{param_struct_instance}}.{{struct_name}}{% for map in parameter_map%}.add_entry(value_{{loop.index}}){% endfor %}
entry = {{param_struct_instance}}.{{struct_name}}{% for map in parameter_map%}.get_entry(value_{{loop.index}}){% endfor %}
param_name = f"{self.prefix_}{{struct_name}}{% for map in parameter_map%}.{value_{{loop.index}}}{% endfor %}.{{parameter_field}}"
if not self.node_.has_parameter(self.prefix_ + param_name):
{%- filter indent(width=4) %}
descriptor = ParameterDescriptor(description="{{parameter_description}}", read_only = {{parameter_read_only}})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ __map_type = __{{struct_name}}
def add_entry(self, name):
if not hasattr(self, name):
setattr(self, name, self.__map_type())
return getattr(self, name)
def get_entry(self, name):
return getattr(self, name)
{% endif -%}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
for value in updated_params.{{mapped_param}}:
{%- filter indent(width=4) %}
param_name = f"{self.prefix_}{{struct_name}}.{value}.{{parameter_field}}"
{% for mapped_param in mapped_params -%}
{%- filter indent(width=4*loop.index) %}
for value_{{loop.index}} in updated_params.{{mapped_param}}:
{%- endfilter -%}
{% endfor -%}
{%- filter indent(width=4*(1+mapped_params|length)) %}
param_name = f"{self.prefix_}{{struct_name}}{% for map in parameter_map%}.{value_{{loop.index}}}{% endfor %}.{{parameter_field}}"
if param.name == param_name:
{%- filter indent(width=4) %}
{% if parameter_validations|length -%}
{{parameter_validations-}}
{% endif -%}
updated_params.{{parameter_map}}[value].{{parameter_field}} = param.{{parameter_as_function}}
updated_params.{{struct_name}}{% for map in parameter_map%}.get_entry(value_{{loop.index}}){% endfor %}.{{parameter_field}} = param.{{parameter_as_function}}
self.logger_.debug(param.name + ": " + param.type_.name + " = " + str(param.value))
{% endfilter -%}
{% endfilter -%}
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,16 @@ def get_dynamic_parameter_field(yaml_parameter_name: str):

def get_dynamic_mapped_parameter(yaml_parameter_name: str):
tmp = yaml_parameter_name.split('.')
tmp2 = tmp[-2]
mapped_param = tmp2.replace('__map_', '')
return mapped_param
mapped_params = [
val.replace('__map_', '') for val in tmp[:-1] if is_mapped_parameter(val)
]
return mapped_params


def get_dynamic_struct_name(yaml_parameter_name: str):
tmp = yaml_parameter_name.split('.')
struct_name = tmp[:-2]
num_nested = sum([is_mapped_parameter(val) for val in tmp])
struct_name = tmp[: -(num_nested + 1)]
return '.'.join(struct_name)


Expand All @@ -116,10 +118,8 @@ def get_dynamic_parameter_name(yaml_parameter_name: str):


def get_dynamic_parameter_map(yaml_parameter_name: str):
tmp = yaml_parameter_name.split('.')
parameter_map = tmp[:-2]
mapped_param = get_dynamic_mapped_parameter(yaml_parameter_name)
parameter_map.append(mapped_param + '_map')
mapped_params = get_dynamic_mapped_parameter(yaml_parameter_name)
parameter_map = [val + '_map' for val in mapped_params]
parameter_map = '.'.join(parameter_map)
return parameter_map

Expand Down Expand Up @@ -402,13 +402,14 @@ def __str__(self):
class UpdateRuntimeParameter(UpdateParameterBase):
def __str__(self):
parameter_validations_str = ''.join(str(x) for x in self.parameter_validations)
mapped_param = get_dynamic_mapped_parameter(self.parameter_name)
mapped_params = get_dynamic_mapped_parameter(self.parameter_name)
parameter_map = get_dynamic_parameter_map(self.parameter_name)
parameter_map = parameter_map.split('.')
struct_name = get_dynamic_struct_name(self.parameter_name)
parameter_field = get_dynamic_parameter_field(self.parameter_name)

data = {
'mapped_param': mapped_param,
'mapped_params': mapped_params,
'parameter_map': parameter_map,
'struct_name': struct_name,
'parameter_field': parameter_field,
Expand Down Expand Up @@ -552,18 +553,19 @@ def __str__(self):

bool_to_str = self.code_gen_variable.conversation.bool_to_str
parameter_field = get_dynamic_parameter_field(self.parameter_name)
mapped_param = get_dynamic_mapped_parameter(self.parameter_name)
mapped_params = get_dynamic_mapped_parameter(self.parameter_name)
parameter_map = get_dynamic_parameter_map(self.parameter_name)
struct_name = get_dynamic_struct_name(self.parameter_name)
parameter_map = parameter_map.split('.')

data = {
'struct_name': struct_name,
'parameter_type': self.code_gen_variable.get_parameter_type(),
'parameter_description': self.parameter_description,
'parameter_read_only': bool_to_str(self.parameter_read_only),
'parameter_as_function': self.code_gen_variable.parameter_as_function_str(),
'mapped_param': mapped_param,
'mapped_param_underscore': mapped_param.replace('.', '_'),
'mapped_params': mapped_params,
'mapped_param_underscore': [val.replace('.', '_') for val in mapped_params],
'set_runtime_parameter': self.set_runtime_parameter,
'parameter_map': parameter_map,
'param_struct_instance': self.param_struct_instance,
Expand Down Expand Up @@ -591,19 +593,20 @@ def __str__(self):
parameter_map = get_dynamic_parameter_map(
self.dynamic_declare_parameter.parameter_name
)
parameter_map = parameter_map.split('.')
struct_name = get_dynamic_struct_name(
self.dynamic_declare_parameter.parameter_name
)
parameter_field = get_dynamic_parameter_field(
self.dynamic_declare_parameter.parameter_name
)
mapped_param = get_dynamic_mapped_parameter(
mapped_params = get_dynamic_mapped_parameter(
self.dynamic_declare_parameter.parameter_name
)

data = {
'parameter_map': parameter_map,
'mapped_param': mapped_param,
'mapped_params': mapped_params,
'dynamic_declare_parameter': str(self.dynamic_declare_parameter),
'struct_name': struct_name,
'parameter_field': parameter_field,
Expand Down
Loading