forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sdf_helpers.cc
395 lines (351 loc) · 15.9 KB
/
sdf_helpers.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include "drake/examples/double_pendulum/sdf_helpers.h"
#include <map>
#include <memory>
#include <string>
#include <utility>
#include "sdf/sdf.hh"
#include "drake/common/drake_assert.h"
#include "drake/common/drake_throw.h"
#include "drake/common/eigen_types.h"
#include "drake/multibody/joints/fixed_joint.h"
#include "drake/multibody/joints/revolute_joint.h"
#include "drake/multibody/parsing/frame_cache.h"
#include "drake/multibody/rigid_body.h"
#include "drake/multibody/rigid_body_tree.h"
#include "drake/util/drakeGeometryUtil.h"
namespace drake {
namespace examples {
namespace double_pendulum {
using multibody::parsing::FrameCache;
// RigidBodyTree model instance descriptor. Helpful
// to carry model name and instance id around.
struct ModelInstance {
std::string name;
int id;
};
// Helper function to express an ignition::math::Vector3d instance as
// a Vector3<double> instance.
Vector3<double> ToVector(const ignition::math::Vector3d& vector) {
return Vector3<double>(vector.X(), vector.Y(), vector.Z());
}
// Helper function to express an ignition::math::Pose3d instance as
// an Isometry3<double> instance.
Isometry3<double> ToIsometry(const ignition::math::Pose3d& pose) {
const Isometry3<double>::TranslationType translation(ToVector(pose.Pos()));
const Quaternion<double> rotation(pose.Rot().W(), pose.Rot().X(),
pose.Rot().Y(), pose.Rot().Z());
return translation * rotation;
}
// Parses a pose from the given SDF element.
Isometry3<double> ParsePose(sdf::ElementPtr sdf_pose_element) {
DRAKE_DEMAND(sdf_pose_element != nullptr);
DRAKE_DEMAND(sdf_pose_element->GetName() == "pose");
return ToIsometry(sdf_pose_element->Get<ignition::math::Pose3d>());
}
// Parses a geometry from the given SDF element and adds a
// DrakeShapes::Geometry instance to the given element.
void ParseGeometry(sdf::ElementPtr sdf_geometry_element,
DrakeShapes::Element* element) {
DRAKE_DEMAND(sdf_geometry_element != nullptr);
DRAKE_DEMAND(sdf_geometry_element->GetName() == "geometry");
DRAKE_DEMAND(element != nullptr);
sdf::ElementPtr sdf_shape_element;
if (sdf_geometry_element->HasElement("cylinder")) {
sdf_shape_element = sdf_geometry_element->GetElement("cylinder");
sdf::ElementPtr sdf_radius_element =
sdf_shape_element->GetElement("radius");
sdf::ElementPtr sdf_length_element =
sdf_shape_element->GetElement("length");
element->setGeometry(DrakeShapes::Cylinder(
sdf_radius_element->Get<double>(), sdf_length_element->Get<double>()));
return;
}
if (sdf_geometry_element->HasElement("box")) {
sdf_shape_element = sdf_geometry_element->GetElement("box");
sdf::ElementPtr sdf_size_element = sdf_shape_element->GetElement("size");
const Vector3<double> xyz = ToVector(
sdf_size_element->Get<ignition::math::Vector3d>());
element->setGeometry(DrakeShapes::Box(xyz));
return;
}
// TODO(hidmic): Support mesh and sphere geometries.
DRAKE_ABORT_MSG("Unsupported geometry!");
}
// Parses a visual geometry from the given SDF element and adds a
// DrakeShapes::VisualElement instance to the given body.
void ParseVisual(sdf::ElementPtr sdf_visual_element, RigidBody<double>* body) {
DRAKE_DEMAND(sdf_visual_element != nullptr);
DRAKE_DEMAND(sdf_visual_element->GetName() == "visual");
DRAKE_DEMAND(body != nullptr);
const auto visual_name = sdf_visual_element->Get<std::string>("name");
// Visual element frame's (E) pose in body frame (B).
auto X_BE = Isometry3<double>::Identity();
if (sdf_visual_element->HasElement("pose")) {
sdf::ElementPtr sdf_pose_element = sdf_visual_element->GetElement("pose");
X_BE = ParsePose(sdf_pose_element);
}
DrakeShapes::VisualElement element(X_BE);
sdf::ElementPtr sdf_geometry_element =
sdf_visual_element->GetElement("geometry");
ParseGeometry(sdf_geometry_element, &element);
body->AddVisualElement(element);
}
// Parses a collision geometry from the given SDF element and adds a
// drake::multibody::collision::Element instance to the given body through the
// given tree.
void ParseCollision(sdf::ElementPtr sdf_collision_element,
RigidBody<double>* body,
RigidBodyTree<double>* tree) {
DRAKE_DEMAND(sdf_collision_element != nullptr);
DRAKE_DEMAND(sdf_collision_element->GetName() == "collision");
DRAKE_DEMAND(body != nullptr);
DRAKE_DEMAND(tree != nullptr);
const auto collision_name = sdf_collision_element->Get<std::string>("name");
// Collision element's frame (E) pose in body frame (B).
auto X_BE = Isometry3<double>::Identity();
if (sdf_collision_element->HasElement("pose")) {
sdf::ElementPtr sdf_pose_element =
sdf_collision_element->GetElement("pose");
X_BE = ParsePose(sdf_pose_element);
}
drake::multibody::collision::Element element(X_BE, body);
sdf::ElementPtr sdf_geometry_element =
sdf_collision_element->GetElement("geometry");
ParseGeometry(sdf_geometry_element, &element);
// Collision elements are added to the rigid body
// tree, which then updates the corresponding rigid body.
tree->addCollisionElement(element, *body, "default");
}
// Parses inertial information (mass, COM) from the given SDF
// element and updates the given body accordingly.
// TODO(hidmic): Parse inertial tensor as well.
void ParseInertial(sdf::ElementPtr sdf_inertial_element,
RigidBody<double>* body) {
DRAKE_DEMAND(sdf_inertial_element != nullptr);
DRAKE_DEMAND(sdf_inertial_element->GetName() == "inertial");
DRAKE_DEMAND(body != nullptr);
// Inertial frame's (I) pose in body frame (B).
auto X_BI = Isometry3<double>::Identity();
if (sdf_inertial_element->HasElement("pose")) {
sdf::ElementPtr sdf_pose_element = sdf_inertial_element->GetElement("pose");
X_BI = ParsePose(sdf_pose_element);
}
// Center of mass p_BBcm measured and expressed in the body frame B,
// as it is at the origin of the inertial frame (I).
body->set_center_of_mass(X_BI.translation());
if (sdf_inertial_element->HasElement("mass")) {
sdf::ElementPtr sdf_mass_element = sdf_inertial_element->GetElement("mass");
body->set_mass(sdf_mass_element->Get<double>());
}
// Define inertia tensor using the link mass on the link's frame,
// and then transform it to the specified link's inertial frame.
SquareTwistMatrix<double> itensor = SquareTwistMatrix<double>::Zero();
itensor.block(3, 3, 3, 3) << body->get_mass() * Matrix3<double>::Identity();
body->set_spatial_inertia(transformSpatialInertia(X_BI, itensor));
}
// Parses inertial, visual and collision details of a body, if any, from the
// given SDF element and adds a RigidBody instance to the given tree as part
// of the given model instance. The frame cache is updated with the body frame's
// pose (B) in its model frame (D).
void ParseLink(sdf::ElementPtr sdf_link_element,
const ModelInstance& instance,
RigidBodyTree<double>* tree,
FrameCache<double>* frame_cache) {
DRAKE_DEMAND(sdf_link_element != nullptr);
DRAKE_DEMAND(sdf_link_element->GetName() == "link");
DRAKE_DEMAND(tree != nullptr);
DRAKE_DEMAND(frame_cache != nullptr);
auto body = std::make_unique<RigidBody<double>>();
const auto link_name = sdf_link_element->Get<std::string>("name");
body->set_name(link_name);
body->set_model_name(instance.name);
body->set_model_instance_id(instance.id);
// Body frame's (B) pose in model frame (D).
auto X_DB = Isometry3<double>::Identity();
if (sdf_link_element->HasElement("pose")) {
sdf::ElementPtr sdf_pose_element = sdf_link_element->GetElement("pose");
X_DB = ParsePose(sdf_pose_element);
}
frame_cache->Update(instance.name, link_name, X_DB);
if (sdf_link_element->HasElement("inertial")) {
sdf::ElementPtr sdf_inertial_element =
sdf_link_element->GetElement("inertial");
ParseInertial(sdf_inertial_element, body.get());
}
if (sdf_link_element->HasElement("visual")) {
sdf::ElementPtr sdf_visual_element = sdf_link_element->GetElement("visual");
while (sdf_visual_element != nullptr) {
ParseVisual(sdf_visual_element, body.get());
sdf_visual_element = sdf_visual_element->GetNextElement("visual");
}
}
if (sdf_link_element->HasElement("collision")) {
sdf::ElementPtr sdf_collision_element =
sdf_link_element->GetElement("collision");
while (sdf_collision_element != nullptr) {
ParseCollision(sdf_collision_element, body.get(), tree);
sdf_collision_element =
sdf_collision_element->GetNextElement("collision");
}
}
tree->add_rigid_body(std::move(body));
}
// Parses a joint type from the given SDF element and instantiates the
// corresponding DrakeJoint instance. It is assumed that the parent body
// frame is already present in the frame cache.
std::unique_ptr<DrakeJoint>
ParseJointType(sdf::ElementPtr sdf_joint_element,
const ModelInstance& instance,
const RigidBody<double>& parent_body,
const FrameCache<double>& frame_cache) {
DRAKE_DEMAND(sdf_joint_element != nullptr);
DRAKE_DEMAND(sdf_joint_element->GetName() == "joint");
const auto joint_name = sdf_joint_element->Get<std::string>("name");
const auto joint_type = sdf_joint_element->Get<std::string>("type");
if (joint_type == "revolute") {
sdf::ElementPtr sdf_axis_element = sdf_joint_element->GetElement("axis");
sdf::ElementPtr sdf_xyz_element = sdf_axis_element->GetElement("xyz");
Vector3<double> axis_of_rotation = ToVector(
sdf_xyz_element->Get<ignition::math::Vector3d>());
if (sdf_joint_element->HasElement("use_parent_model_frame")) {
sdf::ElementPtr sdf_use_parent_frame_element =
sdf_joint_element->GetElement("use_parent_model_frame");
if (sdf_use_parent_frame_element->Get<bool>()) {
// Axis of rotation is defined in the model frame (D).
// Joint frame's (M) pose in model frame (D).
const Isometry3<double> X_DM =
frame_cache.Transform(instance.name, joint_name);
// Axis of rotation must be rotated back to the joint
// frame (M), so the inverse of the rotational part of the
// pose of the joint frame (M) in the model frame (D)
// is applied.
axis_of_rotation = X_DM.linear().inverse() * axis_of_rotation;
}
}
// To instantiate the joint we require the joint location as seen from
// the parent body frame. That is, the joint frame's (F) pose in the parent
// body frame (P).
const Isometry3<double> X_PF =
frame_cache.Transform(parent_body.get_name(), joint_name);
auto joint = std::make_unique<RevoluteJoint>(
joint_name, X_PF, axis_of_rotation);
return std::move(joint);
}
// TODO(hidmic): Support prismatic and fixed joints.
DRAKE_ABORT_MSG("Unsupported joint type!");
}
// Parses a joint from the given SDF element and adds a DrakeJoint instance
// to link the parent and child bodies in the tree.
// It is assumed that all body frames are already present in the frame cache,
// which is further updated with the joint frame's (M) pose in the child body
// frame (B).
void ParseJoint(sdf::ElementPtr sdf_joint_element,
const ModelInstance& instance,
RigidBodyTree<double>* tree,
FrameCache<double>* frame_cache) {
DRAKE_DEMAND(sdf_joint_element != nullptr);
DRAKE_DEMAND(sdf_joint_element->GetName() == "joint");
DRAKE_DEMAND(tree != nullptr);
DRAKE_DEMAND(frame_cache != nullptr);
const auto joint_name = sdf_joint_element->Get<std::string>("name");
sdf::ElementPtr sdf_parent_link_element =
sdf_joint_element->GetElement("parent");
const auto parent_link_name = sdf_parent_link_element->Get<std::string>();
RigidBody<double>* parent_body = tree->FindBody(
parent_link_name, instance.name, instance.id);
sdf::ElementPtr sdf_child_link_element =
sdf_joint_element->GetElement("child");
const auto child_link_name = sdf_child_link_element->Get<std::string>();
RigidBody<double>* child_body = tree->FindBody(
child_link_name, instance.name, instance.id);
// Joint frame's (M) pose in child body frame (B).
auto X_BM = Isometry3<double>::Identity();
if (sdf_joint_element->HasElement("pose")) {
sdf::ElementPtr sdf_pose_element = sdf_joint_element->GetElement("pose");
// Joint poses specified in SDF files are, by default, in the child body
// frame (B). See http://sdformat.org/spec?ver=1.4&elem=joint for details.
X_BM = ParsePose(sdf_pose_element);
}
frame_cache->Update(child_link_name, joint_name, X_BM);
// All child link's inertia, visual, and collision
// elements reference frames must be this joint's frame.
child_body->ApplyTransformToJointFrame(X_BM.inverse());
tree->transformCollisionFrame(child_body, X_BM.inverse());
// Update child link's parent and joint.
child_body->setJoint(ParseJointType(
sdf_joint_element, instance, *parent_body, *frame_cache));
child_body->set_parent(parent_body);
}
// Welds all bodies that have no parent to the world for the given
// model instance in the given tree.
void FixDanglingLinks(const ModelInstance& instance,
RigidBodyTree<double>* tree) {
DRAKE_DEMAND(tree != nullptr);
const int& world_index = RigidBodyTreeConstants::kWorldBodyIndex;
const std::string world_name = RigidBodyTreeConstants::kWorldName;
RigidBody<double>* world = tree->bodies[world_index].get();
for (auto& body : tree->bodies) {
// Filter out by model instance id but also make
// sure we're not dealing with the world body.
if (body->get_model_instance_id() == instance.id &&
!body->has_joint() && body->get_name() != world_name) {
// Fix dangling link body to the world body.
auto fixed_joint = std::make_unique<FixedJoint>(
body->get_name() + "_to_world_joint",
Isometry3<double>::Identity());
body->setJoint(std::move(fixed_joint));
body->set_parent(world);
}
}
}
// Parses all bodies and joints of a model from the given SDF element
// and adds RigidBody and DrakeJoint instances to describe such model
// within the given tree.
//
// Returns the model instance id.
int ParseModel(sdf::ElementPtr sdf_model_element, RigidBodyTree<double>* tree) {
DRAKE_DEMAND(sdf_model_element != nullptr);
DRAKE_DEMAND(sdf_model_element->GetName() == "model");
DRAKE_DEMAND(tree != nullptr);
// Define a model instance and a pose tree rooted at the model's frame (D).
const auto model_name = sdf_model_element->Get<std::string>("name");
const int model_id = tree->add_model_instance();
ModelInstance instance{model_name, model_id};
FrameCache<double> frame_cache(model_name);
if (sdf_model_element->HasElement("link")) {
sdf::ElementPtr sdf_link_element = sdf_model_element->GetElement("link");
while (sdf_link_element != nullptr) {
ParseLink(sdf_link_element, instance, tree, &frame_cache);
sdf_link_element = sdf_link_element->GetNextElement("link");
}
}
if (sdf_model_element->HasElement("joint")) {
sdf::ElementPtr sdf_joint_element = sdf_model_element->GetElement("joint");
while (sdf_joint_element != nullptr) {
ParseJoint(sdf_joint_element, instance, tree, &frame_cache);
sdf_joint_element = sdf_joint_element->GetNextElement("joint");
}
}
FixDanglingLinks(instance, tree);
return model_id;
}
int ParseModelFromFile(const std::string& sdf_path,
RigidBodyTree<double>* tree) {
DRAKE_DEMAND(tree != nullptr);
sdf::SDFPtr parsed_sdf(new sdf::SDF());
sdf::init(parsed_sdf);
sdf::readFile(sdf_path, parsed_sdf);
sdf::ElementPtr sdf_element = parsed_sdf->Root();
DRAKE_DEMAND(sdf_element != nullptr);
while (!sdf_element->HasElement("model")) {
// To deal with both world and model SDFs, get next first
// child element and check for model element existence.
sdf_element = sdf_element->GetFirstElement();
DRAKE_DEMAND(sdf_element != nullptr);
}
sdf::ElementPtr sdf_model_element = sdf_element->GetElement("model");
return ParseModel(sdf_model_element, tree);
}
} // namespace double_pendulum
} // namespace examples
} // namespace drake