ROS 2 Bouncy get_parameter
Hi, I'm using ROS 2 Bouncy and I'm trying to get parameters from a yaml file. I checked a few ways to get parameters and I found one that works but takes 3 lines for each parameter :
auto ip_port_param = rclcpp::Parameter("ip_port", 10940);
nh_->get_parameter("ip_port", ip_port_param);
ip_port_ = ip_port_param.as_int();
Is there a nicer way to get parameters in one line like in ROS 1 ?
pnh_.param<int>("ip_port", ip_port_, 10940);
I could change ip_port_
into a rclcpp::Parameter
instead of an int
but then I would have to modify the whole code. Would that be the "proper" way to do it ?
I also tried to use rclcpp::SyncParametersClient
but I can't use it from within a node, I have to be outside of it (ie: in the main) because the class constructor takes a rclcpp::Node::SharedPtr node
. But when I'm in the node if I use this
I get a rclcpp::Node*
or if I use this->shared_from_this()
then it compiles and run but I don't get the parameters, they take the default value.
auto parameters_client = std::make_shared<rclcpp::SyncParametersClient>(this->shared_from_this());
ip_port_ = parameters_client->get_parameter("ip_port", 10940);
All the examples from the demos pkg that use rclcpp::SyncParametersClient
, do it from the main and not inside the node.
Whereas for rclcpp::AsyncParametersClient
, you can use it inside your node with this
but there is no T get_parameter (const std::string ¶meter_name, const T &default_value)
function, you have to use a get_parameters(...)
and then you don't even get the direct values but a vector of Parameter()
.
Am I missing something or is it just the way we have to get parameters for now ?
Thanks.
Edit : I can do something like :
template <class T>
rclcpp::Parameter get_param (rclcpp::Node* node, std::string param_name, T default_value) {
auto param = rclcpp::Parameter(param_name, default_value);
node->get_parameter(param_name, param);
return param;
}
And then :
ip_port_ = get_param(this, "ip_port", "").as_int();
But I feel like it should be something that's already there.