All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
parser_urdf.hh
Go to the documentation of this file.
1 /*
2  * Copyright 2012 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef URDF2GAZEBO_HH
18 #define URDF2GAZEBO_HH
19 
20 #include <urdf_model/model.h>
21 #include <urdf_model/link.h>
22 #include <tinyxml.h>
23 
24 #include <cstdio>
25 #include <cstdlib>
26 #include <cmath>
27 #include <vector>
28 #include <string>
29 #include <sstream>
30 #include <map>
31 
32 #include "ode/mass.h"
33 #include "ode/rotation.h"
34 
35 #include "gazebo/math/Pose.hh"
36 #include "gazebo/common/Console.hh"
37 
40 namespace urdf2gazebo
41 {
44 
45  typedef boost::shared_ptr<urdf::Collision> UrdfCollisionPtr;
46  typedef boost::shared_ptr<urdf::Visual> UrdfVisualPtr;
47  typedef boost::shared_ptr<urdf::Link> UrdfLinkPtr;
48  typedef boost::shared_ptr<const urdf::Link> ConstUrdfLinkPtr;
49 
52  {
53  private: GazeboExtension()
54  {
55  material.clear();
56  setStaticFlag = false;
57  gravity = true;
58  isDampingFactor = false;
59  isMaxVel = false;
60  isMinDepth = false;
61  fdir1.clear();
62  isMu1 = false;
63  isMu2 = false;
64  isKp = false;
65  isKd = false;
66  selfCollide = false;
67  isLaserRetro = false;
68  isStopCfm = false;
69  isStopErp = false;
70  isInitialJointPosition = false;
71  isFudgeFactor = false;
72  provideFeedback = false;
73  blobs.clear();
74 
75  dampingFactor = 0;
76  maxVel = 0;
77  minDepth = 0;
78  mu1 = 0;
79  mu2 = 0;
80  kp = 100000000;
81  kd = 1;
82  laserRetro = 101;
83  stopCfm = 0;
84  stopErp = 0.1;
85  initialJointPosition = 0;
86  fudgeFactor = 1;
87  };
88 
89  private: GazeboExtension(const GazeboExtension &ge)
90  {
91  material = ge.material;
92  setStaticFlag = ge.setStaticFlag;
93  gravity = ge.gravity;
94  isDampingFactor = ge.isDampingFactor;
95  isMaxVel = ge.isMaxVel;
96  isMinDepth = ge.isMinDepth;
97  fdir1 = ge.fdir1;
98  isMu1 = ge.isMu1;
99  isMu2 = ge.isMu2;
100  isKp = ge.isKp;
101  isKd = ge.isKd;
102  selfCollide = ge.selfCollide;
103  isLaserRetro = ge.isLaserRetro;
104  isStopCfm = ge.isStopCfm;
105  isStopErp = ge.isStopErp;
106  isInitialJointPosition = ge.isInitialJointPosition;
107  isFudgeFactor = ge.isFudgeFactor;
108  provideFeedback = ge.provideFeedback;
109  oldLinkName = ge.oldLinkName;
110  reductionTransform = ge.reductionTransform;
111  blobs = ge.blobs;
112 
113  dampingFactor = ge.dampingFactor;
114  maxVel = ge.maxVel;
115  minDepth = ge.minDepth;
116  mu1 = ge.mu1;
117  mu2 = ge.mu2;
118  kp = ge.kp;
119  kd = ge.kd;
120  laserRetro = ge.laserRetro;
121  stopCfm = ge.stopCfm;
122  stopErp = ge.stopErp;
123  initialJointPosition = ge.initialJointPosition;
124  fudgeFactor = ge.fudgeFactor;
125  };
126 
127  // for reducing fixed joints and removing links
128  private: std::string oldLinkName;
129  private: gazebo::math::Pose reductionTransform;
130 
131  // visual
132  private: std::string material;
133 
134  // body, default off
135  private: bool setStaticFlag;
136  private: bool gravity;
137  private: bool isDampingFactor;
138  private: double dampingFactor;
139  private: bool isMaxVel;
140  private: double maxVel;
141  private: bool isMinDepth;
142  private: double minDepth;
143  private: bool selfCollide;
144 
145  // geom, contact dynamics
146  private: bool isMu1, isMu2, isKp, isKd;
147  private: double mu1, mu2, kp, kd;
148  private: std::string fdir1;
149  private: bool isLaserRetro;
150  private: double laserRetro;
151 
152  // joint, joint limit dynamics
153  private: bool isStopCfm, isStopErp, isInitialJointPosition, isFudgeFactor;
154  private: double stopCfm, stopErp, initialJointPosition, fudgeFactor;
155  private: bool provideFeedback;
156 
157  // blobs into body or robot
158  private: std::vector<TiXmlElement*> blobs;
159 
160  friend class URDF2Gazebo;
161  };
162 
164  {
166  public: URDF2Gazebo();
167 
169  public: ~URDF2Gazebo();
170 
174  public: TiXmlDocument InitModelDoc(TiXmlDocument* _xmlDoc);
175 
179  public: TiXmlDocument InitModelFile(const std::string &_filename);
180 
186  public: TiXmlDocument InitModelString(const std::string &_urdfStr,
187  bool _enforceLimits = true);
188 
193  private: urdf::Vector3 ParseVector3(TiXmlNode* _key, double _scale = 1.0);
194 
199  private: std::string Values2str(unsigned int _count, const double *_values);
200 
204  private: std::string Vector32Str(const urdf::Vector3 _vector);
205 
210  private: void AddKeyValue(TiXmlElement *_elem, const std::string &_key,
211  const std::string &_value);
212 
214  private: void AddTransform(TiXmlElement *_elem,
215  const::gazebo::math::Pose& _transform);
216 
218  private: void PrintMass(UrdfLinkPtr _link);
219 
221  private: void PrintMass(const std::string &_linkName, dMass _mass);
222 
224  private: std::string GetKeyValueAsString(TiXmlElement* _elem);
225 
228  private: void ParseGazeboExtension(TiXmlDocument &_urdfXml);
229 
231  private: void InsertGazeboExtensionCollision(TiXmlElement *_elem,
232  const std::string &_linkName);
233 
235  private: void InsertGazeboExtensionVisual(TiXmlElement *_elem,
236  const std::string &_linkName);
237 
239  private: void InsertGazeboExtensionLink(TiXmlElement *_elem,
240  const std::string &_linkName);
241 
243  private: void InsertGazeboExtensionJoint(TiXmlElement *_elem,
244  const std::string &_jointName);
245 
247  private: void InsertGazeboExtensionRobot(TiXmlElement *_elem);
248 
250  private: void ListGazeboExtensions();
251 
253  private: void ListGazeboExtensions(const std::string &_reference);
254 
256  // collision elements of the child link into the parent link
257  private: void ReduceFixedJoints(TiXmlElement *_root, UrdfLinkPtr _link);
258 
260  private: void ReduceInertialToParent(UrdfLinkPtr _link);
261 
263  private: void ReduceVisualsToParent(UrdfLinkPtr _link);
264 
266  private: void ReduceCollisionsToParent(UrdfLinkPtr _link);
267 
269  private: void ReduceJointsToParent(UrdfLinkPtr _link);
270 
272  private: void ReduceVisualToParent(UrdfLinkPtr _link,
273  const std::string &_groupName,
274  UrdfVisualPtr _visual);
275 
277  private: void ReduceCollisionToParent(UrdfLinkPtr _link,
278  const std::string &_groupName,
279  UrdfCollisionPtr _collision);
280 
291  private: void ReduceGazeboExtensionToParent(UrdfLinkPtr _link);
292 
295  private: void ReduceGazeboExtensionFrameReplace(GazeboExtension* _ge,
296  UrdfLinkPtr _link);
297 
300  private: void ReduceGazeboExtensionPluginFrameReplace(
301  std::vector<TiXmlElement*>::iterator _blobIt,
302  UrdfLinkPtr _link, const std::string &_pluginName,
303  const std::string &_elementName, gazebo::math::Pose _reductionTransform);
304 
307  private: void ReduceGazeboExtensionProjectorFrameReplace(
308  std::vector<TiXmlElement*>::iterator _blobIt,
309  UrdfLinkPtr _link);
310 
313  private: void ReduceGazeboExtensionGripperFrameReplace(
314  std::vector<TiXmlElement*>::iterator _blobIt,
315  UrdfLinkPtr _link);
316 
319  private: void ReduceGazeboExtensionJointFrameReplace(
320  std::vector<TiXmlElement*>::iterator _blobIt,
321  UrdfLinkPtr _link);
322 
325  private: void ReduceGazeboExtensionContactSensorFrameReplace(
326  std::vector<TiXmlElement*>::iterator _blobIt,
327  UrdfLinkPtr _link);
328 
331  private: void ReduceGazeboExtensionsTransform(GazeboExtension* _ge);
332 
335  private: void ReduceGazeboExtensionSensorTransformReduction(
336  std::vector<TiXmlElement*>::iterator _blobIt,
337  gazebo::math::Pose _reductionTransform);
338 
341  private: void ReduceGazeboExtensionProjectorTransformReduction(
342  std::vector<TiXmlElement*>::iterator _blobIt,
343  gazebo::math::Pose _reductionTransform);
344 
346  private: urdf::Pose TransformToParentFrame(
347  urdf::Pose _transformInLinkFrame,
348  urdf::Pose _parentToLinkTransform);
350  private: gazebo::math::Pose TransformToParentFrame(
351  gazebo::math::Pose _transformInLinkFrame,
352  urdf::Pose _parentToLinkTransform);
354  private: gazebo::math::Pose TransformToParentFrame(
355  gazebo::math::Pose _transformInLinkFrame,
356  gazebo::math::Pose _parentToLinkTransform);
358  private: gazebo::math::Pose inverseTransformToParentFrame(
359  gazebo::math::Pose _transformInLinkFrame,
360  urdf::Pose _parentToLinkTransform);
363  private: gazebo::math::Pose CopyPose(urdf::Pose _pose);
366  private: urdf::Pose CopyPose(gazebo::math::Pose _pose);
367 
368  private: std::string GetGeometryBoundingBox(
369  boost::shared_ptr<urdf::Geometry> _geometry, double *_sizeVals);
370 
371 
373  private: void PrintCollisionGroups(UrdfLinkPtr _link);
374 
376  private: void CreateSDF(TiXmlElement *_root,
377  ConstUrdfLinkPtr _link,
378  const gazebo::math::Pose &_transform);
379 
381  private: void CreateLink(TiXmlElement *_root,
382  ConstUrdfLinkPtr _link,
383  gazebo::math::Pose &_currentTransform);
384 
386  private: void CreateCollisions(TiXmlElement* _elem,
387  ConstUrdfLinkPtr _link);
388 
390  private: void CreateVisuals(TiXmlElement* _elem,
391  ConstUrdfLinkPtr _link);
392 
394  private: void CreateInertial(TiXmlElement *_elem,
395  ConstUrdfLinkPtr _link);
396 
398  private: void CreateCollision(TiXmlElement* _elem,
399  ConstUrdfLinkPtr _link,
400  UrdfCollisionPtr _collision,
401  const std::string &_oldLinkName = std::string(""));
402 
404  private: void CreateVisual(TiXmlElement *_elem,
405  ConstUrdfLinkPtr _link,
406  UrdfVisualPtr _visual,
407  const std::string &_oldLinkName = std::string(""));
408 
410  private: void CreateJoint(TiXmlElement *_root,
411  ConstUrdfLinkPtr _link,
412  gazebo::math::Pose &_currentTransform);
413 
415  private: void CreateGeometry(TiXmlElement* _elem,
416  boost::shared_ptr<urdf::Geometry> _geometry);
417 
418  private: std::map<std::string, std::vector<GazeboExtension*> > extensions;
419 
420  private: bool enforceLimits;
421  private: bool reduceFixedJoints;
422  };
424 }
425 
426 #endif