Klamp't  0.8.1
urdf_model.h
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 /* Author: Wim Meeussen */
36 
37 #ifndef URDF_INTERFACE_MODEL_H
38 #define URDF_INTERFACE_MODEL_H
39 
40 #include <string>
41 #include <map>
42 #include "urdf_link.h"
43 #include "urdf_exception.h"
44 
45 namespace urdf {
46 
51  {
52  public:
53 
57  std::shared_ptr<const Link> getRoot(void) const{
58  return this->root_link_;
59  };
60 
64  std::shared_ptr<const Link> getLink(const std::string& name) const {
65  std::shared_ptr<const Link> ptr;
66  if (this->links_.find(name) == this->links_.end())
67  ptr.reset();
68  else
69  ptr = this->links_.find(name)->second;
70  return ptr;
71  };
72 
76  std::shared_ptr<const Joint> getJoint(const std::string& name) const {
77  std::shared_ptr<const Joint> ptr;
78  if (this->joints_.find(name) == this->joints_.end())
79  ptr.reset();
80  else
81  ptr = this->joints_.find(name)->second;
82  return ptr;
83  };
84 
88  const std::string& getName() const {
89  return name_;
90  };
91 
95  void getLinks(std::vector<std::shared_ptr<Link> >& links) const {
96  for (std::map<std::string,std::shared_ptr<Link> >::const_iterator link = this->links_.begin();
97  link != this->links_.end(); link++)
98  {
99  links.push_back(link->second);
100  }
101  };
102 
106  void clear() {
107  name_.clear();
108  this->links_.clear();
109  this->joints_.clear();
110  this->materials_.clear();
111  this->root_link_.reset();
112  };
113 
118  void getLink(const std::string& name,std::shared_ptr<Link> &link) const {
119  std::shared_ptr<Link> ptr;
120  if (this->links_.find(name) == this->links_.end())
121  ptr.reset();
122  else
123  ptr = this->links_.find(name)->second;
124  link = ptr;
125  };
126 
131  std::shared_ptr<Material> getMaterial(const std::string& name) const {
132  std::shared_ptr<Material> ptr;
133  if (this->materials_.find(name) == this->materials_.end())
134  ptr.reset();
135  else
136  ptr = this->materials_.find(name)->second;
137  return ptr;
138  };
139 
144  void initTree(std::map<std::string, std::string> &parent_link_tree) {
145 
146  for (std::map<std::string,std::shared_ptr<Joint> >::iterator joint = this->joints_.begin();
147  joint != this->joints_.end(); joint++) {
148  std::string parent_link_name = joint->second->parent_link_name;
149  std::string child_link_name = joint->second->child_link_name;
150 
151  if (parent_link_name.empty() || child_link_name.empty())
152  {
153  throw ParseError("Joint [" + joint->second->name + "] is missing a parent and/or child link specification.");
154  }
155  else
156  {
157  // find child and parent links
158  std::shared_ptr<Link> child_link, parent_link;
159  this->getLink(child_link_name, child_link);
160  if (!child_link)
161  {
162  throw ParseError("child link [" + child_link_name + "] of joint [" + joint->first + "] not found");
163  }
164  this->getLink(parent_link_name, parent_link);
165  if (!parent_link)
166  {
167  throw ParseError("parent link [" + parent_link_name + "] of joint [" + joint->first + "] not found. This is not valid according to the URDF spec. Every link you refer to from a joint needs to be explicitly defined in the robot description. To fix this problem you can either remove this joint [" + joint->first + "] from your urdf file, or add \"<link name=\"" + parent_link_name + "\" />\" to your urdf file.");
168  }
169 
170  //set parent link for child link
171  child_link->setParent(parent_link);
172 
173  //set parent joint for child link
174  child_link->parent_joint = joint->second;
175 
176  //set child joint for parent link
177  parent_link->child_joints.push_back(joint->second);
178 
179  //set child link for parent link
180  parent_link->child_links.push_back(child_link);
181 
182  // fill in child/parent string map
183  parent_link_tree[child_link->name] = parent_link_name;
184  }
185  }
186  }
187 
191  void initRoot(const std::map<std::string, std::string> &parent_link_tree) {
192  this->root_link_.reset();
193 
194  // find the links that have no parent in the tree
195  for (std::map<std::string, std::shared_ptr<Link> >::const_iterator l=this->links_.begin(); l!=this->links_.end(); l++)
196  {
197  std::map<std::string, std::string >::const_iterator parent = parent_link_tree.find(l->first);
198  if (parent == parent_link_tree.end())
199  {
200  // store root link
201  if (!this->root_link_)
202  {
203  getLink(l->first, this->root_link_);
204  }
205  // we already found a root link
206  else
207  {
208  throw ParseError("Two root links found: [" + this->root_link_->name + "] and [" + l->first + "]");
209  }
210  }
211  }
212  if (!this->root_link_)
213  {
214  throw ParseError("No root link found. The robot xml is not a valid tree.");
215  }
216  }
217 
218 
220  std::map<std::string, std::shared_ptr<Link> > links_;
222  std::map<std::string, std::shared_ptr<Joint> > joints_;
224  std::map<std::string, std::shared_ptr<Material> > materials_;
225 
226  std::string name_;
227 
232  std::shared_ptr<Link> root_link_;
233 
234  };
235 
236 }
237 
238 #endif
Definition: urdf_model.h:50
std::shared_ptr< Link > root_link_
Definition: urdf_model.h:232
std::shared_ptr< const Link > getLink(const std::string &name) const
Definition: urdf_model.h:64
std::map< std::string, std::shared_ptr< Material > > materials_
complete list of Materials
Definition: urdf_model.h:224
void clear()
Definition: urdf_model.h:106
std::shared_ptr< const Joint > getJoint(const std::string &name) const
Definition: urdf_model.h:76
Definition: urdf_exception.h:11
void getLink(const std::string &name, std::shared_ptr< Link > &link) const
non-const getLink()
Definition: urdf_model.h:118
void getLinks(std::vector< std::shared_ptr< Link > > &links) const
Definition: urdf_model.h:95
std::shared_ptr< Material > getMaterial(const std::string &name) const
non-const getMaterial()
Definition: urdf_model.h:131
Definition: urdf_color.h:46
std::shared_ptr< const Link > getRoot(void) const
Definition: urdf_model.h:57
std::map< std::string, std::shared_ptr< Joint > > joints_
complete list of Joints
Definition: urdf_model.h:222
void initTree(std::map< std::string, std::string > &parent_link_tree)
Loop through all joints, for every link, assign children links and children joints.
Definition: urdf_model.h:144
std::map< std::string, std::shared_ptr< Link > > links_
complete list of Links
Definition: urdf_model.h:220
const std::string & getName() const
Definition: urdf_model.h:88
void initRoot(const std::map< std::string, std::string > &parent_link_tree)
Definition: urdf_model.h:191