/
robot_model.cpp
1547 lines (1409 loc) · 57.9 KB
/
robot_model.cpp
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
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*********************************************************************
* Software License Agreement (BSD License)
*
* Copyright (c) 2013, Ioan A. Sucan
* Copyright (c) 2013, Willow Garage, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the Willow Garage nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*********************************************************************/
/* Author: Ioan Sucan */
#include <moveit/robot_model/robot_model.h>
#include <geometric_shapes/shape_operations.h>
#include <boost/math/constants/constants.hpp>
#include <moveit/profiler/profiler.h>
#include <algorithm>
#include <limits>
#include <cmath>
#include <memory>
#include "order_robot_model_items.inc"
#include "rclcpp/rclcpp.hpp"
namespace moveit
{
namespace core
{
static const rclcpp::Logger LOGGER = rclcpp::get_logger("moveit_robot_model.robot_model");
RobotModel::RobotModel(const urdf::ModelInterfaceSharedPtr& urdf_model, const srdf::ModelConstSharedPtr& srdf_model)
{
root_joint_ = nullptr;
urdf_ = urdf_model;
srdf_ = srdf_model;
buildModel(*urdf_model, *srdf_model);
}
RobotModel::~RobotModel()
{
for (std::pair<const std::string, JointModelGroup*>& it : joint_model_group_map_)
delete it.second;
for (JointModel* joint_model : joint_model_vector_)
delete joint_model;
for (LinkModel* link_model : link_model_vector_)
delete link_model;
}
const JointModel* RobotModel::getRootJoint() const
{
return root_joint_;
}
const LinkModel* RobotModel::getRootLink() const
{
return root_link_;
}
void RobotModel::buildModel(const urdf::ModelInterface& urdf_model, const srdf::Model& srdf_model)
{
moveit::tools::Profiler::ScopedStart prof_start;
moveit::tools::Profiler::ScopedBlock prof_block("RobotModel::buildModel");
root_joint_ = nullptr;
root_link_ = nullptr;
link_geometry_count_ = 0;
variable_count_ = 0;
model_name_ = urdf_model.getName();
RCLCPP_INFO(LOGGER, "Loading robot model '%s'...", model_name_.c_str());
if (urdf_model.getRoot())
{
const urdf::Link* root_link_ptr = urdf_model.getRoot().get();
model_frame_ = root_link_ptr->name;
RCLCPP_DEBUG(LOGGER, "... building kinematic chain");
root_joint_ = buildRecursive(nullptr, root_link_ptr, srdf_model);
if (root_joint_)
root_link_ = root_joint_->getChildLinkModel();
RCLCPP_DEBUG(LOGGER, "... building mimic joints");
buildMimic(urdf_model);
RCLCPP_DEBUG(LOGGER, "... computing joint indexing");
buildJointInfo();
if (link_models_with_collision_geometry_vector_.empty())
{
RCLCPP_WARN(LOGGER, "No geometry is associated to any robot links");
}
// build groups
RCLCPP_DEBUG(LOGGER, "... constructing joint groups");
buildGroups(srdf_model);
RCLCPP_DEBUG(LOGGER, "... constructing joint group states");
buildGroupStates(srdf_model);
// For debugging entire model
// printModelInfo(std::cout);
}
else
{
RCLCPP_WARN(LOGGER, "No root link found");
}
}
namespace
{
typedef std::map<const JointModel*, std::pair<std::set<const LinkModel*, OrderLinksByIndex>,
std::set<const JointModel*, OrderJointsByIndex>>>
DescMap;
void computeDescendantsHelper(const JointModel* joint, std::vector<const JointModel*>& parents,
std::set<const JointModel*>& seen, DescMap& descendants)
{
if (!joint)
return;
if (seen.find(joint) != seen.end())
return;
seen.insert(joint);
for (const JointModel* parent : parents)
descendants[parent].second.insert(joint);
const LinkModel* lm = joint->getChildLinkModel();
if (!lm)
return;
for (const JointModel* parent : parents)
descendants[parent].first.insert(lm);
descendants[joint].first.insert(lm);
parents.push_back(joint);
const std::vector<const JointModel*>& ch = lm->getChildJointModels();
for (const JointModel* child_joint_model : ch)
computeDescendantsHelper(child_joint_model, parents, seen, descendants);
const std::vector<const JointModel*>& mim = joint->getMimicRequests();
for (const JointModel* mimic_joint_model : mim)
computeDescendantsHelper(mimic_joint_model, parents, seen, descendants);
parents.pop_back();
}
void computeCommonRootsHelper(const JointModel* joint, std::vector<int>& common_roots, int size)
{
if (!joint)
return;
const LinkModel* lm = joint->getChildLinkModel();
if (!lm)
return;
const std::vector<const JointModel*>& ch = lm->getChildJointModels();
for (std::size_t i = 0; i < ch.size(); ++i)
{
const std::vector<const JointModel*>& a = ch[i]->getDescendantJointModels();
for (std::size_t j = i + 1; j < ch.size(); ++j)
{
const std::vector<const JointModel*>& b = ch[j]->getDescendantJointModels();
for (const JointModel* m : b)
common_roots[ch[i]->getJointIndex() * size + m->getJointIndex()] =
common_roots[ch[i]->getJointIndex() + m->getJointIndex() * size] = joint->getJointIndex();
for (const JointModel* k : a)
{
common_roots[k->getJointIndex() * size + ch[j]->getJointIndex()] =
common_roots[k->getJointIndex() + ch[j]->getJointIndex() * size] = joint->getJointIndex();
for (const JointModel* m : b)
common_roots[k->getJointIndex() * size + m->getJointIndex()] =
common_roots[k->getJointIndex() + m->getJointIndex() * size] = joint->getJointIndex();
}
}
computeCommonRootsHelper(ch[i], common_roots, size);
}
}
} // namespace
void RobotModel::computeCommonRoots()
{
// compute common roots for all pairs of joints;
// there are 3 cases of pairs (X, Y):
// X != Y && X and Y are not descendants of one another
// X == Y
// X != Y && X and Y are descendants of one another
// by default, the common root is always the global root;
common_joint_roots_.resize(joint_model_vector_.size() * joint_model_vector_.size(), 0);
// look at all descendants recursively; for two sibling nodes A, B, both children of X, all the pairs of respective
// descendants of A and B
// have X as the common root.
computeCommonRootsHelper(root_joint_, common_joint_roots_, joint_model_vector_.size());
for (const JointModel* joint_model : joint_model_vector_)
{
// the common root of a joint and itself is the same joint:
common_joint_roots_[joint_model->getJointIndex() * (1 + joint_model_vector_.size())] = joint_model->getJointIndex();
// a node N and one of its descendants have as common root the node N itself:
const std::vector<const JointModel*>& d = joint_model->getDescendantJointModels();
for (const JointModel* descendant_joint_model : d)
common_joint_roots_[descendant_joint_model->getJointIndex() * joint_model_vector_.size() +
joint_model->getJointIndex()] =
common_joint_roots_[descendant_joint_model->getJointIndex() +
joint_model->getJointIndex() * joint_model_vector_.size()] = joint_model->getJointIndex();
}
}
void RobotModel::computeDescendants()
{
// compute the list of descendants for all joints
std::vector<const JointModel*> parents;
std::set<const JointModel*> seen;
DescMap descendants;
computeDescendantsHelper(root_joint_, parents, seen, descendants);
for (std::pair<const JointModel* const, std::pair<std::set<const LinkModel*, OrderLinksByIndex>,
std::set<const JointModel*, OrderJointsByIndex>>>& descendant :
descendants)
{
JointModel* jm = const_cast<JointModel*>(descendant.first);
for (const JointModel* jt : descendant.second.second)
jm->addDescendantJointModel(jt);
for (const LinkModel* jt : descendant.second.first)
jm->addDescendantLinkModel(jt);
}
}
void RobotModel::buildJointInfo()
{
moveit::tools::Profiler::ScopedStart prof_start;
moveit::tools::Profiler::ScopedBlock prof_block("RobotModel::buildJointInfo");
// construct additional maps for easy access by name
variable_count_ = 0;
active_joint_model_start_index_.reserve(joint_model_vector_.size());
variable_names_.reserve(joint_model_vector_.size());
joints_of_variable_.reserve(joint_model_vector_.size());
for (std::size_t i = 0; i < joint_model_vector_.size(); ++i)
{
joint_model_vector_[i]->setJointIndex(i);
const std::vector<std::string>& name_order = joint_model_vector_[i]->getVariableNames();
// compute index map
if (!name_order.empty())
{
for (std::size_t j = 0; j < name_order.size(); ++j)
{
joint_variables_index_map_[name_order[j]] = variable_count_ + j;
variable_names_.push_back(name_order[j]);
joints_of_variable_.push_back(joint_model_vector_[i]);
}
if (joint_model_vector_[i]->getMimic() == nullptr)
{
active_joint_model_start_index_.push_back(variable_count_);
active_joint_model_vector_.push_back(joint_model_vector_[i]);
active_joint_model_names_vector_.push_back(joint_model_vector_[i]->getName());
active_joint_model_vector_const_.push_back(joint_model_vector_[i]);
active_joint_models_bounds_.push_back(&joint_model_vector_[i]->getVariableBounds());
}
if (joint_model_vector_[i]->getType() == JointModel::REVOLUTE &&
static_cast<const RevoluteJointModel*>(joint_model_vector_[i])->isContinuous())
continuous_joint_model_vector_.push_back(joint_model_vector_[i]);
joint_model_vector_[i]->setFirstVariableIndex(variable_count_);
joint_variables_index_map_[joint_model_vector_[i]->getName()] = variable_count_;
// compute variable count
std::size_t vc = joint_model_vector_[i]->getVariableCount();
variable_count_ += vc;
if (vc == 1)
single_dof_joints_.push_back(joint_model_vector_[i]);
else
multi_dof_joints_.push_back(joint_model_vector_[i]);
}
}
std::vector<bool> link_considered(link_model_vector_.size(), false);
for (const LinkModel* link : link_model_vector_)
{
if (link_considered[link->getLinkIndex()])
continue;
LinkTransformMap associated_transforms;
computeFixedTransforms(link, link->getJointOriginTransform().inverse(), associated_transforms);
for (auto& tf_base : associated_transforms)
{
link_considered[tf_base.first->getLinkIndex()] = true;
for (auto& tf_target : associated_transforms)
{
if (&tf_base != &tf_target)
const_cast<LinkModel*>(tf_base.first) // regain write access to base LinkModel*
->addAssociatedFixedTransform(tf_target.first, tf_base.second.inverse() * tf_target.second);
}
}
}
computeDescendants();
computeCommonRoots(); // must be called _after_ list of descendants was computed
}
void RobotModel::buildGroupStates(const srdf::Model& srdf_model)
{
// copy the default states to the groups
const std::vector<srdf::Model::GroupState>& ds = srdf_model.getGroupStates();
for (const srdf::Model::GroupState& group_state : ds)
{
if (hasJointModelGroup(group_state.group_))
{
JointModelGroup* jmg = getJointModelGroup(group_state.group_);
std::vector<const JointModel*> remaining_joints = jmg->getActiveJointModels();
std::map<std::string, double> state;
for (std::map<std::string, std::vector<double>>::const_iterator jt = group_state.joint_values_.begin();
jt != group_state.joint_values_.end(); ++jt)
{
if (jmg->hasJointModel(jt->first))
{
const JointModel* jm = jmg->getJointModel(jt->first);
const std::vector<std::string>& vn = jm->getVariableNames();
// Remove current joint name from remaining list.
auto it_found = std::find(remaining_joints.begin(), remaining_joints.end(), jm);
if (it_found != remaining_joints.end())
remaining_joints.erase(it_found);
if (vn.size() == jt->second.size())
for (std::size_t j = 0; j < vn.size(); ++j)
state[vn[j]] = jt->second[j];
else
RCLCPP_ERROR(LOGGER,
"The model for joint '%s' requires %d variable values, "
"but only %d variable values were supplied in default state '%s' for group '%s'",
jt->first.c_str(), (int)vn.size(), (int)jt->second.size(), group_state.name_.c_str(),
jmg->getName().c_str());
}
else
RCLCPP_ERROR(LOGGER,
"Group state '%s' specifies value for joint '%s', "
"but that joint is not part of group '%s'",
group_state.name_.c_str(), jt->first.c_str(), jmg->getName().c_str());
}
if (!remaining_joints.empty())
{
std::stringstream missing;
missing << (*remaining_joints.begin())->getName();
for (auto j = ++remaining_joints.begin(); j != remaining_joints.end(); j++)
{
missing << ", " << (*j)->getName();
}
RCLCPP_WARN_STREAM(LOGGER, "Group state '" << group_state.name_
<< "' doesn't specify all group joints in group '"
<< group_state.group_ << "'. " << missing.str() << " "
<< (remaining_joints.size() > 1 ? "are" : "is") << " missing.");
}
if (!state.empty())
jmg->addDefaultState(group_state.name_, state);
}
else
RCLCPP_ERROR(LOGGER, "Group state '%s' specified for group '%s', but that group does not exist",
group_state.name_.c_str(), group_state.group_.c_str());
}
}
void RobotModel::buildMimic(const urdf::ModelInterface& urdf_model)
{
// compute mimic joints
for (JointModel* joint_model : joint_model_vector_)
{
const urdf::Joint* jm = urdf_model.getJoint(joint_model->getName()).get();
if (jm)
if (jm->mimic)
{
JointModelMap::const_iterator jit = joint_model_map_.find(jm->mimic->joint_name);
if (jit != joint_model_map_.end())
{
if (joint_model->getVariableCount() == jit->second->getVariableCount())
joint_model->setMimic(jit->second, jm->mimic->multiplier, jm->mimic->offset);
else
RCLCPP_ERROR(LOGGER, "Join '%s' cannot mimic joint '%s' because they have different number of DOF",
joint_model->getName().c_str(), jm->mimic->joint_name.c_str());
}
else
RCLCPP_ERROR(LOGGER, "Joint '%s' cannot mimic unknown joint '%s'", joint_model->getName().c_str(),
jm->mimic->joint_name.c_str());
}
}
// in case we have a joint that mimics a joint that already mimics another joint, we can simplify things:
bool change = true;
while (change)
{
change = false;
for (JointModel* joint_model : joint_model_vector_)
if (joint_model->getMimic())
{
if (joint_model->getMimic()->getMimic())
{
joint_model->setMimic(joint_model->getMimic()->getMimic(),
joint_model->getMimicFactor() * joint_model->getMimic()->getMimicFactor(),
joint_model->getMimicOffset() +
joint_model->getMimicFactor() * joint_model->getMimic()->getMimicOffset());
change = true;
}
if (joint_model == joint_model->getMimic())
{
RCLCPP_ERROR(LOGGER, "Cycle found in joint that mimic each other. Ignoring all mimic joints.");
for (JointModel* joint_model_recal : joint_model_vector_)
joint_model_recal->setMimic(nullptr, 0.0, 0.0);
change = false;
break;
}
}
}
// build mimic requests
for (JointModel* joint_model : joint_model_vector_)
if (joint_model->getMimic())
{
const_cast<JointModel*>(joint_model->getMimic())->addMimicRequest(joint_model);
mimic_joints_.push_back(joint_model);
}
}
bool RobotModel::hasEndEffector(const std::string& eef) const
{
return end_effectors_map_.find(eef) != end_effectors_map_.end();
}
const JointModelGroup* RobotModel::getEndEffector(const std::string& name) const
{
JointModelGroupMap::const_iterator it = end_effectors_map_.find(name);
if (it == end_effectors_map_.end())
{
it = joint_model_group_map_.find(name);
if (it != joint_model_group_map_.end() && it->second->isEndEffector())
return it->second;
RCLCPP_ERROR(LOGGER, "End-effector '%s' not found in model '%s'", name.c_str(), model_name_.c_str());
return nullptr;
}
return it->second;
}
JointModelGroup* RobotModel::getEndEffector(const std::string& name)
{
JointModelGroupMap::const_iterator it = end_effectors_map_.find(name);
if (it == end_effectors_map_.end())
{
it = joint_model_group_map_.find(name);
if (it != joint_model_group_map_.end() && it->second->isEndEffector())
return it->second;
RCLCPP_ERROR(LOGGER, "End-effector '%s' not found in model '%s'", name.c_str(), model_name_.c_str());
return nullptr;
}
return it->second;
}
bool RobotModel::hasJointModelGroup(const std::string& name) const
{
return joint_model_group_map_.find(name) != joint_model_group_map_.end();
}
const JointModelGroup* RobotModel::getJointModelGroup(const std::string& name) const
{
JointModelGroupMap::const_iterator it = joint_model_group_map_.find(name);
if (it == joint_model_group_map_.end())
{
RCLCPP_ERROR(LOGGER, "Group '%s' not found in model '%s'", name.c_str(), model_name_.c_str());
return nullptr;
}
return it->second;
}
JointModelGroup* RobotModel::getJointModelGroup(const std::string& name)
{
JointModelGroupMap::const_iterator it = joint_model_group_map_.find(name);
if (it == joint_model_group_map_.end())
{
RCLCPP_ERROR(LOGGER, "Group '%s' not found in model '%s'", name.c_str(), model_name_.c_str());
return nullptr;
}
return it->second;
}
void RobotModel::buildGroups(const srdf::Model& srdf_model)
{
const std::vector<srdf::Model::Group>& group_configs = srdf_model.getGroups();
// the only thing tricky is dealing with subgroups
std::vector<bool> processed(group_configs.size(), false);
bool added = true;
while (added)
{
added = false;
// going to make passes until we can't do anything else
for (std::size_t i = 0; i < group_configs.size(); ++i)
if (!processed[i])
{
// if we haven't processed, check and see if the dependencies are met yet
bool all_subgroups_added = true;
for (const std::string& subgroup : group_configs[i].subgroups_)
if (joint_model_group_map_.find(subgroup) == joint_model_group_map_.end())
{
all_subgroups_added = false;
break;
}
if (all_subgroups_added)
{
added = true;
processed[i] = true;
if (!addJointModelGroup(group_configs[i]))
{
RCLCPP_WARN(LOGGER, "Failed to add group '%s'", group_configs[i].name_.c_str());
}
}
}
}
for (std::size_t i = 0; i < processed.size(); ++i)
if (!processed[i])
{
RCLCPP_WARN(LOGGER, "Could not process group '%s' due to unmet subgroup dependencies",
group_configs[i].name_.c_str());
}
for (JointModelGroupMap::const_iterator it = joint_model_group_map_.begin(); it != joint_model_group_map_.end(); ++it)
joint_model_groups_.push_back(it->second);
std::sort(joint_model_groups_.begin(), joint_model_groups_.end(), OrderGroupsByName());
for (JointModelGroup* joint_model_group : joint_model_groups_)
{
joint_model_groups_const_.push_back(joint_model_group);
joint_model_group_names_.push_back(joint_model_group->getName());
}
buildGroupsInfoSubgroups(srdf_model);
buildGroupsInfoEndEffectors(srdf_model);
}
void RobotModel::buildGroupsInfoSubgroups(const srdf::Model& srdf_model)
{
// compute subgroups
for (JointModelGroupMap::const_iterator it = joint_model_group_map_.begin(); it != joint_model_group_map_.end(); ++it)
{
JointModelGroup* jmg = it->second;
std::vector<std::string> subgroup_names;
std::set<const JointModel*> joints(jmg->getJointModels().begin(), jmg->getJointModels().end());
for (JointModelGroupMap::const_iterator jt = joint_model_group_map_.begin(); jt != joint_model_group_map_.end();
++jt)
if (jt->first != it->first)
{
bool ok = true;
JointModelGroup* sub_jmg = jt->second;
const std::vector<const JointModel*>& sub_joints = sub_jmg->getJointModels();
for (const JointModel* sub_joint : sub_joints)
if (joints.find(sub_joint) == joints.end())
{
ok = false;
break;
}
if (ok)
subgroup_names.push_back(sub_jmg->getName());
}
if (!subgroup_names.empty())
jmg->setSubgroupNames(subgroup_names);
}
}
void RobotModel::buildGroupsInfoEndEffectors(const srdf::Model& srdf_model)
{
// set the end-effector flags
const std::vector<srdf::Model::EndEffector>& eefs = srdf_model.getEndEffectors();
for (JointModelGroupMap::const_iterator it = joint_model_group_map_.begin(); it != joint_model_group_map_.end(); ++it)
{
// check if this group is a known end effector
for (const srdf::Model::EndEffector& eef : eefs)
if (eef.component_group_ == it->first)
{
// if it is, mark it as such
it->second->setEndEffectorName(eef.name_);
end_effectors_map_[eef.name_] = it->second;
end_effectors_.push_back(it->second);
// check to see if there are groups that contain the parent link of this end effector.
// record this information if found;
std::vector<JointModelGroup*> possible_parent_groups;
for (JointModelGroupMap::const_iterator jt = joint_model_group_map_.begin(); jt != joint_model_group_map_.end();
++jt)
if (jt->first != it->first)
{
if (jt->second->hasLinkModel(eef.parent_link_))
{
jt->second->attachEndEffector(eef.name_);
possible_parent_groups.push_back(jt->second);
}
}
JointModelGroup* eef_parent_group = nullptr;
// if a parent group is specified in SRDF, try to use it
if (!eef.parent_group_.empty())
{
JointModelGroupMap::const_iterator jt = joint_model_group_map_.find(eef.parent_group_);
if (jt != joint_model_group_map_.end())
{
if (jt->second->hasLinkModel(eef.parent_link_))
{
if (jt->second != it->second)
eef_parent_group = jt->second;
else
RCLCPP_ERROR(LOGGER, "Group '%s' for end-effector '%s' cannot be its own parent",
eef.parent_group_.c_str(), eef.name_.c_str());
}
else
RCLCPP_ERROR(LOGGER,
"Group '%s' was specified as parent group for end-effector '%s' "
"but it does not include the parent link '%s'",
eef.parent_group_.c_str(), eef.name_.c_str(), eef.parent_link_.c_str());
}
else
RCLCPP_ERROR(LOGGER, "Group name '%s' not found (specified as parent group for end-effector '%s')",
eef.parent_group_.c_str(), eef.name_.c_str());
}
// if no parent group was specified, use a default one
if (eef_parent_group == nullptr)
if (!possible_parent_groups.empty())
{
// if there are multiple options for the group that contains this end-effector,
// we pick the group with fewest joints.
std::size_t best = 0;
for (std::size_t g = 1; g < possible_parent_groups.size(); ++g)
if (possible_parent_groups[g]->getJointModels().size() <
possible_parent_groups[best]->getJointModels().size())
best = g;
eef_parent_group = possible_parent_groups[best];
}
if (eef_parent_group)
{
it->second->setEndEffectorParent(eef_parent_group->getName(), eef.parent_link_);
}
else
{
RCLCPP_WARN(LOGGER, "Could not identify parent group for end-effector '%s'", eef.name_.c_str());
it->second->setEndEffectorParent("", eef.parent_link_);
}
}
}
std::sort(end_effectors_.begin(), end_effectors_.end(), OrderGroupsByName());
}
bool RobotModel::addJointModelGroup(const srdf::Model::Group& gc)
{
if (joint_model_group_map_.find(gc.name_) != joint_model_group_map_.end())
{
RCLCPP_WARN(LOGGER, "A group named '%s' already exists. Not adding.", gc.name_.c_str());
return false;
}
std::set<const JointModel*> jset;
// add joints from chains
for (const std::pair<std::string, std::string>& chain : gc.chains_)
{
const LinkModel* base_link = getLinkModel(chain.first);
const LinkModel* tip_link = getLinkModel(chain.second);
if (base_link && tip_link)
{
// go from tip, up the chain, until we hit the root or we find the base_link
const LinkModel* lm = tip_link;
std::vector<const JointModel*> cj;
while (lm)
{
if (lm == base_link)
break;
cj.push_back(lm->getParentJointModel());
lm = lm->getParentJointModel()->getParentLinkModel();
}
// if we did not find the base_link, we could have a chain like e.g.,
// from one end-effector to another end-effector, so the root is in between
if (lm != base_link)
{
// we go up the chain from the base this time, and see where we intersect the other chain
lm = base_link;
std::size_t index = 0;
std::vector<const JointModel*> cj2;
while (lm)
{
for (std::size_t j = 0; j < cj.size(); ++j)
if (cj[j] == lm->getParentJointModel())
{
index = j + 1;
break;
}
if (index > 0)
break;
cj2.push_back(lm->getParentJointModel());
lm = lm->getParentJointModel()->getParentLinkModel();
}
if (index > 0)
{
jset.insert(cj.begin(), cj.begin() + index);
jset.insert(cj2.begin(), cj2.end());
}
}
else
// if we have a simple chain, just add the joints
jset.insert(cj.begin(), cj.end());
}
}
// add joints
for (const std::string& joint : gc.joints_)
{
const JointModel* j = getJointModel(joint);
if (j)
jset.insert(j);
}
// add joints that are parents of included links
for (const std::string& link : gc.links_)
{
const LinkModel* l = getLinkModel(link);
if (l)
jset.insert(l->getParentJointModel());
}
// add joints from subgroups
for (const std::string& subgroup : gc.subgroups_)
{
const JointModelGroup* sg = getJointModelGroup(subgroup);
if (sg)
{
// active joints
const std::vector<const JointModel*>& js = sg->getJointModels();
for (const JointModel* j : js)
jset.insert(j);
// fixed joints
const std::vector<const JointModel*>& fs = sg->getFixedJointModels();
for (const JointModel* f : fs)
jset.insert(f);
// mimic joints
const std::vector<const JointModel*>& ms = sg->getMimicJointModels();
for (const JointModel* m : ms)
jset.insert(m);
}
}
if (jset.empty())
{
RCLCPP_WARN(LOGGER, "Group '%s' must have at least one valid joint", gc.name_.c_str());
return false;
}
std::vector<const JointModel*> joints;
joints.reserve(jset.size());
for (const JointModel* it : jset)
joints.push_back(it);
JointModelGroup* jmg = new JointModelGroup(gc.name_, gc, joints, this);
joint_model_group_map_[gc.name_] = jmg;
return true;
}
JointModel* RobotModel::buildRecursive(LinkModel* parent, const urdf::Link* urdf_link, const srdf::Model& srdf_model)
{
// construct the joint
JointModel* joint = urdf_link->parent_joint ?
constructJointModel(urdf_link->parent_joint.get(), urdf_link, srdf_model) :
constructJointModel(nullptr, urdf_link, srdf_model);
if (joint == nullptr)
return nullptr;
// bookkeeping for the joint
joint_model_map_[joint->getName()] = joint;
joint->setJointIndex(joint_model_vector_.size());
joint_model_vector_.push_back(joint);
joint_model_vector_const_.push_back(joint);
joint_model_names_vector_.push_back(joint->getName());
joint->setParentLinkModel(parent);
// construct the link
LinkModel* link = constructLinkModel(urdf_link);
joint->setChildLinkModel(link);
link->setParentLinkModel(parent);
// bookkeeping for the link
link_model_map_[joint->getChildLinkModel()->getName()] = link;
link->setLinkIndex(link_model_vector_.size());
link_model_vector_.push_back(link);
link_model_vector_const_.push_back(link);
link_model_names_vector_.push_back(link->getName());
if (!link->getShapes().empty())
{
link_models_with_collision_geometry_vector_.push_back(link);
link_model_names_with_collision_geometry_vector_.push_back(link->getName());
link->setFirstCollisionBodyTransformIndex(link_geometry_count_);
link_geometry_count_ += link->getShapes().size();
}
link->setParentJointModel(joint);
// recursively build child links (and joints)
for (const urdf::LinkSharedPtr& child_link : urdf_link->child_links)
{
JointModel* jm = buildRecursive(link, child_link.get(), srdf_model);
if (jm)
link->addChildJointModel(jm);
}
return joint;
}
namespace
{
// construct bounds for 1DOF joint
static inline VariableBounds jointBoundsFromURDF(const urdf::Joint* urdf_joint)
{
VariableBounds b;
if (urdf_joint->safety)
{
b.position_bounded_ = true;
b.min_position_ = urdf_joint->safety->soft_lower_limit;
b.max_position_ = urdf_joint->safety->soft_upper_limit;
if (urdf_joint->limits)
{
if (urdf_joint->limits->lower > b.min_position_)
b.min_position_ = urdf_joint->limits->lower;
if (urdf_joint->limits->upper < b.max_position_)
b.max_position_ = urdf_joint->limits->upper;
}
}
else
{
if (urdf_joint->limits)
{
b.position_bounded_ = true;
b.min_position_ = urdf_joint->limits->lower;
b.max_position_ = urdf_joint->limits->upper;
}
}
if (urdf_joint->limits)
{
b.max_velocity_ = fabs(urdf_joint->limits->velocity);
b.min_velocity_ = -b.max_velocity_;
b.velocity_bounded_ = b.max_velocity_ > std::numeric_limits<double>::epsilon();
}
return b;
}
} // namespace
JointModel* RobotModel::constructJointModel(const urdf::Joint* urdf_joint, const urdf::Link* child_link,
const srdf::Model& srdf_model)
{
JointModel* new_joint_model = nullptr;
// if urdf_joint exists, must be the root link transform
if (urdf_joint)
{
switch (urdf_joint->type)
{
case urdf::Joint::REVOLUTE:
{
RevoluteJointModel* j = new RevoluteJointModel(urdf_joint->name);
j->setVariableBounds(j->getName(), jointBoundsFromURDF(urdf_joint));
j->setContinuous(false);
j->setAxis(Eigen::Vector3d(urdf_joint->axis.x, urdf_joint->axis.y, urdf_joint->axis.z));
new_joint_model = j;
}
break;
case urdf::Joint::CONTINUOUS:
{
RevoluteJointModel* j = new RevoluteJointModel(urdf_joint->name);
j->setVariableBounds(j->getName(), jointBoundsFromURDF(urdf_joint));
j->setContinuous(true);
j->setAxis(Eigen::Vector3d(urdf_joint->axis.x, urdf_joint->axis.y, urdf_joint->axis.z));
new_joint_model = j;
}
break;
case urdf::Joint::PRISMATIC:
{
PrismaticJointModel* j = new PrismaticJointModel(urdf_joint->name);
j->setVariableBounds(j->getName(), jointBoundsFromURDF(urdf_joint));
j->setAxis(Eigen::Vector3d(urdf_joint->axis.x, urdf_joint->axis.y, urdf_joint->axis.z));
new_joint_model = j;
}
break;
case urdf::Joint::FLOATING:
new_joint_model = new FloatingJointModel(urdf_joint->name);
break;
case urdf::Joint::PLANAR:
new_joint_model = new PlanarJointModel(urdf_joint->name);
break;
case urdf::Joint::FIXED:
new_joint_model = new FixedJointModel(urdf_joint->name);
break;
default:
RCLCPP_ERROR(LOGGER, "Unknown joint type: %d", (int)urdf_joint->type);
break;
}
}
else // if urdf_joint passed in as null, then we're at root of URDF model
{
const std::vector<srdf::Model::VirtualJoint>& virtual_joints = srdf_model.getVirtualJoints();
for (const srdf::Model::VirtualJoint& virtual_joint : virtual_joints)
{
if (virtual_joint.child_link_ != child_link->name)
{
RCLCPP_WARN(LOGGER,
"Skipping virtual joint '%s' because its child frame '%s' "
"does not match the URDF frame '%s'",
virtual_joint.name_.c_str(), virtual_joint.child_link_.c_str(), child_link->name.c_str());
}
else if (virtual_joint.parent_frame_.empty())
{
RCLCPP_WARN(LOGGER, "Skipping virtual joint '%s' because its parent frame is empty",
virtual_joint.name_.c_str());
}
else
{
if (virtual_joint.type_ == "fixed")
new_joint_model = new FixedJointModel(virtual_joint.name_);
else if (virtual_joint.type_ == "planar")
new_joint_model = new PlanarJointModel(virtual_joint.name_);
else if (virtual_joint.type_ == "floating")
new_joint_model = new FloatingJointModel(virtual_joint.name_);
if (new_joint_model)
{
// for fixed frames we still use the robot root link
if (virtual_joint.type_ != "fixed")
{
model_frame_ = virtual_joint.parent_frame_;
}
break;
}
}
}
if (!new_joint_model)
{
RCLCPP_INFO(LOGGER, "No root/virtual joint specified in SRDF. Assuming fixed joint");
new_joint_model = new FixedJointModel("ASSUMED_FIXED_ROOT_JOINT");
}
}
if (new_joint_model)
{
new_joint_model->setDistanceFactor(new_joint_model->getStateSpaceDimension());
const std::vector<srdf::Model::PassiveJoint>& pjoints = srdf_model.getPassiveJoints();
for (const srdf::Model::PassiveJoint& pjoint : pjoints)
{
if (new_joint_model->getName() == pjoint.name_)
{
new_joint_model->setPassive(true);
break;
}
}
for (const srdf::Model::JointProperty& property : srdf_model.getJointProperties(new_joint_model->getName()))
{
if (property.property_name_ == "angular_distance_weight")
{
double angular_distance_weight;
try
{
std::string::size_type sz;
angular_distance_weight = std::stod(property.value_, &sz);
if (sz != property.value_.size())
{
RCLCPP_WARN_STREAM(LOGGER, "Extra characters after property " << property.property_name_ << " for joint "
<< property.joint_name_ << " as double: '"
<< property.value_.substr(sz) << "'");
}
}
catch (const std::invalid_argument& e)
{
RCLCPP_ERROR_STREAM(LOGGER, "Unable to parse property " << property.property_name_ << " for joint "
<< property.joint_name_ << " as double: '"
<< property.value_ << "'");