XBotInterface  2.4.1
XBotInterface provides a generic API to model and control a robot.
XBotCoreModel.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 Italian Institute of Technology
3 
4  Developer:
5  Luca Muratore (2016-, luca.muratore@iit.it)
6 
7 */
8 
14 #ifndef __X_BOT_CORE_MODEL_HPP__
15 #define __X_BOT_CORE_MODEL_HPP__
16 
18 
19 #include <srdfdom_advr/model.h>
20 #include <urdf_parser/urdf_parser.h>
21 #include <kdl_parser/kdl_parser.hpp>
22 
23 #include <fstream>
24 #include <sstream>
25 #include <stdexcept>
26 #include <algorithm>
27 
28 #include <yaml-cpp/yaml.h>
29 
30 #define CHAIN_PER_GROUP 1
31 
32 namespace XBot
33 {
34  class XBotCoreModel;
35 }
36 
37 typedef std::map<int, std::string> Rid2JointMap;
38 typedef std::map<std::string, int> Joint2RidMap;
39 
40 class XBot::XBotCoreModel : public srdf_advr::Model,
41  public XBot::IXBotModel
42 {
43 private:
44 
45  std::string urdf_string, srdf_string, joint_map_string;
46  std::string srdf_path;
47  urdf::ModelInterfaceSharedPtr urdf_model;
48  KDL::Tree robot_tree;
49 
50 
55  std::map<std::string, std::vector<int>> robot;
56 
61  std::map<std::string, std::vector<std::string>> robot_string;
62 
67  std::map<std::string, int> ft_sensors;
68 
73  std::map<std::string, int> imu_sensors;
74 
79  std::map<std::string, int> hands;
80 
85  Rid2JointMap rid2joint;
86 
91  Joint2RidMap joint2rid;
92 
93  // enabled joint number of the robot
94  int joint_num = 0;
95 
96  // vector for the chain names
97  std::vector<std::string> chain_names;
98 
99  // vector for the chain names
100  std::vector<std::string> ordered_chain_names;
101 
102  // vector for the chain names of the legs ORDERED as the SRDF
103  std::vector<std::string> legs_names;
104 
105  // vector for the chain names of the arms ORDERED as the SRDF
106  std::vector<std::string> arms_names;
107 
108  // vector for the disabled joints
109  std::vector<std::string> disabled_joint_names;
110 
111  // map for the enabled joints in chains
112  std::map<std::string, std::vector<std::string>> enabled_joints_in_chains;
113 
114  // map for the disabled joints in chains
115  std::map<std::string, std::vector<std::string>> disabled_joints_in_chains;
116 
117 
118 
125  urdf::ModelInterfaceSharedPtr loadURDF(const std::string& filename);
126 
127 
133  bool parseSRDF();
134 
144  bool get_joints_in_chain( std::string base_link,
145  std::string tip_link,
146  std::vector<std::string>& enabled_joints_in_chain,
147  std::vector<std::string>& disabled_joints_in_chain);
148 
149 
150 
151  void parseJointMap(const YAML::Node& joint_map);
152 
153 public:
154 
156 
162  urdf::ModelInterfaceSharedPtr get_urdf_model(void) const
163  {
164  return urdf_model;
165  }
166 
172  KDL::Tree get_robot_tree(void)
173  {
174  return robot_tree;
175  }
176 
182  const std::vector<std::string>& get_chain_names(void) const
183  {
184  return chain_names;
185  }
186 
192  const std::vector<std::string>& get_ordered_chain_names(void) const
193  {
194  return ordered_chain_names;
195  }
196 
205  bool init(const std::string& urdf_filename, const std::string& srdf_filename,
206  const std::string& joint_map_config);
207 
208 
217  bool init_from_string(const std::string& urdf, const std::string& srdf,
218  const std::string& joint_map_config);
219 
227  bool init(const std::string& urdf_filename, const std::string& srdf_filename);
228 
235  void generate_robot(void);
236 
237  virtual std::map<std::string, std::vector<int>> get_robot(void) final
238  {
239  return robot;
240  }
241 
242  virtual std::map<std::string,int> get_ft_sensors(void) final
243  {
244  return ft_sensors;
245  }
246 
247  virtual std::map<std::string, int> get_imu_sensors() final
248  {
249  return imu_sensors;
250  }
251 
252  virtual std::map<std::string, int> get_hands() final
253  {
254  return hands;
255  }
256 
257  virtual std::string rid2Joint(int rId) final
258  {
259  return rid2joint.find(rId) != rid2joint.end() ? rid2joint[rId] : "";
260  }
261 
262  virtual int joint2Rid(std::string joint_name) final
263  {
264  return joint2rid.find(joint_name) != joint2rid.end() ? joint2rid[joint_name] : 0;
265  }
266 
267 
275  bool get_enabled_joints_in_chain( std::string chain_name, std::vector<std::string>& enabled_joints) const;
276 
277 
285  bool get_disabled_joints_in_chain( std::string chain_name, std::vector<std::string>& disabled_joints) const;
286 
287 
294  void get_enabled_joint_names(std::vector<std::string>& joint_names) const;
295 
302  void get_enabled_joint_ids(std::vector<int>& joint_ids) const;
303 
311  bool get_enabled_joint_ids_in_chain(std::string chain_name, std::vector<int>& joint_ids) const;
312 
318  int get_joint_num() const;
319 
326  int get_joint_num(std::string chain_name) const;
327 
333  const std::string& get_urdf_string() const;
334 
340  const std::string& get_srdf_string() const;
341 
347  const std::vector<std::string>& get_legs_chain() const;
348 
354  const std::vector<std::string>& get_arms_chain() const;
355 
356  bool check_joint_limits() const;
357 
359  {
360  }
361 
362 };
363 
364 #endif //__X_BOT_CORE_MODEL_HPP__
IXBotModel.h
Joint2RidMap
std::map< std::string, int > Joint2RidMap
Definition: XBotCoreModel.h:38
XBot::XBotCoreModel::~XBotCoreModel
~XBotCoreModel()
Definition: XBotCoreModel.h:358
XBot::XBotCoreModel::generate_robot
void generate_robot(void)
dynamically generate the robot as a map {chain_name} -> {vector of enabled joints}
Definition: XBotCoreModel.cpp:416
XBot::XBotCoreModel::get_srdf_string
const std::string & get_srdf_string() const
Get the robot SRDF as a string.
Definition: XBotCoreModel.cpp:491
XBot::XBotCoreModel::init_from_string
bool init_from_string(const std::string &urdf, const std::string &srdf, const std::string &joint_map_config)
initialization function for the model: it loads the URDF and parses the SRDF
Definition: XBotCoreModel.cpp:296
XBot::XBotCoreModel::XBotCoreModel
XBotCoreModel(void)
Definition: XBotCoreModel.h:155
XBot::XBotCoreModel::get_ordered_chain_names
const std::vector< std::string > & get_ordered_chain_names(void) const
getter for the alphabetically ordered chain names vector
Definition: XBotCoreModel.h:192
XBot::XBotCoreModel::get_disabled_joints_in_chain
bool get_disabled_joints_in_chain(std::string chain_name, std::vector< std::string > &disabled_joints) const
getter for the disabled joints vector in the chain
Definition: XBotCoreModel.cpp:255
XBot::XBotCoreModel::get_ft_sensors
virtual std::map< std::string, int > get_ft_sensors(void) final
Definition: XBotCoreModel.h:242
XBot::XBotCoreModel::get_hands
virtual std::map< std::string, int > get_hands() final
Definition: XBotCoreModel.h:252
XBot::XBotCoreModel::get_enabled_joints_in_chain
bool get_enabled_joints_in_chain(std::string chain_name, std::vector< std::string > &enabled_joints) const
getter for the enabled joints vector in the chain
Definition: XBotCoreModel.cpp:242
XBot::XBotCoreModel::get_legs_chain
const std::vector< std::string > & get_legs_chain() const
Get the legs chain names ordered as in the SRDF.
Definition: XBotCoreModel.cpp:501
XBot::IXBotModel
TBD.
Definition: IXBotModel.h:29
XBot::XBotCoreModel::get_robot
virtual std::map< std::string, std::vector< int > > get_robot(void) final
Definition: XBotCoreModel.h:237
XBot::XBotCoreModel::check_joint_limits
bool check_joint_limits() const
Definition: XBotCoreModel.cpp:511
XBot::XBotCoreModel::rid2Joint
virtual std::string rid2Joint(int rId) final
Definition: XBotCoreModel.h:257
XBot::XBotCoreModel::joint2Rid
virtual int joint2Rid(std::string joint_name) final
Definition: XBotCoreModel.h:262
XBot::XBotCoreModel::get_joint_num
int get_joint_num() const
get the number of joint in the robot
Definition: XBotCoreModel.cpp:486
XBot::XBotCoreModel
Definition: XBotCoreModel.h:40
Rid2JointMap
std::map< int, std::string > Rid2JointMap
Definition: XBotCoreModel.h:37
XBot::XBotCoreModel::init
bool init(const std::string &urdf_filename, const std::string &srdf_filename, const std::string &joint_map_config)
initialization function for the model: it loads the URDF and parses the SRDF
Definition: XBotCoreModel.cpp:339
XBot::XBotCoreModel::get_chain_names
const std::vector< std::string > & get_chain_names(void) const
getter for the chain names vector
Definition: XBotCoreModel.h:182
XBot::XBotCoreModel::get_urdf_string
const std::string & get_urdf_string() const
Get the robot URDF as a string.
Definition: XBotCoreModel.cpp:496
XBot::XBotCoreModel::get_enabled_joint_names
void get_enabled_joint_names(std::vector< std::string > &joint_names) const
get the vector of the enabled joint names of the whole robot
Definition: XBotCoreModel.cpp:464
XBot::XBotCoreModel::get_enabled_joint_ids_in_chain
bool get_enabled_joint_ids_in_chain(std::string chain_name, std::vector< int > &joint_ids) const
get the vector of the joint ids of the requested chain
Definition: XBotCoreModel.cpp:442
XBot::XBotCoreModel::get_arms_chain
const std::vector< std::string > & get_arms_chain() const
Get the arms chain names ordered as in the SRDF.
Definition: XBotCoreModel.cpp:506
XBot::XBotCoreModel::get_enabled_joint_ids
void get_enabled_joint_ids(std::vector< int > &joint_ids) const
get the vector of the enabled joint ids of the whole robot
Definition: XBotCoreModel.cpp:455
XBot::XBotCoreModel::get_urdf_model
urdf::ModelInterfaceSharedPtr get_urdf_model(void) const
getter for the URDF ModelInterface
Definition: XBotCoreModel.h:162
XBot::XBotCoreModel::get_imu_sensors
virtual std::map< std::string, int > get_imu_sensors() final
Definition: XBotCoreModel.h:247
XBot
Definition: IXBotModel.h:20
XBot::XBotCoreModel::get_robot_tree
KDL::Tree get_robot_tree(void)
getter for the KDL robot tree
Definition: XBotCoreModel.h:172