/
detail_urdf_parser.cc
827 lines (730 loc) · 30.9 KB
/
detail_urdf_parser.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
#include "drake/multibody/parsing/detail_urdf_parser.h"
#include <limits>
#include <map>
#include <memory>
#include <set>
#include <stdexcept>
#include <string>
#include <utility>
#include <Eigen/Dense>
#include <fmt/format.h>
#include <tinyxml2.h>
#include "drake/common/sorted_pair.h"
#include "drake/math/rotation_matrix.h"
#include "drake/multibody/parsing/detail_path_utils.h"
#include "drake/multibody/parsing/detail_tinyxml.h"
#include "drake/multibody/parsing/detail_urdf_geometry.h"
#include "drake/multibody/parsing/package_map.h"
#include "drake/multibody/tree/ball_rpy_joint.h"
#include "drake/multibody/tree/fixed_offset_frame.h"
#include "drake/multibody/tree/planar_joint.h"
#include "drake/multibody/tree/prismatic_joint.h"
#include "drake/multibody/tree/revolute_joint.h"
#include "drake/multibody/tree/universal_joint.h"
#include "drake/multibody/tree/weld_joint.h"
namespace drake {
namespace multibody {
namespace internal {
using Eigen::Matrix3d;
using Eigen::Vector3d;
using Eigen::Vector4d;
using math::RigidTransformd;
using tinyxml2::XMLDocument;
using tinyxml2::XMLElement;
namespace {
const char* kWorldName = "world";
SpatialInertia<double> ExtractSpatialInertiaAboutBoExpressedInB(
XMLElement* node) {
RigidTransformd X_BBi;
XMLElement* origin = node->FirstChildElement("origin");
if (origin) {
X_BBi = OriginAttributesToTransform(origin);
}
double body_mass = 0;
XMLElement* mass = node->FirstChildElement("mass");
if (mass) {
ParseScalarAttribute(mass, "value", &body_mass);
}
double ixx = 0;
double ixy = 0;
double ixz = 0;
double iyy = 0;
double iyz = 0;
double izz = 0;
XMLElement* inertia = node->FirstChildElement("inertia");
if (inertia) {
ParseScalarAttribute(inertia, "ixx", &ixx);
ParseScalarAttribute(inertia, "ixy", &ixy);
ParseScalarAttribute(inertia, "ixz", &ixz);
ParseScalarAttribute(inertia, "iyy", &iyy);
ParseScalarAttribute(inertia, "iyz", &iyz);
ParseScalarAttribute(inertia, "izz", &izz);
}
const RotationalInertia<double> I_BBcm_Bi(ixx, iyy, izz, ixy, ixz, iyz);
// If this is a massless body, return a zero SpatialInertia.
if (body_mass == 0. && I_BBcm_Bi.get_moments().isZero() &&
I_BBcm_Bi.get_products().isZero()) {
return SpatialInertia<double>(body_mass, {0., 0., 0.}, {0., 0., 0});
}
// B and Bi are not necessarily aligned.
const math::RotationMatrix<double> R_BBi(X_BBi.rotation());
// Re-express in frame B as needed.
const RotationalInertia<double> I_BBcm_B = I_BBcm_Bi.ReExpress(R_BBi);
// Bi's origin is at the COM as documented in
// http://wiki.ros.org/urdf/XML/link#Elements
const Vector3d p_BoBcm_B = X_BBi.translation();
return SpatialInertia<double>::MakeFromCentralInertia(
body_mass, p_BoBcm_B, I_BBcm_B);
}
void ParseBody(const multibody::PackageMap& package_map,
const std::string& root_dir,
ModelInstanceIndex model_instance,
XMLElement* node,
MaterialMap* materials,
MultibodyPlant<double>* plant) {
std::string drake_ignore;
if (ParseStringAttribute(node, "drake_ignore", &drake_ignore) &&
drake_ignore == std::string("true")) {
return;
}
std::string body_name;
if (!ParseStringAttribute(node, "name", &body_name)) {
throw std::runtime_error(
"ERROR: link tag is missing name attribute.");
}
if (body_name == kWorldName) {
return;
}
SpatialInertia<double> M_BBo_B;
XMLElement* inertial_node = node->FirstChildElement("inertial");
if (!inertial_node) {
M_BBo_B = SpatialInertia<double>(
0, Vector3d::Zero(), UnitInertia<double>(0, 0, 0));
} else {
M_BBo_B = ExtractSpatialInertiaAboutBoExpressedInB(inertial_node);
}
// Add a rigid body to model each link.
const RigidBody<double>& body =
plant->AddRigidBody(body_name, model_instance, M_BBo_B);
if (plant->geometry_source_is_registered()) {
for (XMLElement* visual_node = node->FirstChildElement("visual");
visual_node;
visual_node = visual_node->NextSiblingElement("visual")) {
geometry::GeometryInstance geometry_instance =
ParseVisual(body_name, package_map, root_dir, visual_node, materials);
// The parsing should *always* produce an IllustrationProperties
// instance, even if it is empty.
DRAKE_DEMAND(geometry_instance.illustration_properties() != nullptr);
plant->RegisterVisualGeometry(
body, geometry_instance.pose(), geometry_instance.shape(),
geometry_instance.name(),
*geometry_instance.illustration_properties());
}
for (XMLElement* collision_node = node->FirstChildElement("collision");
collision_node;
collision_node = collision_node->NextSiblingElement("collision")) {
geometry::GeometryInstance geometry_instance =
ParseCollision(body_name, package_map, root_dir, collision_node);
DRAKE_DEMAND(geometry_instance.proximity_properties());
plant->RegisterCollisionGeometry(
body, geometry_instance.pose(), geometry_instance.shape(),
geometry_instance.name(),
std::move(*geometry_instance.mutable_proximity_properties()));
}
}
}
// Parse the collision filter group tag information into the collision filter
// groups and a set of pairs between which the collisions will be excluded.
// @pre plant.geometry_source_is_registered() is `true`.
void RegisterCollisionFilterGroup(
ModelInstanceIndex model_instance,
const MultibodyPlant<double>& plant, XMLElement* node,
std::map<std::string, geometry::GeometrySet>* collision_filter_groups,
std::set<SortedPair<std::string>>* collision_filter_pairs) {
DRAKE_DEMAND(plant.geometry_source_is_registered());
std::string drake_ignore;
if (ParseStringAttribute(node, "ignore", &drake_ignore) &&
drake_ignore == std::string("true")) {
return;
}
std::string group_name;
if (!ParseStringAttribute(node, "name", &group_name)) {
throw std::runtime_error("ERROR: group tag is missing name attribute.");
}
geometry::GeometrySet collision_filter_geometry_set;
for (XMLElement* member_node = node->FirstChildElement("drake:member");
member_node;
member_node = member_node->NextSiblingElement("drake:member")) {
const char* body_name = member_node->Attribute("link");
if (!body_name) {
throw std::runtime_error(
fmt::format("'{}':'{}':'{}': Collision filter group '{}' provides a "
"member tag without specifying the \"link\" attribute.",
__FILE__, __func__, node->GetLineNum(), group_name));
}
const auto& body = plant.GetBodyByName(body_name, model_instance);
collision_filter_geometry_set.Add(
plant.GetBodyFrameIdOrThrow(body.index()));
}
collision_filter_groups->insert({group_name, collision_filter_geometry_set});
for (XMLElement* ignore_node =
node->FirstChildElement("drake:ignored_collision_filter_group");
ignore_node; ignore_node = ignore_node->NextSiblingElement(
"drake:ignored_collision_filter_group")) {
const char* target_name = ignore_node->Attribute("name");
if (!target_name) {
throw std::runtime_error(fmt::format(
"'{}':'{}':'{}': Collision filter group provides a tag specifying a "
"group to ignore without specifying the \"name\" attribute.",
__FILE__, __func__, node->GetLineNum()));
}
// These two group names are allowed to be identical, which means the bodies
// inside this collision filter group should be collision excluded among
// each other.
collision_filter_pairs->insert({group_name, target_name});
}
}
// @pre plant->geometry_source_is_registered() is `true`.
void ParseCollisionFilterGroup(ModelInstanceIndex model_instance,
XMLElement* node,
MultibodyPlant<double>* plant) {
DRAKE_DEMAND(plant->geometry_source_is_registered());
std::map<std::string, geometry::GeometrySet> collision_filter_groups;
std::set<SortedPair<std::string>> collision_filter_pairs;
for (XMLElement* group_node =
node->FirstChildElement("drake:collision_filter_group");
group_node; group_node = group_node->NextSiblingElement(
"drake:collision_filter_group")) {
RegisterCollisionFilterGroup(model_instance, *plant, group_node,
&collision_filter_groups,
&collision_filter_pairs);
}
for (const auto& collision_filter_pair : collision_filter_pairs) {
const auto collision_filter_group_a =
collision_filter_groups.find(collision_filter_pair.first());
DRAKE_DEMAND(collision_filter_group_a != collision_filter_groups.end());
const auto collision_filter_group_b =
collision_filter_groups.find(collision_filter_pair.second());
DRAKE_DEMAND(collision_filter_group_b != collision_filter_groups.end());
plant->ExcludeCollisionGeometriesWithCollisionFilterGroupPair(
{collision_filter_group_a->first, collision_filter_group_a->second},
{collision_filter_group_b->first, collision_filter_group_b->second});
}
}
// Parses a joint URDF specification to obtain the names of the joint, parent
// link, child link, and the joint type. An exception is thrown if any of these
// names cannot be determined.
//
// @param[in] node The XML node parsing the URDF joint description.
// @param[out] name A reference to a string where the name of the joint
// should be saved.
// @param[out] type A reference to a string where the joint type should be
// saved.
// @param[out] parent_link_name A reference to a string where the name of the
// parent link should be saved.
// @param[out] child_link_name A reference to a string where the name of the
// child link should be saved.
void ParseJointKeyParams(XMLElement* node,
std::string* name,
std::string* type,
std::string* parent_link_name,
std::string* child_link_name) {
if (!ParseStringAttribute(node, "name", name)) {
throw std::runtime_error(
"ERROR: joint tag is missing name attribute");
}
if (!ParseStringAttribute(node, "type", type)) {
throw std::runtime_error(
"ERROR: joint " + *name + " is missing type attribute");
}
// Obtains the name of the joint's parent link.
XMLElement* parent_node = node->FirstChildElement("parent");
if (!parent_node) {
throw std::runtime_error(
"ERROR: joint " + *name + " doesn't have a parent node!");
}
if (!ParseStringAttribute(parent_node, "link", parent_link_name)) {
throw std::runtime_error(
"ERROR: joint " + *name + "'s parent does not have a link attribute!");
}
// Obtains the name of the joint's child link.
XMLElement* child_node = node->FirstChildElement("child");
if (!child_node) {
throw std::runtime_error(
"ERROR: joint " + *name + " doesn't have a child node");
}
if (!ParseStringAttribute(child_node, "link", child_link_name)) {
throw std::runtime_error(
"ERROR: joint " + *name + "'s child does not have a link attribute!");
}
}
void ParseJointLimits(XMLElement* node, double* lower, double* upper,
double* velocity, double* effort) {
*lower = -std::numeric_limits<double>::infinity();
*upper = std::numeric_limits<double>::infinity();
*velocity = std::numeric_limits<double>::infinity();
*effort = std::numeric_limits<double>::infinity();
XMLElement* limit_node = node->FirstChildElement("limit");
if (limit_node) {
ParseScalarAttribute(limit_node, "lower", lower);
ParseScalarAttribute(limit_node, "upper", upper);
ParseScalarAttribute(limit_node, "velocity", velocity);
ParseScalarAttribute(limit_node, "effort", effort);
}
}
void ParseJointDynamics(const std::string& joint_name,
XMLElement* node, double* damping) {
*damping = 0.0;
double coulomb_friction = 0.0;
double coulomb_window = std::numeric_limits<double>::epsilon();
XMLElement* dynamics_node = node->FirstChildElement("dynamics");
if (dynamics_node) {
ParseScalarAttribute(dynamics_node, "damping", damping);
if (ParseScalarAttribute(dynamics_node, "friction", &coulomb_friction) &&
coulomb_friction != 0.0) {
drake::log()->warn("Joint {} specifies non-zero friction, which is "
"not supported by MultibodyPlant", joint_name);
}
if (ParseScalarAttribute(dynamics_node, "coulomb_window",
&coulomb_window) &&
coulomb_window != std::numeric_limits<double>::epsilon()) {
drake::log()->warn("Joint {} specifies non-zero coulomb_window, which is "
"not supported by MultibodyPlant", joint_name);
}
}
}
const Body<double>& GetBodyForElement(
const std::string& element_name,
const std::string& link_name,
ModelInstanceIndex model_instance,
MultibodyPlant<double>* plant) {
if (link_name == kWorldName) {
return plant->world_body();
}
if (!plant->HasBodyNamed(link_name, model_instance)) {
throw std::runtime_error(
"ERROR: Could not find link named\"" +
link_name + "\" with model instance ID " +
std::to_string(model_instance) + " for element " + element_name + ".");
}
return plant->GetBodyByName(link_name, model_instance);
}
void ParseJoint(ModelInstanceIndex model_instance,
std::map<std::string, double>* joint_effort_limits,
XMLElement* node,
MultibodyPlant<double>* plant) {
std::string drake_ignore;
if (ParseStringAttribute(node, "drake_ignore", &drake_ignore) &&
drake_ignore == std::string("true")) {
return;
}
// Parses the parent and child link names.
std::string name, type, parent_name, child_name;
ParseJointKeyParams(node, &name, &type, &parent_name, &child_name);
const Body<double>& parent_body = GetBodyForElement(
name, parent_name, model_instance, plant);
const Body<double>& child_body = GetBodyForElement(
name, child_name, model_instance, plant);
RigidTransformd X_PJ;
XMLElement* origin = node->FirstChildElement("origin");
if (origin) {
X_PJ = OriginAttributesToTransform(origin);
}
Vector3d axis(1, 0, 0);
XMLElement* axis_node = node->FirstChildElement("axis");
if (axis_node && type.compare("fixed") != 0 &&
type.compare("floating") != 0 && type.compare("ball") != 0) {
ParseVectorAttribute(axis_node, "xyz", &axis);
if (axis.norm() < 1e-8) {
throw std::runtime_error(
"ERROR: Joint " + name + "axis is zero. Don't do that.");
}
axis.normalize();
}
// Joint properties -- these are only used by some joint types.
// Dynamic properties
double damping = 0;
// Limits
double upper = std::numeric_limits<double>::infinity();
double lower = -std::numeric_limits<double>::infinity();
double velocity = std::numeric_limits<double>::infinity();
// In MultibodyPlant, the effort limit is a property of the actuator, which
// isn't created until the transmission element is parsed. Stash a value
// for all joints when parsing the joint element so that we can look it up
// later if/when an actuator is created.
double effort = std::numeric_limits<double>::infinity();
auto throw_on_custom_joint = [node, name, type](bool want_custom_joint) {
const std::string node_name(node->Name());
const bool is_custom_joint = node_name == "drake:joint";
if (want_custom_joint && !is_custom_joint) {
throw std::runtime_error(
"ERROR: Joint " + name + " of type " + type +
" is a custom joint type, and should be a <drake:joint>");
} else if (!want_custom_joint && is_custom_joint) {
throw std::runtime_error(
"ERROR: Joint " + name + " of type " + type +
" is a standard joint type, and should be a <joint>");
}
};
if (type.compare("revolute") == 0 || type.compare("continuous") == 0) {
throw_on_custom_joint(false);
ParseJointLimits(node, &lower, &upper, &velocity, &effort);
ParseJointDynamics(name, node, &damping);
const JointIndex index = plant->AddJoint<RevoluteJoint>(
name, parent_body, X_PJ,
child_body, std::nullopt, axis, lower, upper, damping).index();
Joint<double>& joint = plant->get_mutable_joint(index);
joint.set_velocity_limits(Vector1d(-velocity), Vector1d(velocity));
} else if (type.compare("fixed") == 0) {
throw_on_custom_joint(false);
plant->AddJoint<WeldJoint>(name, parent_body, X_PJ,
child_body, std::nullopt,
RigidTransformd::Identity());
} else if (type.compare("prismatic") == 0) {
throw_on_custom_joint(false);
ParseJointLimits(node, &lower, &upper, &velocity, &effort);
ParseJointDynamics(name, node, &damping);
const JointIndex index = plant->AddJoint<PrismaticJoint>(
name, parent_body, X_PJ,
child_body, std::nullopt, axis, lower, upper, damping).index();
Joint<double>& joint = plant->get_mutable_joint(index);
joint.set_velocity_limits(Vector1d(-velocity), Vector1d(velocity));
} else if (type.compare("floating") == 0) {
throw_on_custom_joint(false);
drake::log()->warn("Joint {} specified as type floating which is not "
"supported by MultibodyPlant. Leaving {} as a "
"free body.", name, child_name);
} else if (type.compare("ball") == 0) {
throw_on_custom_joint(true);
ParseJointDynamics(name, node, &damping);
plant->AddJoint<BallRpyJoint>(name, parent_body, X_PJ,
child_body, std::nullopt, damping);
} else if (type.compare("planar") == 0) {
throw_on_custom_joint(true);
Vector3d damping_vec(0, 0, 0);
XMLElement* dynamics_node = node->FirstChildElement("dynamics");
if (dynamics_node) {
ParseVectorAttribute(dynamics_node, "damping", &damping_vec);
}
plant->AddJoint<PlanarJoint>(name, parent_body, X_PJ,
child_body, std::nullopt, damping_vec);
} else if (type.compare("universal") == 0) {
throw_on_custom_joint(true);
ParseJointDynamics(name, node, &damping);
plant->AddJoint<UniversalJoint>(name, parent_body, X_PJ,
child_body, std::nullopt, damping);
} else {
throw std::runtime_error(
"ERROR: Joint " + name + " has unrecognized type: " + type);
}
joint_effort_limits->emplace(name, effort);
}
void ParseTransmission(
ModelInstanceIndex model_instance,
const std::map<std::string, double>& joint_effort_limits,
XMLElement* node,
MultibodyPlant<double>* plant) {
// Determines the transmission type.
std::string type;
XMLElement* type_node = node->FirstChildElement("type");
if (type_node) {
type = type_node->GetText();
} else {
// Old URDF format, kept for convenience
if (!ParseStringAttribute(node, "type", &type)) {
throw std::runtime_error(
std::string(__FILE__) + ": " + __func__ +
"ERROR: Transmission element is missing a type.");
}
}
// Checks if the transmission type is not SimpleTransmission. If it is not,
// print a warning and then abort this method call since only simple
// transmissions are supported at this time.
if (type.find("SimpleTransmission") == std::string::npos) {
drake::log()->warn(
"Only SimpleTransmissions are supported right now. This element "
"will be skipped.");
return;
}
// Determines the actuator's name.
XMLElement* actuator_node = node->FirstChildElement("actuator");
if (!actuator_node) {
throw std::runtime_error(
"ERROR: Transmission is missing an actuator element.");
}
std::string actuator_name;
if (!ParseStringAttribute(actuator_node, "name", &actuator_name)) {
throw std::runtime_error(
"ERROR: Transmission is missing an actuator name.");
}
// Determines the name of the joint to which the actuator is attached.
XMLElement* joint_node = node->FirstChildElement("joint");
if (!joint_node) {
throw std::runtime_error(
"ERROR: Transmission is missing a joint element.");
}
std::string joint_name;
if (!ParseStringAttribute(joint_node, "name", &joint_name)) {
throw std::runtime_error(
"ERROR: Transmission is missing a joint name.");
}
if (!plant->HasJointNamed(joint_name, model_instance)) {
throw std::runtime_error(
"ERROR: Transmission specifies joint " + joint_name +
" which does not exist.");
}
const Joint<double>& joint = plant->GetJointByName(
joint_name, model_instance);
// Checks if the actuator is attached to a fixed joint. If so, abort this
// method call.
if (joint.num_positions() == 0) {
drake::log()->warn(
"WARNING: Skipping transmission since it's attached to "
"a fixed joint \"" + joint_name + "\".");
return;
}
const auto effort_iter = joint_effort_limits.find(joint_name);
DRAKE_DEMAND(effort_iter != joint_effort_limits.end());
if (effort_iter->second < 0) {
throw std::runtime_error(
"ERROR: Transmission specifies joint " + joint_name +
" which has a negative effort limit.");
}
if (effort_iter->second <= 0) {
drake::log()->warn(
"WARNING: Skipping transmission since it's attached to "
"joint \"" + joint_name + "\" which has a zero "
"effort limit {}.", effort_iter->second);
return;
}
const JointActuator<double>& actuator =
plant->AddJointActuator(actuator_name, joint, effort_iter->second);
// Parse and add the optional drake:rotor_inertia parameter.
XMLElement* rotor_inertia_node =
actuator_node->FirstChildElement("drake:rotor_inertia");
if (rotor_inertia_node) {
double rotor_inertia;
if (!ParseScalarAttribute(rotor_inertia_node, "value", &rotor_inertia)) {
throw std::runtime_error(
"ERROR: joint actuator " + actuator_name +
"'s drake:rotor_inertia does not have a \"value\" attribute!");
}
plant->get_mutable_joint_actuator(actuator.index())
.set_default_rotor_inertia(rotor_inertia);
}
// Parse and add the optional drake:gear_ratio parameter.
XMLElement* gear_ratio_node =
actuator_node->FirstChildElement("drake:gear_ratio");
if (gear_ratio_node) {
double gear_ratio;
if (!ParseScalarAttribute(gear_ratio_node, "value", &gear_ratio)) {
throw std::runtime_error(
"ERROR: joint actuator " + actuator_name +
"'s drake:gear_ratio does not have a \"value\" attribute!");
}
plant->get_mutable_joint_actuator(actuator.index())
.set_default_gear_ratio(gear_ratio);
}
}
void ParseFrame(ModelInstanceIndex model_instance,
XMLElement* node,
MultibodyPlant<double>* plant) {
std::string name;
if (!ParseStringAttribute(node, "name", &name)) {
throw std::runtime_error("ERROR parsing frame name.");
}
std::string body_name;
if (!ParseStringAttribute(node, "link", &body_name)) {
throw std::runtime_error(
"ERROR: missing link name for frame " + name + ".");
}
const Body<double>& body =
GetBodyForElement(name, body_name, model_instance, plant);
RigidTransformd X_BF = OriginAttributesToTransform(node);
plant->AddFrame(std::make_unique<FixedOffsetFrame<double>>(
name, body.body_frame(), X_BF));
}
void ParseBushing(XMLElement* node, MultibodyPlant<double>* plant) {
// Functor to read a child element with a vector valued `value` attribute
// Throws an error if unable to find the tag or if the value attribute is
// improperly formed.
auto read_vector = [node](const char* element_name) -> Eigen::Vector3d {
const XMLElement* value_node = node->FirstChildElement(element_name);
if (value_node != nullptr) {
Eigen::Vector3d value;
if (ParseVectorAttribute(value_node, "value", &value)) {
return value;
} else {
throw std::runtime_error(
fmt::format("Unable to read the 'value' attribute for the <{}> "
"tag on line {}",
element_name, value_node->GetLineNum()));
}
} else {
throw std::runtime_error(
fmt::format("Unable to find the <{}> "
"tag on line {}",
element_name, node->GetLineNum()));
}
};
// Functor to read a child element with a string valued `name` attribute.
// Throws an error if unable to find the tag of if the name attribute is
// improperly formed.
auto read_frame = [node,
plant](const char* element_name) -> const Frame<double>& {
XMLElement* value_node = node->FirstChildElement(element_name);
if (value_node != nullptr) {
std::string frame_name;
if (ParseStringAttribute(value_node, "name", &frame_name)) {
if (!plant->HasFrameNamed(frame_name)) {
throw std::runtime_error(fmt::format(
"Frame: {} specified for <{}> does not exist in the model.",
frame_name, element_name));
}
return plant->GetFrameByName(frame_name);
} else {
throw std::runtime_error(
fmt::format("Unable to read the 'name' attribute for the <{}> "
"tag on line {}",
element_name, value_node->GetLineNum()));
}
} else {
throw std::runtime_error(
fmt::format("Unable to find the <{}> tag on line {}", element_name,
node->GetLineNum()));
}
};
ParseLinearBushingRollPitchYaw(read_vector, read_frame, plant);
}
ModelInstanceIndex ParseUrdf(
const std::string& model_name_in,
const multibody::PackageMap& package_map,
const std::string& root_dir,
XMLDocument* xml_doc,
MultibodyPlant<double>* plant) {
XMLElement* node = xml_doc->FirstChildElement("robot");
if (!node) {
throw std::runtime_error("ERROR: URDF does not contain a robot tag.");
}
std::string model_name = model_name_in;
if (model_name.empty() && !ParseStringAttribute(node, "name", &model_name)) {
throw std::runtime_error(
"ERROR: Your robot must have a name attribute or a model name "
"must be specified.");
}
// Parses the model's material elements. Throws an exception if there's a
// material name clash regardless of whether the associated RGBA values are
// the same.
MaterialMap materials;
for (XMLElement* material_node = node->FirstChildElement("material");
material_node;
material_node = material_node->NextSiblingElement("material")) {
ParseMaterial(material_node, true /* name_required */, package_map,
root_dir, &materials);
}
const ModelInstanceIndex model_instance =
plant->AddModelInstance(model_name);
// Parses the model's link elements.
for (XMLElement* link_node = node->FirstChildElement("link");
link_node;
link_node = link_node->NextSiblingElement("link")) {
ParseBody(package_map, root_dir, model_instance, link_node,
&materials, plant);
}
// Parses the collision filter groups only if the scene graph is registered.
if (plant->geometry_source_is_registered()) {
ParseCollisionFilterGroup(model_instance, node, plant);
}
// Joint effort limits are stored with joints, but used when creating the
// actuator (which is done when parsing the transmission).
std::map<std::string, double> joint_effort_limits;
// Parses the model's joint elements. While it may not be strictly required
// to be true in MultibodyPlant, generally the JointIndex for any given
// joint follows the declaration order in the model (and users probably
// should avoid caring about the ordering of JointIndex), we still parse the
// joints in model order regardless of the element type so that the results
// are consistent with an SDF version of the same model.
for (XMLElement* joint_node = node->FirstChildElement(); joint_node;
joint_node = joint_node->NextSiblingElement()) {
const std::string node_name(joint_node->Name());
if (node_name == "joint" || node_name == "drake:joint") {
ParseJoint(model_instance, &joint_effort_limits, joint_node, plant);
}
}
// Parses the model's transmission elements.
for (XMLElement* transmission_node = node->FirstChildElement("transmission");
transmission_node;
transmission_node =
transmission_node->NextSiblingElement("transmission")) {
ParseTransmission(model_instance, joint_effort_limits,
transmission_node, plant);
}
if (node->FirstChildElement("loop_joint")) {
throw std::runtime_error(
"ERROR: loop joints are not supported in MultibodyPlant");
}
// Parses the model's Drake frame elements.
for (XMLElement* frame_node = node->FirstChildElement("frame"); frame_node;
frame_node = frame_node->NextSiblingElement("frame")) {
ParseFrame(model_instance, frame_node, plant);
}
// Parses the model's custom Drake bushing tags.
for (XMLElement* bushing_node =
node->FirstChildElement("drake:linear_bushing_rpy");
bushing_node; bushing_node = bushing_node->NextSiblingElement(
"drake:linear_bushing_rpy")) {
ParseBushing(bushing_node, plant);
}
return model_instance;
}
} // namespace
ModelInstanceIndex AddModelFromUrdf(
const DataSource& data_source,
const std::string& model_name_in,
const PackageMap& package_map,
MultibodyPlant<double>* plant,
geometry::SceneGraph<double>* scene_graph) {
DRAKE_THROW_UNLESS(plant != nullptr);
DRAKE_THROW_UNLESS(!plant->is_finalized());
data_source.DemandExactlyOne();
// When the data_source is a filename, we'll use its parent directory to be
// the root directory to search for files referenced within the URDF file.
// If data_source is a string, this will remain unset and relative-path
// resources that would otherwise require a root directory will not be found.
std::string root_dir;
// Opens the URDF file and feeds it into the XML parser.
XMLDocument xml_doc;
if (data_source.file_name) {
const std::string full_path = GetFullPath(*data_source.file_name);
size_t found = full_path.find_last_of("/\\");
if (found != std::string::npos) {
root_dir = full_path.substr(0, found);
} else {
// TODO(jwnimmer-tri) This is not unit tested. In any case, we should be
// using drake::filesystem for path manipulation, not string searching.
root_dir = ".";
}
xml_doc.LoadFile(full_path.c_str());
if (xml_doc.ErrorID()) {
throw std::runtime_error(fmt::format(
"Failed to parse XML file {}:\n{}",
full_path, xml_doc.ErrorName()));
}
} else {
DRAKE_DEMAND(data_source.file_contents);
xml_doc.Parse(data_source.file_contents->c_str());
if (xml_doc.ErrorID()) {
throw std::runtime_error(fmt::format(
"Failed to parse XML string: {}",
xml_doc.ErrorName()));
}
}
if (scene_graph != nullptr && !plant->geometry_source_is_registered()) {
plant->RegisterAsSourceForSceneGraph(scene_graph);
}
return ParseUrdf(model_name_in, package_map, root_dir,
&xml_doc, plant);
}
} // namespace internal
} // namespace multibody
} // namespace drake