MTC
modify_planning_scene.h
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2017, Bielefeld University
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Bielefeld University nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Authors: Robert Haschke
36  Desc: Modify planning scene
37 */
38 
39 #pragma once
40 
41 #include <moveit/task_constructor/stage.h>
42 #include <moveit/task_constructor/properties.h>
43 #include <moveit/task_constructor/type_traits.h>
44 #include <moveit_msgs/CollisionObject.h>
45 #include <map>
46 
47 namespace moveit {
48 namespace core {
49 MOVEIT_CLASS_FORWARD(JointModelGroup);
50 }
51 } // namespace moveit
52 
53 namespace moveit {
54 namespace task_constructor {
55 namespace stages {
56 
66 {
67 public:
68  using Names = std::vector<std::string>;
69  using ApplyCallback = std::function<void(const planning_scene::PlanningScenePtr&, const PropertyMap&)>;
70  ModifyPlanningScene(const std::string& name = "modify planning scene");
71 
72  void computeForward(const InterfaceState& from) override;
73  void computeBackward(const InterfaceState& to) override;
74 
76  void setCallback(const ApplyCallback& cb) { callback_ = cb; }
77 
79  void attachObjects(const Names& objects, const std::string& attach_link, bool attach);
81  void addObject(const moveit_msgs::CollisionObject& collision_object);
83  void removeObject(const std::string& object_name);
85  void moveObject(const moveit_msgs::CollisionObject& collision_object);
86 
88  inline void attachObject(const std::string& object, const std::string& link);
89  inline void detachObject(const std::string& object, const std::string& link);
90 
92  template <typename T, typename E = typename std::enable_if_t<
93  is_container<T>::value && std::is_base_of<std::string, typename T::value_type>::value>>
94  inline void attachObjects(const T& objects, const std::string& link) {
95  attachObjects(Names(objects.cbegin(), objects.cend()), link, true);
96  }
97  template <typename T, typename E = typename std::enable_if_t<
98  is_container<T>::value && std::is_base_of<std::string, typename T::value_type>::value>>
99  inline void detachObjects(const T& objects, const std::string& link) {
100  attachObjects(Names(objects.cbegin(), objects.cend()), link, false);
101  }
102 
104  void allowCollisions(const Names& first, const Names& second, bool allow);
106  void allowCollisions(const std::string& first, const std::string& second, bool allow) {
107  allowCollisions(Names{ first }, Names{ second }, allow);
108  }
110  void allowCollisions(const std::string& object, bool allow) { allowCollisions(Names({ object }), Names(), allow); }
111 
113  template <typename T1, typename T2,
114  typename E1 = typename std::enable_if_t<is_container<T1>::value &&
115  std::is_convertible<typename T1::value_type, std::string>::value>,
116  typename E2 = typename std::enable_if_t<is_container<T2>::value &&
117  std::is_convertible<typename T1::value_type, std::string>::value>>
118  inline void allowCollisions(const T1& first, const T2& second, bool enable_collision) {
119  allowCollisions(Names(first.cbegin(), first.cend()), Names(second.cbegin(), second.cend()), enable_collision);
120  }
122  template <typename T, typename E = typename std::enable_if_t<
123  is_container<T>::value && std::is_convertible<typename T::value_type, std::string>::value>>
124  inline void allowCollisions(const std::string& first, const T& second, bool enable_collision) {
125  allowCollisions(Names({ first }), Names(second.cbegin(), second.cend()), enable_collision);
126  }
128  template <typename T, typename E = typename std::enable_if_t<
129  is_container<T>::value && std::is_convertible<typename T::value_type, std::string>::value>>
130  inline void allowCollisions(const char* first, const T& second, bool enable_collision) {
131  allowCollisions(Names({ first }), Names(second.cbegin(), second.cend()), enable_collision);
132  }
134  void allowCollisions(const std::string& first, const moveit::core::JointModelGroup& jmg, bool allow);
135 
136 protected:
137  // list of objects to attach (true) / detach (false) to a given link
138  std::map<std::string, std::pair<Names, bool>> attach_objects_;
139  // list of objects to add / remove to the planning scene
140  std::vector<moveit_msgs::CollisionObject> collision_objects_;
141 
142  // list of objects to mutually
144  {
145  Names first;
146  Names second;
147  bool allow;
148  };
149  std::list<CollisionMatrixPairs> collision_matrix_edits_;
150  ApplyCallback callback_;
151 
152 protected:
153  // apply stored modifications to scene
154  std::pair<InterfaceState, SubTrajectory> apply(const InterfaceState& from, bool invert);
155  void processCollisionObject(planning_scene::PlanningScene& scene, const moveit_msgs::CollisionObject& object,
156  bool invert);
157  void attachObjects(planning_scene::PlanningScene& scene, const std::pair<std::string, std::pair<Names, bool>>& pair,
158  bool invert);
159  void allowCollisions(planning_scene::PlanningScene& scene, const CollisionMatrixPairs& pairs, bool invert);
160 };
161 
162 inline void ModifyPlanningScene::attachObject(const std::string& object, const std::string& link) {
163  attachObjects(Names({ object }), link, true);
164 }
165 
166 inline void ModifyPlanningScene::detachObject(const std::string& object, const std::string& link) {
167  attachObjects(Names({ object }), link, false);
168 }
169 } // namespace stages
170 } // namespace task_constructor
171 } // namespace moveit
Definition: properties.h:257
Definition: modify_planning_scene.h:66
void allowCollisions(const char *first, const T &second, bool enable_collision)
conveniency method accepting const char* and an arbitrary container of names
Definition: modify_planning_scene.h:130
void addObject(const moveit_msgs::CollisionObject &collision_object)
Add an object to the planning scene.
Definition: modify_planning_scene.cpp:60
void attachObjects(const Names &objects, const std::string &attach_link, bool attach)
attach or detach a list of objects to the given link
Definition: modify_planning_scene.cpp:54
void allowCollisions(const std::string &first, const std::string &second, bool allow)
allow / forbid collisions for pair (first, second)
Definition: modify_planning_scene.h:106
void allowCollisions(const T1 &first, const T2 &second, bool enable_collision)
conveniency method accepting arbitrary container types
Definition: modify_planning_scene.h:118
void allowCollisions(const Names &first, const Names &second, bool allow)
allow / forbid collisions for each combination of pairs in first and second lists
Definition: modify_planning_scene.cpp:86
void allowCollisions(const std::string &first, const T &second, bool enable_collision)
conveniency method accepting std::string and an arbitrary container of names
Definition: modify_planning_scene.h:124
void attachObject(const std::string &object, const std::string &link)
conviency methods accepting a single object name
Definition: modify_planning_scene.h:162
void moveObject(const moveit_msgs::CollisionObject &collision_object)
Move an object from the planning scene.
Definition: modify_planning_scene.cpp:77
void setCallback(const ApplyCallback &cb)
call an arbitrary function
Definition: modify_planning_scene.h:76
void attachObjects(const T &objects, const std::string &link)
conviency methods accepting any container of object names
Definition: modify_planning_scene.h:94
void allowCollisions(const std::string &object, bool allow)
allow / forbid all collisions for given object
Definition: modify_planning_scene.h:110
void removeObject(const std::string &object_name)
Remove an object from the planning scene.
Definition: modify_planning_scene.cpp:70
Definition: type_traits.h:49