XBotInterface  2.4.1
XBotInterface provides a generic API to model and control a robot.
ConfigOptions.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 IIT-ADVR
3  * Author: Arturo Laurenzi, Luca Muratore
4  * email: arturo.laurenzi@iit.it, luca.muratore@iit.it
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>
18 */
19 
20 #ifndef __XBOT_CONFIG_OPTIONS_H__
21 #define __XBOT_CONFIG_OPTIONS_H__
22 
23 
24 #include <vector>
25 #include <map>
26 #include <memory>
27 #include <algorithm>
28 #include <boost/any.hpp>
29 #include <yaml-cpp/yaml.h>
30 #include <XBotInterface/Logger.hpp>
31 #include <XBotInterface/Utils.h>
32 
33 namespace XBot
34 {
35 
37  {
38  public:
39 
40  static ConfigOptions FromConfigFile(std::string path_to_config_file);
41 
42  const std::string& get_urdf() const;
43  const std::string& get_srdf() const;
44  const std::string& get_joint_id_map() const;
45 
51  bool get_urdf_path(std::string& urdf_path);
52 
58  bool get_srdf_path(std::string& srdf_path);
59 
60  bool has_config() const;
61  const YAML::Node& get_config() const;
62  std::string get_path_to_config() const;
63 
64  template <typename ParameterType>
65  bool get_parameter(std::string key, ParameterType& value) const;
66 
67  template <typename ParameterType>
68  bool set_parameter(std::string key, const ParameterType& value);
69 
70  bool is_same_robot(const ConfigOptions& other) const;
71 
72  bool set_urdf_path(std::string path_to_urdf);
73  bool set_srdf_path(std::string path_to_srdf);
74  bool set_jidmap_path(std::string path_to_jidmap);
75 
76  bool set_urdf(std::string urdf);
77  bool set_srdf(std::string srdf);
78  bool set_jidmap(std::string jidmap);
79 
80  void make_floating_base(bool fb);
81  bool is_floating_base();
82 
83  bool generate_jidmap();
84 
85  protected:
86 
87  std::string _urdf, _srdf, _jidmap;
88  std::string _urdf_path, _srdf_path;
89  YAML::Node _config;
90  std::string _path_to_cfg;
91 
92  private:
93 
94  std::map<std::string, boost::any> _any_values;
95 
96  };
97 
98 
99 
100 }
101 
102 template <typename ParameterType>
103 inline bool XBot::ConfigOptions::get_parameter(std::string key, ParameterType& value) const
104 {
105  if(_any_values.count(key) == 0)
106  {
107  return false;
108  }
109 
110  try
111  {
112  value = boost::any_cast<ParameterType>(_any_values.at(key));
113  }
114  catch(std::exception& e)
115  {
116  Logger::error("Unable to convert parameter %s to the desired type: %s", key.c_str(), e.what());
117  return false;
118  }
119 
120  return true;
121 
122 }
123 
124 
125 template <typename ParameterType>
126 inline bool XBot::ConfigOptions::set_parameter(std::string key, const ParameterType& value)
127 {
128  _any_values[key] = boost::any(value);
129  return true;
130 }
131 
132 #endif
XBot::ConfigOptions
Definition: ConfigOptions.h:36
XBot::ConfigOptions::get_joint_id_map
const std::string & get_joint_id_map() const
Definition: ConfigOptions.cpp:12
XBot::ConfigOptions::has_config
bool has_config() const
Definition: ConfigOptions.cpp:27
XBot::ConfigOptions::_path_to_cfg
std::string _path_to_cfg
Definition: ConfigOptions.h:90
XBot::ConfigOptions::get_urdf_path
bool get_urdf_path(std::string &urdf_path)
get_urdf_path retrieve the path to the urdf passed
Definition: ConfigOptions.cpp:368
XBot::ConfigOptions::_urdf
std::string _urdf
Definition: ConfigOptions.h:87
XBot::Logger::error
static std::ostream & error(Logger::Severity s=Logger::Severity::HIGH)
Logs an error message (in red, with bold [ERROR] header).
Definition: RtLog.cpp:41
XBot::ConfigOptions::set_jidmap_path
bool set_jidmap_path(std::string path_to_jidmap)
Definition: ConfigOptions.cpp:217
XBot::ConfigOptions::get_srdf_path
bool get_srdf_path(std::string &srdf_path)
get_srdf_path retrieve the path to the srdf passed
Definition: ConfigOptions.cpp:377
XBot::ConfigOptions::is_floating_base
bool is_floating_base()
Definition: ConfigOptions.cpp:351
XBot::ConfigOptions::get_path_to_config
std::string get_path_to_config() const
Definition: ConfigOptions.cpp:363
XBot::ConfigOptions::set_urdf
bool set_urdf(std::string urdf)
Definition: ConfigOptions.cpp:222
XBot::ConfigOptions::_jidmap
std::string _jidmap
Definition: ConfigOptions.h:87
Utils.h
XBot::ConfigOptions::_srdf_path
std::string _srdf_path
Definition: ConfigOptions.h:88
XBot::ConfigOptions::set_srdf
bool set_srdf(std::string srdf)
Definition: ConfigOptions.cpp:257
XBot::ConfigOptions::get_urdf
const std::string & get_urdf() const
Definition: ConfigOptions.cpp:22
XBot::ConfigOptions::_srdf
std::string _srdf
Definition: ConfigOptions.h:87
XBot::ConfigOptions::set_parameter
bool set_parameter(std::string key, const ParameterType &value)
Definition: ConfigOptions.h:126
XBot::ConfigOptions::_config
YAML::Node _config
Definition: ConfigOptions.h:89
XBot::ConfigOptions::set_urdf_path
bool set_urdf_path(std::string path_to_urdf)
Definition: ConfigOptions.cpp:199
XBot::ConfigOptions::get_srdf
const std::string & get_srdf() const
Definition: ConfigOptions.cpp:17
XBot::ConfigOptions::generate_jidmap
bool generate_jidmap()
Definition: ConfigOptions.cpp:386
Logger.hpp
XBot::ConfigOptions::make_floating_base
void make_floating_base(bool fb)
Definition: ConfigOptions.cpp:281
XBot::ConfigOptions::set_srdf_path
bool set_srdf_path(std::string path_to_srdf)
Definition: ConfigOptions.cpp:211
XBot::ConfigOptions::set_jidmap
bool set_jidmap(std::string jidmap)
Definition: ConfigOptions.cpp:269
XBot::ConfigOptions::is_same_robot
bool is_same_robot(const ConfigOptions &other) const
Definition: ConfigOptions.cpp:32
XBot::ConfigOptions::_urdf_path
std::string _urdf_path
Definition: ConfigOptions.h:88
XBot::ConfigOptions::get_config
const YAML::Node & get_config() const
Definition: ConfigOptions.cpp:7
XBot::ConfigOptions::FromConfigFile
static ConfigOptions FromConfigFile(std::string path_to_config_file)
Definition: ConfigOptions.cpp:58
XBot
Definition: IXBotModel.h:20
XBot::ConfigOptions::get_parameter
bool get_parameter(std::string key, ParameterType &value) const
Definition: ConfigOptions.h:103