libmove3d-planners
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros Groups Pages
param_server.hpp
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2010, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
38 #ifndef POLICY_IMPROVEMENT_UTILITIES_PARAM_SERVER_H_
39 #define POLICY_IMPROVEMENT_UTILITIES_PARAM_SERVER_H_
40 
41 #include <string>
42 #include <vector>
43 #include <sstream>
44 
45 //#include <ros/node_handle.h>
46 //#include <ros/assert.h>
47 
48 #include <Eigen/Core>
49 
50 namespace stomp_motion_planner
51 {
52 
53 inline bool readDoubleArray(ros::NodeHandle& node_handle, const std::string& parameter_name, std::vector<double>& array)
54 {
55  XmlRpc::XmlRpcValue list;
56  node_handle.getParam(parameter_name, list);
57 
58  if (list.getType() != XmlRpc::XmlRpcValue::TypeArray)
59  {
60  ROS_ERROR("Parameter %s/%s needs to be an *array* of doubles", node_handle.getNamespace().c_str(), parameter_name.c_str());
61  return false;
62  }
63 
64  array.clear();
65  for (int32_t i = 0; i < list.size(); ++i)
66  {
67  if (list[i].getType() != XmlRpc::XmlRpcValue::TypeDouble &&
68  list[i].getType() != XmlRpc::XmlRpcValue::TypeInt)
69  {
70  ROS_ERROR("Parameter %s/%s needs to be an array of *doubles*", node_handle.getNamespace().c_str(), parameter_name.c_str());
71  return false;
72  }
73  double value=0.0;
74  if (list[i].getType() == XmlRpc::XmlRpcValue::TypeInt)
75  value = static_cast<double>(static_cast<int>(list[i]));
76  else value = static_cast<double>(list[i]);
77  array.push_back(value);
78  }
79  return true;
80 }
81 
82 inline bool readEigenVector(ros::NodeHandle& node_handle, const std::string& parameter_name, Eigen::VectorXd& vector)
83 {
84  std::vector<double> array;
85  if (!readDoubleArray(node_handle, parameter_name, array))
86  {
87  return false;
88  }
89 
90  vector = Eigen::VectorXd::Zero(array.size());
91  for (int i=0; i<int(array.size()); ++i)
92  {
93  vector(i) = array[i];
94  }
95 
96  return true;
97 }
98 
99 // TODO: make this a templated function
100 inline bool read(ros::NodeHandle& node_handle, const std::string& parameter_name, std::string& parameter_value)
101 {
102  if (!node_handle.getParam(parameter_name, parameter_value))
103  {
104  ROS_ERROR("Parameter %s/%s not found!", node_handle.getNamespace().c_str(), parameter_name.c_str());
105  return false;
106  }
107  return true;
108 }
109 inline bool read(ros::NodeHandle& node_handle, const std::string& parameter_name, double& parameter_value)
110 {
111  if (!node_handle.getParam(parameter_name, parameter_value))
112  {
113  ROS_ERROR("Parameter %s/%s not found!", node_handle.getNamespace().c_str(), parameter_name.c_str());
114  return false;
115  }
116  return true;
117 }
118 inline bool read(ros::NodeHandle& node_handle, const std::string& parameter_name, int& parameter_value)
119 {
120  if (!node_handle.getParam(parameter_name, parameter_value))
121  {
122  ROS_ERROR("Parameter %s/%s not found!", node_handle.getNamespace().c_str(), parameter_name.c_str());
123  return false;
124  }
125  return true;
126 }
127 inline bool read(ros::NodeHandle& node_handle, const std::string& parameter_name, bool& parameter_value)
128 {
129  if (!node_handle.getParam(parameter_name, parameter_value))
130  {
131  ROS_ERROR("Parameter %s/%s not found!", node_handle.getNamespace().c_str(), parameter_name.c_str());
132  return false;
133  }
134  return true;
135 }
136 
137 inline bool readStringArraySpaceSeparated(ros::NodeHandle& node_handle, const std::string& parameter_name, std::vector<std::string>& array)
138 {
139  std::string str_array;
140 
141  if (!node_handle.getParam(parameter_name, str_array))
142  {
143  ROS_ERROR("Parameter %s/%s not found!", node_handle.getNamespace().c_str(), parameter_name.c_str());
144  return false;
145  }
146 
147  array.clear();
148  std::stringstream str_stream(str_array);
149  std::string item;
150  while (str_stream >> item)
151  {
152  array.push_back(item);
153  }
154  return true;
155 }
156 
157 inline void appendTrailingSlash(std::string& directory_name)
158 {
159  if (directory_name.compare(directory_name.size() - 1, 1, "/") != 0) // the directory name ends NOT with a slash
160  {
161  directory_name.append("/");
162  }
163 }
164 inline void removeLeadingSlash(std::string& topic_name)
165 {
166  if (topic_name.compare(0, 1, "/") == 0) // the topic name starts with a slash
167  {
168  topic_name.assign(topic_name.substr(1, topic_name.length()));
169  }
170 }
171 
172 inline std::string getString(const int number)
173 {
174  std::stringstream ss;
175  ss << number;
176  return ss.str();
177 }
178 
179 } // namespace policy_improvement_utilities
180 #endif /* POLICY_IMPROVEMENT_UTILITIES_PARAM_SERVER_H_ */