Repository navigation
add a new Node::get_parameter() with a default value - #309
Conversation
|
Looking at the ROS 1 code, it does not set the "set the parameter" with the default value if the parameter is not set: |
|
Note to self, if this is merged, revert/update this commit: |
|
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. |
|
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. |
that matches what I would expect. if I want to change the stored value of the parameter, I should call 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 what I am suggesting is to rename the function parameter On the other hand, documentation that outlines the behaviour should be just as good, this was just food for thought. |
Ok, so we'll leave the non-const pass by reference.
So no additional API that returns
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 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:
What about this prototype?: template<typename ParameterT>
bool
get_parameter(
const std::string & parameter_name,
ParameterT & parameter_value,
const ParameterT & default_value) const; |
|
@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; |
I might not have understood you, but isn't this what the
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?
yeah we just had parameter referring to the function parameter, the ROS parameter, and the variable name "
yes, just a tiny subtle change (I know you enjoy thinking carefully about naming) |
I'm talking about remote nodes. They don't have this interface. They have the
It can be in addition. However, right now it would need to throw if the parameter is unset, because it doesn't have the
|
|
Ok, so after talking about it a bunch with @mikaelarguedas @dhood and @tfoote in person, we've changed our minds about this 😛:
|
|
Ok, changes made, and new tests in ros2/system_tests#193. |
|
Making use of this in ros2/navigation#4. |
mikaelarguedas
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Nit " unset the previously set parameter"
| void | ||
| set_parameter_if_not_set( | ||
| const std::string & name, | ||
| const ParameterT & default_value); |
There was a problem hiding this comment.
NIt: "default_value" -> "value" or "new_value", default doesn't really carry meaning in this new approach. Same below
| rclcpp::parameter::ParameterVariant parameter_variant; | ||
| if (!this->get_parameter(name, parameter_variant)) { | ||
| this->set_parameters({ | ||
| rclcpp::parameter::ParameterVariant(name, default_value), |
There was a problem hiding this comment.
looks weird/over-indented but I guess it's what it takes to make uncrustify happy...
|
My changes in a8f5d06 compile locally, so I won't rerun CI for them. |
👍 Thanks for the quick iteration |
|
This passed CI (unrelated test failures on macOS and Windows). Any other comments before I merge these? |
|
If I read the 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 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 |
|
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. |
| rclcpp::parameter::ParameterVariant parameter_variant; | ||
| bool result = get_parameter(name, parameter_variant); | ||
| parameter = parameter_variant.get_value<ParameterT>(); | ||
| if (result) { |
There was a problem hiding this comment.
I don't know what is configured differently on Portable but this line seems to have caused a warning: http://ci.ros2.org/job/ci_windows/2463/warnings41Result/
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:
This new API is meant to replicate ROS 1's API (http://docs.ros.org/kinetic/api/roscpp/html/classros_1_1NodeHandle.html#a05941572641a453fd416c6603b91d39f):
This is what it would look like with this pr:
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):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 justconst T &. Theconstshould 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 thanT &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 themap_serverto be clearer with the rule applied:Versus:
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.