This repository has been archived by the owner on Nov 13, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 76
/
planning_scene.cpp
2188 lines (1922 loc) · 79.5 KB
/
planning_scene.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) 2011, 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 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 <boost/algorithm/string.hpp>
#include <moveit/planning_scene/planning_scene.h>
#include <moveit/collision_detection_fcl/collision_detector_allocator_fcl.h>
#include <geometric_shapes/shape_operations.h>
#include <moveit/collision_detection/collision_tools.h>
#include <moveit/trajectory_processing/trajectory_tools.h>
#include <moveit/robot_state/conversions.h>
#include <moveit/exceptions/exceptions.h>
#include <octomap_msgs/conversions.h>
#include <eigen_conversions/eigen_msg.h>
#include <set>
namespace planning_scene
{
const std::string PlanningScene::OCTOMAP_NS = "<octomap>";
const std::string PlanningScene::DEFAULT_SCENE_NAME = "(noname)";
class SceneTransforms : public robot_state::Transforms
{
public:
SceneTransforms(const PlanningScene *scene) :
Transforms(scene->getRobotModel()->getModelFrame()),
scene_(scene)
{
}
virtual bool canTransform(const std::string &from_frame) const
{
return scene_->knowsFrameTransform(from_frame);
}
virtual bool isFixedFrame(const std::string &frame) const
{
if (frame.empty())
return false;
if (Transforms::isFixedFrame(frame))
return true;
if (frame[0] == '/')
return knowsObject(frame.substr(1));
else
return knowsObject(frame);
}
virtual const Eigen::Affine3d& getTransform(const std::string &from_frame) const
{ // the call below also calls Transforms::getTransform() too
return scene_->getFrameTransform(from_frame);
}
private:
bool knowsObject(const std::string &id) const
{
if (scene_->getWorld()->hasObject(id))
{
collision_detection::World::ObjectConstPtr obj = scene_->getWorld()->getObject(id);
return obj->shape_poses_.size() == 1;
}
return false;
}
const PlanningScene *scene_;
};
}
bool planning_scene::PlanningScene::isEmpty(const moveit_msgs::PlanningScene &msg)
{
return msg.name.empty() && msg.fixed_frame_transforms.empty() && msg.allowed_collision_matrix.entry_names.empty() &&
msg.link_padding.empty() && msg.link_scale.empty() && isEmpty(msg.robot_state) && isEmpty(msg.world);
}
bool planning_scene::PlanningScene::isEmpty(const moveit_msgs::RobotState &msg)
{
/* a state is empty if it includes no information and it is a diff; if the state is not a diff, then the implicit information is
that the set of attached bodies is empty, so they must be cleared from the state to be updated */
return msg.is_diff == true && msg.multi_dof_joint_state.joint_names.empty() && msg.joint_state.name.empty() &&
msg.attached_collision_objects.empty() && msg.joint_state.position.empty() && msg.joint_state.velocity.empty() &&
msg.joint_state.effort.empty() && msg.multi_dof_joint_state.transforms.empty() && msg.multi_dof_joint_state.twist.empty() &&
msg.multi_dof_joint_state.wrench.empty();
}
bool planning_scene::PlanningScene::isEmpty(const moveit_msgs::PlanningSceneWorld &msg)
{
return msg.collision_objects.empty() && msg.octomap.octomap.data.empty();
}
planning_scene::PlanningScene::PlanningScene(const robot_model::RobotModelConstPtr &robot_model,
collision_detection::WorldPtr world) :
kmodel_(robot_model),
world_(world),
world_const_(world)
{
initialize();
}
planning_scene::PlanningScene::PlanningScene(const boost::shared_ptr<const urdf::ModelInterface> &urdf_model,
const boost::shared_ptr<const srdf::Model> &srdf_model,
collision_detection::WorldPtr world) :
world_(world),
world_const_(world)
{
if (!urdf_model)
throw moveit::ConstructException("The URDF model cannot be NULL");
if (!srdf_model)
throw moveit::ConstructException("The SRDF model cannot be NULL");
kmodel_ = createRobotModel(urdf_model, srdf_model);
if (!kmodel_)
throw moveit::ConstructException("Could not create RobotModel");
initialize();
}
planning_scene::PlanningScene::~PlanningScene()
{
if (current_world_object_update_callback_)
world_->removeObserver(current_world_object_update_observer_handle_);
}
void planning_scene::PlanningScene::initialize()
{
name_ = DEFAULT_SCENE_NAME;
ftf_.reset(new SceneTransforms(this));
kstate_.reset(new robot_state::RobotState(kmodel_));
kstate_->setToDefaultValues();
acm_.reset(new collision_detection::AllowedCollisionMatrix());
// Use default collision operations in the SRDF to setup the acm
const std::vector<std::string>& collision_links = kmodel_->getLinkModelNamesWithCollisionGeometry();
acm_->setEntry(collision_links, collision_links, false);
// allow collisions for pairs that have been disabled
const std::vector<srdf::Model::DisabledCollision> &dc = getRobotModel()->getSRDF()->getDisabledCollisionPairs();
for (std::vector<srdf::Model::DisabledCollision>::const_iterator it = dc.begin(); it != dc.end(); ++it)
acm_->setEntry(it->link1_, it->link2_, true);
setActiveCollisionDetector(collision_detection::CollisionDetectorAllocatorFCL::create());
}
/* return NULL on failure */
robot_model::RobotModelPtr planning_scene::PlanningScene::createRobotModel(const boost::shared_ptr<const urdf::ModelInterface> &urdf_model,
const boost::shared_ptr<const srdf::Model> &srdf_model)
{
robot_model::RobotModelPtr robot_model(new robot_model::RobotModel(urdf_model, srdf_model));
if (!robot_model || !robot_model->getRootJoint())
return robot_model::RobotModelPtr();
return robot_model;
}
planning_scene::PlanningScene::PlanningScene(const PlanningSceneConstPtr &parent) :
parent_(parent)
{
if (!parent_)
throw moveit::ConstructException("NULL parent pointer for planning scene");
if (!parent_->getName().empty())
name_ = parent_->getName() + "+";
kmodel_ = parent_->kmodel_;
// maintain a separate world. Copy on write ensures that most of the object
// info is shared until it is modified.
world_.reset(new collision_detection::World(*parent_->world_));
world_const_ = world_;
// record changes to the world
world_diff_.reset(new collision_detection::WorldDiff(world_));
// Set up the same collision detectors as the parent
for (CollisionDetectorConstIterator it = parent_->collision_.begin() ; it != parent_->collision_.end() ; ++it)
{
const CollisionDetectorPtr& parent_detector = it->second;
CollisionDetectorPtr& detector = collision_[it->first];
detector.reset(new CollisionDetector());
detector->alloc_ = parent_detector->alloc_;
detector->parent_ = parent_detector;
detector->cworld_ = detector->alloc_->allocateWorld(parent_detector->cworld_, world_);
detector->cworld_const_ = detector->cworld_;
// leave these empty and use parent collision_robot_ unless/until a non-const one
// is requested (e.g. to modify link padding or scale)
detector->crobot_.reset();
detector->crobot_const_.reset();
detector->crobot_unpadded_.reset();
detector->crobot_unpadded_const_.reset();
}
setActiveCollisionDetector(parent_->getActiveCollisionDetectorName());
}
planning_scene::PlanningScenePtr planning_scene::PlanningScene::clone(const planning_scene::PlanningSceneConstPtr &scene)
{
PlanningScenePtr result = scene->diff();
result->decoupleParent();
result->setName(scene->getName());
return result;
}
planning_scene::PlanningScenePtr planning_scene::PlanningScene::diff() const
{
return PlanningScenePtr(new PlanningScene(shared_from_this()));
}
planning_scene::PlanningScenePtr planning_scene::PlanningScene::diff(const moveit_msgs::PlanningScene &msg) const
{
planning_scene::PlanningScenePtr result = diff();
result->setPlanningSceneDiffMsg(msg);
return result;
}
void planning_scene::PlanningScene::CollisionDetector::copyPadding(const planning_scene::PlanningScene::CollisionDetector& src)
{
if (!crobot_)
{
alloc_->allocateRobot(parent_->getCollisionRobot());
crobot_const_ = crobot_;
}
crobot_->setLinkPadding(src.getCollisionRobot()->getLinkPadding());
crobot_->setLinkScale(src.getCollisionRobot()->getLinkScale());
}
void planning_scene::PlanningScene::propogateRobotPadding()
{
if (!active_collision_->crobot_)
return;
for (CollisionDetectorIterator it = collision_.begin() ; it != collision_.end() ; ++it)
{
if (it->second != active_collision_)
it->second->copyPadding(*active_collision_);
}
}
void planning_scene::PlanningScene::CollisionDetector::findParent(const PlanningScene& scene)
{
if (parent_ || !scene.parent_)
return;
CollisionDetectorConstIterator it = scene.parent_->collision_.find(alloc_->getName());
if (it != scene.parent_->collision_.end())
parent_ = it->second->parent_;
}
void planning_scene::PlanningScene::addCollisionDetector(const collision_detection::CollisionDetectorAllocatorPtr& allocator)
{
const std::string& name = allocator->getName();
CollisionDetectorPtr& detector = collision_[name];
if (detector) // already added this one
return;
detector.reset(new CollisionDetector());
detector->alloc_ = allocator;
if (!active_collision_)
active_collision_ = detector;
detector->findParent(*this);
detector->cworld_ = detector->alloc_->allocateWorld(world_);
detector->cworld_const_ = detector->cworld_;
// Allocate CollisionRobot unless we can use the parent's crobot_.
// If active_collision_->crobot_ is non-NULL there is local padding and we cannot use the parent's crobot_.
if (!detector->parent_ || active_collision_->crobot_)
{
detector->crobot_ = detector->alloc_->allocateRobot(getRobotModel());
detector->crobot_const_ = detector->crobot_;
if (detector != active_collision_)
detector->copyPadding(*active_collision_);
}
// Allocate CollisionRobot unless we can use the parent's crobot_unpadded_.
if (!detector->parent_)
{
detector->crobot_unpadded_ = detector->alloc_->allocateRobot(getRobotModel());
detector->crobot_unpadded_const_ = detector->crobot_unpadded_;
}
}
void planning_scene::PlanningScene::setActiveCollisionDetector(const collision_detection::CollisionDetectorAllocatorPtr& allocator,
bool exclusive)
{
if (exclusive)
{
CollisionDetectorPtr p;
CollisionDetectorIterator it = collision_.find(allocator->getName());
if (it != collision_.end())
p = it->second;
collision_.clear();
active_collision_.reset();
if (p)
{
collision_[allocator->getName()] = p;
active_collision_ = p;
return;
}
}
addCollisionDetector(allocator);
setActiveCollisionDetector(allocator->getName());
}
bool planning_scene::PlanningScene::setActiveCollisionDetector(const std::string& collision_detector_name)
{
CollisionDetectorIterator it = collision_.find(collision_detector_name);
if (it != collision_.end())
{
active_collision_ = it->second;
return true;
}
else
{
logError("Cannot setActiveCollisionDetector to '%s' -- it has been added to PlanningScene. Keeping existing active collision detector '%s'",
collision_detector_name.c_str(),
active_collision_->alloc_->getName().c_str());
return false;
}
}
void planning_scene::PlanningScene::getCollisionDetectorNames(std::vector<std::string>& names) const
{
names.clear();
names.reserve(collision_.size());
for (CollisionDetectorConstIterator it = collision_.begin() ; it != collision_.end() ; ++it)
names.push_back(it->first);
}
const collision_detection::CollisionWorldConstPtr& planning_scene::PlanningScene::getCollisionWorld(
const std::string& collision_detector_name) const
{
CollisionDetectorConstIterator it = collision_.find(collision_detector_name);
if (it == collision_.end())
{
logError("Could not get CollisionWorld named '%s'. Returning active CollisionWorld '%s' instead",
collision_detector_name.c_str(),
active_collision_->alloc_->getName().c_str());
return active_collision_->cworld_const_;
}
return it->second->cworld_const_;
}
const collision_detection::CollisionRobotConstPtr& planning_scene::PlanningScene::getCollisionRobot(
const std::string& collision_detector_name) const
{
CollisionDetectorConstIterator it = collision_.find(collision_detector_name);
if (it == collision_.end())
{
logError("Could not get CollisionRobot named '%s'. Returning active CollisionRobot '%s' instead",
collision_detector_name.c_str(),
active_collision_->alloc_->getName().c_str());
return active_collision_->getCollisionRobot();
}
return it->second->getCollisionRobot();
}
const collision_detection::CollisionRobotConstPtr& planning_scene::PlanningScene::getCollisionRobotUnpadded(
const std::string& collision_detector_name) const
{
CollisionDetectorConstIterator it = collision_.find(collision_detector_name);
if (it == collision_.end())
{
logError("Could not get CollisionRobotUnpadded named '%s'. Returning active CollisionRobotUnpadded '%s' instead",
collision_detector_name.c_str(),
active_collision_->alloc_->getName().c_str());
return active_collision_->getCollisionRobotUnpadded();
}
return it->second->getCollisionRobotUnpadded();
}
void planning_scene::PlanningScene::clearDiffs()
{
if (!parent_)
return;
// clear everything, reset the world, record diffs
world_.reset(new collision_detection::World(*parent_->world_));
world_const_ = world_;
world_diff_.reset(new collision_detection::WorldDiff(world_));
if (current_world_object_update_callback_)
current_world_object_update_observer_handle_ = world_->addObserver(current_world_object_update_callback_);
// use parent crobot_ if it exists. Otherwise copy padding from parent.
for (CollisionDetectorIterator it = collision_.begin() ; it != collision_.end() ; ++it)
{
if (!it->second->parent_)
it->second->findParent(*this);
if (it->second->parent_)
{
it->second->crobot_.reset();
it->second->crobot_const_.reset();
it->second->crobot_unpadded_.reset();
it->second->crobot_unpadded_const_.reset();
it->second->cworld_ = it->second->alloc_->allocateWorld(it->second->parent_->cworld_, world_);
it->second->cworld_const_ = it->second->cworld_;
}
else
{
it->second->copyPadding(*parent_->active_collision_);
it->second->cworld_ = it->second->alloc_->allocateWorld(world_);
it->second->cworld_const_ = it->second->cworld_;
}
}
ftf_.reset();
kstate_.reset();
acm_.reset();
object_colors_.reset();
object_types_.reset();
}
void planning_scene::PlanningScene::pushDiffs(const PlanningScenePtr &scene)
{
if (!parent_)
return;
if (ftf_)
scene->getTransformsNonConst().setAllTransforms(ftf_->getAllTransforms());
if (kstate_)
{
scene->getCurrentStateNonConst() = *kstate_;
// push colors and types for attached objects
std::vector<const moveit::core::AttachedBody*> attached_objs;
kstate_->getAttachedBodies(attached_objs);
for(std::vector<const moveit::core::AttachedBody*>::const_iterator it = attached_objs.begin();
it != attached_objs.end(); ++it)
{
if(hasObjectType((*it)->getName()))
scene->setObjectType((*it)->getName(), getObjectType((*it)->getName()));
if(hasObjectColor((*it)->getName()))
scene->setObjectColor((*it)->getName(), getObjectColor((*it)->getName()));
}
}
if (acm_)
scene->getAllowedCollisionMatrixNonConst() = *acm_;
if (active_collision_->crobot_)
{
collision_detection::CollisionRobotPtr active_crobot = scene->getCollisionRobotNonConst();
active_crobot->setLinkPadding(active_collision_->crobot_->getLinkPadding());
active_crobot->setLinkScale(active_collision_->crobot_->getLinkScale());
scene->propogateRobotPadding();
}
if (world_diff_)
{
for (collision_detection::WorldDiff::const_iterator it = world_diff_->begin() ; it != world_diff_->end() ; ++it)
{
if (it->second == collision_detection::World::DESTROY)
{
scene->world_->removeObject(it->first);
scene->removeObjectColor(it->first);
}
else
{
const collision_detection::World::Object& obj = *world_->getObject(it->first);
scene->world_->removeObject(obj.id_);
scene->world_->addToObject(obj.id_, obj.shapes_, obj.shape_poses_);
if (hasObjectColor(it->first))
scene->setObjectColor(it->first, getObjectColor(it->first));
if (hasObjectType(it->first))
scene->setObjectType(it->first, getObjectType(it->first));
}
}
}
}
void planning_scene::PlanningScene::checkCollision(const collision_detection::CollisionRequest& req, collision_detection::CollisionResult &res)
{
if (getCurrentState().dirtyCollisionBodyTransforms())
checkCollision(req, res, getCurrentStateNonConst());
else
checkCollision(req, res, getCurrentState());
}
void planning_scene::PlanningScene::checkCollision(const collision_detection::CollisionRequest& req, collision_detection::CollisionResult &res,
const robot_state::RobotState &kstate) const
{
// check collision with the world using the padded version
getCollisionWorld()->checkRobotCollision(req, res, *getCollisionRobot(), kstate, getAllowedCollisionMatrix());
if (!res.collision || (req.contacts && res.contacts.size() < req.max_contacts))
{
// do self-collision checking with the unpadded version of the robot
getCollisionRobotUnpadded()->checkSelfCollision(req, res, kstate, getAllowedCollisionMatrix());
}
}
void planning_scene::PlanningScene::checkSelfCollision(const collision_detection::CollisionRequest& req, collision_detection::CollisionResult &res)
{
if (getCurrentState().dirtyCollisionBodyTransforms())
checkSelfCollision(req, res, getCurrentStateNonConst());
else
checkSelfCollision(req, res, getCurrentState());
}
void planning_scene::PlanningScene::checkCollision(const collision_detection::CollisionRequest& req,
collision_detection::CollisionResult &res,
const robot_state::RobotState &kstate,
const collision_detection::AllowedCollisionMatrix& acm) const
{
// check collision with the world using the padded version
getCollisionWorld()->checkRobotCollision(req, res, *getCollisionRobot(), kstate, acm);
// do self-collision checking with the unpadded version of the robot
if (!res.collision || (req.contacts && res.contacts.size() < req.max_contacts))
getCollisionRobotUnpadded()->checkSelfCollision(req, res, kstate, acm);
}
void planning_scene::PlanningScene::checkCollisionUnpadded(const collision_detection::CollisionRequest& req,
collision_detection::CollisionResult &res)
{
if (getCurrentState().dirtyCollisionBodyTransforms())
checkCollisionUnpadded(req, res, getCurrentStateNonConst(), getAllowedCollisionMatrix());
else
checkCollisionUnpadded(req, res, getCurrentState(), getAllowedCollisionMatrix());
}
void planning_scene::PlanningScene::checkCollisionUnpadded(const collision_detection::CollisionRequest& req,
collision_detection::CollisionResult &res,
const robot_state::RobotState &kstate,
const collision_detection::AllowedCollisionMatrix& acm) const
{
// check collision with the world using the unpadded version
getCollisionWorld()->checkRobotCollision(req, res, *getCollisionRobotUnpadded(), kstate, acm);
// do self-collision checking with the unpadded version of the robot
if (!res.collision || (req.contacts && res.contacts.size() < req.max_contacts))
{
getCollisionRobotUnpadded()->checkSelfCollision(req, res, kstate, acm);
}
}
void planning_scene::PlanningScene::getCollidingPairs(collision_detection::CollisionResult::ContactMap &contacts)
{
if (getCurrentState().dirtyCollisionBodyTransforms())
getCollidingPairs(contacts, getCurrentStateNonConst(), getAllowedCollisionMatrix());
else
getCollidingPairs(contacts, getCurrentState(), getAllowedCollisionMatrix());
}
void planning_scene::PlanningScene::getCollidingPairs(collision_detection::CollisionResult::ContactMap &contacts,
const robot_state::RobotState &kstate,
const collision_detection::AllowedCollisionMatrix& acm) const
{
collision_detection::CollisionRequest req;
req.contacts = true;
req.max_contacts = getRobotModel()->getLinkModelsWithCollisionGeometry().size() + 1;
req.max_contacts_per_pair = 1;
collision_detection::CollisionResult res;
checkCollision(req, res, kstate, acm);
res.contacts.swap(contacts);
}
void planning_scene::PlanningScene::getCollidingLinks(std::vector<std::string> &links)
{
if (getCurrentState().dirtyCollisionBodyTransforms())
getCollidingLinks(links, getCurrentStateNonConst(), getAllowedCollisionMatrix());
else
getCollidingLinks(links, getCurrentState(), getAllowedCollisionMatrix());
}
void planning_scene::PlanningScene::getCollidingLinks(std::vector<std::string> &links,
const robot_state::RobotState &kstate,
const collision_detection::AllowedCollisionMatrix& acm) const
{
collision_detection::CollisionResult::ContactMap contacts;
getCollidingPairs(contacts, kstate, acm);
links.clear();
for (collision_detection::CollisionResult::ContactMap::const_iterator it = contacts.begin() ; it != contacts.end() ; ++it)
for (std::size_t j = 0 ; j < it->second.size() ; ++j)
{
if (it->second[j].body_type_1 == collision_detection::BodyTypes::ROBOT_LINK)
links.push_back(it->second[j].body_name_1);
if (it->second[j].body_type_2 == collision_detection::BodyTypes::ROBOT_LINK)
links.push_back(it->second[j].body_name_2);
}
}
const collision_detection::CollisionRobotPtr& planning_scene::PlanningScene::getCollisionRobotNonConst()
{
if (!active_collision_->crobot_)
{
active_collision_->crobot_ = active_collision_->alloc_->allocateRobot(active_collision_->parent_->getCollisionRobot());
active_collision_->crobot_const_ = active_collision_->crobot_;
}
return active_collision_->crobot_;
}
robot_state::RobotState& planning_scene::PlanningScene::getCurrentStateNonConst()
{
if (!kstate_)
{
kstate_.reset(new robot_state::RobotState(parent_->getCurrentState()));
kstate_->setAttachedBodyUpdateCallback(current_state_attached_body_callback_);
}
kstate_->update();
return *kstate_;
}
robot_state::RobotStatePtr planning_scene::PlanningScene::getCurrentStateUpdated(const moveit_msgs::RobotState &update) const
{
robot_state::RobotStatePtr state(new robot_state::RobotState(getCurrentState()));
robot_state::robotStateMsgToRobotState(getTransforms(), update, *state);
return state;
}
void planning_scene::PlanningScene::setAttachedBodyUpdateCallback(const robot_state::AttachedBodyCallback &callback)
{
current_state_attached_body_callback_ = callback;
if (kstate_)
kstate_->setAttachedBodyUpdateCallback(callback);
}
void planning_scene::PlanningScene::setCollisionObjectUpdateCallback(const collision_detection::World::ObserverCallbackFn &callback)
{
if (current_world_object_update_callback_)
world_->removeObserver(current_world_object_update_observer_handle_);
if (callback)
current_world_object_update_observer_handle_ = world_->addObserver(callback);
current_world_object_update_callback_ = callback;
}
collision_detection::AllowedCollisionMatrix& planning_scene::PlanningScene::getAllowedCollisionMatrixNonConst()
{
if (!acm_)
acm_.reset(new collision_detection::AllowedCollisionMatrix(parent_->getAllowedCollisionMatrix()));
return *acm_;
}
const robot_state::Transforms& planning_scene::PlanningScene::getTransforms()
{
getCurrentStateNonConst().update();
return const_cast<const PlanningScene*>(this)->getTransforms();
}
robot_state::Transforms& planning_scene::PlanningScene::getTransformsNonConst()
{
getCurrentStateNonConst().update();
if (!ftf_)
{
ftf_.reset(new SceneTransforms(this));
ftf_->setAllTransforms(parent_->getTransforms().getAllTransforms());
}
return *ftf_;
}
void planning_scene::PlanningScene::getPlanningSceneDiffMsg(moveit_msgs::PlanningScene &scene_msg) const
{
scene_msg.name = name_;
scene_msg.robot_model_name = getRobotModel()->getName();
scene_msg.is_diff = true;
if (ftf_)
ftf_->copyTransforms(scene_msg.fixed_frame_transforms);
else
scene_msg.fixed_frame_transforms.clear();
if (kstate_)
robot_state::robotStateToRobotStateMsg(*kstate_, scene_msg.robot_state);
else
scene_msg.robot_state = moveit_msgs::RobotState();
if (acm_)
acm_->getMessage(scene_msg.allowed_collision_matrix);
else
scene_msg.allowed_collision_matrix = moveit_msgs::AllowedCollisionMatrix();
if (active_collision_->crobot_)
{
active_collision_->crobot_->getPadding(scene_msg.link_padding);
active_collision_->crobot_->getScale(scene_msg.link_scale);
}
else
{
scene_msg.link_padding.clear();
scene_msg.link_scale.clear();
}
scene_msg.object_colors.clear();
if (object_colors_)
{
unsigned int i = 0;
scene_msg.object_colors.resize(object_colors_->size());
for (ObjectColorMap::const_iterator it = object_colors_->begin() ; it != object_colors_->end() ; ++it, ++i)
{
scene_msg.object_colors[i].id = it->first;
scene_msg.object_colors[i].color = it->second;
}
}
scene_msg.world.collision_objects.clear();
scene_msg.world.octomap = octomap_msgs::OctomapWithPose();
if (world_diff_)
{
bool do_omap = false;
for (collision_detection::WorldDiff::const_iterator it = world_diff_->begin() ; it != world_diff_->end() ; ++it)
{
if (it->first == OCTOMAP_NS)
do_omap = true;
else if (it->second == collision_detection::World::DESTROY)
{
moveit_msgs::CollisionObject co;
co.header.frame_id = getPlanningFrame();
co.id = it->first;
co.operation = moveit_msgs::CollisionObject::REMOVE;
scene_msg.world.collision_objects.push_back(co);
}
else
getPlanningSceneMsgCollisionObject(scene_msg, it->first);
}
if (do_omap)
getPlanningSceneMsgOctomap(scene_msg);
}
}
namespace planning_scene
{
namespace
{
class ShapeVisitorAddToCollisionObject : public boost::static_visitor<void>
{
public:
ShapeVisitorAddToCollisionObject(moveit_msgs::CollisionObject *obj) :
boost::static_visitor<void>(), obj_(obj)
{
}
void setPoseMessage(const geometry_msgs::Pose *pose)
{
pose_ = pose;
}
void operator()(const shape_msgs::Plane &shape_msg) const
{
obj_->planes.push_back(shape_msg);
obj_->plane_poses.push_back(*pose_);
}
void operator()(const shape_msgs::Mesh &shape_msg) const
{
obj_->meshes.push_back(shape_msg);
obj_->mesh_poses.push_back(*pose_);
}
void operator()(const shape_msgs::SolidPrimitive &shape_msg) const
{
obj_->primitives.push_back(shape_msg);
obj_->primitive_poses.push_back(*pose_);
}
private:
moveit_msgs::CollisionObject *obj_;
const geometry_msgs::Pose *pose_;
};
}
}
void planning_scene::PlanningScene::getPlanningSceneMsgCollisionObject(moveit_msgs::PlanningScene &scene_msg, const std::string &ns) const
{
moveit_msgs::CollisionObject co;
co.header.frame_id = getPlanningFrame();
co.id = ns;
co.operation = moveit_msgs::CollisionObject::ADD;
collision_detection::CollisionWorld::ObjectConstPtr obj = world_->getObject(ns);
if (!obj)
return;
ShapeVisitorAddToCollisionObject sv(&co);
for (std::size_t j = 0 ; j < obj->shapes_.size() ; ++j)
{
shapes::ShapeMsg sm;
if (constructMsgFromShape(obj->shapes_[j].get(), sm))
{
geometry_msgs::Pose p;
tf::poseEigenToMsg(obj->shape_poses_[j], p);
sv.setPoseMessage(&p);
boost::apply_visitor(sv, sm);
}
}
if (!co.primitives.empty() || !co.meshes.empty() || !co.planes.empty())
{
if (hasObjectType(co.id))
co.type = getObjectType(co.id);
scene_msg.world.collision_objects.push_back(co);
}
}
void planning_scene::PlanningScene::getPlanningSceneMsgCollisionObjects(moveit_msgs::PlanningScene &scene_msg) const
{
scene_msg.world.collision_objects.clear();
const std::vector<std::string> &ns = world_->getObjectIds();
for (std::size_t i = 0 ; i < ns.size() ; ++i)
if (ns[i] != OCTOMAP_NS)
getPlanningSceneMsgCollisionObject(scene_msg, ns[i]);
}
void planning_scene::PlanningScene::getPlanningSceneMsgOctomap(moveit_msgs::PlanningScene &scene_msg) const
{
scene_msg.world.octomap.header.frame_id = getPlanningFrame();
scene_msg.world.octomap.octomap = octomap_msgs::Octomap();
collision_detection::CollisionWorld::ObjectConstPtr map = world_->getObject(OCTOMAP_NS);
if (map)
{
if (map->shapes_.size() == 1)
{
const shapes::OcTree *o = static_cast<const shapes::OcTree*>(map->shapes_[0].get());
octomap_msgs::fullMapToMsg(*o->octree, scene_msg.world.octomap.octomap);
tf::poseEigenToMsg(map->shape_poses_[0], scene_msg.world.octomap.origin);
}
else
logError("Unexpected number of shapes in octomap collision object. Not including '%s' object", OCTOMAP_NS.c_str());
}
}
void planning_scene::PlanningScene::getPlanningSceneMsg(moveit_msgs::PlanningScene &scene_msg) const
{
scene_msg.name = name_;
scene_msg.is_diff = false;
scene_msg.robot_model_name = getRobotModel()->getName();
getTransforms().copyTransforms(scene_msg.fixed_frame_transforms);
robot_state::robotStateToRobotStateMsg(getCurrentState(), scene_msg.robot_state);
getAllowedCollisionMatrix().getMessage(scene_msg.allowed_collision_matrix);
getCollisionRobot()->getPadding(scene_msg.link_padding);
getCollisionRobot()->getScale(scene_msg.link_scale);
getPlanningSceneMsgObjectColors(scene_msg);
// add collision objects
getPlanningSceneMsgCollisionObjects(scene_msg);
// get the octomap
getPlanningSceneMsgOctomap(scene_msg);
}
void planning_scene::PlanningScene::getPlanningSceneMsgObjectColors(moveit_msgs::PlanningScene &scene_msg) const
{
scene_msg.object_colors.clear();
unsigned int i = 0;
ObjectColorMap cmap;
getKnownObjectColors(cmap);
scene_msg.object_colors.resize(cmap.size());
for (ObjectColorMap::const_iterator it = cmap.begin() ; it != cmap.end() ; ++it, ++i)
{
scene_msg.object_colors[i].id = it->first;
scene_msg.object_colors[i].color = it->second;
}
}
void planning_scene::PlanningScene::getPlanningSceneMsg(moveit_msgs::PlanningScene &scene_msg, const moveit_msgs::PlanningSceneComponents &comp) const
{
scene_msg.is_diff = false;
if (comp.components & moveit_msgs::PlanningSceneComponents::SCENE_SETTINGS)
{
scene_msg.name = name_;
scene_msg.robot_model_name = getRobotModel()->getName();
}
if (comp.components & moveit_msgs::PlanningSceneComponents::TRANSFORMS)
getTransforms().copyTransforms(scene_msg.fixed_frame_transforms);
if (comp.components & moveit_msgs::PlanningSceneComponents::ROBOT_STATE_ATTACHED_OBJECTS)
{
robot_state::robotStateToRobotStateMsg(getCurrentState(), scene_msg.robot_state, true);
for(std::vector<moveit_msgs::AttachedCollisionObject>::iterator it = scene_msg.robot_state.attached_collision_objects.begin();
it != scene_msg.robot_state.attached_collision_objects.end(); ++it)
{
if(hasObjectType(it->object.id))
{
it->object.type = getObjectType(it->object.id);
}
}
}
else if (comp.components & moveit_msgs::PlanningSceneComponents::ROBOT_STATE)
{
robot_state::robotStateToRobotStateMsg(getCurrentState(), scene_msg.robot_state, false);
}
if (comp.components & moveit_msgs::PlanningSceneComponents::ALLOWED_COLLISION_MATRIX)
getAllowedCollisionMatrix().getMessage(scene_msg.allowed_collision_matrix);
if (comp.components & moveit_msgs::PlanningSceneComponents::LINK_PADDING_AND_SCALING)
{
getCollisionRobot()->getPadding(scene_msg.link_padding);
getCollisionRobot()->getScale(scene_msg.link_scale);
}
if (comp.components & moveit_msgs::PlanningSceneComponents::OBJECT_COLORS)
getPlanningSceneMsgObjectColors(scene_msg);
// add collision objects
if (comp.components & moveit_msgs::PlanningSceneComponents::WORLD_OBJECT_GEOMETRY)
getPlanningSceneMsgCollisionObjects(scene_msg);
else
if (comp.components & moveit_msgs::PlanningSceneComponents::WORLD_OBJECT_NAMES)
{
const std::vector<std::string> &ns = world_->getObjectIds();
scene_msg.world.collision_objects.clear();
scene_msg.world.collision_objects.reserve(ns.size());
for (std::size_t i = 0 ; i < ns.size() ; ++i)
if (ns[i] != OCTOMAP_NS)
{
moveit_msgs::CollisionObject co;
co.id = ns[i];
if (hasObjectType(co.id))
co.type = getObjectType(co.id);
scene_msg.world.collision_objects.push_back(co);
}
}
// get the octomap
if (comp.components & moveit_msgs::PlanningSceneComponents::OCTOMAP)
getPlanningSceneMsgOctomap(scene_msg);
}
void planning_scene::PlanningScene::saveGeometryToStream(std::ostream &out) const
{
out << name_ << std::endl;
const std::vector<std::string> &ns = world_->getObjectIds();
for (std::size_t i = 0 ; i < ns.size() ; ++i)
if (ns[i] != OCTOMAP_NS)
{
collision_detection::CollisionWorld::ObjectConstPtr obj = world_->getObject(ns[i]);
if (obj)
{
out << "* " << ns[i] << std::endl;
out << obj->shapes_.size() << std::endl;
for (std::size_t j = 0 ; j < obj->shapes_.size() ; ++j)
{
shapes::saveAsText(obj->shapes_[j].get(), out);
out << obj->shape_poses_[j].translation().x() << " " << obj->shape_poses_[j].translation().y() << " " << obj->shape_poses_[j].translation().z() << std::endl;
Eigen::Quaterniond r(obj->shape_poses_[j].rotation());
out << r.x() << " " << r.y() << " " << r.z() << " " << r.w() << std::endl;
if (hasObjectColor(ns[i]))
{
const std_msgs::ColorRGBA &c = getObjectColor(ns[i]);
out << c.r << " " << c.g << " " << c.b << " " << c.a << std::endl;