moveit2
The MoveIt Motion Planning Framework for ROS 2.
collision_matrix.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2022, Peter David Fagan
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 PickNik Inc. 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 /* Author: Peter David Fagan */
36 
37 #include "collision_matrix.h"
38 
39 namespace moveit_py
40 {
41 namespace bind_collision_detection
42 {
43 // TODO: Create a custom typecaster/revise the current implementation to return std::pair<bool,
44 // collision_detection::AllowedCollision::Type>
45 std::pair<bool, std::string> getEntry(const collision_detection::AllowedCollisionMatrix& acm, const std::string& name1,
46  const std::string& name2)
47 {
48  // check acm for collision
50  bool collision_allowed = acm.getEntry(name1, name2, type);
51  std::string type_str;
53  {
54  type_str = "NEVER";
55  }
57  {
58  type_str = "ALWAYS";
59  }
61  {
62  type_str = "CONDITIONAL";
63  }
64  else
65  {
66  type_str = "UNKNOWN";
67  }
68 
69  // should return a tuple true/false and the allowed collision type
70  std::pair<bool, std::string> result = std::make_pair(collision_allowed, type_str);
71  return result;
72 }
73 
74 void initAcm(py::module& m)
75 {
76  py::module collision_detection = m.def_submodule("collision_detection");
77 
78  py::class_<collision_detection::AllowedCollisionMatrix, std::shared_ptr<collision_detection::AllowedCollisionMatrix>>(
79  collision_detection, "AllowedCollisionMatrix",
80  R"(
81  Definition of a structure for the allowed collision matrix. All elements in the collision world are referred to by their names. This class represents which collisions are allowed to happen and which are not.
82  )")
83  .def(py::init<std::vector<std::string>&, bool>(),
84  R"(
85  Initialize the allowed collision matrix using a list of names of collision objects.
86 
87  Args:
88  names (list of str): A list of names of the objects in the collision world (corresponding to object IDs in the collision world).
89  allowed (bool): If false, indicates that collisions between all elements must be checked for and no collisions will be ignored.
90  )",
91  py::arg("names"), py::arg("default_entry") = false)
92 
94  R"(
95  Get the allowed collision entry for a pair of objects.
96 
97  Args:
98  name1 (str): The name of the first object.
99  name2 (str): The name of the second object.
100 
101  Returns:
102  (bool, str): Whether the collision is allowed and the type of allowed collision.
103  )",
104  py::arg("name1"), py::arg("name2"))
105 
106  .def("set_entry",
107  py::overload_cast<const std::string&, const std::string&, bool>(
109  py::arg("name1"), py::arg("name2"), py::arg("allowed"),
110  R"(
111  Set the allowed collision state between two objects.
112 
113  Args:
114  name1 (str): The name of the first object.
115  name2 (str): The name of the second object.
116  allowed (bool): If true, indicates that the collision between the two objects is allowed. If false, indicates that the collision between the two objects is not allowed.
117  )")
118 
119  .def("remove_entry",
120  py::overload_cast<const std::string&, const std::string&>(
122  py::arg("name1"), py::arg("name2"),
123  R"(
124  Remove an entry corresponding to a pair of elements. Nothing happens if the pair does not exist in the collision matrix.
125 
126  Args:
127  name1 (str): The name of the first object.
128  name2 (str): The name of the second object.
129  )")
130 
131  .def("clear", &collision_detection::AllowedCollisionMatrix::clear, R"(Clear the allowed collision matrix.)");
132 }
133 // getEntry
134 
135 } // namespace bind_collision_detection
136 } // namespace moveit_py
Definition of a structure for the allowed collision matrix. All elements in the collision world are r...
void clear()
Clear the allowed collision matrix.
void setEntry(const std::string &name1, const std::string &name2, bool allowed)
Set an entry corresponding to a pair of elements.
bool getEntry(const std::string &name1, const std::string &name2, AllowedCollision::Type &allowed_collision_type) const
Get the type of the allowed collision between two elements. Return true if the entry is included in t...
void removeEntry(const std::string &name1, const std::string &name2)
Remove an entry corresponding to a pair of elements. Nothing happens if the pair does not exist in th...
@ CONDITIONAL
The collision is allowed depending on a predicate evaluated on the produced contact....
@ NEVER
Collisions between the pair of bodies is never ok, i.e., if two bodies are in contact in a particular...
@ ALWAYS
Collisions between a particular pair of bodies does not imply that the robot configuration is in coll...
std::pair< bool, std::string > getEntry(const collision_detection::AllowedCollisionMatrix &acm, const std::string &name1, const std::string &name2)