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  cfmDamping = false;
74  blobs.clear();
75 
76  dampingFactor = 0;
77  maxVel = 0;
78  minDepth = 0;
79  mu1 = 0;
80  mu2 = 0;
81  kp = 100000000;
82  kd = 1;
83  laserRetro = 101;
84  stopCfm = 0;
85  stopErp = 0.1;
86  initialJointPosition = 0;
87  fudgeFactor = 1;
88  };
89 
90  private: GazeboExtension(const GazeboExtension &ge)
91  : material(ge.material), fdir1(ge.fdir1),
92  oldLinkName(ge.oldLinkName),
93  reductionTransform(ge.reductionTransform),
94  blobs(ge.blobs)
95  {
96  setStaticFlag = ge.setStaticFlag;
97  gravity = ge.gravity;
98  isDampingFactor = ge.isDampingFactor;
99  isMaxVel = ge.isMaxVel;
100  isMinDepth = ge.isMinDepth;
101  isMu1 = ge.isMu1;
102  isMu2 = ge.isMu2;
103  isKp = ge.isKp;
104  isKd = ge.isKd;
105  selfCollide = ge.selfCollide;
106  isLaserRetro = ge.isLaserRetro;
107  isStopCfm = ge.isStopCfm;
108  isStopErp = ge.isStopErp;
109  isInitialJointPosition = ge.isInitialJointPosition;
110  isFudgeFactor = ge.isFudgeFactor;
111  provideFeedback = ge.provideFeedback;
112  cfmDamping = ge.cfmDamping;
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  // visual
128  private: std::string material;
129 
130  private: std::string fdir1;
131 
132  // for reducing fixed joints and removing links
133  private: std::string oldLinkName;
134  private: gazebo::math::Pose reductionTransform;
135 
136  // blobs into body or robot
137  private: std::vector<TiXmlElement*> blobs;
138 
139  // body, default off
140  private: bool setStaticFlag;
141  private: bool gravity;
142  private: bool isDampingFactor;
143  private: double dampingFactor;
144  private: bool isMaxVel;
145  private: double maxVel;
146  private: bool isMinDepth;
147  private: double minDepth;
148  private: bool selfCollide;
149 
150  // geom, contact dynamics
151  private: bool isMu1, isMu2, isKp, isKd;
152  private: double mu1, mu2, kp, kd;
153  private: bool isLaserRetro;
154  private: double laserRetro;
155 
156  // joint, joint limit dynamics
157  private: bool isStopCfm, isStopErp, isInitialJointPosition, isFudgeFactor;
158  private: double stopCfm, stopErp, initialJointPosition, fudgeFactor;
159  private: bool provideFeedback;
160  private: bool cfmDamping;
161 
162  friend class URDF2Gazebo;
163  };
164 
166  {
168  public: URDF2Gazebo();
169 
171  public: ~URDF2Gazebo();
172 
176  public: TiXmlDocument InitModelDoc(TiXmlDocument* _xmlDoc);
177 
181  public: TiXmlDocument InitModelFile(const std::string &_filename);
182 
188  public: TiXmlDocument InitModelString(const std::string &_urdfStr,
189  bool _enforceLimits = true);
190 
195  private: urdf::Vector3 ParseVector3(TiXmlNode* _key, double _scale = 1.0);
196 
201  private: std::string Values2str(unsigned int _count, const double *_values);
202 
206  private: std::string Vector32Str(const urdf::Vector3 _vector);
207 
212  private: void AddKeyValue(TiXmlElement *_elem, const std::string &_key,
213  const std::string &_value);
214 
216  private: void AddTransform(TiXmlElement *_elem,
217  const::gazebo::math::Pose& _transform);
218 
220  private: void PrintMass(UrdfLinkPtr _link);
221 
223  private: void PrintMass(const std::string &_linkName, dMass _mass);
224 
226  private: std::string GetKeyValueAsString(TiXmlElement* _elem);
227 
230  private: void ParseGazeboExtension(TiXmlDocument &_urdfXml);
231 
233  private: void InsertGazeboExtensionCollision(TiXmlElement *_elem,
234  const std::string &_linkName);
235 
237  private: void InsertGazeboExtensionVisual(TiXmlElement *_elem,
238  const std::string &_linkName);
239 
241  private: void InsertGazeboExtensionLink(TiXmlElement *_elem,
242  const std::string &_linkName);
243 
245  private: void InsertGazeboExtensionJoint(TiXmlElement *_elem,
246  const std::string &_jointName);
247 
249  private: void InsertGazeboExtensionRobot(TiXmlElement *_elem);
250 
252  private: void ListGazeboExtensions();
253 
255  private: void ListGazeboExtensions(const std::string &_reference);
256 
258  // collision elements of the child link into the parent link
259  private: void ReduceFixedJoints(TiXmlElement *_root, UrdfLinkPtr _link);
260 
262  private: void ReduceInertialToParent(UrdfLinkPtr _link);
263 
265  private: void ReduceVisualsToParent(UrdfLinkPtr _link);
266 
268  private: void ReduceCollisionsToParent(UrdfLinkPtr _link);
269 
271  private: void ReduceJointsToParent(UrdfLinkPtr _link);
272 
274  private: void ReduceVisualToParent(UrdfLinkPtr _link,
275  const std::string &_groupName,
276  UrdfVisualPtr _visual);
277 
279  private: void ReduceCollisionToParent(UrdfLinkPtr _link,
280  const std::string &_groupName,
281  UrdfCollisionPtr _collision);
282 
293  private: void ReduceGazeboExtensionToParent(UrdfLinkPtr _link);
294 
297  private: void ReduceGazeboExtensionFrameReplace(GazeboExtension* _ge,
298  UrdfLinkPtr _link);
299 
302  private: void ReduceGazeboExtensionPluginFrameReplace(
303  std::vector<TiXmlElement*>::iterator _blobIt,
304  UrdfLinkPtr _link, const std::string &_pluginName,
305  const std::string &_elementName, gazebo::math::Pose _reductionTransform);
306 
309  private: void ReduceGazeboExtensionProjectorFrameReplace(
310  std::vector<TiXmlElement*>::iterator _blobIt,
311  UrdfLinkPtr _link);
312 
315  private: void ReduceGazeboExtensionGripperFrameReplace(
316  std::vector<TiXmlElement*>::iterator _blobIt,
317  UrdfLinkPtr _link);
318 
321  private: void ReduceGazeboExtensionJointFrameReplace(
322  std::vector<TiXmlElement*>::iterator _blobIt,
323  UrdfLinkPtr _link);
324 
327  private: void ReduceGazeboExtensionContactSensorFrameReplace(
328  std::vector<TiXmlElement*>::iterator _blobIt,
329  UrdfLinkPtr _link);
330 
333  private: void ReduceGazeboExtensionsTransform(GazeboExtension* _ge);
334 
337  private: void ReduceGazeboExtensionSensorTransformReduction(
338  std::vector<TiXmlElement*>::iterator _blobIt,
339  gazebo::math::Pose _reductionTransform);
340 
343  private: void ReduceGazeboExtensionProjectorTransformReduction(
344  std::vector<TiXmlElement*>::iterator _blobIt,
345  gazebo::math::Pose _reductionTransform);
346 
348  private: urdf::Pose TransformToParentFrame(
349  urdf::Pose _transformInLinkFrame,
350  urdf::Pose _parentToLinkTransform);
352  private: gazebo::math::Pose TransformToParentFrame(
353  gazebo::math::Pose _transformInLinkFrame,
354  urdf::Pose _parentToLinkTransform);
356  private: gazebo::math::Pose TransformToParentFrame(
357  gazebo::math::Pose _transformInLinkFrame,
358  gazebo::math::Pose _parentToLinkTransform);
360  private: gazebo::math::Pose inverseTransformToParentFrame(
361  gazebo::math::Pose _transformInLinkFrame,
362  urdf::Pose _parentToLinkTransform);
365  private: gazebo::math::Pose CopyPose(urdf::Pose _pose);
368  private: urdf::Pose CopyPose(gazebo::math::Pose _pose);
369 
370  private: std::string GetGeometryBoundingBox(
371  boost::shared_ptr<urdf::Geometry> _geometry, double *_sizeVals);
372 
373 
375  private: void PrintCollisionGroups(UrdfLinkPtr _link);
376 
378  private: void CreateSDF(TiXmlElement *_root,
379  ConstUrdfLinkPtr _link,
380  const gazebo::math::Pose &_transform);
381 
383  private: void CreateLink(TiXmlElement *_root,
384  ConstUrdfLinkPtr _link,
385  gazebo::math::Pose &_currentTransform);
386 
388  private: void CreateCollisions(TiXmlElement* _elem,
389  ConstUrdfLinkPtr _link);
390 
392  private: void CreateVisuals(TiXmlElement* _elem,
393  ConstUrdfLinkPtr _link);
394 
396  private: void CreateInertial(TiXmlElement *_elem,
397  ConstUrdfLinkPtr _link);
398 
400  private: void CreateCollision(TiXmlElement* _elem,
401  ConstUrdfLinkPtr _link,
402  UrdfCollisionPtr _collision,
403  const std::string &_oldLinkName = std::string(""));
404 
406  private: void CreateVisual(TiXmlElement *_elem,
407  ConstUrdfLinkPtr _link,
408  UrdfVisualPtr _visual,
409  const std::string &_oldLinkName = std::string(""));
410 
412  private: void CreateJoint(TiXmlElement *_root,
413  ConstUrdfLinkPtr _link,
414  gazebo::math::Pose &_currentTransform);
415 
417  private: void CreateGeometry(TiXmlElement* _elem,
418  boost::shared_ptr<urdf::Geometry> _geometry);
419 
420  private: std::map<std::string, std::vector<GazeboExtension*> > extensions;
421 
422  private: bool enforceLimits;
423  private: bool reduceFixedJoints;
424  };
426 }
427 
428 #endif