Ask Your Question
0

[ROS2] I can publish a parameter with wrong type and get no exception

asked 2019-08-13 01:56:42 -0500

madmax gravatar image

Hi guys,

is this expected behavior, that when I publish a parameter with a wrong datatype, the result is always true?

ros2 param set /test/my_double_value 1

Returns success although value is written als int into the parameter.

And inside the test node where the parameter is declared, there is also no exception:

declare_parameter( frequencyParamName, rclcpp::ParameterValue( 20.0 ));  

for ( auto& changed_parameter : event->changed_parameters )
{
      auto paramName = changed_parameter.name;
      if ( paramName == frequencyParamName )
      {
         updateFrequencyParam( changed_parameter.value.double_value );
      }
}

Do we really need to check for correct type by ourselves or are we handling parameters wrong?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2019-08-13 09:24:10 -0500

alsora gravatar image

Yes, at the moment that is the expected behavior. If you want to avoid the type of the parameters to be changed, then you have to register a validation callback to do so.

https://github.com/ros2/rclcpp/issues... This is a discussion that I started to think about how to facilitate the validation process in some common cases as the one that you mentioned.

This is what you can do in the validation callback to check for the type. Note that it has to be done for all parameters

if (parameter.get_name().compare("my_parameter") == 0) {
              if (rclcpp::ParameterType::PARAMETER_INTEGER != parameter_type) {
                RCLCPP_WARN(this->get_logger(),
                  "Trying to set parameter '%s' with wrong type",
                  parameter.get_name().c_str()
                );
                result.successful = false;
                result.reason = "parameter \'"+parameter.get_name()+"\' must be an integer";

Here you find a full working example https://github.com/alsora/ros2-code-e...

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2019-08-13 01:56:42 -0500

Seen: 17 times

Last updated: Aug 13