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 all 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 @@ -12,17 +12,23 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef RCLCPP__COPY_ALL_PARAMETER_VALUES_HPP_
#define RCLCPP__COPY_ALL_PARAMETER_VALUES_HPP_
#ifndef RCLCPP__PARAMETER_INTERFACES_HPP_
#define RCLCPP__PARAMETER_INTERFACES_HPP_

#include <string>
#include <vector>

#include "node_interfaces/node_base_interface.hpp"
#include "node_interfaces/node_parameters_interface.hpp"
#include "rcl_interfaces/srv/list_parameters.hpp"
#include "rcl_interfaces/msg/parameter_descriptor.hpp"
#include "rcl_interfaces/msg/set_parameters_result.hpp"

#include "rclcpp/node_interfaces/node_interfaces.hpp"
#include "rclcpp/node_interfaces/node_base_interface.hpp"
#include "rclcpp/node_interfaces/node_parameters_interface.hpp"
#include "rclcpp/parameter.hpp"
#include "rclcpp/parameter_map.hpp"
#include "rclcpp/logger.hpp"
#include "rclcpp/logging.hpp"

Expand Down Expand Up @@ -77,6 +83,36 @@ copy_all_parameter_values(
}
}

/// 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.
* \param[in] node_interface The list of variadic NodeInterfaces taken as a template to set parameters for.
* \returns an instance of a parameter map.
* \throws InvalidParametersException if no valid parameters found.
*/
std::vector<rcl_interfaces::msg::SetParametersResult>
load_parameters(
Copy link
Contributor

Choose a reason for hiding this comment

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

Just to be clear; further to my comments in #2406 (comment) , this PR is currently a NAK from me.

const std::string & yaml_filepath, rclcpp::node_interfaces::NodeInterfaces<
rclcpp::node_interfaces::NodeBaseInterface,
rclcpp::node_interfaces::NodeParametersInterface> node_interface)
{
auto base_node = node_interface.get<rclcpp::node_interfaces::NodeBaseInterface>();
base_node = node_interface.get_node_base_interface();
auto parameter_node = node_interface.get<rclcpp::node_interfaces::NodeParametersInterface>();
parameter_node = node_interface.get_node_parameters_interface();

rclcpp::ParameterMap parameter_map =
rclcpp::parameter_map_from_yaml_file(yaml_filepath, base_node->get_fully_qualified_name());

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

return params_result;
}

} // namespace rclcpp

#endif // RCLCPP__COPY_ALL_PARAMETER_VALUES_HPP_
#endif // RCLCPP__PARAMETER_INTERFACES_HPP_
2 changes: 1 addition & 1 deletion rclcpp/include/rclcpp/rclcpp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
#include <csignal>
#include <memory>

#include "rclcpp/copy_all_parameter_values.hpp"
#include "rclcpp/parameter_interfaces.hpp"
#include "rclcpp/executors.hpp"
#include "rclcpp/guard_condition.hpp"
#include "rclcpp/logging.hpp"
Expand Down
6 changes: 3 additions & 3 deletions rclcpp/test/rclcpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ ament_add_gtest(test_client test_client.cpp)
if(TARGET test_client)
target_link_libraries(test_client ${PROJECT_NAME} mimick ${rcl_interfaces_TARGETS} ${test_msgs_TARGETS})
endif()
ament_add_gtest(test_copy_all_parameter_values test_copy_all_parameter_values.cpp)
if(TARGET test_copy_all_parameter_values)
target_link_libraries(test_copy_all_parameter_values ${PROJECT_NAME})
ament_add_gtest(test_parameter_interfaces test_parameter_interfaces.cpp)
if(TARGET test_parameter_interfaces)
target_link_libraries(test_parameter_interfaces ${PROJECT_NAME})
endif()
ament_add_gtest(test_create_timer test_create_timer.cpp)
if(TARGET test_create_timer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

#include <gtest/gtest.h>
#include "rclcpp/copy_all_parameter_values.hpp"
#include "rclcpp/parameter_interfaces.hpp"
#include "rclcpp/rclcpp.hpp"

class TestNode : public ::testing::Test
Expand Down Expand Up @@ -86,3 +86,22 @@ TEST_F(TestNode, TestParamCopyingExceptions)
node2->declare_parameter("Foo1", rclcpp::ParameterValue(0.123));
EXPECT_NO_THROW(rclcpp::copy_all_parameter_values(node1, node2, override));
}

TEST_F(TestNode, TestFileImport)
{
using rclcpp::node_interfaces::NodeBaseInterface;
using rclcpp::node_interfaces::NodeParametersInterface;

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 = rclcpp::load_parameters(parameters_filepath, *load_node);
ASSERT_EQ(load_vector.size(), expected_param_count);
}