rtt_rosparam_service.cpp
Go to the documentation of this file.
1 #include <rtt/RTT.hpp>
2 #include <rtt/Property.hpp>
5 
6 #include <boost/utility/enable_if.hpp>
7 #include <boost/type_traits/is_convertible.hpp>
8 
9 #include <XmlRpcException.h>
10 
11 #include <Eigen/Dense>
12 
13 #include <ros/ros.h>
14 
15 #include <rtt_rosparam/rosparam.h>
16 
17 #ifndef ADD_ROSPARAM_OPERATION
18 #define ADD_ROSPARAM_OPERATION(return_type_str, return_type, func) \
19  this->addOperation("get"#return_type_str, &ROSParamService::get##func< return_type , RELATIVE >, this).doc("Get a " #return_type " from rosparam"); \
20  this->addOperation("set"#return_type_str, &ROSParamService::set##func< return_type , RELATIVE >, this).doc("Set a " #return_type " in rosparam"); \
21  this->addOperation("get"#return_type_str"Relative", &ROSParamService::get##func< return_type , RELATIVE >, this).doc("Get a " #return_type " from rosparam using the relative resolution policy : `relative/param`"); \
22  this->addOperation("set"#return_type_str"Relative", &ROSParamService::set##func< return_type , RELATIVE >, this).doc("Set a " #return_type " in rosparam using the relative resolution policy : `relative/param`"); \
23  this->addOperation("get"#return_type_str"Absolute", &ROSParamService::get##func< return_type , ABSOLUTE >, this).doc("Get a " #return_type " from rosparam using the absolute resolution policy : `/global/param`"); \
24  this->addOperation("set"#return_type_str"Absolute", &ROSParamService::set##func< return_type , ABSOLUTE >, this).doc("Set a " #return_type " in rosparam using the absolute resolution policy : `/global/param`"); \
25  this->addOperation("get"#return_type_str"Private", &ROSParamService::get##func< return_type , PRIVATE >, this).doc("Get a " #return_type " from rosparam using the private resolution policy : `~private/param`"); \
26  this->addOperation("set"#return_type_str"Private", &ROSParamService::set##func< return_type , PRIVATE >, this).doc("Set a " #return_type " in rosparam using the private resolution policy : `~private/param`"); \
27  this->addOperation("get"#return_type_str"ComponentPrivate", &ROSParamService::get##func< return_type , COMPONENT_PRIVATE >, this).doc("Get a " #return_type " from rosparam using the following resolution policy : `~component_name/param`"); \
28  this->addOperation("set"#return_type_str"ComponentPrivate", &ROSParamService::set##func< return_type , COMPONENT_PRIVATE >, this).doc("Set a " #return_type " in rosparam using the following resolution policy : `~component_name/param`"); \
29  this->addOperation("get"#return_type_str"ComponentRelative", &ROSParamService::get##func< return_type , COMPONENT_RELATIVE >, this).doc("Get a " #return_type " from rosparam using the following resolution policy : `component_name/param`"); \
30  this->addOperation("set"#return_type_str"ComponentRelative", &ROSParamService::set##func< return_type , COMPONENT_RELATIVE >, this).doc("Set a " #return_type " in rosparam using the following resolution policy : `component_name/param`"); \
31  this->addOperation("get"#return_type_str"ComponentAbsolute", &ROSParamService::get##func< return_type , COMPONENT_ABSOLUTE >, this).doc("Get a " #return_type " from rosparam using the following resolution policy : `/component_name/param`"); \
32  this->addOperation("set"#return_type_str"ComponentAbsolute", &ROSParamService::set##func< return_type , COMPONENT_ABSOLUTE >, this).doc("Set a " #return_type " in rosparam using the following resolution policy : `/component_name/param`");
33 #endif
34 
35 
36 using namespace RTT;
37 using namespace std;
38 
39 using namespace rtt_rosparam;
40 
42 {
43 public:
44 
46  Service("rosparam", owner)
47  {
48  this->doc("RTT Service for synchronizing ROS parameters with the properties of a corresponding RTT component");
49 
50  this->setValue(new Constant<int>("RELATIVE", static_cast<int>(RELATIVE)));
51  this->setValue(new Constant<int>("ABSOLUTE", static_cast<int>(ABSOLUTE)));
52  this->setValue(new Constant<int>("PRIVATE", static_cast<int>(PRIVATE)));
53  this->setValue(new Constant<int>("COMPONENT", static_cast<int>(COMPONENT)));
54  this->setValue(new Constant<int>("COMPONENT_PRIVATE", static_cast<int>(COMPONENT_PRIVATE)));
55  this->setValue(new Constant<int>("COMPONENT_RELATIVE", static_cast<int>(COMPONENT_RELATIVE)));
56  this->setValue(new Constant<int>("COMPONENT_ABSOLUTE", static_cast<int>(COMPONENT_ABSOLUTE)));
57 
58  this->addOperation("getAllRelative", &ROSParamService::getParamsRelative, this)
59  .doc("Gets all properties of this component (and its sub-services) from the ROS param server in the relative namespace.");
60  this->addOperation("getAllAbsolute", &ROSParamService::getParamsAbsolute, this)
61  .doc("Gets all properties of this component (and its sub-services) from the ROS param server in the absolute namespace.");
62  this->addOperation("getAllPrivate", &ROSParamService::getParamsPrivate, this)
63  .doc("Gets all properties of this component (and its sub-services) from the ROS param server in the node's private namespace.");
64  this->addOperation("getAllComponentPrivate", &ROSParamService::getParamsComponentPrivate, this)
65  .doc("Gets all properties of this component (and its sub-services) from the ROS param server in the component's private namespace.");
66  this->addOperation("getAllComponentRelative", &ROSParamService::getParamsComponentRelative, this)
67  .doc("Gets all properties of this component (and its sub-services) from the ROS param server in the component's relative namespace.");
68  this->addOperation("getAllComponentAbsolute", &ROSParamService::getParamsComponentAbsolute, this)
69  .doc("Gets all properties of this component (and its sub-services) from the ROS param server in the component's absolute namespace.");
70  this->addOperation("getAll", &ROSParamService::getParamsComponentPrivate, this)
71  .doc("Gets all properties of this component (and its sub-services) from the ROS param server in the component's private namespace. This is an alias for getAllComponentPrivate().");
72 
73  this->addOperation("setAllRelative", &ROSParamService::setParamsRelative, this)
74  .doc("Stores all properties of this component (and its sub-services) on the ROS param server from the similarly-named property in the relative namespace.");
75  this->addOperation("setAllAbsolute", &ROSParamService::setParamsAbsolute, this)
76  .doc("Stores all properties of this component (and its sub-services) on the ROS param server from the similarly-named property in the absolute namespace.");
77  this->addOperation("setAllPrivate", &ROSParamService::setParamsPrivate, this)
78  .doc("Stores all properties of this component (and its sub-services) on the ROS param server from the similarly-named property in the node's private namespace.");
79  this->addOperation("setAllComponentPrivate", &ROSParamService::setParamsComponentPrivate, this)
80  .doc("Stores all properties of this component (and its sub-services) on the ROS param server from the similarly-named property in the component's private namespace.");
81  this->addOperation("setAllComponentRelative", &ROSParamService::setParamsComponentRelative, this)
82  .doc("Stores all properties of this component (and its sub-services) on the ROS param server from the similarly-named property in the component's relative namespace.");
83  this->addOperation("setAllComponentAbsolute", &ROSParamService::setParamsComponentAbsolute, this)
84  .doc("Stores all properties of this component (and its sub-services) on the ROS param server from the similarly-named property in the component's absolute namespace.");
85  this->addOperation("setAll", &ROSParamService::setParamsComponentPrivate, this)
86  .doc("Stores all properties of this component (and its sub-services) on the ROS param server from the similarly-named property in the component's private namespace. This is an alias for setAllComponentPrivate().");
87 
88  this->addOperation("get", &ROSParamService::get, this)
89  .doc("Gets one property of this component (or populates the properties of a named RTT sub-service) from the ROS param server based on the given resolution policy.")
90  .arg("name", "Name of the property / service / parameter.")
91  .arg("policy", "ROS parameter namespace resolution policy.");
92  this->addOperation("getParam", &ROSParamService::getParam, this)
93  .doc("Gets one property of this component (or populates the properties of a named RTT sub-service) from the ROS param server based on the given ROS parameter name.")
94  .arg("param_name", "Name of the ROS parameter. Use '~' and '/' leaders for private or absolute resolution.")
95  .arg("name", "Name of the RTT property or service.");
96 
97  this->addOperation("getRelative", &ROSParamService::getParamRelative, this)
98  .doc("Gets one property of this component (or populates the properties of a named RTT sub-service) from the ROS param server in the relative namespace.")
99  .arg("name", "Name of the property / service / parameter.");
100  this->addOperation("getAbsolute", &ROSParamService::getParamAbsolute, this)
101  .doc("Gets one property of this component (or populates the properties of a named RTT sub-service) from the ROS param server in the absolute namespace.")
102  .arg("name", "Name of the property / service / parameter.");
103  this->addOperation("getPrivate", &ROSParamService::getParamPrivate, this)
104  .doc("Gets one property of this component (or populates the properties of a named RTT sub-service) from the ROS param server in the node's private namespace.")
105  .arg("name", "Name of the property / service / parameter.");
106  this->addOperation("getComponentPrivate", &ROSParamService::getParamComponentPrivate, this)
107  .doc("Gets one property of this component (or populates the properties of a named RTT sub-service) from the ROS param server in the component's private namespace.")
108  .arg("name", "Name of the property / service / parameter.");
109  this->addOperation("getComponentRelative", &ROSParamService::getParamComponentRelative, this)
110  .doc("Gets one property of this component (or populates the properties of a named RTT sub-service) from the ROS param server in the component's relative namespace.")
111  .arg("name", "Name of the property / service / parameter.");
112  this->addOperation("getComponentAbsolute", &ROSParamService::getParamComponentAbsolute, this)
113  .doc("Gets one property of this component (or populates the properties of a named RTT sub-service) from the ROS param server in the component's absolute namespace.")
114  .arg("name", "Name of the property / service / parameter.");
115 
116  this->addOperation("set", &ROSParamService::set, this)
117  .doc("Sets one parameter on the ROS param server from the similarly-named property of this component (or stores the properties of a named RTT sub-service) in the ROS parameter namespace based on the given resolution policy.")
118  .arg("name", "Name of the property / service / parameter.")
119  .arg("policy", "ROS parameter namespace resolution policy.");
120  this->addOperation("setParam", &ROSParamService::setParam, this)
121  .doc("Sets one parameter on the ROS param server from the similarly-named property of this component (or stores the properties of a named RTT sub-service) in the ROS parameter namespace based on the given ROS parameter name.")
122  .arg("param_name", "Name of the ROS parameter. Use '~' and '/' leaders for private or absolute resolution.")
123  .arg("name", "Name of the RTT property or service.");
124 
125  this->addOperation("setRelative", &ROSParamService::setParamRelative, this)
126  .doc("Sets one parameter on the ROS param server from the similarly-named property of this component (or stores the properties of a named RTT sub-service) in the relative namespace.")
127  .arg("name", "Name of the property / service / parameter.");
128  this->addOperation("setAbsolute", &ROSParamService::setParamAbsolute, this)
129  .doc("Sets one parameter on the ROS param server from the similarly-named property of this component (or stores the properties of a named RTT sub-service) in the absolute namespace.")
130  .arg("name", "Name of the property / service / parameter.");
131  this->addOperation("setPrivate", &ROSParamService::setParamPrivate, this)
132  .doc("Sets one parameter on the ROS param server from the similarly-named property of this component (or stores the properties of a named RTT sub-service) in the node's private namespace.")
133  .arg("name", "Name of the property / service / parameter.");
134  this->addOperation("setComponentPrivate", &ROSParamService::setParamComponentPrivate, this)
135  .doc("Sets one parameter on the ROS param server from the similarly-named property of this component (or stores the properties of a named RTT sub-service) in the component's private namespace.")
136  .arg("name", "Name of the property / service / parameter.");
137  this->addOperation("setComponentRelative", &ROSParamService::setParamComponentRelative, this)
138  .doc("Sets one parameter on the ROS param server from the similarly-named property of this component (or stores the properties of a named RTT sub-service) in the component's relative namespace.")
139  .arg("name", "Name of the property / service / parameter.");
140  this->addOperation("setComponentAbsolute", &ROSParamService::setParamComponentAbsolute, this)
141  .doc("Sets one parameter on the ROS param server from the similarly-named property of this component (or stores the properties of a named RTT sub-service) in the component's absolute namespace.")
142  .arg("name", "Name of the property / service / parameter.");
143 
144  ADD_ROSPARAM_OPERATION(String, std::string, ParamImpl)
145  ADD_ROSPARAM_OPERATION(Double, double, ParamImpl)
146  ADD_ROSPARAM_OPERATION(Float, float, ParamImpl)
147  ADD_ROSPARAM_OPERATION(Int, int, ParamImpl)
148  ADD_ROSPARAM_OPERATION(Bool, bool, ParamImpl)
149 
150  // Vector parameters
151  ADD_ROSPARAM_OPERATION(VectorOfString, std::vector<std::string>, ParamImpl)
152  ADD_ROSPARAM_OPERATION(VectorOfDouble, std::vector<double>, ParamImpl)
153  ADD_ROSPARAM_OPERATION(VectorOfFloat, std::vector<float>, ParamImpl)
154  ADD_ROSPARAM_OPERATION(VectorOfInt, std::vector<int>, ParamImpl)
155  ADD_ROSPARAM_OPERATION(VectorOfBool, std::vector<bool>, ParamImpl)
156 
157  ADD_ROSPARAM_OPERATION(EigenVectorXd, double, EigenVectorParamImpl)
158  ADD_ROSPARAM_OPERATION(EigenVectorXf, float, EigenVectorParamImpl)
159 
160  }
161 
162 private:
163 
164  template <typename T, ResolutionPolicy P> bool getParamImpl(const std::string& ros_param_name, T& value)
165  {
166  if (!ros::param::get(resolvedName(ros_param_name,P), value)) {
167  RTT::log(RTT::Debug) << "ROS Parameter \"" << ros_param_name << "\" not found on the parameter server!" << RTT::endlog();
168  return false;
169  }
170  return true;
171  }
172 
173  template <typename T, ResolutionPolicy P> void setParamImpl(const std::string& ros_param_name, const T& value)
174  {
175  ros::param::set(resolvedName(ros_param_name,P), value);
176  }
177 
178  template <typename T, ResolutionPolicy P> bool getEigenVectorParamImpl(const std::string& ros_param_name, Eigen::Matrix<T,Eigen::Dynamic,1>& eigen_vector)
179  {
180  std::vector<T> value;
181  if (!getParamImpl< std::vector<T> , P >(ros_param_name,value)) {
182  return false;
183  }
184  eigen_vector = Eigen::Matrix<T,Eigen::Dynamic,1>::Map(value.data(),value.size());
185  return true;
186  }
187 
188  template <typename T, ResolutionPolicy P> void setEigenVectorParamImpl(const std::string& ros_param_name, const Eigen::Matrix<T,Eigen::Dynamic,1>& eigen_vector)
189  {
190  std::vector<T> value(eigen_vector.data(),eigen_vector.data() + eigen_vector.size() );
191  setParamImpl< std::vector<T> , P >(ros_param_name,value);
192  }
193 
195  const std::string resolvedName(
196  const std::string &param_name,
197  const ResolutionPolicy policy);
198 
199  bool getParams(RTT::Service::shared_ptr service, const std::string& ns);
200  bool getParams(const ResolutionPolicy policy);
201  bool getParamsRelative() { return getParams(RELATIVE); }
202  bool getParamsAbsolute() { return getParams(ABSOLUTE); }
203  bool getParamsPrivate() { return getParams(PRIVATE); }
204  bool getParamsComponentPrivate() { return getParams(COMPONENT_PRIVATE); }
205  bool getParamsComponentRelative() { return getParams(COMPONENT_RELATIVE); }
206  bool getParamsComponentAbsolute() { return getParams(COMPONENT_ABSOLUTE); }
207 
208  bool get(
209  const std::string &param_name,
210  const unsigned int policy = (unsigned int) COMPONENT_PRIVATE);
211  bool getParam(
212  const std::string &ros_name,
213  const std::string &rtt_name);
214  bool getParamRelative(const std::string &name) { return get(name, RELATIVE); }
215  bool getParamAbsolute(const std::string &name) { return get(name, ABSOLUTE); }
216  bool getParamPrivate(const std::string &name) { return get(name, PRIVATE); }
217  bool getParamComponentPrivate(const std::string &name) { return get(name, COMPONENT_PRIVATE); }
218  bool getParamComponentRelative(const std::string &name) { return get(name, COMPONENT_RELATIVE); }
219  bool getParamComponentAbsolute(const std::string &name) { return get(name, COMPONENT_ABSOLUTE); }
220 
221  bool setParams(RTT::Service::shared_ptr service, const std::string& ns);
222  bool setParams(const ResolutionPolicy policy);
223  bool setParamsRelative() { return setParams(RELATIVE); }
224  bool setParamsAbsolute() { return setParams(ABSOLUTE); }
225  bool setParamsPrivate() { return setParams(PRIVATE); }
226  bool setParamsComponentPrivate() { return setParams(COMPONENT_PRIVATE); }
227  bool setParamsComponentRelative() { return setParams(COMPONENT_RELATIVE); }
228  bool setParamsComponentAbsolute() { return setParams(COMPONENT_ABSOLUTE); }
229 
230  bool set(
231  const std::string &param_name,
232  const unsigned int policy = (unsigned int) COMPONENT_PRIVATE);
233  bool setParam(
234  const std::string &ros_name,
235  const std::string &rtt_name);
236  bool setParamRelative(const std::string &name) { return set(name, RELATIVE); }
237  bool setParamAbsolute(const std::string &name) { return set(name, ABSOLUTE); }
238  bool setParamPrivate(const std::string &name) { return set(name, PRIVATE); }
239  bool setParamComponentPrivate(const std::string &name) { return set(name, COMPONENT_PRIVATE); }
240  bool setParamComponentRelative(const std::string &name) { return set(name, COMPONENT_RELATIVE); }
241  bool setParamComponentAbsolute(const std::string &name) { return set(name, COMPONENT_ABSOLUTE); }
242 };
243 
245  const std::string &param_name,
246  const ResolutionPolicy policy)
247 {
248  std::string leader = "";
249  std::string resolved_name = "";
250 
251  if(param_name.length() > 0) {
252  leader = param_name[0];
253  }
254 
255  switch(policy) {
256  case RELATIVE:
257  resolved_name = param_name;
258  break;
259  case ABSOLUTE:
260  resolved_name = (leader == "/") ? param_name : std::string("/") + param_name;
261  break;
262  case PRIVATE:
263  resolved_name = (leader == "~") ? param_name : std::string("~") + param_name;
264  break;
265  case COMPONENT_PRIVATE:
266  resolved_name = std::string("~") + ros::names::append(this->getOwner()->getName(),param_name);
267  break;
268  case COMPONENT_RELATIVE:
269  resolved_name = ros::names::append(this->getOwner()->getName(),param_name);
270  break;
271  case COMPONENT_ABSOLUTE:
272  resolved_name = std::string("/") +ros::names::append(this->getOwner()->getName(),param_name);
273  break;
274  };
275 
276  RTT::log(RTT::Debug) << "["<<this->getOwner()->getName()<<"] Resolving ROS param \""<<param_name<<"\" to \""<<resolved_name<<"\"" << RTT::endlog();
277 
278  return resolved_name;
279 }
280 
281 
282 
284 template<class T>
285 bool castable(const RTT::base::PropertyBase *prop);
287 template<class T>
290 template<>
291 XmlRpc::XmlRpcValue rttPropertyToXmlParam<RTT::PropertyBag>(const RTT::PropertyBag &bag);
293 template<class T>
294 XmlRpc::XmlRpcValue rttPropertyToXmlParam(const std::vector<T> &vec);
297 
298 template<class T>
300 {
301  return dynamic_cast<const Property<T>*>(prop);
302 }
303 
304 template<class T>
306 {
307  return XmlRpc::XmlRpcValue(prop);
308 }
309 
310 template<>
312 {
313  return XmlRpc::XmlRpcValue(static_cast<double>(prop));
314 }
315 
316 template<>
318 {
319  return XmlRpc::XmlRpcValue(static_cast<int>(prop));
320 }
321 
322 template<>
324 {
325  return XmlRpc::XmlRpcValue(static_cast<int>(prop));
326 }
327 
328 template<>
330 {
331  return XmlRpc::XmlRpcValue(static_cast<int>(prop));
332 }
333 
334 template<>
335 XmlRpc::XmlRpcValue rttPropertyToXmlParam<RTT::PropertyBag>(const RTT::PropertyBag &bag)
336 {
337  // Make the xml value a struct
338  XmlRpc::XmlRpcValue xml_struct;
339  const XmlRpc::XmlRpcValue::ValueStruct &xml_map = (const XmlRpc::XmlRpcValue::ValueStruct &)(xml_struct);
340 
341  // Get the properties
342  const RTT::PropertyBag::Properties &properties = bag.getProperties();
343 
344  for(RTT::PropertyBag::Properties::const_iterator it = properties.begin();
345  it != properties.end();
346  ++it)
347  {
348  xml_struct[(*it)->getName()] = rttPropertyBaseToXmlParam(*it);
349  }
350 
351  return xml_struct;
352 }
353 
354 template<class T>
355 XmlRpc::XmlRpcValue rttPropertyToXmlParam(const std::vector<T> &vec)
356 {
357  XmlRpc::XmlRpcValue xml_array;
358  xml_array.setSize(vec.size());
359 
360  for(unsigned i=0; i<vec.size(); i++) {
361  xml_array[i] = rttPropertyToXmlParam<T>(vec.at(i));
362  }
363 
364  return xml_array;
365 }
366 
367 template<>
368 XmlRpc::XmlRpcValue rttPropertyToXmlParam(const Eigen::VectorXd &vec)
369 {
370  XmlRpc::XmlRpcValue xml_array;
371  xml_array.setSize(vec.size());
372 
373  for(unsigned i=0; i<vec.size(); i++) {
374  xml_array[i] = rttPropertyToXmlParam<double>(vec(i));
375  }
376 
377  return xml_array;
378 }
379 
380 template<>
381 XmlRpc::XmlRpcValue rttPropertyToXmlParam(const Eigen::VectorXf &vec)
382 {
383  XmlRpc::XmlRpcValue xml_array;
384  xml_array.setSize(vec.size());
385 
386  for(unsigned i=0; i<vec.size(); i++) {
387  xml_array[i] = rttPropertyToXmlParam<double>(vec(i));
388  }
389 
390  return xml_array;
391 }
392 
393 
394 // These just save typing
395 #define RETURN_RTT_PROPERTY_TO_XML_PARAM(type,prop)\
396  if(castable< type >(prop)) { return rttPropertyToXmlParam< type >(static_cast<const RTT::Property< type >*>(prop)->rvalue()); }
397 
398 #define RETURN_RTT_PROPERTY_CONTAINER_TO_XML_PARAM(type,elem_type,prop)\
399  if(castable< type >(prop)) { return rttPropertyToXmlParam< elem_type >(static_cast<const RTT::Property< type >*>(prop)->rvalue()); }
400 
402 {
403  // Primitive parameters
404  RETURN_RTT_PROPERTY_TO_XML_PARAM(std::string, prop);
405  RETURN_RTT_PROPERTY_TO_XML_PARAM(double, prop);
408  RETURN_RTT_PROPERTY_TO_XML_PARAM(unsigned int, prop);
410  RETURN_RTT_PROPERTY_TO_XML_PARAM(unsigned char, prop);
412 
413  // Vector parameters
414  RETURN_RTT_PROPERTY_CONTAINER_TO_XML_PARAM(std::vector<std::string>, std::string, prop);
415  RETURN_RTT_PROPERTY_CONTAINER_TO_XML_PARAM(std::vector<double>, double, prop);
416  RETURN_RTT_PROPERTY_CONTAINER_TO_XML_PARAM(std::vector<float>, float, prop);
417  RETURN_RTT_PROPERTY_CONTAINER_TO_XML_PARAM(std::vector<int>, int, prop);
418  RETURN_RTT_PROPERTY_CONTAINER_TO_XML_PARAM(std::vector<unsigned int>, unsigned int, prop);
419  RETURN_RTT_PROPERTY_CONTAINER_TO_XML_PARAM(std::vector<char>, char, prop);
420  RETURN_RTT_PROPERTY_CONTAINER_TO_XML_PARAM(std::vector<unsigned char>, unsigned char, prop);
421  RETURN_RTT_PROPERTY_CONTAINER_TO_XML_PARAM(std::vector<bool>, bool, prop);
422 
423  RETURN_RTT_PROPERTY_TO_XML_PARAM(Eigen::VectorXd, prop);
424  RETURN_RTT_PROPERTY_TO_XML_PARAM(Eigen::VectorXf, prop);
425 
426  // Struct parameters
428 
429  // Try to decompose property into a property bag
430  RTT::PropertyBag bag;
431  if (RTT::types::propertyDecomposition(prop, bag)) {
432  return rttPropertyToXmlParam(bag);
433  }
434 
435  return XmlRpc::XmlRpcValue();
436 }
437 
438 
440  const std::string &param_name,
441  const unsigned int policy)
442 {
443  RTT::Logger::In in("ROSParamService::set");
444 
445  const std::string resolved_name = resolvedName(param_name,ResolutionPolicy(policy));
446 
447  return this->setParam(resolved_name, param_name);
448 }
449 
451  const std::string &ros_name,
452  const std::string &rtt_name)
453 {
454  RTT::Logger::In in("ROSParamService::setParam");
455 
456  XmlRpc::XmlRpcValue xml_value;
457 
458  // Try to find a property named rtt_name
459  RTT::base::PropertyBase *property = this->getOwner()->getProperty(rtt_name);
460  if (property) {
461  xml_value = rttPropertyBaseToXmlParam(this->getOwner()->getProperty(rtt_name));
462  ros::param::set(ros_name, xml_value);
463  return true;
464  }
465 
466  // Try to find a sub-service named rtt_name
467  RTT::Service::shared_ptr service = this->getOwner()->provides()->getService(rtt_name);
468  if (service) {
469  // Set all parameters of the sub-service
470  return setParams(service, ros_name);
471  }
472 
473  RTT::log(RTT::Debug) << "RTT component does not have a property or service named \"" << rtt_name << "\"" << RTT::endlog();
474  return false;
475 }
476 
478 {
479  return setParams(this->getOwner()->provides(), resolvedName(std::string(), policy));
480 }
481 
483  RTT::Service::shared_ptr service,
484  const std::string& ns) {
485  XmlRpc::XmlRpcValue xml_value;
486  xml_value = rttPropertyToXmlParam(*(service->properties()));
487  ros::param::set(ns, xml_value);
488 
489  // Recurse into sub-services
490  RTT::Service::ProviderNames names = service->getProviderNames();
491  for (RTT::Service::ProviderNames::const_iterator it = names.begin(); it != names.end(); ++it)
492  {
493  RTT::Service::shared_ptr sub(service->getService(*it));
494  if (sub) {
495  if (!setParams(sub, ros::names::append(ns,sub->getName()))) return false;
496  }
497  }
498 
499  return true;
500 }
501 
502 // Declarations
504 template <class T>
505 bool xmlParamToProp(const XmlRpc::XmlRpcValue &xml_value, RTT::Property<T>* prop);
507 template <class T>
508 bool xmlParamToProp(const XmlRpc::XmlRpcValue &xml_value, RTT::Property<std::vector<T> >* prop);
510 template <>
512 template <>
515 template <>
516 bool xmlParamToProp<RTT::PropertyBag>(const XmlRpc::XmlRpcValue &xml_value, RTT::Property<RTT::PropertyBag>* prop);
518 bool xmlParamToProp( const XmlRpc::XmlRpcValue &xml_value, RTT::base::PropertyBase* prop_base);
519 
520 template <class XMLRPCType,class PropertyType, class enabled=void> struct XmlParamToValue{
521  static bool assign(const XMLRPCType& xml_value, PropertyType &prop_value)
522  {
523  return false;
524  }
525 };
526 
527 template <class XMLRPCType, class PropertyType >
528 struct XmlParamToValue <XMLRPCType, PropertyType, typename boost::enable_if<boost::is_convertible<XMLRPCType,PropertyType> >::type >{
529  static bool assign(const XMLRPCType& xml_value, PropertyType &prop_value)
530  {
531  prop_value = xml_value;
532  return true;
533  }
534 };
535 
537 template <class PropertyType> bool xmlParamToValue(const XmlRpc::XmlRpcValue &xml_value, PropertyType &value) {
538  switch(xml_value.getType()) {
540  return XmlParamToValue<std::string, PropertyType>::assign(static_cast<const std::string&>(const_cast<XmlRpc::XmlRpcValue &>(xml_value)),value);
542  return XmlParamToValue<double, PropertyType>::assign(static_cast<double>(const_cast<XmlRpc::XmlRpcValue &>(xml_value)),value);
544  return XmlParamToValue<int, PropertyType>::assign(static_cast<int>(const_cast<XmlRpc::XmlRpcValue &>(xml_value)),value);
546  return XmlParamToValue<bool, PropertyType>::assign(static_cast<bool>(const_cast<XmlRpc::XmlRpcValue &>(xml_value)),value);
547  }
548  return false;
549 }
550 
551 template <class T>
553  const XmlRpc::XmlRpcValue &xml_value,
554  RTT::Property<T>* prop)
555 {
556  // Check if the property value is the requested type T
557  if(!prop) {
558  return false;
559  }
560 
561  // Set the value
562  return xmlParamToValue(xml_value, prop->set());
563 
564 }
565 
566 template <class T>
568  const XmlRpc::XmlRpcValue &xml_value,
569  RTT::Property<std::vector<T> >* prop)
570 {
571  // Check if the property value is the requested type T
572  if(!prop) {
573  return false;
574  }
575 
576  // Make sure it's an array
577  if(xml_value.getType() != XmlRpc::XmlRpcValue::TypeArray) {
578  return false;
579  }
580 
581  // Copy the data into the vector property
582  std::vector<T> &vec = prop->value();
583  vec.resize(xml_value.size());
584  bool result = true;
585  for(size_t i=0; i<vec.size(); i++) {
586  result &= xmlParamToValue(xml_value[i], vec[i]);
587  }
588 
589  return result;
590 }
591 
592 template <>
594  const XmlRpc::XmlRpcValue &xml_value,
596 {
597  // Check if the property value is the requested type T
598  if(!prop) {
599  return false;
600  }
601 
602  // Make sure it's an array
603  if(xml_value.getType() != XmlRpc::XmlRpcValue::TypeArray) {
604  return false;
605  }
606 
607  // Copy the data into the vector property
608  std::vector<bool> &vec = prop->value();
609  vec.resize(xml_value.size());
610  bool result = true;
611  for(size_t i=0; i<vec.size(); i++) {
612  bool temp;
613  result &= xmlParamToValue(xml_value[i], temp);
614  vec[i] = temp;
615  }
616 
617  return result;
618 }
619 
620 template <>
622  const XmlRpc::XmlRpcValue &xml_value,
624 {
625  // Check if the property value is the requested type T
626  if(!prop) {
627  return false;
628  }
629 
630  // Make sure it's an array
631  if(xml_value.getType() != XmlRpc::XmlRpcValue::TypeArray) {
632  return false;
633  }
634 
635  // Copy the data into the vector property
636  Eigen::VectorXd &vec = prop->value();
637  vec.resize(xml_value.size());
638  bool result = true;
639  for(size_t i=0; i<vec.size(); i++) {
640  double temp;
641  result &= xmlParamToValue(xml_value[i], temp);
642  vec[i] = temp;
643  }
644 
645  return result;
646 }
647 
648 template <>
650  const XmlRpc::XmlRpcValue &xml_value,
652 {
653  // Check if the property value is the requested type T
654  if(!prop) {
655  return false;
656  }
657 
658  // Make sure it's an array
659  if(xml_value.getType() != XmlRpc::XmlRpcValue::TypeArray) {
660  return false;
661  }
662 
663  // Copy the data into the vector property
664  Eigen::VectorXf &vec = prop->value();
665  vec.resize(xml_value.size());
666  bool result = true;
667  for(size_t i=0; i<vec.size(); i++) {
668  double temp;
669  result &= xmlParamToValue(xml_value[i], temp);
670  vec[i] = temp;
671  }
672 
673  return result;
674 }
675 
676 template <>
677 bool xmlParamToProp<RTT::PropertyBag>(
678  const XmlRpc::XmlRpcValue &xml_value,
680 {
681  // Check if the property value is the requested type T
682  if(!prop) {
683  return false;
684  }
685 
686  // Make sure it's a struct
687  if(xml_value.getType() != XmlRpc::XmlRpcValue::TypeStruct) {
688  return false;
689  }
690 
691  // Copy the properties
692  bool success = true;
693  // We need to copy the struct because XmlRpc++ doesn't have const operations for this
694  XmlRpc::XmlRpcValue xml_value_struct(xml_value);
695  for(XmlRpc::XmlRpcValue::ValueStruct::iterator it = xml_value_struct.begin();
696  it != xml_value_struct.end();
697  ++it)
698  {
699  RTT::base::PropertyBase *sub_prop_base = prop->value().getProperty(it->first);
700  if (sub_prop_base) {
701  success &= xmlParamToProp(it->second, sub_prop_base);
702  } else {
703  // create property based on XmlRpc type?
704  }
705  }
706 
707  return success;
708 }
709 
711  const XmlRpc::XmlRpcValue &xml_value,
712  RTT::base::PropertyBase* prop_base)
713 {
714  // Switch based on the type of XmlRpcValue
715  switch(xml_value.getType()) {
717  return
718  xmlParamToProp(xml_value, dynamic_cast<RTT::Property<std::string>*>(prop_base));
719 
721  return
722  xmlParamToProp(xml_value, dynamic_cast<RTT::Property<double>*>(prop_base)) ||
723  xmlParamToProp(xml_value, dynamic_cast<RTT::Property<float>*>(prop_base));
724 
726  return
727  xmlParamToProp(xml_value, dynamic_cast<RTT::Property<double>*>(prop_base)) ||
728  xmlParamToProp(xml_value, dynamic_cast<RTT::Property<float>*>(prop_base)) ||
729  xmlParamToProp(xml_value, dynamic_cast<RTT::Property<int>*>(prop_base)) ||
730  xmlParamToProp(xml_value, dynamic_cast<RTT::Property<unsigned int>*>(prop_base)) ||
731  xmlParamToProp(xml_value, dynamic_cast<RTT::Property<char>*>(prop_base)) ||
732  xmlParamToProp(xml_value, dynamic_cast<RTT::Property<unsigned char>*>(prop_base));
733 
735  return
736  xmlParamToProp(xml_value, dynamic_cast<RTT::Property<bool>*>(prop_base));
737 
739  if (
740  xmlParamToProp(xml_value, dynamic_cast<RTT::Property<std::vector<std::string> >*>(prop_base)) ||
741  xmlParamToProp(xml_value, dynamic_cast<RTT::Property<std::vector<double> >*>(prop_base)) ||
742  xmlParamToProp(xml_value, dynamic_cast<RTT::Property<std::vector<float> >*>(prop_base)) ||
743  xmlParamToProp(xml_value, dynamic_cast<RTT::Property<std::vector<int> >*>(prop_base)) ||
744  xmlParamToProp(xml_value, dynamic_cast<RTT::Property<std::vector<unsigned int> >*>(prop_base)) ||
745  xmlParamToProp(xml_value, dynamic_cast<RTT::Property<std::vector<char> >*>(prop_base)) ||
746  xmlParamToProp(xml_value, dynamic_cast<RTT::Property<std::vector<unsigned char> >*>(prop_base)) ||
747  xmlParamToProp(xml_value, dynamic_cast<RTT::Property<std::vector<bool> >*>(prop_base)) ||
748  xmlParamToProp(xml_value, dynamic_cast<RTT::Property<Eigen::VectorXd >*>(prop_base)) ||
749  xmlParamToProp(xml_value, dynamic_cast<RTT::Property<Eigen::VectorXf >*>(prop_base)) )
750  {
751  // Return true if it gets parsed into an array structure
752  return true;
753  }
754  break;
755 
757  if (
758  xmlParamToProp(xml_value, dynamic_cast<RTT::Property<RTT::PropertyBag>*>(prop_base)) )
759  {
760  // Return true if it gets parsed into a PropertyBag structure
761  return true;
762  }
763  break;
764  };
765 
766  // try property bag decomposition
767  {
768  RTT::Property<RTT::PropertyBag> bag(prop_base->getName());
769  if (RTT::types::propertyDecomposition(prop_base, bag.set()) && xmlParamToProp(xml_value, &bag)) {
770  return true;
771  } else {
772  RTT::log(RTT::Debug) << "Could not decompose property bag for property type \"" << prop_base->getName() << "\"" << RTT::endlog();
773  return false;
774  }
775  }
776 
777  RTT::log(RTT::Debug) << "No appropriate conversion for property \"" << prop_base->getName() << "\"" << RTT::endlog();
778  return false;
779 }
780 
782  const std::string &param_name,
783  const unsigned int policy)
784 {
785  RTT::Logger::In in("ROSParamService::get");
786 
787  const std::string resolved_name = resolvedName(
788  param_name,ResolutionPolicy(policy));
789 
790  return this->getParam(resolved_name, param_name);
791 }
792 
794  const std::string &ros_name,
795  const std::string &rtt_name)
796 {
797  RTT::Logger::In in("ROSParamService::getParam");
798 
799  try {
800  // Get the parameter
801  XmlRpc::XmlRpcValue xml_value;
802 
803  if(!ros::param::get(ros_name, xml_value)) {
804  RTT::log(RTT::Debug) << "ROS Parameter \"" << ros_name << "\" not found on the parameter server!" << RTT::endlog();
805  return false;
806  }
807 
808  // Try to get the property if it exists
809  RTT::base::PropertyBase *prop_base = this->getOwner()->getProperty(rtt_name);
810  if(prop_base) {
811  // Deal with the xml value
812  bool ret = xmlParamToProp(xml_value, prop_base);
813  if(!ret) {
814  RTT::log(RTT::Warning) << "Could not convert \"" << ros_name << "\" from an XMLRPC value to an RTT property." << RTT::endlog();
815  }
816  return ret;
817  }
818 
819  // Try to get the properties of a sub-service if it exists
820  RTT::Service::shared_ptr service = this->getOwner()->provides()->getService(rtt_name);
821  if(service) {
822  // Get all parameters of the sub-service
823  return getParams(service, ros_name);
824  }
825 
826  RTT::log(RTT::Debug) << "RTT component does not have a property or service named \"" << rtt_name << "\"" << RTT::endlog();
827 
828  } catch(XmlRpc::XmlRpcException &err) {
829  RTT::log(RTT::Error) << "XmlRpcException when getting ROS parameter \""<<ros_name<<"\": " << err.getMessage() << RTT::endlog();
830  RTT::log(RTT::Debug) << " -- Make sure your parameters are the right primitive type." << RTT::endlog();
831  }
832 
833  return false;
834 }
835 
836 
838 {
839  return getParams(this->getOwner()->provides(), resolvedName(std::string(), policy));
840 }
841 
843  RTT::Service::shared_ptr service,
844  const std::string& ns)
845 {
846  RTT::Logger::In in("ROSParamService::getParams");
847 
848  // Get the parameter
849  XmlRpc::XmlRpcValue xml_value;
850 
851  if(!ros::param::get(ns, xml_value)) {
852  RTT::log(RTT::Debug) << "ROS Parameter namespace \"" << ns << "\" not found on the parameter server!" << RTT::endlog();
853  return false;
854  }
855 
856  // Create a Property<> wrapper around the propertybag
857  RTT::PropertyBag *properties = service->properties();
859  RTT::Property<RTT::PropertyBag> prop(this->getOwner()->getName(),"",datasource);
860 
861  // Deal with the xml value
862  bool ret = xmlParamToProp(xml_value, &prop);
863  if (!ret) {
864  RTT::log(RTT::Warning) << "Could not convert \"" << ns << "\" from an XMLRPC value to an RTT property." << RTT::endlog();
865  return false;
866  }
867 
868  // Recurse into sub-services
869  RTT::Service::ProviderNames names = service->getProviderNames();
870  for (RTT::Service::ProviderNames::const_iterator it = names.begin(); it != names.end(); ++it)
871  {
872  RTT::Service::shared_ptr sub(service->getService(*it));
873  if (sub) {
874  std::string sub_ns = sub->getName();
875  if (!ns.empty()) sub_ns = ros::names::append(ns,sub_ns);
876  getParams(sub, sub_ns);
877  }
878  }
879 
880  return true;
881 }
882 
static bool assign(const XMLRPCType &xml_value, PropertyType &prop_value)
const std::string & getMessage() const
ROSParamService(TaskContext *owner)
void setEigenVectorParamImpl(const std::string &ros_param_name, const Eigen::Matrix< T, Eigen::Dynamic, 1 > &eigen_vector)
bool getParamAbsolute(const std::string &name)
XmlRpc::XmlRpcValue rttPropertyToXmlParam< char >(const char &prop)
bool propertyDecomposition(base::PropertyBase *source, PropertyBag &targetbag, bool recurse)
int size() const
Component resolution: "name" -> "COMPONENT_NAME/name".
Definition: rosparam.h:76
std::string getName(void *handle)
#define ORO_SERVICE_NAMED_PLUGIN(SERVICE, NAME)
XmlRpc::XmlRpcValue rttPropertyToXmlParam(const T &prop)
Convert a value to an XmlRpc value.
bool getParamComponentPrivate(const std::string &name)
bool xmlParamToProp< bool >(const XmlRpc::XmlRpcValue &xml_value, RTT::Property< std::vector< bool > > *prop)
std::vector< std::string > ProviderNames
Type const & getType() const
const std::string resolvedName(const std::string &param_name, const ResolutionPolicy policy)
Resolve a parameter name based on the given ResolutionPolicy.
Absolute resolution: "name" -> "/name".
Definition: rosparam.h:73
reference_t value()
bool set(const std::string &param_name, const unsigned int policy=(unsigned int) COMPONENT_PRIVATE)
ROSCPP_DECL void set(const std::string &key, const XmlRpc::XmlRpcValue &v)
bool getParamPrivate(const std::string &name)
std::map< std::string, XmlRpcValue > ValueStruct
bool setParams(RTT::Service::shared_ptr service, const std::string &ns)
XmlRpc::XmlRpcValue rttPropertyToXmlParam< unsigned int >(const unsigned int &prop)
void setSize(int size)
bool setParamPrivate(const std::string &name)
ROSCPP_DECL bool get(const std::string &key, std::string &s)
#define RETURN_RTT_PROPERTY_CONTAINER_TO_XML_PARAM(type, elem_type, prop)
bool getParamImpl(const std::string &ros_param_name, T &value)
bool castable(const RTT::base::PropertyBase *prop)
Determine if the RTT property can be casted into an RTT::Property<T>
XmlRpc::XmlRpcValue rttPropertyBaseToXmlParam(RTT::base::PropertyBase *prop)
Convert an abstract RTT PropertyBase to an XmlRpc value.
bool setParamComponentPrivate(const std::string &name)
bool getParamComponentAbsolute(const std::string &name)
bool get(const std::string &param_name, const unsigned int policy=(unsigned int) COMPONENT_PRIVATE)
bool xmlParamToValue(const XmlRpc::XmlRpcValue &xml_value, PropertyType &value)
Convert an XmlRpc value to type T.
bool xmlParamToProp(const XmlRpc::XmlRpcValue &xml_value, RTT::Property< T > *prop)
Convert an XmlRpc value into an RTT property.
boost::intrusive_ptr< AssignableDataSource< T > > shared_ptr
bool getParamRelative(const std::string &name)
bool setParamAbsolute(const std::string &name)
bool getParam(const std::string &ros_name, const std::string &rtt_name)
Relative resolution: "name" -> "name".
Definition: rosparam.h:72
XmlRpc::XmlRpcValue rttPropertyToXmlParam< float >(const float &prop)
bool setParam(const std::string &ros_name, const std::string &rtt_name)
Private resolution: "name" -> "~name".
Definition: rosparam.h:74
bool setParamRelative(const std::string &name)
bool getEigenVectorParamImpl(const std::string &ros_param_name, Eigen::Matrix< T, Eigen::Dynamic, 1 > &eigen_vector)
const std::string & getName() const
XmlRpc::XmlRpcValue rttPropertyToXmlParam< unsigned char >(const unsigned char &prop)
bool getParamComponentRelative(const std::string &name)
ROSCPP_DECL std::string append(const std::string &left, const std::string &right)
#define RETURN_RTT_PROPERTY_TO_XML_PARAM(type, prop)
reference_t set()
bool setParamComponentAbsolute(const std::string &name)
bool getParams(RTT::Service::shared_ptr service, const std::string &ns)
static Logger & log()
#define ADD_ROSPARAM_OPERATION(return_type_str, return_type, func)
static Logger::LogFunction endlog()
Component resolution: "name" -> "~COMPONENT_NAME/name".
Definition: rosparam.h:75
bool setParamComponentRelative(const std::string &name)
std::vector< base::PropertyBase * > Properties
void setParamImpl(const std::string &ros_param_name, const T &value)
Component resolution: "name" -> "/COMPONENT_NAME/name".
Definition: rosparam.h:77


rtt_rosparam
Author(s): Ruben Smits
autogenerated on Sat Jun 8 2019 18:05:28