parameter_pa_ros.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 *                                                                             *
00003 * parameter_pa_ros.c                                                          *
00004 * ==================                                                          *
00005 *                                                                             *
00006 *******************************************************************************
00007 *                                                                             *
00008 * github repository                                                           *
00009 *   https://github.com/peterweissig/ros_parameter                             *
00010 *                                                                             *
00011 * Chair of Automation Technology, Technische Universität Chemnitz             *
00012 *   https://www.tu-chemnitz.de/etit/proaut                                    *
00013 *                                                                             *
00014 *******************************************************************************
00015 *                                                                             *
00016 * New BSD License                                                             *
00017 *                                                                             *
00018 * Copyright (c) 2015-2017, Peter Weissig, Technische Universität Chemnitz     *
00019 * All rights reserved.                                                        *
00020 *                                                                             *
00021 * Redistribution and use in source and binary forms, with or without          *
00022 * modification, are permitted provided that the following conditions are met: *
00023 *     * Redistributions of source code must retain the above copyright        *
00024 *       notice, this list of conditions and the following disclaimer.         *
00025 *     * Redistributions in binary form must reproduce the above copyright     *
00026 *       notice, this list of conditions and the following disclaimer in the   *
00027 *       documentation and/or other materials provided with the distribution.  *
00028 *     * Neither the name of the Technische Universität Chemnitz nor the       *
00029 *       names of its contributors may be used to endorse or promote products  *
00030 *       derived from this software without specific prior written permission. *
00031 *                                                                             *
00032 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" *
00033 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE   *
00034 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE  *
00035 * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY      *
00036 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES  *
00037 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR          *
00038 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER  *
00039 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT          *
00040 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY   *
00041 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
00042 * DAMAGE.                                                                     *
00043 *                                                                             *
00044 ******************************************************************************/
00045 
00046 // local headers
00047 #include "parameter_pa/parameter_pa_ros.h"
00048 
00049 // ros headers
00050 #include <ros/package.h>
00051 #include <XmlRpcValue.h>
00052 
00053 // standard headers
00054 #include <sstream>
00055 
00056 //**************************[load - bool]**************************************
00057 bool cParameterPaRos::load (const std::string name,
00058   bool &value,
00059   const bool print_default) const {
00060     bool result;
00061 
00062     result = nh_.getParam(ros::names::resolve(name), value);
00063 
00064     loadSub(name, bool_to_str(value), print_default, result);
00065 
00066     return result;
00067 }
00068 
00069 //**************************[load - string]************************************
00070 bool cParameterPaRos::load (const std::string name,
00071   std::string &value,
00072   const bool print_default) const {
00073     bool result;
00074 
00075     result = nh_.getParam(ros::names::resolve(name), value);
00076 
00077     loadSub(name, value, print_default, result);
00078 
00079     return result;
00080 }
00081 
00082 //**************************[load_topic]***************************************
00083 bool cParameterPaRos::load_topic (const std::string name,
00084   std::string &value,
00085   const bool print_default) const {
00086     bool result;
00087 
00088     result = nh_.getParam(ros::names::resolve(name), value);
00089     if (value != "") {
00090         resolve_ressourcename(value);
00091     }
00092 
00093     loadSub(name, value, print_default, result);
00094 
00095     return result;
00096 }
00097 
00098 //**************************[load_path]****************************************
00099 bool cParameterPaRos::load_path (const std::string name,
00100   std::string &value,
00101   const bool print_default) const {
00102     bool result;
00103 
00104     result = nh_.getParam(ros::names::resolve(name), value);
00105     replace_findpack(value);
00106 
00107     loadSub(name, value, print_default, result);
00108 
00109     return result;
00110 }
00111 
00112 //**************************[load - int]***************************************
00113 bool cParameterPaRos::load (const std::string name,
00114   int &value,
00115   const bool print_default) const {
00116     bool result;
00117 
00118     result = nh_.getParam(ros::names::resolve(name), value);
00119 
00120     std::stringstream value_s;
00121     value_s << value;
00122     loadSub(name, value_s.str(), print_default, result);
00123 
00124     return result;
00125 }
00126 
00127 //**************************[load - double]************************************
00128 bool cParameterPaRos::load (const std::string name,
00129   double &value,
00130   const bool print_default) const {
00131     bool result;
00132 
00133     result = nh_.getParam(ros::names::resolve(name), value);
00134 
00135     std::stringstream value_s;
00136     value_s << value;
00137     loadSub(name, value_s.str(), print_default, result);
00138 
00139     return result;
00140 }
00141 
00142 //**************************[load - vector<bool>]******************************
00143 bool cParameterPaRos::load (const std::string name,
00144   std::vector<bool> &value,
00145   const bool print_default) const {
00146     bool result;
00147 
00148     std::vector<bool> value_temp;
00149     result = nh_.getParam(ros::names::resolve(name), value_temp);
00150     if (result) {value = value_temp;}
00151 
00152     std::stringstream value_s;
00153     value_s << "[";
00154     if (value.size() > 0) {
00155         value_s << bool_to_str(value[0]);
00156         for (int i = 1; i < value.size(); i++) {
00157             value_s << ", " << bool_to_str(value[i]);
00158         }
00159     }
00160     value_s << "]";
00161     loadSub(name, value_s.str(), print_default, result);
00162 
00163     return result;
00164 }
00165 
00166 //**************************[load - vector<string>]****************************
00167 bool cParameterPaRos::load (const std::string name,
00168   std::vector<std::string> &value,
00169   const bool print_default) const {
00170     bool result;
00171 
00172     std::vector<std::string> value_temp;
00173     result = nh_.getParam(ros::names::resolve(name), value_temp);
00174     if (result) {value = value_temp;}
00175 
00176     std::stringstream value_s;
00177     value_s << "[";
00178     if (value.size() > 0) {
00179         value_s << value[0];
00180         for (int i = 1; i < value.size(); i++) {
00181             value_s << ", " << value[i];
00182         }
00183     }
00184     value_s << "]";
00185     loadSub(name, value_s.str(), print_default, result);
00186 
00187     return result;
00188 }
00189 
00190 //**************************[load - vector<int>]*******************************
00191 bool cParameterPaRos::load (const std::string name,
00192   std::vector<int> &value,
00193   const bool print_default) const {
00194     bool result;
00195 
00196     std::vector<int> value_temp;
00197     result = nh_.getParam(ros::names::resolve(name), value_temp);
00198     if (result) {value = value_temp;}
00199 
00200     std::stringstream value_s;
00201     value_s << "[";
00202     if (value.size() > 0) {
00203         value_s << value[0];
00204         for (int i = 1; i < value.size(); i++) {
00205             value_s << ", " << value[i];
00206         }
00207     }
00208     value_s << "]";
00209     loadSub(name, value_s.str(), print_default, result);
00210 
00211     return result;
00212 }
00213 
00214 //**************************[load - vector<double>]****************************
00215 bool cParameterPaRos::load (const std::string name,
00216   std::vector<double> &value,
00217   const bool print_default) const {
00218     bool result;
00219 
00220     std::vector<double> value_temp;
00221     result = nh_.getParam(ros::names::resolve(name), value_temp);
00222     if (result) {value = value_temp;}
00223 
00224     std::stringstream value_s;
00225     value_s << "[";
00226     if (value.size() > 0) {
00227         value_s << value[0];
00228         for (int i = 1; i < value.size(); i++) {
00229             value_s << ", " << value[i];
00230         }
00231     }
00232     value_s << "]";
00233     loadSub(name, value_s.str(), print_default, result);
00234 
00235     return result;
00236 }
00237 
00238 //**************************[load - matrix]************************************
00239 bool cParameterPaRos::load (const std::string name,
00240   Eigen::MatrixXf &value,
00241   const bool print_default) const {
00242     bool result = true;
00243 
00244     Eigen::MatrixXf mat;
00245 
00246     XmlRpc::XmlRpcValue xml;
00247     nh_.getParam(ros::names::resolve(name), xml);
00248 
00249     // check type and minimum size
00250     if((xml.getType() != XmlRpc::XmlRpcValue::TypeArray) ||
00251       (xml.size() < 1)) {
00252         result = false;
00253     } else {
00254         for (int y = 0; y < xml.size(); y++) {
00255             // check type and minimum size
00256             if ((xml[y].getType() != XmlRpc::XmlRpcValue::TypeArray) ||
00257                 (xml[y].size() < 1)) {
00258                 result = false;
00259                 break;
00260             }
00261 
00262             // set and check size
00263             if (y == 0) {
00264                 mat.resize((int) xml.size(),(int) xml[y].size());
00265             } else if (xml[y].size() != mat.cols()){
00266                 result = false;
00267                 break;
00268             }
00269 
00270             for (int x = 0; x < xml[y].size(); x++) {
00271                 if (result == false) {break;}
00272 
00273                 switch (xml[y][x].getType()) {
00274                     case XmlRpc::XmlRpcValue::TypeDouble:
00275                         mat(y,x) = (double) xml[y][x];
00276                         break;
00277                     case XmlRpc::XmlRpcValue::TypeInt:
00278                         mat(y,x) = (int) xml[y][x];
00279                         break;
00280 
00281                     default:
00282                         result = false;
00283                         break;
00284                 }
00285             }
00286             if (result == false) {
00287                 break;
00288             }
00289         }
00290     }
00291 
00292     if (result) {value = mat;}
00293 
00294     std::stringstream value_s;
00295     value_s << '[';
00296     for (int y = 0; y < value.rows(); y++) {
00297         if (y > 0) { value_s << ", ";}
00298         value_s << '[';
00299         for (int x = 0; x < value.cols(); x++) {
00300             if (x > 0) { value_s << ", ";}
00301             value_s  << value(y,x);
00302         }
00303         value_s << ']';
00304     }
00305     value_s << ']';
00306 
00307     // Vergleich
00308     value_s << std::endl << "Vergleich:" << std::endl << value;
00309 
00310     loadSub(name, value_s.str(), print_default, result);
00311 
00312     return result;
00313 }
00314 
00315 //**************************[loadSub]******************************************
00316 void cParameterPaRos::loadSub(const std::string &n, const std::string &v,
00317   const bool p, const bool r) const {
00318     std::string result;
00319 
00320     if (r) {
00321         result = "load parameter " + n + " (" + v + ")";
00322     } else {
00323         result = "parameter " + n + " not set";
00324 
00325         if (p) {
00326         result+= " (defaults to " + v + ")";
00327         }
00328     }
00329 
00330     ROS_INFO_STREAM(result);
00331 }
00332 
00333 //**************************[replace_findpack]*********************************
00334 bool cParameterPaRos::replace_findpack(std::string &path) {
00335 
00336     while(1) {
00337         // check for string "$(find ...)
00338         std::string::size_type start;
00339         start = (int) path.find("$(find ");
00340         if ((start < 0) || (start >= path.length())) {return true;}
00341 
00342         std::string::size_type end;
00343         end = path.find(")", (std::string::size_type) start);
00344         if ((end  < 0) || (end  >= path.length())) {return false;}
00345 
00346         // get replacement
00347         std::string replace;
00348         try {
00349             replace = ros::package::command(
00350               path.substr(start + 2, end - start - 2));
00351         } catch (std::runtime_error &e) {
00352             return false;
00353         }
00354         if (replace == "") { return false;}
00355 
00356         // extract white spaces from replacement
00357         std::string::size_type pos_save = 0;
00358         std::string::size_type pos_current;
00359         for (pos_current = 0; pos_current < replace.length(); pos_current++) {
00360             char c;
00361             c = replace[pos_current];
00362 
00363             if ((c == '\r') || (c == '\n') || (c == ' ') || (c == '\t')) {
00364                 continue;
00365             }
00366 
00367             if (pos_save != pos_current) {
00368                 replace[pos_save] = c;
00369             }
00370             pos_save++;
00371         }
00372         if (pos_save != replace.length()) {
00373             replace.resize(pos_save);
00374         }
00375 
00376         // replace
00377         path = path.substr(0,start) + replace + path.substr(end + 1);
00378     }
00379 
00380 }
00381 
00382 //**************************[resolve_ressourcename]****************************
00383 void cParameterPaRos::resolve_ressourcename(std::string &name) {
00384     name = ros::names::resolve(name);
00385 }
00386 
00387 //**************************[bool_to_str]**************************************
00388 std::string cParameterPaRos::bool_to_str(const bool value) {
00389     if (value) {
00390         return "true";
00391     } else {
00392         return "false";
00393     }
00394 }


parameter_pa
Author(s):
autogenerated on Thu Aug 3 2017 03:01:58