Skip to content

Conversation

@bpwilcox
Copy link
Contributor

@bpwilcox bpwilcox commented Oct 30, 2019

This PR (built on top of required PR ros2/rcl#533) allows for parameter descriptions to be parsed from the yaml parameter file (proposed syntax described in above PR link) into the parameter_overrides when declaring a parameter via declare_parameter. This enables users to describe the parameter constraints at run-time to fill a ParameterDescriptor.msg for parameter validation and documentation. As mentioned in #807 (comment), this feature may be particularly useful for a "parameter blackboard" or for dynamic parameter tuning.

Matching modifications can be made to enable this feature for rclpy.

@bpwilcox bpwilcox force-pushed the add_parameter_descriptors_from_yaml branch from fbde81f to 532f273 Compare October 30, 2019 00:57
@wjwwood wjwwood added the enhancement New feature or request label Nov 14, 2019
@wjwwood
Copy link
Member

wjwwood commented Nov 15, 2019

@hidmic can you please take a look at these pr's?

Copy link
Contributor

@hidmic hidmic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First pass, though most comments that apply to ros2/rcl#533 (review) will likely apply to this patch.

Also, rclpy must be updated as well (but hold on until we've settled on these PRs first).

/// A map of fully qualified node names to a list of parameters
using ParameterMap = std::unordered_map<std::string, std::vector<Parameter>>;
using rcl_interfaces::msg::ParameterDescriptor;
using ParameterAndDescriptor = std::unordered_map<std::string, std::pair<Parameter,
Copy link
Contributor

@hidmic hidmic Nov 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bpwilcox std::pair is convenient but obscure when reading code (e.g. what is param.second.second??). Consider defining a struct instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it may be even easier to directly create the ParameterInfo struct here.

const char * const c_param_name = c_param_descriptors_node->parameter_names[p];
if (NULL == c_param_name) {
std::string message(
"At node " + std::to_string(n) + " parameter " + std::to_string(p) + " name is NULL");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bpwilcox do we need an auxiliary variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The message string? Probably not needed, this was left from the previous code, not my additions.

@bpwilcox
Copy link
Contributor Author

@hidmic Thanks for the first pass reviews! In regard to changes to rclpy, I do have have a local branch with modifications that I can update for a PR soon after this one.

@hidmic
Copy link
Contributor

hidmic commented Feb 12, 2021

@bpwilcox do you still plan on submitting changes? Or shall I close this patch?

@bpwilcox
Copy link
Contributor Author

@bpwilcox do you still plan on submitting changes? Or shall I close this patch?

Yes, I believe I should have some more cycles to work on this in the near future.

@bpwilcox bpwilcox force-pushed the add_parameter_descriptors_from_yaml branch from 532f273 to b8d89d3 Compare June 29, 2021 16:44
bpwilcox and others added 9 commits June 29, 2021 16:46
Signed-off-by: bpwilcox <[email protected]>
Signed-off-by: Brian Wilcox <[email protected]>
Signed-off-by: bpwilcox <[email protected]>
Signed-off-by: Brian Wilcox <[email protected]>
Signed-off-by: bpwilcox <[email protected]>
Signed-off-by: Brian Wilcox <[email protected]>
Signed-off-by: bpwilcox <[email protected]>
Signed-off-by: Brian Wilcox <[email protected]>
minor cleanup and lint fix

Signed-off-by: bpwilcox <[email protected]>
Signed-off-by: Brian Wilcox <[email protected]>
Signed-off-by: Brian Wilcox <[email protected]>
@bpwilcox bpwilcox force-pushed the add_parameter_descriptors_from_yaml branch from b8d89d3 to 070abdd Compare June 29, 2021 16:47
@alsora
Copy link
Collaborator

alsora commented Aug 9, 2021

FYI this PR has been rebased to work with Galactic and it's ready for review, together with ros2/rcl#533

@hidmic hidmic self-requested a review August 9, 2021 13:17
ParameterMap
parameter_map_from_yaml_file(const std::string & yaml_filename);


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bpwilcox nit: drop extra blank line

// but did not get declared explcitily by this point.
if (automatically_declare_parameters_from_overrides) {
rcl_interfaces::msg::ParameterDescriptor descriptor;
descriptor.dynamic_typing = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bpwilcox why the change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've reverted this change, though I am still open to discussing the most appropriate behavior for dynamic_tuning field being set from a yaml file. For example, if you have a blackboard node and wanted to have a parameter become static typing, this change would overwrite. Before allowing yaml descriptor overrides, I'd agree it makes more sense to have parameters be dynamic typing when automatically declared, but now with this feature, I'm unsure if this should be the case.

Comment on lines 353 to 370
auto has_parameter_override = false;
auto has_descriptor_override = false;

if (overrides_it->second.value.get_type() != rclcpp::ParameterType::PARAMETER_NOT_SET) {
has_parameter_override = true;
}
if (overrides_it->second.descriptor.name != "") {
has_descriptor_override = true;
}

if (has_parameter_override && has_descriptor_override) {
initial_value = &overrides_it->second.value;
parameter_infos.at(name).descriptor = overrides_it->second.descriptor;
} else if (has_parameter_override) {
initial_value = &overrides_it->second.value;
} else if (has_descriptor_override) {
parameter_infos.at(name).descriptor = overrides_it->second.descriptor;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bpwilcox isn't this equivalent to:

Suggested change
auto has_parameter_override = false;
auto has_descriptor_override = false;
if (overrides_it->second.value.get_type() != rclcpp::ParameterType::PARAMETER_NOT_SET) {
has_parameter_override = true;
}
if (overrides_it->second.descriptor.name != "") {
has_descriptor_override = true;
}
if (has_parameter_override && has_descriptor_override) {
initial_value = &overrides_it->second.value;
parameter_infos.at(name).descriptor = overrides_it->second.descriptor;
} else if (has_parameter_override) {
initial_value = &overrides_it->second.value;
} else if (has_descriptor_override) {
parameter_infos.at(name).descriptor = overrides_it->second.descriptor;
}
if (overrides_it->second.value.get_type() != rclcpp::ParameterType::PARAMETER_NOT_SET) {
initial_value = &overrides_it->second.value;
}
if (overrides_it->second.descriptor.name != "") {
parameter_infos[name].descriptor = overrides_it->second.descriptor;
}

}
// zero init parameter value
c_node_param_descriptors->parameter_descriptors =
static_cast<rcl_param_descriptor_t *>(alloc.allocate(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bpwilcox nit: using zero_allocate() would have the same net effect.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree but is there any benefit of changing?

EXPECT_TRUE(params.count("bar"));
EXPECT_STREQ("Integer Range Descriptor", params.at("bar").descriptor.description.c_str());
EXPECT_STREQ("Even numbers only", params.at("bar").descriptor.additional_constraints.c_str());
EXPECT_EQ(-1234, params.at("bar").descriptor.integer_range[0].from_value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bpwilcox can't we use min_value in place for the literal? Same in all other cases below.


/// A description of the parameter
rcl_interfaces::msg::ParameterDescriptor descriptor;
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bpwilcox meta: hmm, why not pushing this descriptor attribute into rclcpp::Parameter? Then we can use rclcpp::Parameter everywhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do agree that you could put the descriptor in the rclcpp::Parameter object, however that seems to be a more far-reaching change than this PR, especially since many user-code uses rclcpp::Parameter. I think it is a good candidate for a follow-up PR.

@bpwilcox bpwilcox force-pushed the add_parameter_descriptors_from_yaml branch from 29be629 to ff1b8e5 Compare December 12, 2021 12:59
@bpwilcox
Copy link
Contributor Author

@hidmic Could you take another pass through this review since latest changes?

@hidmic hidmic self-requested a review January 31, 2022 23:25
@hidmic
Copy link
Contributor

hidmic commented Jan 31, 2022

@bpwilcox yes! I'll take a look at these tomorrow morning first thing. Sorry for the insane delay, I've been too busy.

@clalancette clalancette changed the base branch from master to rolling June 28, 2022 14:28
nnmm pushed a commit to ApexAI/rclcpp that referenced this pull request Jul 9, 2022
)

Signed-off-by: Simon Honigmann <[email protected]>

Co-authored-by: Simon Honigmann <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants