Skip to content

add a new Node::get_parameter() with a default value - #309

Merged
wjwwood merged 10 commits into
masterfrom
get_parameter_with_default
Mar 21, 2017
Merged

wjwwood merged 10 commits into
masterfrom
get_parameter_with_default

Conversation

@wjwwood

@wjwwood wjwwood commented Mar 2, 2017 •

Copy link
Copy Markdown
Member

I ran into needing something like this when converting map_server. And I think it's useful, but I'm not sure about a few details.

First, If the parameter is not set, so it be set to the default value or not? Put another way:

int foo;
ASSERT_FALSE(node->get_parameter("foo", foo, 42));  // this is the new signature
bool result = node->get_parameter("foo", foo);  // Should result be true or false?

This new API is meant to replicate ROS 1's API (http://docs.ros.org/kinetic/api/roscpp/html/classros_1_1NodeHandle.html#a05941572641a453fd416c6603b91d39f):

// ROS 1
std::string frame_id;
node_handle.param("frame_id", frame_id, std::string("map"));

This is what it would look like with this pr:

// ROS 2
std::string frame_id;
node->get_parameter("frame_id", frame_id, std::string("map"));

Separately, we could choose to support the other API provided by ROS 1 which is similar but it returns T (http://docs.ros.org/kinetic/api/roscpp/html/classros_1_1NodeHandle.html#aa9b23d4206216ed13b5833fb1a090f1a):

// ROS 1 alternative
std::string frame_id = node_handle.param("frame_id", std::string("map"));

We could do this, but it begins to get a little confusing when you have one signature that takes T &, const T & and another that takes just const T &. The const should prevent you from getting into strange situations, but it might be the case that having fewer options would be better.


Finally, the Google style guide says you should pass T * rather than T & when you intend to modify the variable because it is easier to see what's going on from the calling code. In this case, I think that might be worth following. I consider this snippet from the map_server to be clearer with the rule applied:

// ROS 1 with pass by reference
private_nh.param("negate", negate, 0);
private_nh.param("occupied_thresh", occ_th, 0.65);
private_nh.param("free_thresh", free_th, 0.196);

Versus:

// ROS 1 with pass by pointer
private_nh.param("negate", &negate, 0);
private_nh.param("occupied_thresh", &occ_th, 0.65);
private_nh.param("free_thresh", &free_th, 0.196);

Novice users will probably lament that they have to add a & and they don't know why, but I think that it is easier to infer what is happening here if you know C++ well enough.


Anyways, I'd appreciate anyone who's interested in giving some feedback on this.

@wjwwood wjwwood added the in review Waiting for review (Kanban column) label Mar 2, 2017
@wjwwood

wjwwood commented Mar 2, 2017 •

Copy link
Copy Markdown
Member Author

@wjwwood

wjwwood commented Mar 15, 2017

Copy link
Copy Markdown
Member Author

Note to self, if this is merged, revert/update this commit:

ros2/navigation@d3d6c99

@wjwwood

wjwwood commented Mar 15, 2017

Copy link
Copy Markdown
Member Author

Also, I would appreciate it if someone on @ros2/team could review this or just give some feedback on the things I put in the description.

@Karsten1987

Karsten1987 commented Mar 15, 2017 •

Copy link
Copy Markdown
Contributor

I like the API in the way it is - meaning passing by ref. It's kind of clear to see what's happening here, ROS1 confirm and also since it's mainly primitives (int, floats, strings, etc), passing by pointer is kind of pointless :)

For the ROS1 alternative, I appreciate to keep the API simple and only provide the way you do it in this PR.

@dhood

dhood commented Mar 15, 2017

Copy link
Copy Markdown
Member

Looking at the ROS 1 code, it does not set the "set the parameter" with the default value if the parameter is not set

that matches what I would expect. if I want to change the stored value of the parameter, I should call set_parameter.

I think this would be less likely to trip people up (by having them wonder if it sets the parameter) if the function parameter weren't called parameter, but instead something that suggests that it is instead simply the "return value". (this is getting difficult to explain with three different meanings of the word parameter in one sentence!)

what I am suggesting is to rename the function parameter ParameterT & parameter to ParameterT & value since, in the end, what is returned in parameter does not necessarily represent the value of the parameter that is stored.

On the other hand, documentation that outlines the behaviour should be just as good, this was just food for thought.

@wjwwood

wjwwood commented Mar 15, 2017 •

Copy link
Copy Markdown
Member Author

@Karsten1987

I like the API in the way it is - meaning passing by ref. It's kind of clear to see what's happening here, ROS1 confirm and also since it's mainly primitives (int, floats, strings, etc), passing by pointer is kind of pointless :)

Ok, so we'll leave the non-const pass by reference.

For the ROS1 alternative, I appreciate to keep the API simple and only provide the way you do it in this PR.

So no additional API that returns T and takes const & std::string, const & T. Got it.


@dhood

Looking at the ROS 1 code, it does not set the "set the parameter" with the default value if the parameter is not set

that matches what I would expect. if I want to change the stored value of the parameter, I should call set_parameter.

I sort of agree. The only thing that caused me pause was that idea that if someone external from the node wants to know what the value of the parameter is (just a get), then they won't know what the node is actually using.

Dynamic reconfigure, for instance, has a publicly defined "default" value for parameters which addresses this issue. @mikaelarguedas do you have an opinion on this one?

This is somehow related to other meta information that should be defined at some point, like the range and/or discrete choices, and other things that dynamic reconfigure config files currently capture. Perhaps that should happen in a separate step, something like:

node->declare_parameter(
  "frame_id",                              // parameter name
  "The tf frame id for the camera data.",  // parameter description
  rclcpp::parameter::string_type,          // parameter type
  rclcpp::parameter::any_value,            // vector of parameter value constraints
  "camera",                                // default value
  rclcpp::parameter::readonly              // options
);

std::map<int, std::string> resolution_choices = {
  {1, "640x480"},
  {2, "1024x768"},
};
node->declare_parameter(
  "resolution",                       // parameter name
  "Resolution of the camera image.",  // parameter description
  rclcpp::parameter::enum_type,       // parameter type
  resolution_choices,                 // map of valid enum values to description (used in gui tools)
  1,                                  // default value
  rclcpp::parameter::on_change(
    [resolution_choices](rclcpp::parameter::enum & new_value) -> bool {
      if (resolution_choices.count(new_value) == 1 && other_conditions) {
        // update the camera to use new resolution, if necessary...
        return true;
      }
      return false;
  })
);

node->declare_parameter(
  "drop_frame",                                          // parameter name
  "Drop every Nth image, a way to throttle data rate.",  // parameter description
  rclcpp::parameter::int_type,                           // parameter type
  rclcpp::parameter::inclusive_range(0, 10),             // valid values
  0,                                                     // default value
  rclcpp::parameter::on_change(
    [resolution_choices](rclcpp::parameter::Description & desc, int new_value) -> bool {
      if (desc.constraints[0].value_in_rage(new_value)) {
        // update the driver to drop frames according to the new value...
        return true;
      }
      return false;
  })
);

And then you could later use these parameters without worrying about the default:

ASSERT_TRUE(node->get_parameter("frame_id", frame_id));
ASSERT_TRUE(node->get_parameter("resolution", resolution_index));
ASSERT_TRUE(node->get_parameter("drop_frame", drop_frame));

And since they have defaults, you can use a function that returns the parameter without worrying about an exception due to "not set":

std::string frame_id = node->get_parameter_as<std::string>("frame_id");
int resolution_index = node->get_parameter_as<int>("resolution");
int drop_frame = node->get_parameter_as<int>("drop_frame");

I'm making a lot of this up on the spot, but that's what I was imagining when I began to wonder whether or not the default should be set when utilized so external parties can see what the node is "using".

I think this would be less likely to trip people up (by having them wonder if it sets the parameter) if the function parameter weren't called parameter, but instead something that suggests that it is instead simply the "return value". (this is getting difficult to explain with three different meanings of the word parameter in one sentence!)

I think you're doing it right, cppreference uses the term "function parameter". If you're talking about an item being passed into a function, that can be called an argument, imo:

http://en.cppreference.com/w/cpp/language/functions

what I am suggesting is to rename the function parameter ParameterT & parameter to ParameterT & value since, in the end, what is returned in parameter does not necessarily represent the value of the parameter that is stored.

What about this prototype?:

template<typename ParameterT>
bool
get_parameter(
  const std::string & parameter_name,
  ParameterT & parameter_value,
  const ParameterT & default_value) const;

@wjwwood

wjwwood commented Mar 15, 2017

Copy link
Copy Markdown
Member Author

@dhood I suppose, reading this again, that you meant this prototype:

template<typename ParameterT>
bool
get_parameter(
  const std::string & name,
  ParameterT & value,
  const ParameterT & default_value) const;

@dhood

dhood commented Mar 15, 2017 •

Copy link
Copy Markdown
Member

I sort of agree. The only thing that caused me pause was that idea that if someone external from the node wants to know what the value of the parameter is (just a get), then they won't know what the node is actually using.

I might not have understood you, but isn't this what the bool return value is for? If it's true, then you know that the value that you got is the value that the node is using. if it's false, then you know that the node isn't using anything and that's why the provided default was used.

And then you could later use these parameters without worrying about the default:
ASSERT_TRUE(node->get_parameter("frame_id", frame_id));

This seems to me like functionality that could be added in addition to the signature proposed in this PR. Or do you see it as a competing alternative?

I think you're doing it right, cppreference uses the term "function parameter". If you're talking about an item being passed into a function, that can be called an argument, imo:

yeah we just had parameter referring to the function parameter, the ROS parameter, and the variable name "parameter" all in one sentence heh. doesn't matter, was just an aside.

@dhood I suppose, reading this again, that you meant this prototype:

yes, just a tiny subtle change (I know you enjoy thinking carefully about naming)

@wjwwood

wjwwood commented Mar 15, 2017

Copy link
Copy Markdown
Member Author

I might not have understood you, but isn't this what the bool return value is for? If it's true, then you know that the value that you got is the value that the node is using. if it's false, then you know that the node isn't using anything and that's why the provided default was used.

I'm talking about remote nodes. They don't have this interface. They have the /get_parameter service call. If the parameter remains unset, then that service call cannot tell you if the node itself is using that parameter and if so what value it was using.

This seems to me like functionality that could be added in addition to the signature proposed in this PR. Or do you see it as a competing alternative?

It can be in addition. However, right now it would need to throw if the parameter is unset, because it doesn't have the bool return code to indicate if it was set or not, nor does it have a default value to fall back on (without the "describe parameter" call preceding it).

yes, just a tiny subtle change (I know you enjoy thinking carefully about naming)

Sure, I'll update it then: 7955c2e and 0ee9f25

@wjwwood

wjwwood commented Mar 15, 2017

Copy link
Copy Markdown
Member Author

CI (since new change):

  • Linux:
    • Build Status
  • macOS:
    • Build Status
  • Windows:
    • Build Status

@wjwwood

wjwwood commented Mar 15, 2017

Copy link
Copy Markdown
Member Author

Ok, so after talking about it a bunch with @mikaelarguedas @dhood and @tfoote in person, we've changed our minds about this 😛:

  • We should not set the parameter if it is unset, because it departs from the behavior of dict.get(key, default) in Python and std::optional in C++ does not have a method like this either ("get and set if not set").
  • Instead I will rename this function to be node->get_parameter_or(std::string name, T & value, const T & alternative) to mimic the std::optional::value_or method, which we think is clearer.
    • It will behave just as implemented right now, get the parameter value if set, otherwise return the default, but do not set the parameter value afterwards.
    • We could just use std::optional directly, but it's not widely available yet 😞.
  • I'll also implement a node->set_parameter_if_not_set(std::string name, const T & default_value) method in this pr.
    • That way if you want the proposed new behavior (that we're declining) you can do it in two lines, with a set_parameter_if_not_set(name, const T & default_value) followed by a get_parameter(name, T & value).
  • In the future (not this pr), a user can use the "describe parameter" like method to set a default and then use get_parameter(name, T & value) freely afterwards without worrying about it being set, if that pattern is desired.
    • This is a better pattern for when you need to ensure a value is in the parameter before getting it, but avoids race conditions if this kind of "get with default" or "set if not set" function would be called from multiple threads (especially with different default values, which would not be advised but could happen accidentally).

@wjwwood

wjwwood commented Mar 16, 2017

Copy link
Copy Markdown
Member Author

Ok, changes made, and new tests in ros2/system_tests#193.

@wjwwood

wjwwood commented Mar 16, 2017

Copy link
Copy Markdown
Member Author

Making use of this in ros2/navigation#4.

@wjwwood

wjwwood commented Mar 16, 2017 •

Copy link
Copy Markdown
Member Author
  • Linux:
    • Build Status
  • macOS:
    • Build Status
  • Windows:
    • Build Status

@mikaelarguedas mikaelarguedas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

looks good to me. One nitpick on the function parameter name

parameter_event->changed_parameters.push_back(p.to_parameter());
} else {
// case: parameter was set before, and input is "NOT_SET"
// therefore we will "unset" the previous set parameter

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit " unset the previously set parameter"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Comment thread rclcpp/include/rclcpp/node.hpp Outdated
void
set_parameter_if_not_set(
const std::string & name,
const ParameterT & default_value);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

NIt: "default_value" -> "value" or "new_value", default doesn't really carry meaning in this new approach. Same below

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Comment thread rclcpp/include/rclcpp/node_impl.hpp Outdated
rclcpp::parameter::ParameterVariant parameter_variant;
if (!this->get_parameter(name, parameter_variant)) {
this->set_parameters({
rclcpp::parameter::ParameterVariant(name, default_value),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

looks weird/over-indented but I guess it's what it takes to make uncrustify happy...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yup, it even got it's own commit 👿: ff65663

@wjwwood

wjwwood commented Mar 16, 2017

Copy link
Copy Markdown
Member Author

My changes in a8f5d06 compile locally, so I won't rerun CI for them.

@mikaelarguedas

Copy link
Copy Markdown
Member

My changes in a8f5d06 compile locally, so I won't rerun CI for them.

👍 Thanks for the quick iteration

@wjwwood

wjwwood commented Mar 16, 2017 •

Copy link
Copy Markdown
Member Author

This passed CI (unrelated test failures on macOS and Windows).

Any other comments before I merge these?

@dirk-thomas

Copy link
Copy Markdown
Member

If I read the function name get_parameter_or I immediately ask myself "or what?". I don't have a proposal at hand but +-0 for using that function name.

@wjwwood

wjwwood commented Mar 17, 2017

Copy link
Copy Markdown
Member Author

If I read the function name get_parameter_or I immediately ask myself "or what?". I don't have a proposal at hand but +-0 for using that function name.

I agree it's a bit strange, and I'm open to alternative names.

I think it would be more natural if it were a member of a ParameterVariant for example, and it were called value_or, because that method would only have one parameter and the "or what" becomes more obvious. Though I'm uncertain as to whether or not there are other issues this approach brings up. If you're passionate about this as an alternative, speak up otherwise I'll just merge this one for lack of a better alternative.

On the positive side, this name would probably prompt you to try and understand it better. This happens for me with C++ all the time. I say "want the thing inside of that" so I go and look for methods and find value() and value_or (in this case get_parameter and get_parameter_or) and I think "oh what's the value_or option" and so I go and read about it. (and based on this realization I added some more docs to the pr)

@wjwwood

wjwwood commented Mar 21, 2017

Copy link
Copy Markdown
Member Author

Since I haven't gotten anyone speaking up for the alternative in my previous comment, I'm going to merge this as-is and we can iterate on it again in the future.

@wjwwood
wjwwood merged commit 2009ca6 into master Mar 21, 2017
@wjwwood
wjwwood deleted the get_parameter_with_default branch March 21, 2017 00:04
@wjwwood wjwwood removed the in review Waiting for review (Kanban column) label Mar 21, 2017
rclcpp::parameter::ParameterVariant parameter_variant;
bool result = get_parameter(name, parameter_variant);
parameter = parameter_variant.get_value<ParameterT>();
if (result) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants