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

Adding ability to add parameters through yaml at runtime #2406

Open
wants to merge 5 commits into
base: rolling
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions rclcpp/include/rclcpp/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,14 @@ class Node : public std::enable_shared_from_this<Node>
void
undeclare_parameter(const std::string & name);

/// Load a list of parameters from a yaml parameter file.
/**
* \param[in] yaml_name The name of the yaml file that needs to be loaded.
*/
RCLCPP_PUBLIC
std::vector<rcl_interfaces::msg::SetParametersResult>
load_parameters(const std::string & yaml_filepath, const std::string & node_name_);
Copy link
Collaborator

Choose a reason for hiding this comment

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

aside the on-going discussion on how to implement, do we need to have 2nd argument node_name_? what about load_parameters takes yaml file and internally calls get_fully_qualified_name(). if load_parameters belong to the Node class, i think it should load the parameters from yaml file in the context of the Node?


/// Return true if a given parameter is declared.
/**
* \param[in] name The name of the parameter to check for being declared.
Expand Down
5 changes: 5 additions & 0 deletions rclcpp/include/rclcpp/node_interfaces/node_parameters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ class NodeParameters : public NodeParametersInterface
void
undeclare_parameter(const std::string & name) override;

RCLCPP_PUBLIC
std::vector<rcl_interfaces::msg::SetParametersResult>
load_parameters(
const std::string & yaml_filepath, const std::string & node_name_) override;

RCLCPP_PUBLIC
bool
has_parameter(const std::string & name) const override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ class NodeParametersInterface
void
undeclare_parameter(const std::string & name) = 0;

/// Load a list parameters from a yaml parameter file.
/**
* \sa rclcpp::Node::load_parameters
*/
RCLCPP_PUBLIC
virtual
std::vector<rcl_interfaces::msg::SetParametersResult>
load_parameters(
const std::string & yaml_filepath, const std::string & node_name_) = 0;

/// Return true if the parameter has been declared, otherwise false.
/**
* \sa rclcpp::Node::has_parameter
Expand Down
6 changes: 6 additions & 0 deletions rclcpp/src/rclcpp/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,12 @@ Node::undeclare_parameter(const std::string & name)
this->node_parameters_->undeclare_parameter(name);
}

std::vector<rcl_interfaces::msg::SetParametersResult>
Node::load_parameters(const std::string & yaml_filepath, const std::string & node_name_)
{
return this->node_parameters_->load_parameters(yaml_filepath, node_name_);
}

bool
Node::has_parameter(const std::string & name) const
{
Expand Down
16 changes: 16 additions & 0 deletions rclcpp/src/rclcpp/node_interfaces/node_parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,22 @@ NodeParameters::undeclare_parameter(const std::string & name)
parameters_.erase(parameter_info);
}

std::vector<rcl_interfaces::msg::SetParametersResult>
NodeParameters::load_parameters(
const std::string & yaml_filepath, const std::string & node_name_)
{
rclcpp::ParameterMap parameter_map =
rclcpp::parameter_map_from_yaml_file(yaml_filepath, node_name_.c_str());

auto iter = parameter_map.find(node_name_);
if (iter == parameter_map.end() || iter->second.size() == 0) {
throw rclcpp::exceptions::InvalidParametersException("No valid parameter");
}
auto params_result = set_parameters(iter->second);

return params_result;
}

bool
NodeParameters::has_parameter(const std::string & name) const
{
Expand Down
14 changes: 14 additions & 0 deletions rclcpp/test/rclcpp/node_interfaces/test_node_parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,20 @@ TEST_F(TestNodeParameters, list_parameters)
list_result5.names.end());
}

TEST_F(TestNodeParameters, load_parameters) {
const uint64_t expected_param_count = 4;
auto load_node = std::make_shared<rclcpp::Node>(
"load_node",
"namespace",
rclcpp::NodeOptions().allow_undeclared_parameters(true));
// load parameters
rcpputils::fs::path test_resources_path{TEST_RESOURCES_DIRECTORY};
const std::string parameters_filepath = (
test_resources_path / "test_node" / "load_parameters.yaml").string();
auto load_vector = node_parameters->load_parameters(parameters_filepath, "/namespace/load_node");
ASSERT_EQ(load_vector.size(), expected_param_count);
}

TEST_F(TestNodeParameters, parameter_overrides)
{
rclcpp::NodeOptions node_options;
Expand Down