filter_base.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef FILTERS_FILTER_BASE_HPP_
31 #define FILTERS_FILTER_BASE_HPP_
32 
33 #include <typeinfo>
34 #include "ros/assert.h"
35 #include "ros/console.h"
36 #include "ros/ros.h"
37 
38 namespace filters
39 {
40 
41 typedef std::map<std::string, XmlRpc::XmlRpcValue> string_map_t;
42 
46 template<typename T>
48 {
49 public:
52  FilterBase():configured_(false){};
53 
56  virtual ~FilterBase(){};
57 
62  bool configure(const std::string& param_name, ros::NodeHandle node_handle = ros::NodeHandle())
63  {
64  XmlRpc::XmlRpcValue config;
65  if (!node_handle.getParam(param_name, config))
66  {
67  ROS_ERROR("Could not find parameter %s on the server, are you sure that it was pushed up correctly?", param_name.c_str());
68  return false;
69  }
70  return configure(config);
71 
72  }
73 
78  {
79  if (configured_)
80  {
81  ROS_WARN("Filter %s of type %s already being reconfigured", filter_name_.c_str(), filter_type_.c_str());
82  };
83  configured_ = false;
84  bool retval = true;
85 
86  retval = retval && loadConfiguration(config);
87  retval = retval && configure();
88  configured_ = retval;
89  return retval;
90  }
91 
97  virtual bool update(const T& data_in, T& data_out)=0;
98 
100  std::string getType() {return filter_type_;};
101 
103  inline const std::string& getName() const {return filter_name_;};
104 
105 
106 protected:
107 
111  virtual bool configure()=0;
112 
113 
118  bool getParam(const std::string& name, std::string& value) const
119  {
120  string_map_t::const_iterator it = params_.find(name);
121  if (it == params_.end())
122  {
123  return false;
124  }
125 
126  if(it->second.getType() != XmlRpc::XmlRpcValue::TypeString)
127  {
128  return false;
129  }
130 
131  auto tmp = it->second;
132  value = std::string(tmp);
133  return true;
134  }
135 
140  bool getParam(const std::string& name, bool& value) const
141  {
142  string_map_t::const_iterator it = params_.find(name);
143  if (it == params_.end())
144  {
145  return false;
146  }
147 
148  if(it->second.getType() != XmlRpc::XmlRpcValue::TypeBoolean)
149  {
150  return false;
151  }
152 
153  auto tmp = it->second;
154  value = (bool)(tmp);
155  return true;
156  }
157 
162  bool getParam(const std::string&name, double& value) const
163  {
164  string_map_t::const_iterator it = params_.find(name);
165  if (it == params_.end())
166  {
167  return false;
168  }
169 
170  if(it->second.getType() != XmlRpc::XmlRpcValue::TypeDouble && it->second.getType() != XmlRpc::XmlRpcValue::TypeInt)
171  {
172  return false;
173  }
174 
175  auto tmp = it->second;
176  value = it->second.getType() == XmlRpc::XmlRpcValue::TypeInt ? (int)(tmp) : (double)(tmp);
177  return true;
178  }
179 
184  bool getParam(const std::string&name, int& value) const
185  {
186  string_map_t::const_iterator it = params_.find(name);
187  if (it == params_.end())
188  {
189  return false;
190  }
191 
192  if(it->second.getType() != XmlRpc::XmlRpcValue::TypeInt)
193  {
194  return false;
195  }
196 
197  auto tmp = it->second;
198  value = tmp;
199  return true;
200  }
201 
206  bool getParam(const std::string&name, unsigned int& value) const
207  {
208  int signed_value;
209  if (!getParam(name, signed_value))
210  return false;
211  if (signed_value < 0)
212  return false;
213  value = signed_value;
214  return true;
215  };
216 
221  bool getParam(const std::string&name, std::vector<double>& value) const
222  {
223  string_map_t::const_iterator it = params_.find(name);
224  if (it == params_.end())
225  {
226  return false;
227  }
228 
229  value.clear();
230 
231  if(it->second.getType() != XmlRpc::XmlRpcValue::TypeArray)
232  {
233  return false;
234  }
235 
236  XmlRpc::XmlRpcValue double_array = it->second;
237 
238  for (int i = 0; i < double_array.size(); ++i){
239  if(double_array[i].getType() != XmlRpc::XmlRpcValue::TypeDouble && double_array[i].getType() != XmlRpc::XmlRpcValue::TypeInt)
240  {
241  return false;
242  }
243 
244  double double_value = double_array[i].getType() == XmlRpc::XmlRpcValue::TypeInt ? (int)(double_array[i]) : (double)(double_array[i]);
245  value.push_back(double_value);
246  }
247 
248  return true;
249  }
250 
255  bool getParam(const std::string&name, std::vector<std::string>& value) const
256  {
257  string_map_t::const_iterator it = params_.find(name);
258  if (it == params_.end())
259  {
260  return false;
261  }
262 
263  value.clear();
264 
265  if(it->second.getType() != XmlRpc::XmlRpcValue::TypeArray)
266  {
267  return false;
268  }
269 
270  XmlRpc::XmlRpcValue string_array = it->second;
271 
272  for (unsigned int i = 0; i < string_array.size(); ++i){
273  if(string_array[i].getType() != XmlRpc::XmlRpcValue::TypeString)
274  {
275  return false;
276  }
277 
278  value.push_back(string_array[i]);
279  }
280 
281  return true;
282  }
283 
288  bool getParam(const std::string& name, XmlRpc::XmlRpcValue& value) const
289  {
290  string_map_t::const_iterator it = params_.find(name);
291  if (it == params_.end())
292  {
293  return false;
294  }
295 
296  auto tmp = it->second;
297  value = tmp;
298  return true;
299  }
300 
302  std::string filter_name_;
304  std::string filter_type_;
307 
310 
311 private:
316  {
317  if(!config.hasMember("name"))
318  {
319  ROS_ERROR("Filter didn't have name defined, other strings are not allowed");
320  return false;
321  }
322 
323  std::string name = config["name"];
324 
325  if(!config.hasMember("type"))
326  {
327  ROS_ERROR("Filter %s didn't have type defined, other strings are not allowed", name.c_str());
328  return false;
329  }
330 
331  std::string type = config["type"];
332 
333  filter_name_ = name;
334  filter_type_ = type;
335  ROS_DEBUG("Configuring Filter of Type: %s with name %s", type.c_str(), name.c_str());
336  return true;
337  }
338 
339 protected:
341  {
343  {
344  ROS_ERROR("A filter configuration must be a map with fields name, type, and params");
345  return false;
346  }
347 
348  if (!setNameAndType(config))
349  {
350  return false;
351  }
352 
353  //check to see if we have parameters in our list
354  if(config.hasMember("params"))
355  {
356  //get the params map
357  XmlRpc::XmlRpcValue params = config["params"];
358 
360  {
361  ROS_ERROR("params must be a map");
362  return false;
363  }
364  else{
365  //Load params into map
366  for(XmlRpc::XmlRpcValue::iterator it = params.begin(); it != params.end(); ++it)
367  {
368  ROS_DEBUG("Loading param %s\n", it->first.c_str());
369  params_[it->first] = it->second;
370  }
371  }
372  }
373 
374  return true;
375  }
376 };
377 
378 
379 template <typename T>
381 {
382 public:
384 
390  bool configure(unsigned int number_of_channels, const std::string& param_name, ros::NodeHandle node_handle = ros::NodeHandle())
391  {
392  XmlRpc::XmlRpcValue config;
393  if (!node_handle.getParam(param_name, config))
394  {
395  ROS_ERROR("Could not find parameter %s on the server, are you sure that it was pushed up correctly?", param_name.c_str());
396  return false;
397  }
398  return configure(number_of_channels, config);
399 
400  }
401 
402 
407  bool configure(unsigned int number_of_channels, XmlRpc::XmlRpcValue& config)
408  {
409  ROS_DEBUG("FilterBase being configured with XmlRpc xml: %s type: %d", config.toXml().c_str(), config.getType());
410  if (configured_)
411  {
412  ROS_WARN("Filter %s of type %s already being reconfigured", filter_name_.c_str(), filter_type_.c_str());
413  };
414  configured_ = false;
415  number_of_channels_ = number_of_channels;
416  ROS_DEBUG("MultiChannelFilterBase configured with %d channels", number_of_channels_);
417  bool retval = true;
418 
419  retval = retval && FilterBase<T>::loadConfiguration(config);
420  retval = retval && configure();
421  configured_ = retval;
422  return retval;
423  };
424 
425 
428  {
429  ROS_ERROR("MultiChannelFilterBase configure should be called with a number of channels argument, assuming 1");
430  return configure(1, config);
431  }
432 
433  virtual bool configure()=0;
434 
435 
441  virtual bool update(const std::vector<T>& data_in, std::vector<T>& data_out)=0;
442 
443  virtual bool update(const T& /*data_in*/, T& /*data_out*/)
444  {
445  ROS_ERROR("THIS IS A MULTI FILTER DON'T CALL SINGLE FORM OF UPDATE");
446  return false;
447  };
448 
449 
450 protected:
454 
456  unsigned int number_of_channels_;
457 
458 
459 };
460 
461 }
462 #endif //#ifndef FILTERS_FILTER_BASE_HPP_
XmlRpc::XmlRpcValue::size
int size() const
filters::FilterBase
A Base filter class to provide a standard interface for all filters.
Definition: filter_base.hpp:47
filters::FilterBase::~FilterBase
virtual ~FilterBase()
Virtual Destructor.
Definition: filter_base.hpp:56
XmlRpc::XmlRpcValue::toXml
std::string toXml() const
filters::FilterBase::setNameAndType
bool setNameAndType(XmlRpc::XmlRpcValue &config)
Set the name and type of the filter from the parameter server.
Definition: filter_base.hpp:315
filters::MultiChannelFilterBase::configure
bool configure(unsigned int number_of_channels, XmlRpc::XmlRpcValue &config)
The public method to configure a filter from XML.
Definition: filter_base.hpp:407
filters::FilterBase::configure
bool configure(const std::string &param_name, ros::NodeHandle node_handle=ros::NodeHandle())
Configure the filter from the parameter server.
Definition: filter_base.hpp:62
filters
Definition: filter_base.hpp:38
filters::MultiChannelFilterBase::configure
bool configure(unsigned int number_of_channels, const std::string &param_name, ros::NodeHandle node_handle=ros::NodeHandle())
Configure the filter from the parameter server.
Definition: filter_base.hpp:390
filters::string_map_t
std::map< std::string, XmlRpc::XmlRpcValue > string_map_t
Definition: filter_base.hpp:41
filters::MultiChannelFilterBase::update
virtual bool update(const T &, T &)
Update the filter and return the data seperately This is an inefficient way to do this and can be ove...
Definition: filter_base.hpp:443
filters::MultiChannelFilterBase::update
virtual bool update(const std::vector< T > &data_in, std::vector< T > &data_out)=0
Update the filter and return the data seperately.
ros.h
XmlRpc::XmlRpcValue::TypeInt
TypeInt
filters::FilterBase::filter_name_
std::string filter_name_
The name of the filter.
Definition: filter_base.hpp:302
filters::FilterBase::getParam
bool getParam(const std::string &name, std::vector< std::string > &value) const
Get a filter parameter as a std::vector<string>
Definition: filter_base.hpp:255
filters::FilterBase::getParam
bool getParam(const std::string &name, double &value) const
Get a filter parameter as a double.
Definition: filter_base.hpp:162
XmlRpc::XmlRpcValue::TypeStruct
TypeStruct
filters::FilterBase::getParam
bool getParam(const std::string &name, XmlRpc::XmlRpcValue &value) const
Get a filter parameter as a XmlRpcValue.
Definition: filter_base.hpp:288
filters::FilterBase::getType
std::string getType()
Get the type of the filter as a string.
Definition: filter_base.hpp:100
console.h
filters::FilterBase::getParam
bool getParam(const std::string &name, bool &value) const
Get a filter parameter as a boolean.
Definition: filter_base.hpp:140
filters::FilterBase::loadConfiguration
bool loadConfiguration(XmlRpc::XmlRpcValue &config)
Definition: filter_base.hpp:340
XmlRpc::XmlRpcValue::TypeDouble
TypeDouble
filters::MultiChannelFilterBase::configure
virtual bool configure()=0
Pure virtual function for the sub class to configure the filter This function must be implemented in ...
filters::FilterBase::getParam
bool getParam(const std::string &name, int &value) const
Get a filter parameter as a int.
Definition: filter_base.hpp:184
filters::FilterBase::update
virtual bool update(const T &data_in, T &data_out)=0
Update the filter and return the data seperately This is an inefficient way to do this and can be ove...
filters::MultiChannelFilterBase::number_of_channels_
unsigned int number_of_channels_
How many parallel inputs for which the filter is to be configured.
Definition: filter_base.hpp:456
ROS_DEBUG
#define ROS_DEBUG(...)
XmlRpc::XmlRpcValue::TypeString
TypeString
filters::MultiChannelFilterBase::MultiChannelFilterBase
MultiChannelFilterBase()
Definition: filter_base.hpp:383
ROS_WARN
#define ROS_WARN(...)
filters::FilterBase::getParam
bool getParam(const std::string &name, std::string &value) const
Get a filter parameter as a string.
Definition: filter_base.hpp:118
filters::MultiChannelFilterBase
Definition: filter_base.hpp:380
filters::FilterBase::getParam
bool getParam(const std::string &name, std::vector< double > &value) const
Get a filter parameter as a std::vector<double>
Definition: filter_base.hpp:221
XmlRpc::XmlRpcValue::iterator
ValueStruct::iterator iterator
XmlRpc::XmlRpcValue::end
iterator end()
XmlRpc::XmlRpcValue::getType
const Type & getType() const
XmlRpc::XmlRpcValue::TypeArray
TypeArray
XmlRpc::XmlRpcValue::hasMember
bool hasMember(const std::string &name) const
filters::FilterBase::getName
const std::string & getName() const
Get the name of the filter as a string.
Definition: filter_base.hpp:103
filters::FilterBase::FilterBase
FilterBase()
Default constructor used by Filter Factories.
Definition: filter_base.hpp:52
filters::FilterBase::configured_
bool configured_
Whether the filter has been configured.
Definition: filter_base.hpp:306
ROS_ERROR
#define ROS_ERROR(...)
filters::FilterBase::getParam
bool getParam(const std::string &name, unsigned int &value) const
Get a filter parameter as an unsigned int.
Definition: filter_base.hpp:206
filters::FilterBase::params_
string_map_t params_
Storage of the parsed xml parameters.
Definition: filter_base.hpp:309
filters::FilterBase::configure
virtual bool configure()=0
Pure virtual function for the sub class to configure the filter This function must be implemented in ...
XmlRpc::XmlRpcValue::begin
iterator begin()
XmlRpc::XmlRpcValue::TypeBoolean
TypeBoolean
assert.h
filters::FilterBase::configure
bool configure(XmlRpc::XmlRpcValue &config)
The public method to configure a filter from XML.
Definition: filter_base.hpp:77
filters::FilterBase::filter_type_
std::string filter_type_
The type of the filter (Used by FilterChain for Factory construction)
Definition: filter_base.hpp:304
filters::MultiChannelFilterBase::configure
bool configure(XmlRpc::XmlRpcValue &config)
A method to hide the base class method and warn if improperly called.
Definition: filter_base.hpp:427
XmlRpc::XmlRpcValue
ros::NodeHandle


filters
Author(s):
autogenerated on Fri Nov 11 2022 03:09:05