ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

ROS2 How to get an array from get_parameter? [closed]

asked 2020-06-05 15:26:08 -0600

Ana de Sousa gravatar image

Hello, I would like to know how to get an array from a YAML file in ROS2.

I currently have the following YAML file:

node:
    ros__parameters:
        ros_st: "hello"
        ros_i: 2
        ros_vec: [1,2]

I can declare and get parameters from YAML for 'ros_st' and 'ros_i' easily by:

this->declare_parameter("ros_st", "Hello");
this->declare_parameter("ros_i", 0);
this->get_parameter("ros_st", _st);
this->get_parameter("ros_i", _n);

But, I have no idea how to declare 'ros_vec' and get the parameters. None of these commands work:

this->declare_parameter("ros_vec", {1,2});
this->declare_parameter("ros_vec", [1,2]);

I tried to declare empty and then get values, but I also get errors:

this->declare_parameter("ros_vec");
this->get_parameter("ros_vec", x);
this->get_parameters("ros_vec", x);
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Ana de Sousa
close date 2020-08-05 16:57:33.296713

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-06-08 01:54:25 -0600

ahcorde gravatar image

Hello,

review the official demos in this link. This two lines in particular can help you.

this->declare_parameter("toto");
rclcpp::Parameter("toto", std::vector<int>({1, 2})),
edit flag offensive delete link more

Comments

Thank you for your answer. I adapted my code, and I feel that I am halfway there.

I declared and set an initial value:

this->declare_parameter("ros_vec");
this->set_parameters({rclcpp::Parameter("ros_vec", std::vector<uint8_t>({1}))});

I also got info from this parameter like the demo:

  std::stringstream ss;
  for ( auto & parameter : this->get_parameters({"ros_vec"})){
      ss << "\nParameter name: " << parameter.get_name();
      ss << "\nParameter value (" << parameter.get_type_name() << "): " <<
      parameter.value_to_string();
    }
    RCLCPP_INFO(this->get_logger(), ss.str().c_str());

But, when I try to get the values from the YAML file as I do for the other parameters:

std::vector<uint8_t> test;
this->get_parameter("ros_vec", test);

It gets them only from the init above. If I don't initialize, it does not get any value, even though the name in the YAML and in the cpp file is the same.

What am I missing here?

Ana de Sousa gravatar image Ana de Sousa  ( 2020-06-08 14:20:54 -0600 )edit

Can you share your .yaml file ?

ahcorde gravatar image ahcorde  ( 2020-06-09 02:12:33 -0600 )edit

Sure, here it goes:

my_node:
    ros__parameters:
        ros_vec: [1,2]

I was able to make things work for doubles if I initialize ros_vec: [1.0, 2.0]. I could use it and then cast it to int. But it seems very odd that it does not work for integers...

Ana de Sousa gravatar image Ana de Sousa  ( 2020-06-09 13:59:30 -0600 )edit

You can do something like this:

std::vector<long> fizz;
rclcpp::Parameter param;
this->get_parameter("ros_vec", param);
fizz = param.as_integer_array();
for(int v: fizz) {
  std::cout << "v: " << v << std::endl;
}
ahcorde gravatar image ahcorde  ( 2020-06-10 02:56:17 -0600 )edit

At

fizz = param.as_integer_array();

I get:

terminate called after throwing an instance of 'rclcpp::ParameterTypeException'
  what():  expected [integer_array] got [not set]
Ana de Sousa gravatar image Ana de Sousa  ( 2020-06-10 15:18:37 -0600 )edit

rclcpp::Parameter

std::vector<int> ros_vec; 
ros_vec = this->get_parameter("ros_vec").as_integer_array();
jxl gravatar image jxl  ( 2023-08-02 02:55:14 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-06-05 15:26:08 -0600

Seen: 5,508 times

Last updated: Jun 08 '20