tools.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2016 SoftBank Robotics Europe
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  *
00016 */
00017 
00018 #include <boost/algorithm/string.hpp>
00019 
00020 #include "naoqi_dcm_driver/tools.hpp"
00021 
00022 qi::AnyValue fromStringVectorToAnyValue(const std::vector<std::string> &vector)
00023 {
00024   qi::AnyValue res;
00025   try
00026   {
00027     std::vector<qi::AnyValue> vector_qi;
00028     vector_qi.reserve(vector.size());
00029     vector_qi.resize(vector.size());
00030 
00031     std::vector<std::string>::const_iterator it = vector.begin();
00032     std::vector<qi::AnyValue>::iterator it_qi = vector_qi.begin();
00033     for(; it != vector.end(); ++it, ++it_qi)
00034     {
00035       *it_qi = qi::AnyValue(qi::AnyReference::from(*it), false, false);
00036     }
00037     res = qi::AnyValue(qi::AnyReference::from(vector_qi), false, false);
00038   }
00039   catch(const std::exception& e)
00040   {
00041     std::cout << "Could not convert to qi::AnyValue \n\tTrace: " << e.what() << std::endl;
00042   }
00043   return res;
00044 }
00045 
00046 qi::AnyValue fromDoubleVectorToAnyValue(const std::vector<double> &vector)
00047 {
00048   qi::AnyValue res;
00049   try
00050   {
00051     std::vector<qi::AnyValue> vector_qi;
00052     vector_qi.reserve(vector.size());
00053     vector_qi.resize(vector.size());
00054 
00055     std::vector<double>::const_iterator it = vector.begin();
00056     std::vector<qi::AnyValue>::iterator it_qi = vector_qi.begin();
00057     for(; it != vector.end(); ++it, ++it_qi)
00058     {
00059       *it_qi = qi::AnyValue(qi::AnyReference::from(static_cast<float>(*it)), false, false);
00060     }
00061     res = qi::AnyValue(qi::AnyReference::from(vector_qi), false, false);
00062   }
00063   catch(const std::exception& e)
00064   {
00065     std::cout << "Could not convert to qi::AnyValue \n\tTrace: " << e.what() << std::endl;
00066   }
00067   return res;
00068 }
00069 
00070 std::vector<float> fromAnyValueToFloatVector(qi::AnyValue& value)
00071 {
00072   std::vector<float> result;
00073   qi::AnyReferenceVector anyrefs = value.asListValuePtr();
00074 
00075   for(int i=0; i<anyrefs.size(); ++i)
00076   {
00077     try
00078     {
00079       result.push_back(anyrefs[i].content().toFloat());
00080     }
00081     catch(std::runtime_error& e)
00082     {
00083       result.push_back(0.0f);
00084       std::cout << e.what() << "=> set to 0.0f" << std::endl;
00085     }
00086   }
00087   return result;
00088 }
00089 
00090 std::vector<int> fromAnyValueToIntVector(qi::AnyValue& value)
00091 {
00092   std::vector<int> result;
00093   qi::AnyReferenceVector anyrefs = value.asListValuePtr();
00094 
00095   for(int i=0; i<anyrefs.size();i++)
00096   {
00097     try
00098     {
00099       result.push_back(anyrefs[i].content().toInt());
00100     }
00101     catch(std::runtime_error& e)
00102     {
00103       result.push_back(-1);
00104       std::cout << e.what() << "=> set to -1" << std::endl;
00105     }
00106   }
00107   return result;
00108 }
00109 
00110 std::string print(const std::vector <std::string> &vector)
00111 {
00112   std::stringstream ss;
00113   if (vector.size() > 0)
00114   {
00115     std::copy(vector.begin(), vector.end()-1, std::ostream_iterator<std::string>(ss,", "));
00116     std::copy(vector.end()-1, vector.end(), std::ostream_iterator<std::string>(ss));
00117   }
00118   return ss.str();
00119 }
00120 
00121 std::vector <std::string> toVector(const std::string &input)
00122 {
00123   std::vector <std::string> value;
00124   boost::split (value, input, boost::is_any_of(" "));
00125 
00126   //check for empty values
00127   if (!value.empty())
00128     for(std::vector<std::string>::iterator it=value.begin(); it != value.end(); ++it)
00129       if ((*it).empty())
00130       {
00131         value.erase(it);
00132         it--;
00133       }
00134   return value;
00135 }
00136 
00137 void xmlToVector(XmlRpc::XmlRpcValue &topicList,
00138                 std::vector <std::string> *joints)
00139 {
00140   if (topicList.size() == 0)
00141   {
00142     ROS_WARN("Mentioned controller does not have joints");
00143     return;
00144   }
00145   for (int i = 0; i < topicList.size(); ++i)
00146   {
00147     std::string tmp = static_cast<std::string>(topicList[i]);
00148     if (tmp.compare("") != std::string::npos)
00149       joints->push_back(tmp);
00150   }
00151 }


naoqi_dcm_driver
Author(s): Konstantinos Chatzilygeroudis , Mikael Arguedas , Karsten Knese , Natalia Lyubova
autogenerated on Thu Jun 6 2019 20:50:50