constraint_sampler_manager.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011, Willow Garage, Inc.
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 Willow Garage 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: Ioan Sucan */
36 
40 #include <sstream>
41 
42 constraint_samplers::ConstraintSamplerPtr constraint_samplers::ConstraintSamplerManager::selectSampler(
43  const planning_scene::PlanningSceneConstPtr& scene, const std::string& group_name,
44  const moveit_msgs::Constraints& constr) const
45 {
46  for (std::size_t i = 0; i < sampler_alloc_.size(); ++i)
47  if (sampler_alloc_[i]->canService(scene, group_name, constr))
48  return sampler_alloc_[i]->alloc(scene, group_name, constr);
49 
50  // if no default sampler was used, try a default one
51  return selectDefaultSampler(scene, group_name, constr);
52 }
53 
55  const planning_scene::PlanningSceneConstPtr& scene, const std::string& group_name,
56  const moveit_msgs::Constraints& constr)
57 {
58  const robot_model::JointModelGroup* jmg = scene->getRobotModel()->getJointModelGroup(group_name);
59  if (!jmg)
60  return constraint_samplers::ConstraintSamplerPtr();
61  std::stringstream ss;
62  ss << constr;
63  logDebug("Attempting to construct constrained state sampler for group '%s', using constraints:\n%s.\n",
64  jmg->getName().c_str(), ss.str().c_str());
65 
66  ConstraintSamplerPtr joint_sampler; // location to put chosen joint sampler if needed
67  // if there are joint constraints, we could possibly get a sampler from those
68  if (!constr.joint_constraints.empty())
69  {
70  logDebug("There are joint constraints specified. Attempting to construct a JointConstraintSampler for group '%s'",
71  jmg->getName().c_str());
72 
73  std::map<std::string, bool> joint_coverage;
74  for (std::size_t i = 0; i < jmg->getVariableNames().size(); ++i)
75  joint_coverage[jmg->getVariableNames()[i]] = false;
76 
77  // construct the constraints
78  std::vector<kinematic_constraints::JointConstraint> jc;
79  for (std::size_t i = 0; i < constr.joint_constraints.size(); ++i)
80  {
81  kinematic_constraints::JointConstraint j(scene->getRobotModel());
82  if (j.configure(constr.joint_constraints[i]))
83  {
84  if (joint_coverage.find(j.getJointVariableName()) != joint_coverage.end())
85  {
86  joint_coverage[j.getJointVariableName()] = true;
87  jc.push_back(j);
88  }
89  }
90  }
91 
92  // check if every joint is covered (constrained) by just joint samplers
93  bool full_coverage = true;
94  for (std::map<std::string, bool>::iterator it = joint_coverage.begin(); it != joint_coverage.end(); ++it)
95  if (!it->second)
96  {
97  full_coverage = false;
98  break;
99  }
100 
101  // if we have constrained every joint, then we just use a sampler using these constraints
102  if (full_coverage)
103  {
104  JointConstraintSamplerPtr sampler(new JointConstraintSampler(scene, jmg->getName()));
105  if (sampler->configure(jc))
106  {
107  logDebug("Allocated a sampler satisfying joint constraints for group '%s'", jmg->getName().c_str());
108  return sampler;
109  }
110  }
111  else
112  // if a smaller set of joints has been specified, keep the constraint sampler around, but use it only if no IK
113  // sampler has been specified.
114  if (!jc.empty())
115  {
116  JointConstraintSamplerPtr sampler(new JointConstraintSampler(scene, jmg->getName()));
117  if (sampler->configure(jc))
118  {
119  logDebug("Temporary sampler satisfying joint constraints for group '%s' allocated. Looking for different types "
120  "of constraints before returning though.",
121  jmg->getName().c_str());
122  joint_sampler = sampler;
123  }
124  }
125  }
126 
127  std::vector<ConstraintSamplerPtr> samplers;
128  if (joint_sampler) // Start making a union of constraint samplers
129  samplers.push_back(joint_sampler);
130 
131  // read the ik allocators, if any
132  const robot_model::JointModelGroup::KinematicsSolver& ik_alloc = jmg->getGroupKinematics().first;
133  const robot_model::JointModelGroup::KinematicsSolverMap& ik_subgroup_alloc = jmg->getGroupKinematics().second;
134 
135  // if we have a means of computing complete states for the group using IK, then we try to see if any IK constraints
136  // should be used
137  if (ik_alloc)
138  {
139  logDebug("There is an IK allocator for '%s'. Checking for corresponding position and/or orientation constraints",
140  jmg->getName().c_str());
141 
142  // keep track of which links we constrained
143  std::map<std::string, IKConstraintSamplerPtr> usedL;
144 
145  // if we have position and/or orientation constraints on links that we can perform IK for,
146  // we will use a sampleable goal region that employs IK to sample goals;
147  // if there are multiple constraints for the same link, we keep the one with the smallest
148  // volume for sampling
149  for (std::size_t p = 0; p < constr.position_constraints.size(); ++p)
150  for (std::size_t o = 0; o < constr.orientation_constraints.size(); ++o)
151  if (constr.position_constraints[p].link_name == constr.orientation_constraints[o].link_name)
152  {
153  kinematic_constraints::PositionConstraintPtr pc(
154  new kinematic_constraints::PositionConstraint(scene->getRobotModel()));
155  kinematic_constraints::OrientationConstraintPtr oc(
156  new kinematic_constraints::OrientationConstraint(scene->getRobotModel()));
157  if (pc->configure(constr.position_constraints[p], scene->getTransforms()) &&
158  oc->configure(constr.orientation_constraints[o], scene->getTransforms()))
159  {
160  IKConstraintSamplerPtr iks(new IKConstraintSampler(scene, jmg->getName()));
161  if (iks->configure(IKSamplingPose(pc, oc)))
162  {
163  bool use = true;
164  // Check if there already is a constraint on this link
165  if (usedL.find(constr.position_constraints[p].link_name) != usedL.end())
166  // If there is, check if the previous one has a smaller volume for sampling
167  if (usedL[constr.position_constraints[p].link_name]->getSamplingVolume() < iks->getSamplingVolume())
168  use = false; // only use new constraint if it has a smaller sampling volume
169  if (use)
170  {
171  // assign the link to a new constraint sampler
172  usedL[constr.position_constraints[p].link_name] = iks;
173  logDebug("Allocated an IK-based sampler for group '%s' satisfying position and orientation constraints "
174  "on link '%s'",
175  jmg->getName().c_str(), constr.position_constraints[p].link_name.c_str());
176  }
177  }
178  }
179  }
180 
181  // keep track of links constrained with a full pose
182  std::map<std::string, IKConstraintSamplerPtr> usedL_fullPose = usedL;
183 
184  for (std::size_t p = 0; p < constr.position_constraints.size(); ++p)
185  {
186  // if we are constraining this link with a full pose, we do not attempt to constrain it with a position constraint
187  // only
188  if (usedL_fullPose.find(constr.position_constraints[p].link_name) != usedL_fullPose.end())
189  continue;
190 
191  kinematic_constraints::PositionConstraintPtr pc(
192  new kinematic_constraints::PositionConstraint(scene->getRobotModel()));
193  if (pc->configure(constr.position_constraints[p], scene->getTransforms()))
194  {
195  IKConstraintSamplerPtr iks(new IKConstraintSampler(scene, jmg->getName()));
196  if (iks->configure(IKSamplingPose(pc)))
197  {
198  bool use = true;
199  if (usedL.find(constr.position_constraints[p].link_name) != usedL.end())
200  if (usedL[constr.position_constraints[p].link_name]->getSamplingVolume() < iks->getSamplingVolume())
201  use = false;
202  if (use)
203  {
204  usedL[constr.position_constraints[p].link_name] = iks;
205  logDebug("Allocated an IK-based sampler for group '%s' satisfying position constraints on link '%s'",
206  jmg->getName().c_str(), constr.position_constraints[p].link_name.c_str());
207  }
208  }
209  }
210  }
211 
212  for (std::size_t o = 0; o < constr.orientation_constraints.size(); ++o)
213  {
214  // if we are constraining this link with a full pose, we do not attempt to constrain it with an orientation
215  // constraint only
216  if (usedL_fullPose.find(constr.orientation_constraints[o].link_name) != usedL_fullPose.end())
217  continue;
218 
219  kinematic_constraints::OrientationConstraintPtr oc(
220  new kinematic_constraints::OrientationConstraint(scene->getRobotModel()));
221  if (oc->configure(constr.orientation_constraints[o], scene->getTransforms()))
222  {
223  IKConstraintSamplerPtr iks(new IKConstraintSampler(scene, jmg->getName()));
224  if (iks->configure(IKSamplingPose(oc)))
225  {
226  bool use = true;
227  if (usedL.find(constr.orientation_constraints[o].link_name) != usedL.end())
228  if (usedL[constr.orientation_constraints[o].link_name]->getSamplingVolume() < iks->getSamplingVolume())
229  use = false;
230  if (use)
231  {
232  usedL[constr.orientation_constraints[o].link_name] = iks;
233  logDebug("Allocated an IK-based sampler for group '%s' satisfying orientation constraints on link '%s'",
234  jmg->getName().c_str(), constr.orientation_constraints[o].link_name.c_str());
235  }
236  }
237  }
238  }
239 
240  if (usedL.size() == 1)
241  {
242  if (samplers.empty())
243  return usedL.begin()->second;
244  else
245  {
246  samplers.push_back(usedL.begin()->second);
247  return ConstraintSamplerPtr(new UnionConstraintSampler(scene, jmg->getName(), samplers));
248  }
249  }
250  else if (usedL.size() > 1)
251  {
252  logDebug("Too many IK-based samplers for group '%s'. Keeping the one with minimal sampling volume",
253  jmg->getName().c_str());
254  // find the sampler with the smallest sampling volume; delete the rest
255  IKConstraintSamplerPtr iks = usedL.begin()->second;
256  double msv = iks->getSamplingVolume();
257  for (std::map<std::string, IKConstraintSamplerPtr>::const_iterator it = ++usedL.begin(); it != usedL.end(); ++it)
258  {
259  double v = it->second->getSamplingVolume();
260  if (v < msv)
261  {
262  iks = it->second;
263  msv = v;
264  }
265  }
266  if (samplers.empty())
267  {
268  return iks;
269  }
270  else
271  {
272  samplers.push_back(iks);
273  return ConstraintSamplerPtr(new UnionConstraintSampler(scene, jmg->getName(), samplers));
274  }
275  }
276  }
277 
278  // if we got to this point, we have not decided on a sampler.
279  // we now check to see if we can use samplers from subgroups
280  if (!ik_subgroup_alloc.empty())
281  {
282  logDebug("There are IK allocators for subgroups of group '%s'. Checking for corresponding position and/or "
283  "orientation constraints",
284  jmg->getName().c_str());
285 
286  bool some_sampler_valid = false;
287 
288  std::set<std::size_t> usedP, usedO;
289  for (robot_model::JointModelGroup::KinematicsSolverMap::const_iterator it = ik_subgroup_alloc.begin();
290  it != ik_subgroup_alloc.end(); ++it)
291  {
292  // construct a sub-set of constraints that operate on the sub-group for which we have an IK allocator
293  moveit_msgs::Constraints sub_constr;
294  for (std::size_t p = 0; p < constr.position_constraints.size(); ++p)
295  if (it->first->hasLinkModel(constr.position_constraints[p].link_name))
296  if (usedP.find(p) == usedP.end())
297  {
298  sub_constr.position_constraints.push_back(constr.position_constraints[p]);
299  usedP.insert(p);
300  }
301 
302  for (std::size_t o = 0; o < constr.orientation_constraints.size(); ++o)
303  if (it->first->hasLinkModel(constr.orientation_constraints[o].link_name))
304  if (usedO.find(o) == usedO.end())
305  {
306  sub_constr.orientation_constraints.push_back(constr.orientation_constraints[o]);
307  usedO.insert(o);
308  }
309 
310  // if some matching constraints were found, construct the allocator
311  if (!sub_constr.orientation_constraints.empty() || !sub_constr.position_constraints.empty())
312  {
313  logDebug("Attempting to construct a sampler for the '%s' subgroup of '%s'", it->first->getName().c_str(),
314  jmg->getName().c_str());
315  ConstraintSamplerPtr cs = selectDefaultSampler(scene, it->first->getName(), sub_constr);
316  if (cs)
317  {
318  logDebug("Constructed a sampler for the joints corresponding to group '%s', but part of group '%s'",
319  it->first->getName().c_str(), jmg->getName().c_str());
320  some_sampler_valid = true;
321  samplers.push_back(cs);
322  }
323  }
324  }
325  if (some_sampler_valid)
326  {
327  logDebug("Constructing sampler for group '%s' as a union of %u samplers", jmg->getName().c_str(),
328  (unsigned int)samplers.size());
329  return ConstraintSamplerPtr(new UnionConstraintSampler(scene, jmg->getName(), samplers));
330  }
331  }
332 
333  // if we've gotten here, just return joint sampler
334  if (joint_sampler)
335  {
336  logDebug("Allocated a sampler satisfying joint constraints for group '%s'", jmg->getName().c_str());
337  return joint_sampler;
338  }
339 
340  logDebug("No constraints sampler allocated for group '%s'", jmg->getName().c_str());
341 
342  return ConstraintSamplerPtr();
343 }
A structure for potentially holding a position constraint and an orientation constraint for use durin...
const std::vector< std::string > & getVariableNames() const
Get the names of the variables that make up the joints included in this group. The number of returned...
Class for constraints on the orientation of a link.
std::vector< ConstraintSamplerAllocatorPtr > sampler_alloc_
Holds the constraint sampler allocators, which will be tested in order.
const std::string & getName() const
Get the name of the joint group.
Class for handling single DOF joint constraints.
This class exists as a union of constraint samplers. It contains a vector of constraint samplers...
JointConstraintSampler is a class that allows the sampling of joints in a particular group of the rob...
const std::pair< KinematicsSolver, KinematicsSolverMap > & getGroupKinematics() const
A class that allows the sampling of IK constraints.
std::map< const JointModelGroup *, KinematicsSolver > KinematicsSolverMap
Map from group instances to allocator functions & bijections.
Class for constraints on the XYZ position of a link.
ConstraintSamplerPtr selectSampler(const planning_scene::PlanningSceneConstPtr &scene, const std::string &group_name, const moveit_msgs::Constraints &constr) const
Selects among the potential sampler allocators.
static ConstraintSamplerPtr selectDefaultSampler(const planning_scene::PlanningSceneConstPtr &scene, const std::string &group_name, const moveit_msgs::Constraints &constr)
Default logic to select a ConstraintSampler given a constraints message.


moveit_core
Author(s): Ioan Sucan , Sachin Chitta , Acorn Pooley
autogenerated on Sun Jan 21 2018 03:54:28