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

Automatic parameter declaration - enable existence of undeclared parameters from overrides #504

Merged
merged 2 commits into from
Aug 28, 2021
Merged
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 @@ -100,6 +100,26 @@ class ControllerInterface : public rclcpp_lifecycle::node_interfaces::LifecycleN
CONTROLLER_INTERFACE_PUBLIC
std::shared_ptr<rclcpp::Node> get_node();

/// Declare and initialize a parameter with a type.
/**
*
* Wrapper function for templated node's declare_parameter() which checks if
* parameter is already declared.
* For use in all components that inherit from ControllerInterface
*/
template <typename ParameterT>
auto auto_declare(const std::string & name, const ParameterT & default_value)
{
if (!node_->has_parameter(name))
{
return node_->declare_parameter<ParameterT>(name, default_value);
}
else
{
return node_->get_parameter(name).get_value<ParameterT>();
}
}

/**
* The methods below are a substitute to the LifecycleNode methods with the same name.
* The Life cycle is shown in ROS2 design document:
Expand Down
10 changes: 7 additions & 3 deletions controller_interface/src/controller_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ namespace controller_interface
return_type ControllerInterface::init(const std::string & controller_name)
{
node_ = std::make_shared<rclcpp::Node>(
controller_name, rclcpp::NodeOptions().allow_undeclared_parameters(true));
controller_name, rclcpp::NodeOptions()
.allow_undeclared_parameters(true)
.automatically_declare_parameters_from_overrides(true));
Copy link
Member

Choose a reason for hiding this comment

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

I have a follow-up issue with this parameter

lifecycle_state_ = rclcpp_lifecycle::State(
lifecycle_msgs::msg::State::PRIMARY_STATE_UNCONFIGURED, state_names::UNCONFIGURED);
return return_type::OK;
Expand All @@ -36,8 +38,10 @@ return_type ControllerInterface::init(const std::string & controller_name)
return_type ControllerInterface::init(
const std::string & controller_name, rclcpp::NodeOptions & node_options)
{
node_ =
std::make_shared<rclcpp::Node>(controller_name, node_options.allow_undeclared_parameters(true));
node_ = std::make_shared<rclcpp::Node>(
controller_name,
node_options.allow_undeclared_parameters(true).automatically_declare_parameters_from_overrides(
true));
lifecycle_state_ = rclcpp_lifecycle::State(
lifecycle_msgs::msg::State::PRIMARY_STATE_UNCONFIGURED, state_names::UNCONFIGURED);
return return_type::OK;
Expand Down