ros2 / rclcpp

rclcpp (ROS Client Library for C++)
Apache License 2.0
541 stars 418 forks source link

Expected vs Unexpected parameters Node::create_param() #475

Closed sloretz closed 9 months ago

sloretz commented 6 years ago

Feature request

Allow node author to define expected parameters and what happens when an unexpected parameter is set.

Feature description

This issue is a first step towards larger features like parameter constraints (allowed types, read-only parameters, allowed ranges, etc...). These will be ticketed separately.

If a node does not use parameters:

If a node uses parameters and knows all parameters it can use at compile time

If a node uses parameters but does not know all parameters it can use at compile time

Implementation considerations

Some parameters functionality will move to rcl (ros2/rcl#194). This ticket assumes the client library (rclcpp, rclpy, etc) will be responsible for parameter storage including default values and having knowledge of expected versus unexpected parameters.

Lifecycle considerations

If a node author wants custom logic for unexpected parameters then that logic must be set up before a parameter can be set by a run-time user. This includes defining expected parameters. Parameters cannot be set until a node is added to an executor, and that can't happen until a node is constructed. Therefore, a node author may define parameters and set up unexpected parameter handling in their constructor.

Creating a parameter
/// Create a parameter with an optional initial value
/*
 * \param[in] initial_value the name and initial value of the parameter
 * \raise an exception if an error occurs
 */
void
NodeParameters::create_parameter(const ParameterVariant & initial_value);

void
NodePamameters::create_parameter(const std::string & name)
{
  /* TODO it does not appear possible to create a ParameterVariant with a name but no type */
}

// Convenience call
template <typename T>
void
NodePamameters::create_parameter(const std::string & name, T value)
{
  ParameterVariant param(name, value);
  return create_parameter(param);
}
Deleting/undeclaring a parameter
/// Undeclare parameter with a given name
/* Deleted parameters are treated as unexpected the next time a user tries to set them
 * \param[in] name the name of the parameter to declare
 * \raise an exception if an error occurs
 */
void
NodeParameters::delete_parameter(const std::string & name);
Registering callback for unexpected parameters.
// Set callback to be called when when an unexpected parameter is set
// This callback controls whether the parameter change is accepted or rejected
// \sa register_param_change_callback() for a callback called on all parameter changes
void
NodeParameters::register_unexpected_param_change_callback(ParametersCallbackFunction callback);
Does setting a parameter define/create/declare it?

If No:

if yes:

Behavior: Assuming node author does not declare parameters or change how unexpected parameters are handled

sloretz commented 6 years ago

Related to the discussion concluded here: https://github.com/ros2/rclcpp/pull/309#issuecomment-286906648

clalancette commented 9 months ago

Allow node author to define expected parameters and what happens when an unexpected parameter is set.

With the design of parameters to be declared, I think that this functionality is basically already present. I'm going to close this out, but please feel free to reopen if you disagree.