robot_frames
RobotFrames.hpp
Go to the documentation of this file.
1 #ifndef _ROBOTFRAMETRANSFORMATIONS_ROBOTFRAMETRANSFORMATIONS_HPP_
2 #define _ROBOTFRAMETRANSFORMATIONS_ROBOTFRAMETRANSFORMATIONS_HPP_
3 
4 #include <iostream>
5 #include <string>
6 #include <map>
7 #include <Eigen/Geometry>
8 #include "base/samples/Joints.hpp"
9 #include "base/samples/RigidBodyState.hpp"
10 #include "kdl/tree.hpp"
11 
12 template<typename F,typename T>
13 inline std::vector<F> extract_keys(std::map<F,T> m){
14  std::vector<F> v;
15  for(typename std::map<F,T>::iterator it = m.begin(); it != m.end(); ++it) {
16  v.push_back(it->first);
17  }
18  return v;
19 }
20 
21 template<typename F, typename T>
22 inline std::vector<T> extract_values(std::map<F,T> m){
23  std::vector<T> v;
24  for(typename std::map<F,T>::iterator it = m.begin(); it != m.end(); ++it) {
25  v.push_back(it->second);
26  }
27  return v;
28 }
29 
30 template<typename T>
31 inline bool is_invalid(T val){
32  return base::isInfinity(val) || base::isNaN(val);
33 }
34 
35 inline void convert(const KDL::Frame& from, base::Pose& to){
36  double x,y,z,w;
37  from.M.GetQuaternion(x,y,z,w);
38  to.orientation.x() = x;
39  to.orientation.y() = y;
40  to.orientation.z() = z;
41  to.orientation.w() = w;
42 
43  to.position.x() = from.p.x();
44  to.position.y() = from.p.y();
45  to.position.z() = from.p.z();
46 }
47 
48 inline void convert(const KDL::Frame& from, base::samples::RigidBodyState& to){
49  base::Pose pose;
50  convert(from, pose);
51  to.setPose(pose);
52 }
53 
54 
55 namespace robot_frames
56 {
57 
59 {
60 public:
69  void load_robot_model(std::string filepath, bool init_invalid=true);
70 
71  bool is_valid_joint_name(std::string j_name);
72  bool is_valid_joint_type(const KDL::Joint& joint);
73  bool is_valid_joint(const KDL::Joint& joint);
74 
75  void set_blacklist(const std::vector<std::string>& blacklist);
76  void init_blacklist();
77  void clear_blacklist();
78  void add_to_blacklist(std::string j_name);
79 
86  void update(const base::samples::Joints& joints);
87 
93  bool get_all_transforms(std::vector<base::samples::RigidBodyState>& transforms, bool keep_content=false);
94  bool get_transform_by_joint_name(const std::string& j_name, base::samples::RigidBodyState& transform);
95 
96  bool get_moving_joints_transforms(std::vector<base::samples::RigidBodyState>& transforms, bool keep_content=false);
97  bool get_static_joints_transforms(std::vector<base::samples::RigidBodyState>& transforms, bool keep_content=false);
98 
102  bool knownJoint(const std::string& link_name);
103 
108  void output_only_valid(bool arg);
109 
115  inline const KDL::TreeElement& get_tree_element(const std::string& segment_name){
116  KDL::SegmentMap::const_iterator elem = kdl_tree_.getSegment(segment_name);
117  if(elem == kdl_tree_.getSegments().end())
118  throw(std::runtime_error(segment_name));
119 
120 #ifdef KDL_USE_NEW_TREE_INTERFACE
121  return *(elem->second.get());
122 #else
123  return elem->second;
124 #endif
125  }
126 
127  inline const KDL::Segment& get_segment_by_segment_name(const std::string& segment_name){
128  return get_tree_element(segment_name).segment;
129  }
130 
131  inline const std::string& get_segment_name_from_joint_name(const std::string& joint_name){
132  std::map<std::string, std::string>::iterator it;
133  it = joint_name2seg_name_.find(joint_name);
134  if(it == joint_name2seg_name_.end())
135  throw(std::runtime_error(joint_name));
136  return it->second;
137  }
138 
139  inline std::vector<std::string> get_all_segment_names(){
141  }
142 
143  inline std::vector<std::string> get_static_segment_names(){
144  return static_segment_names_;
145  }
146 
147  inline const std::vector<std::string>& get_moving_joint_names(){
148  return moving_joint_names_;
149  }
150 
151  inline const std::vector<std::string>& get_static_joint_names(){
152  return static_joint_names_;
153  }
154 
155  inline const std::vector<std::string>& get_all_joint_names(){
156  return all_joint_names_;
157  }
158 
159  inline const KDL::Segment& get_segment_by_joint(const KDL::Joint joint){
161  get_segment_name_from_joint_name(joint.getName()));
162  }
163 
164  inline const std::string get_parent_name_by_segment_name(std::string seg_name){
165  std::string root_name = kdl_tree_.getRootSegment()->first;
166  if(seg_name == root_name)
167  return "";
168 
169  KDL::TreeElement elem = get_tree_element(seg_name);
170 #ifdef KDL_USE_NEW_TREE_INTERFACE
171  return elem.parent->second->segment.getName();
172 #else
173  return elem.parent->second.segment.getName();
174 #endif
175  }
176 
177 
181  inline bool is_fixed(const KDL::Joint& joint){
182  return joint.getType() == KDL::Joint::None;
183  }
184 
185  inline bool is_fixed(const KDL::Segment& segment){
186  return is_fixed(segment.getJoint());
187  }
188 
189  protected:
190  inline void clear_all(){
191  static_segment_names_.clear();
193  moving_joint_names_.clear();
194  static_joint_names_.clear();
195  joint_name2seg_name_.clear();
196  is_initialized_=false;
197  init_blacklist();
198  }
199 
200  std::map<std::string, base::samples::RigidBodyState> moving_joints_transforms_;
201  std::map<std::string, base::samples::RigidBodyState> static_joints_transforms_;
202  std::vector<std::string> moving_joint_names_;
203  std::vector<std::string> static_joint_names_;
204  std::vector<std::string> all_joint_names_;
205  std::vector<std::string> static_segment_names_;
206  std::vector<std::string> blacklist_;
207  std::map<std::string, std::string> joint_name2seg_name_;
208  KDL::Tree kdl_tree_;
211 };
212 
213 } // end namespace robot_frame_transformations
214 
215 #endif // _ROBOTFRAMETRANSFORMATIONS_ROBOTFRAMETRANSFORMATIONS_HPP_
std::vector< T > extract_values(std::map< F, T > m)
Definition: RobotFrames.hpp:22
bool get_transform_by_joint_name(const std::string &j_name, base::samples::RigidBodyState &transform)
Definition: RobotFrames.cpp:245
std::vector< std::string > get_static_segment_names()
Definition: RobotFrames.hpp:143
bool is_initialized_
Definition: RobotFrames.hpp:209
void clear_blacklist()
Definition: RobotFrames.cpp:158
bool is_fixed(const KDL::Joint &joint)
Definition: RobotFrames.hpp:181
bool is_fixed(const KDL::Segment &segment)
Definition: RobotFrames.hpp:185
std::map< std::string, std::string > joint_name2seg_name_
Definition: RobotFrames.hpp:207
void convert(const KDL::Frame &from, base::Pose &to)
Definition: RobotFrames.hpp:35
void update(const base::samples::Joints &joints)
Definition: RobotFrames.cpp:172
const KDL::Segment & get_segment_by_joint(const KDL::Joint joint)
Definition: RobotFrames.hpp:159
const std::vector< std::string > & get_static_joint_names()
Definition: RobotFrames.hpp:151
const KDL::Segment & get_segment_by_segment_name(const std::string &segment_name)
Definition: RobotFrames.hpp:127
void clear_all()
Definition: RobotFrames.hpp:190
std::vector< std::string > all_joint_names_
Definition: RobotFrames.hpp:204
std::vector< std::string > static_segment_names_
Definition: RobotFrames.hpp:205
bool output_only_valid_
Definition: RobotFrames.hpp:210
const std::vector< std::string > & get_moving_joint_names()
Definition: RobotFrames.hpp:147
KDL::Tree kdl_tree_
Definition: RobotFrames.hpp:208
std::vector< std::string > moving_joint_names_
Definition: RobotFrames.hpp:202
std::vector< std::string > static_joint_names_
Definition: RobotFrames.hpp:203
std::vector< F > extract_keys(std::map< F, T > m)
Definition: RobotFrames.hpp:13
const std::vector< std::string > & get_all_joint_names()
Definition: RobotFrames.hpp:155
std::vector< std::string > blacklist_
Definition: RobotFrames.hpp:206
void init_blacklist()
Definition: RobotFrames.cpp:151
std::map< std::string, base::samples::RigidBodyState > moving_joints_transforms_
Definition: RobotFrames.hpp:200
TransformationCalculator()
Definition: RobotFrames.cpp:10
Definition: RobotFrames.cpp:7
void output_only_valid(bool arg)
Definition: RobotFrames.cpp:14
bool get_moving_joints_transforms(std::vector< base::samples::RigidBodyState > &transforms, bool keep_content=false)
Definition: RobotFrames.cpp:227
bool is_valid_joint_name(std::string j_name)
Definition: RobotFrames.cpp:19
bool is_invalid(T val)
Definition: RobotFrames.hpp:31
const std::string & get_segment_name_from_joint_name(const std::string &joint_name)
Definition: RobotFrames.hpp:131
std::map< std::string, base::samples::RigidBodyState > static_joints_transforms_
Definition: RobotFrames.hpp:201
bool get_all_transforms(std::vector< base::samples::RigidBodyState > &transforms, bool keep_content=false)
Definition: RobotFrames.cpp:236
bool knownJoint(const std::string &link_name)
Definition: RobotFrames.cpp:38
bool get_static_joints_transforms(std::vector< base::samples::RigidBodyState > &transforms, bool keep_content=false)
Definition: RobotFrames.cpp:218
Definition: RobotFrames.hpp:58
bool is_valid_joint_type(const KDL::Joint &joint)
Definition: RobotFrames.cpp:23
void add_to_blacklist(std::string j_name)
Definition: RobotFrames.cpp:163
const KDL::TreeElement & get_tree_element(const std::string &segment_name)
Definition: RobotFrames.hpp:115
bool is_valid_joint(const KDL::Joint &joint)
Definition: RobotFrames.cpp:29
void set_blacklist(const std::vector< std::string > &blacklist)
Definition: RobotFrames.cpp:145
std::vector< std::string > get_all_segment_names()
Definition: RobotFrames.hpp:139
const std::string get_parent_name_by_segment_name(std::string seg_name)
Definition: RobotFrames.hpp:164
void load_robot_model(std::string filepath, bool init_invalid=true)
Definition: RobotFrames.cpp:48