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

Add ignore override argument to declare parameter #767

Merged
merged 2 commits into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 15 additions & 9 deletions rclcpp/include/rclcpp/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ class Node : public std::enable_shared_from_this<Node>
const std::string & name,
const rclcpp::ParameterValue & default_value = rclcpp::ParameterValue(),
const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor =
rcl_interfaces::msg::ParameterDescriptor());
rcl_interfaces::msg::ParameterDescriptor(),
bool ignore_override = false);
wjwwood marked this conversation as resolved.
Show resolved Hide resolved

/// Declare and initialize a parameter with a type.
/**
Expand Down Expand Up @@ -425,7 +426,8 @@ class Node : public std::enable_shared_from_this<Node>
const std::string & name,
const ParameterT & default_value,
const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor =
rcl_interfaces::msg::ParameterDescriptor());
rcl_interfaces::msg::ParameterDescriptor(),
bool ignore_override = false);

/// Declare and initialize several parameters with the same namespace and type.
/**
Expand All @@ -440,11 +442,13 @@ class Node : public std::enable_shared_from_this<Node>
* expanding "namespace.key".
* This allows you to declare several parameters at once without a namespace.
*
* The map may either contain default values for parameters, or a std::pair
* where the first element is a default value and the second is a
* parameter descriptor.
* This function only takes the default value, but there is another overload
* which takes the std::pair with the default value and descriptor.
* The map contains default values for parameters.
* There is another overload which takes the std::pair with the default value
* and descriptor, and another which takes an std::tuple with the same two
* parameters and a bool indicating if overriding the parameter default is allowed.
wjwwood marked this conversation as resolved.
Show resolved Hide resolved
*
* If `ignore_overrides` is `true`, all the overrides of the parameters declared
* by the function call will be ignored.
*
* This method, if successful, will result in any callback registered with
* set_on_parameters_set_callback to be called, once for each parameter.
Expand All @@ -464,7 +468,8 @@ class Node : public std::enable_shared_from_this<Node>
std::vector<ParameterT>
declare_parameters(
const std::string & namespace_,
const std::map<std::string, ParameterT> & parameters);
const std::map<std::string, ParameterT> & parameters,
bool ignore_overrides = false);

/// Declare and initialize several parameters with the same namespace and type.
/**
Expand All @@ -480,7 +485,8 @@ class Node : public std::enable_shared_from_this<Node>
const std::map<
std::string,
std::pair<ParameterT, rcl_interfaces::msg::ParameterDescriptor>
> & parameters);
> & parameters,
bool ignore_overrides = false);

/// Undeclare a previously declared parameter.
/**
Expand Down
25 changes: 17 additions & 8 deletions rclcpp/include/rclcpp/node_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,27 +250,34 @@ auto
Node::declare_parameter(
const std::string & name,
const ParameterT & default_value,
const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor)
const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor,
bool ignore_override)
{
return this->declare_parameter(
name,
rclcpp::ParameterValue(default_value),
parameter_descriptor
parameter_descriptor,
ignore_override
).get<ParameterT>();
}

template<typename ParameterT>
std::vector<ParameterT>
Node::declare_parameters(
const std::string & namespace_,
const std::map<std::string, ParameterT> & parameters)
const std::map<std::string, ParameterT> & parameters,
bool ignore_overrides)
{
std::vector<ParameterT> result;
std::string normalized_namespace = namespace_.empty() ? "" : (namespace_ + ".");
std::transform(
parameters.begin(), parameters.end(), std::back_inserter(result),
[this, &normalized_namespace](auto element) {
return this->declare_parameter(normalized_namespace + element.first, element.second);
[this, &normalized_namespace, ignore_overrides](auto element) {
return this->declare_parameter(
normalized_namespace + element.first,
element.second,
rcl_interfaces::msg::ParameterDescriptor(),
ignore_overrides);
}
);
return result;
Expand All @@ -283,18 +290,20 @@ Node::declare_parameters(
const std::map<
std::string,
std::pair<ParameterT, rcl_interfaces::msg::ParameterDescriptor>
> & parameters)
> & parameters,
bool ignore_overrides)
{
std::vector<ParameterT> result;
std::string normalized_namespace = namespace_.empty() ? "" : (namespace_ + ".");
std::transform(
parameters.begin(), parameters.end(), std::back_inserter(result),
[this, &normalized_namespace](auto element) {
[this, &normalized_namespace, ignore_overrides](auto element) {
return static_cast<ParameterT>(
this->declare_parameter(
normalized_namespace + element.first,
element.second.first,
element.second.second)
element.second.second,
ignore_overrides)
);
}
);
Expand Down
3 changes: 2 additions & 1 deletion rclcpp/include/rclcpp/node_interfaces/node_parameters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ class NodeParameters : public NodeParametersInterface
declare_parameter(
const std::string & name,
const rclcpp::ParameterValue & default_value,
const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor) override;
const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor,
bool ignore_override) override;

RCLCPP_PUBLIC
void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class NodeParametersInterface
const std::string & name,
const rclcpp::ParameterValue & default_value = rclcpp::ParameterValue(),
const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor =
rcl_interfaces::msg::ParameterDescriptor()) = 0;
rcl_interfaces::msg::ParameterDescriptor(),
bool ignore_override = false) = 0;

/// Undeclare a parameter.
/**
Expand Down
11 changes: 8 additions & 3 deletions rclcpp/src/rclcpp/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,14 @@ const rclcpp::ParameterValue &
Node::declare_parameter(
const std::string & name,
const rclcpp::ParameterValue & default_value,
const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor)
{
return this->node_parameters_->declare_parameter(name, default_value, parameter_descriptor);
const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor,
bool ignore_override)
{
return this->node_parameters_->declare_parameter(
name,
default_value,
parameter_descriptor,
ignore_override);
}

void
Expand Down
15 changes: 9 additions & 6 deletions rclcpp/src/rclcpp/node_interfaces/node_parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ NodeParameters::NodeParameters(
this->declare_parameter(
pair.first,
pair.second,
rcl_interfaces::msg::ParameterDescriptor());
rcl_interfaces::msg::ParameterDescriptor(),
true);
}
}
}
Expand Down Expand Up @@ -333,7 +334,7 @@ __declare_parameter_common(
const std::map<std::string, rclcpp::ParameterValue> & overrides,
OnParametersSetCallbackType on_set_parameters_callback,
rcl_interfaces::msg::ParameterEvent * parameter_event_out,
bool use_overrides = true)
bool ignore_override = false)
{
using rclcpp::node_interfaces::ParameterInfo;
std::map<std::string, ParameterInfo> parameter_infos {{name, ParameterInfo()}};
Expand All @@ -342,7 +343,7 @@ __declare_parameter_common(
// Use the value from the overrides if available, otherwise use the default.
const rclcpp::ParameterValue * initial_value = &default_value;
auto overrides_it = overrides.find(name);
if (use_overrides && overrides_it != overrides.end()) {
if (!ignore_override && overrides_it != overrides.end()) {
initial_value = &overrides_it->second;
}

Expand All @@ -369,7 +370,8 @@ const rclcpp::ParameterValue &
NodeParameters::declare_parameter(
const std::string & name,
const rclcpp::ParameterValue & default_value,
const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor)
const rcl_interfaces::msg::ParameterDescriptor & parameter_descriptor,
bool ignore_override)
{
std::lock_guard<std::mutex> lock(mutex_);

Expand All @@ -392,7 +394,8 @@ NodeParameters::declare_parameter(
parameters_,
parameter_overrides_,
on_parameters_set_callback_,
&parameter_event);
&parameter_event,
ignore_override);

// If it failed to be set, then throw an exception.
if (!result.successful) {
Expand Down Expand Up @@ -524,7 +527,7 @@ NodeParameters::set_parameters_atomically(const std::vector<rclcpp::Parameter> &
parameter_overrides_,
nullptr, // callback is explicitly null, so that it is called only once, when setting below.
&parameter_event_msg,
false);
true);
if (!result.successful) {
// Declare failed, return knowing that nothing was changed because the
// staged changes were not applied.
Expand Down
24 changes: 18 additions & 6 deletions rclcpp/test/test_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,14 +362,15 @@ TEST_F(TestNode, declare_parameter_with_no_initial_values) {
}
}

TEST_F(TestNode, declare_parameter_with_initial_values) {
// test cases with initial values
TEST_F(TestNode, declare_parameter_with_overrides) {
// test cases with overrides
rclcpp::NodeOptions no;
no.parameter_overrides({
{"parameter_no_default", 42},
{"parameter_no_default_set", 42},
{"parameter_no_default_set_cvref", 42},
{"parameter_and_default", 42},
{"parameter_and_default_ignore_override", 42},
{"parameter_custom", 42},
{"parameter_template", 42},
{"parameter_already_declared", 42},
Expand All @@ -378,13 +379,13 @@ TEST_F(TestNode, declare_parameter_with_initial_values) {
});
auto node = std::make_shared<rclcpp::Node>("test_declare_parameter_node"_unq, no);
{
// no default, with initial
// no default, with override
rclcpp::ParameterValue value = node->declare_parameter("parameter_no_default");
EXPECT_EQ(value.get_type(), rclcpp::PARAMETER_INTEGER);
EXPECT_EQ(value.get<int>(), 42);
}
{
// no default, with initial, and set after
// no default, with override, and set after
rclcpp::ParameterValue value = node->declare_parameter("parameter_no_default_set");
EXPECT_EQ(value.get_type(), rclcpp::PARAMETER_INTEGER);
EXPECT_EQ(value.get<int>(), 42);
Expand All @@ -393,7 +394,7 @@ TEST_F(TestNode, declare_parameter_with_initial_values) {
EXPECT_EQ(node->get_parameter("parameter_no_default_set").get_value<int>(), 44);
}
{
// no default, with initial
// no default, with override
const rclcpp::ParameterValue & value =
node->declare_parameter("parameter_no_default_set_cvref");
EXPECT_EQ(value.get_type(), rclcpp::PARAMETER_INTEGER);
Expand All @@ -403,12 +404,23 @@ TEST_F(TestNode, declare_parameter_with_initial_values) {
EXPECT_EQ(value.get<int>(), 44);
}
{
// int default, with initial
// int default, with override
rclcpp::ParameterValue default_value(43);
rclcpp::ParameterValue value = node->declare_parameter("parameter_and_default", default_value);
EXPECT_EQ(value.get_type(), rclcpp::PARAMETER_INTEGER);
EXPECT_EQ(value.get<int>(), 42); // and not 43 which is the default value
}
{
// int default, with override and ignoring it
rclcpp::ParameterValue default_value(43);
rclcpp::ParameterValue value = node->declare_parameter(
"parameter_and_default_ignore_override",
default_value,
rcl_interfaces::msg::ParameterDescriptor(),
true);
EXPECT_EQ(value.get_type(), rclcpp::PARAMETER_INTEGER);
EXPECT_EQ(value.get<int>(), 43); // and not 43 which is the default value
wjwwood marked this conversation as resolved.
Show resolved Hide resolved
}
{
// int default, with initial, custom parameter descriptor
rclcpp::ParameterValue default_value(43);
Expand Down