moveit2
The MoveIt Motion Planning Framework for ROS 2.
default_collisions.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2021, PickNik Robotics
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 Robotics 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: David V. Lu!! */
36 
38 
39 namespace moveit_setup
40 {
41 namespace srdf_setup
42 {
43 std::vector<std::string> DefaultCollisions::getCollidingLinks()
44 {
45  return srdf_config_->getPlanningScene()->getRobotModel()->getLinkModelNamesWithCollisionGeometry();
46 }
47 
48 // Output Link Pairs to SRDF Format and update the collision matrix
49 // ******************************************************************************************
51 {
52  // reset the data in the SRDF Writer class
53  auto& disabled_list = srdf_config_->getDisabledCollisions();
54  disabled_list.clear();
55 
56  // Create temp disabled collision
57  srdf::Model::CollisionPair dc;
58 
59  // copy the data in this class's LinkPairMap datastructure to srdf::Model::CollisionPair format
60  for (LinkPairMap::const_iterator pair_it = link_pairs_.begin(); pair_it != link_pairs_.end(); ++pair_it)
61  {
62  // Only copy those that are actually disabled
63  if (pair_it->second.disable_check)
64  {
65  dc.link1_ = pair_it->first.first;
66  dc.link2_ = pair_it->first.second;
67  dc.reason_ = disabledReasonToString(pair_it->second.reason);
68  disabled_list.push_back(dc);
69  }
70  }
71  srdf_config_->updateRobotModel(COLLISIONS); // mark as changed
72 }
73 
75 {
76  auto& disabled_list = srdf_config_->getDisabledCollisions();
77  // Create temp disabled collision
78  srdf::Model::CollisionPair dc;
79 
80  std::set<srdf::Model::CollisionPair, CollisionPairLess> disabled_collisions;
81  for (auto& p : disabled_list)
82  {
83  if (p.link1_ >= p.link2_)
84  std::swap(p.link1_, p.link2_); // unify link1, link2 sorting
85  disabled_collisions.insert(p);
86  }
87 
88  // copy the data in this class's LinkPairMap datastructure to srdf::Model::CollisionPair format
89  for (const std::pair<const std::pair<std::string, std::string>, LinkPairData>& link_pair : link_pairs_)
90  {
91  // Only copy those that are actually disabled
92  if (link_pair.second.disable_check)
93  {
94  if ((1 << link_pair.second.reason) & skip_mask)
95  continue;
96 
97  dc.link1_ = link_pair.first.first;
98  dc.link2_ = link_pair.first.second;
99  if (dc.link1_ >= dc.link2_)
100  std::swap(dc.link1_, dc.link2_);
101  dc.reason_ = disabledReasonToString(link_pair.second.reason);
102 
103  disabled_collisions.insert(dc);
104  }
105  }
106 
107  disabled_list.assign(disabled_collisions.begin(), disabled_collisions.end());
108 }
109 
110 // ******************************************************************************************
111 // Load Link Pairs from SRDF Format
112 // ******************************************************************************************
114 {
115  // Clear all the previous data in the compute_default_collisions tool
116  link_pairs_.clear();
117 
118  // Create new instance of planning scene using pointer
119  planning_scene::PlanningScenePtr scene = srdf_config_->getPlanningScene()->diff();
120 
121  // Populate link_pairs_ list with every possible n choose 2 combination of links
123 
124  // Create temp link pair data struct
125  LinkPairData link_pair_data;
126  std::pair<std::string, std::string> link_pair;
127 
128  // Loop through all disabled collisions in SRDF and update the comprehensive list that has already been created
129  for (const auto& disabled_collision : srdf_config_->getDisabledCollisions())
130  {
131  // Set the link names
132  link_pair.first = disabled_collision.link1_;
133  link_pair.second = disabled_collision.link2_;
134  if (link_pair.first >= link_pair.second)
135  std::swap(link_pair.first, link_pair.second);
136 
137  // Set the link meta data
138  link_pair_data.reason = disabledReasonFromString(disabled_collision.reason_);
139  link_pair_data.disable_check = true; // disable checking the collision btw the 2 links
140 
141  // Insert into map
142  link_pairs_[link_pair] = link_pair_data;
143  }
144 }
145 
146 void DefaultCollisions::startGenerationThread(unsigned int num_trials, double min_frac, bool verbose)
147 {
148  progress_ = 0;
149 
150  // start worker thread
151  worker_ =
152  boost::thread([this, num_trials, min_frac, verbose] { generateCollisionTable(num_trials, min_frac, verbose); });
153 }
154 
155 // ******************************************************************************************
156 // The worker function to compute the collision matrix
157 // ******************************************************************************************
158 void DefaultCollisions::generateCollisionTable(unsigned int num_trials, double min_frac, bool verbose)
159 {
160  const bool include_never_colliding = true;
161 
162  // clear previously loaded collision matrix entries
163  srdf_config_->getPlanningScene()->getAllowedCollisionMatrixNonConst().clear();
164 
165  // Find the default collision matrix - all links that are allowed to collide
166  link_pairs_ = computeDefaultCollisions(srdf_config_->getPlanningScene(), &progress_, include_never_colliding,
167  num_trials, min_frac, verbose);
168 
169  // End the progress bar loop
170  progress_ = 100;
171 
172  RCLCPP_INFO_STREAM(getLogger(), "Thread complete " << link_pairs_.size());
173 }
174 
176 {
177  worker_.interrupt();
178 }
179 
181 {
182  worker_.join();
183 }
184 
186 {
187  return progress_;
188 }
189 
190 } // namespace srdf_setup
191 } // namespace moveit_setup
const rclcpp::Logger & getLogger() const
Makes a namespaced logger for this step available to the widget.
Definition: setup_step.hpp:91
std::vector< std::string > getCollidingLinks()
void startGenerationThread(unsigned int num_trials, double min_frac, bool verbose=true)
void linkPairsFromSRDF()
Load Link Pairs from SRDF Format.
LinkPairMap link_pairs_
main storage of link pair data
void generateCollisionTable(unsigned int num_trials, double min_frac, bool verbose)
void linkPairsToSRDFSorted(size_t skip_mask=0)
Output Link Pairs to SRDF Format; sorted; with optional filter.
void linkPairsToSRDF()
Output Link Pairs to SRDF Format.
std::shared_ptr< SRDFConfig > srdf_config_
Definition: srdf_step.hpp:67
LinkPairMap computeDefaultCollisions(const planning_scene::PlanningSceneConstPtr &parent_scene, unsigned int *progress, const bool include_never_colliding, const unsigned int trials, const double min_collision_faction, const bool verbose)
Generate an adjacency list of links that are always and never in collision, to speed up collision det...
void computeLinkPairs(const planning_scene::PlanningScene &scene, LinkPairMap &link_pairs)
Generate a list of unique link pairs for all links with geometry. Order pairs alphabetically....
DisabledReason disabledReasonFromString(const std::string &reason)
Converts a string reason for disabling a link pair into a struct data type.
const std::string disabledReasonToString(DisabledReason reason)
Converts a reason for disabling a link pair into a string.
scene
Definition: pick.py:52
p
Definition: pick.py:62