Ask Your Question
0

Passing an array of vectors via nodehandle

asked 2020-02-22 06:41:02 -0500

crisdeodates gravatar image

I have a requirement to pass an array of vectors from a config file (linked to the rosnode) via nodehandle

The structure of my data in the config file is:

Points:
    - [0.0, 0.0, 0.0]
    - [0.0, 1.0, 0.0]
    - [0.0, 1.0, 1.0]
    - [0.0, 0.0, 1.0]

Inside my node, I am trying to get this data as follows:

std::vector<std::vector<double>> Points;
nodehandle.getParam("Points", Points);

However, I get the following error:

My_node.cpp:284: error: no matching function for call to 'ros::NodeHandle::getParam(const char [14], std::vector<std::vector<double> >&)'
                 nh.getParam("Point", Point);

When I checked the NodHandle docs, there is no similar options available to receive an array of vectors. Are there any workarounds, rather than receiving each vector in the array separately by calling nodehandle.getParam() for each vectors? ^

edit retag flag offensive close merge delete

Comments

Other ROS packages that store or communicate a matrix (with known size) often use one vector to store all the values. For example the 6x6 matrix in PoseWithCovariance use a float[36] datatype to store the values. Is this an option for you?

Wilco Bonestroo gravatar image Wilco Bonestroo  ( 2020-02-26 14:44:07 -0500 )edit

The example config files from robot_localization also have flat vectors/arrays. In the file they format the values to make them more readable.

odom0_config: [true,  true,  false,
               false, false, false,
               false, false, false,
               false, false, true,
               false, false, false]
Wilco Bonestroo gravatar image Wilco Bonestroo  ( 2020-02-26 14:47:14 -0500 )edit
1

Thanks. Unless I get some way to explicitly group the elements inside the vector, this will be my last resort. I did not go by this method initially as it is just a large array of elements rather than a group of elements.

crisdeodates gravatar image crisdeodates  ( 2020-02-26 15:35:31 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-02-26 08:56:50 -0500

Weasfas gravatar image

updated 2020-02-27 02:20:57 -0500

Hi,

For that complex structure you will need to use xmlrpcpp in order to parse the config file.

You can find a small example of the usage here and the documentation/class doc here.

EDIT: To be more specific about the capabilities of this tool and assuming you follow the structure provided in your comment:

//...
//#include <XmlRpcValue.h>
//#include <vector>
//...

//ros::NodeHandle& p_nh
//...

XmlRpc::XmlRpcValue positions;
p_nh.getParam("Points", positions);
std::vector<double> aux_vector;
std::vector<std::vector<double>> Points;

for(int ia = 0; ia < positions.size(); ++ia)
{
    for(int ib = 0; ib < positions[ia].size(); ++ib)
    {
        aux_vector.push_back(double(positions[ia][ib]));
    }

    Points.push_back(aux_vector);
    aux_vector.clear();
}

for (auto ptr = Points.begin(); ptr < Points.end(); ++ptr) 
{
    for (auto ptr2 = ptr->begin(); ptr2 < ptr->end(); ++ptr2) 
    {
        std::cout << *ptr2 << " ";
    }
    std::cout << std::endl;
}

With this chunck of code you will be able to read that config and store it is the std::vector<std::vector<double>> Points data structure.

edit flag offensive delete link more

Comments

Thanks for the point. But I quite didn't understand how this would be useful in my case. The examples I saw in the links deal with simple variables and values. My requirement is to pack an array of vectors inside a vector

crisdeodates gravatar image crisdeodates  ( 2020-02-26 15:38:24 -0500 )edit

Hi @crisdeodates,

I edit my answers to be more specific to your case.

Cheers.

Weasfas gravatar image Weasfas  ( 2020-02-27 02:21:39 -0500 )edit

Thanks. This seems more helpful. I will give it a try :)

crisdeodates gravatar image crisdeodates  ( 2020-02-27 14:41:05 -0500 )edit

@Weasfas, It worked. :)

crisdeodates gravatar image crisdeodates  ( 2020-02-27 18:26:24 -0500 )edit

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-02-22 06:41:02 -0500

Seen: 26 times

Last updated: Feb 27