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

How to set an array as a ros parameter in launch file

asked 2022-03-06 22:35:56 -0500

Kevin1719 gravatar image

I have these lines in my launch file:

<rosparam param="x">
    {1,2,3}
</rosparam>

and in another file, I try to get the parameter:

int x[3];
ros::param::get("x", x);

and I got this error when catkin_make:

error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive]
   39 |    ros::param::get("x", x);
      |                         ^
      |                         |
      |                         int*

I think I misunderstand something, or my syntax is not correct. Any suggestion?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2022-03-07 14:18:35 -0500

aarsh_t gravatar image

Referring to this

Retrieving Lists

New in ROS groovy

You can get and set lists and dictionaries of primitives and strings as std::vector and std::map containers with the following templated value types:

bool
int
float
double
string

For example, you can get vectors and maps with both the ros::NodeHandle::getParam / ros::NodeHandle::setParam interface or the ros::param::get / ros::param::set interface:

// Create a ROS node handle
ros::NodeHandle nh;

// Construct a map of strings
std::map<std::string,std::string> map_s, map_s2;
map_s["a"] = "foo";
map_s["b"] = "bar";
map_s["c"] = "baz";

// Set and get a map of strings
nh.setParam("my_string_map", map_s);
nh.getParam("my_string_map", map_s2);

// Sum a list of doubles from the parameter server
std::vector<double> my_double_list;
double sum = 0;
nh.getParam("my_double_list", my_double_list);
for(unsigned i=0; i < my_double_list.size(); i++) {
  sum += my_double_list[i];
}

On ROS Fuerte and earlier, lists on the parameter server can only be retreived through the use of the XmlRpc::XmlRpcValue class, which can represent any of the types on the parameter server. This is still a valid method in later ROS versions.

XmlRpc::XmlRpcValue my_list;
nh.getParam("my_list", my_list);
ROS_ASSERT(my_list.getType() == XmlRpc::XmlRpcValue::TypeArray);

for (int32_t i = 0; i < my_list.size(); ++i) 
{
  ROS_ASSERT(my_list[i].getType() == XmlRpc::XmlRpcValue::TypeDouble);
  sum += static_cast<double>(my_list[i]);
}
edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2022-03-06 22:35:56 -0500

Seen: 1,299 times

Last updated: Mar 07 '22