utilities.hpp
Go to the documentation of this file.
00001 #pragma once
00002 
00003 #include <limits>
00004 #include <stdlib.h>
00005 #include <string>
00006 #include <ros/ros.h>
00007 #include <boost/algorithm/string.hpp>
00008 
00010 template <typename T>
00011 using is_vector = std::is_same<T, std::vector<typename T::value_type, typename T::allocator_type>>;
00012 
00014 template <typename T>
00015 using is_map = std::is_same<
00016     T, std::map<typename T::key_type, typename T::mapped_type, typename T::key_compare, typename T::allocator_type>>;
00017 
00018 namespace rosparam_handler {
00019 
00020 template <typename T>
00021 inline std::string to_string(const std::vector<T>& v)
00022 {
00023   std::string s;
00024   if (!v.empty()) {
00025       std::stringstream ss;
00026       ss << '[';
00027       std::copy(v.begin(), v.end(), std::ostream_iterator<T>(ss, ", "));
00028       ss << "\b\b]";
00029       s = ss.str();
00030   }
00031   return s;
00032 }
00033 
00034 template <typename... T>
00035 inline std::string to_string(const std::map<T...>& map)
00036 {
00037   std::stringstream ss;
00038   ss << '{';
00039   for (typename std::map<T...>::const_iterator it = map.begin(); it != map.end(); ++it) {
00040       ss << (*it).first << " --> " << (*it).second << ", ";
00041   }
00042   ss << '}';
00043   return ss.str();
00044 }
00045 
00049 inline void setLoggerLevel(const ros::NodeHandle& nodeHandle) {
00050 
00051     std::string verbosity;
00052     if (!nodeHandle.getParam("verbosity", verbosity)) {
00053         verbosity = "warning";
00054     }
00055 
00056     ros::console::Level level_ros;
00057     bool valid_verbosity{true};
00058     if (verbosity == "debug") {
00059         level_ros = ros::console::levels::Debug;
00060     } else if (verbosity == "info") {
00061         level_ros = ros::console::levels::Info;
00062     } else if (verbosity == "warning") {
00063         level_ros = ros::console::levels::Warn;
00064     } else if (verbosity == "error") {
00065         level_ros = ros::console::levels::Error;
00066     } else if (verbosity == "fatal") {
00067         level_ros = ros::console::levels::Fatal;
00068     } else {
00069         ROS_WARN_STREAM("Invalid verbosity level specified: " << verbosity << "! Falling back to INFO.");
00070         valid_verbosity = false;
00071     }
00072     if (valid_verbosity) {
00073         if (ros::console::set_logger_level(ROSCONSOLE_DEFAULT_NAME, level_ros)) {
00074             ros::console::notifyLoggerLevelsChanged();
00075             ROS_DEBUG_STREAM("Verbosity set to " << verbosity);
00076         }
00077     }
00078 }
00079 
00081 inline void showNodeInfo() {
00082 
00083     using namespace ros::this_node;
00084 
00085     std::vector<std::string> subscribed_topics, advertised_topics;
00086     getSubscribedTopics(subscribed_topics);
00087     getAdvertisedTopics(advertised_topics);
00088 
00089     std::ostringstream msg_subscr, msg_advert;
00090     for (auto const& t : subscribed_topics) {
00091         msg_subscr << t << "\n";
00092     }
00093     for (auto const& t : advertised_topics) {
00094         msg_advert << t << "\n";
00095     }
00096 
00097     ROS_INFO_STREAM("Started '" << getName() << "' in namespace '" << getNamespace() << "'.\n"
00098                                 << "Subscribed topics:\n"
00099                                 << msg_subscr.str() << "Advertised topics:\n"
00100                                 << msg_advert.str());
00101 }
00102 
00108 inline std::string getNodeName(const ros::NodeHandle& privateNodeHandle) {
00109     std::string name_space = privateNodeHandle.getNamespace();
00110     std::stringstream tempString(name_space);
00111     std::string name;
00112     while (std::getline(tempString, name, '/')) {
00113         ;
00114     }
00115     return name;
00116 }
00117 
00119 inline void exit(const std::string msg = "Runtime Error in rosparam handler.") {
00120     // std::exit(EXIT_FAILURE);
00121     throw std::runtime_error(msg);
00122 }
00123 
00128 template <typename T>
00129 inline void setParam(const std::string key, T val) {
00130     ros::param::set(key, val);
00131 }
00132 
00137 template <typename T>
00138 inline bool getParam(const std::string key, T& val) {
00139     if (!ros::param::has(key)) {
00140         ROS_ERROR_STREAM("Parameter '" << key << "' is not defined.");
00141         return false;
00142     } else if (!ros::param::get(key, val)) {
00143         ROS_ERROR_STREAM("Could not retrieve parameter'" << key << "'. Does it have a different type?");
00144         return false;
00145     } else {
00146         return true;
00147     }
00148 }
00149 
00156 template <typename T>
00157 inline bool getParam(const std::string key, T& val, const T& defaultValue) {
00158     if (!ros::param::has(key) || !ros::param::get(key, val)) {
00159         val = defaultValue;
00160         ros::param::set(key, defaultValue);
00161         ROS_INFO_STREAM("Setting default value for parameter '" << key << "'.");
00162         return true;
00163     } else {
00164         // Param was already retrieved with last if statement.
00165         return true;
00166     }
00167 }
00168 
00170 inline bool testConstParam(const std::string key) {
00171     if (ros::param::has(key)) {
00172         ROS_WARN_STREAM("Parameter " << key
00173                                      << "' was set on the parameter server eventhough it was defined to be constant.");
00174         return false;
00175     } else {
00176         return true;
00177     }
00178 }
00179 
00185 template <typename T>
00186 inline void testMin(const std::string key, T& val, T min = std::numeric_limits<T>::min()) {
00187     if (val < min) {
00188         ROS_WARN_STREAM("Value of " << val << " for " << key
00189                                     << " is smaller than minimal allowed value. Correcting value to min=" << min);
00190         val = min;
00191     }
00192 }
00193 
00199 template <typename T>
00200 inline void testMin(const std::string key, std::vector<T>& val, T min = std::numeric_limits<T>::min()) {
00201     for (auto& v : val)
00202         testMin(key, v, min);
00203 }
00204 
00210 template <typename K, typename T>
00211 inline void testMin(const std::string key, std::map<K, T>& val, T min = std::numeric_limits<T>::min()) {
00212     for (auto& v : val)
00213         testMin(key, v.second, min);
00214 }
00215 
00221 template <typename T>
00222 inline void testMax(const std::string key, T& val, T max = std::numeric_limits<T>::max()) {
00223     if (val > max) {
00224         ROS_WARN_STREAM("Value of " << val << " for " << key
00225                                     << " is greater than maximal allowed. Correcting value to max=" << max);
00226         val = max;
00227     }
00228 }
00229 
00235 template <typename T>
00236 inline void testMax(const std::string key, std::vector<T>& val, T max = std::numeric_limits<T>::max()) {
00237     for (auto& v : val)
00238         testMax(key, v, max);
00239 }
00240 
00246 template <typename K, typename T>
00247 inline void testMax(const std::string key, std::map<K, T>& val, T max = std::numeric_limits<T>::max()) {
00248     for (auto& v : val)
00249         testMax(key, v.second, max);
00250 }
00251 
00252 } // namespace rosparam_handler


rosparam_handler
Author(s): Claudio Bandera
autogenerated on Sat Jun 8 2019 20:21:53