ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
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]);
}