parameters.cpp
Go to the documentation of this file.
00001 //=================================================================================================
00002 // Copyright (c) 2011, Johannes Meyer, TU Darmstadt
00003 // All rights reserved.
00004 
00005 // Redistribution and use in source and binary forms, with or without
00006 // modification, are permitted provided that the following conditions are met:
00007 //     * Redistributions of source code must retain the above copyright
00008 //       notice, this list of conditions and the following disclaimer.
00009 //     * Redistributions in binary form must reproduce the above copyright
00010 //       notice, this list of conditions and the following disclaimer in the
00011 //       documentation and/or other materials provided with the distribution.
00012 //     * Neither the name of the Flight Systems and Automatic Control group,
00013 //       TU Darmstadt, nor the names of its contributors may be used to
00014 //       endorse or promote products derived from this software without
00015 //       specific prior written permission.
00016 
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00018 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00019 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00020 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 //=================================================================================================
00028 
00029 #include <hector_pose_estimation/parameters.h>
00030 #include <hector_pose_estimation/ros/parameters.h>
00031 
00032 #include <boost/algorithm/string.hpp>
00033 
00034 #include <hector_pose_estimation/matrix.h>
00035 
00036 #include <iostream>
00037 
00038 namespace hector_pose_estimation {
00039 
00040 template <typename T>
00041 struct ParameterRegistryROS::Handler
00042 {
00043   bool operator()(const ParameterPtr& parameter, ros::NodeHandle& nh) {
00044     try {
00045       TypedParameter<T> p(*parameter);
00046       std::string param_key(boost::algorithm::to_lower_copy(parameter->key));
00047       if (!nh.getParam(param_key, p.value)) {
00048         nh.setParam(param_key, p.value);
00049         ROS_DEBUG_STREAM("Registered parameter " << param_key << " with new value " << p.value);
00050       } else {
00051         ROS_DEBUG_STREAM("Found parameter " << param_key << " with value " << p.value);
00052       }
00053       return true;
00054     } catch(std::bad_cast&) {
00055       return false;
00056     }
00057   }
00058 };
00059 
00060 template <>
00061 struct ParameterRegistryROS::Handler<ColumnVector>
00062 {
00063   bool operator()(const ParameterPtr& parameter, ros::NodeHandle& nh) {
00064     try {
00065       TypedParameter<ColumnVector> p(*parameter);
00066       std::string param_key(boost::algorithm::to_lower_copy(parameter->key));
00067       XmlRpc::XmlRpcValue vector;
00068       if (!nh.getParam(param_key, vector)) {
00070         ROS_DEBUG_STREAM("Not registered vector parameter " << param_key << ". Using defaults.");
00071       } else {
00072         if (vector.getType() != XmlRpc::XmlRpcValue::TypeArray) {
00073           ROS_WARN_STREAM("Found parameter " << param_key << ", but it's not an array!");
00074           return false;
00075         }
00076         p.value.resize(vector.size());
00077         for(int i = 0; i < vector.size(); ++i) p.value[i] = vector[i];
00078         ROS_DEBUG_STREAM("Found parameter " << param_key << " with value " << p.value);
00079       }
00080       return true;
00081     } catch(std::bad_cast&) {
00082       return false;
00083     }
00084   }
00085 };
00086 
00087 template <typename T>
00088 std::ostream& operator<<(std::ostream& os, const std::vector<T>& vector) {
00089   os << "[";
00090   for(typename std::vector<T>::const_iterator it = vector.begin(); it != vector.end(); ++it) {
00091     if (it != vector.begin()) os << ", ";
00092     os << *it;
00093   }
00094   os << "]";
00095   return os;
00096 }
00097 
00098 template <typename T>
00099 struct ParameterRegistryROS::Handler< std::vector<T> >
00100 {
00101   bool operator()(const ParameterPtr& parameter, ros::NodeHandle& nh) {
00102     try {
00103       TypedParameter< std::vector<T> > p(*parameter);
00104       std::string param_key(boost::algorithm::to_lower_copy(parameter->key));
00105       XmlRpc::XmlRpcValue vector;
00106       if (!nh.getParam(param_key, vector)) {
00108         ROS_DEBUG_STREAM("Not registered vector parameter " << param_key << ". Using defaults.");
00109       } else {
00110         if (vector.getType() != XmlRpc::XmlRpcValue::TypeArray) {
00111           ROS_WARN_STREAM("Found parameter " << param_key << ", but it's not an array!");
00112           return false;
00113         }
00114         p.value.resize(vector.size());
00115         for(int i = 0; i < vector.size(); ++i) p.value[i] = vector[i];
00116         ROS_DEBUG_STREAM("Found parameter " << param_key << " with value " << p.value);
00117       }
00118       return true;
00119     } catch(std::bad_cast&) {
00120       return false;
00121     }
00122   }
00123 };
00124 
00125 ParameterRegistryROS::ParameterRegistryROS(ros::NodeHandle nh)
00126   : nh_(nh)
00127 {}
00128 
00129 void ParameterRegistryROS::operator ()(ParameterPtr parameter) {
00130   if (Handler<std::string>()(parameter, nh_) ||
00131       Handler<double>()(parameter, nh_) ||
00132       Handler<std::vector<double> >()(parameter, nh_) ||
00133       Handler<int>()(parameter, nh_) ||
00134       Handler<bool>()(parameter, nh_) ||
00135       Handler<ColumnVector>()(parameter, nh_)
00136      ) {
00137     return;
00138   }
00139   ROS_ERROR("Could not register parameter %s due to unknown type %s!", parameter->key.c_str(), parameter->type());
00140 }
00141 
00142 void ParameterList::initialize(ParameterRegisterFunc func) const {
00143   for(const_iterator it = begin(); it != end(); ++it) func(*it);
00144 }
00145 
00146 } // namespace hector_pose_estimation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends


hector_pose_estimation_core
Author(s): Johannes Meyer
autogenerated on Mon Jul 15 2013 16:48:43