moveit2
The MoveIt Motion Planning Framework for ROS 2.
joint_model_group.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2013, Ioan A. Sucan
5  * Copyright (c) 2013, Willow Garage, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of the Willow Garage nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *********************************************************************/
35 
36 /* Author: Ioan Sucan, Dave Coleman */
37 
42 #include <rclcpp/logger.hpp>
43 #include <rclcpp/logging.hpp>
44 #include <algorithm>
45 
47 
48 namespace moveit
49 {
50 namespace core
51 {
52 namespace
53 {
54 const rclcpp::Logger LOGGER = rclcpp::get_logger("moveit_robot_model.joint_model_group");
55 
56 // check if a parent or ancestor of joint is included in this group
57 bool includesParent(const JointModel* joint, const JointModelGroup* group)
58 {
59  bool found = false;
60  // if we find that an ancestor is also in the group, then the joint is not a root
61  while (joint->getParentLinkModel() != nullptr)
62  {
63  joint = joint->getParentLinkModel()->getParentJointModel();
64  if (group->hasJointModel(joint->getName()) && joint->getVariableCount() > 0 && joint->getMimic() == nullptr)
65  {
66  found = true;
67  break;
68  }
69  else if (joint->getMimic() != nullptr)
70  {
71  const JointModel* mjoint = joint->getMimic();
72  if (group->hasJointModel(mjoint->getName()) && mjoint->getVariableCount() > 0 && mjoint->getMimic() == nullptr)
73  {
74  found = true;
75  }
76  else if (includesParent(mjoint, group))
77  {
78  found = true;
79  }
80  if (found)
81  break;
82  }
83  }
84  return found;
85 }
86 
87 // check if joint a is right below b, in the kinematic chain, with no active DOF missing
88 bool jointPrecedes(const JointModel* a, const JointModel* b)
89 {
90  if (!a->getParentLinkModel())
91  return false;
92  const JointModel* p = a->getParentLinkModel()->getParentJointModel();
93  while (p)
94  {
95  if (p == b)
96  return true;
97  if (p->getType() == JointModel::FIXED)
98  {
99  p = p->getParentLinkModel() ? p->getParentLinkModel()->getParentJointModel() : nullptr;
100  }
101  else
102  {
103  break;
104  }
105  }
106 
107  return false;
108 }
109 } // namespace
110 
111 JointModelGroup::JointModelGroup(const std::string& group_name, const srdf::Model::Group& config,
112  const std::vector<const JointModel*>& unsorted_group_joints,
113  const RobotModel* parent_model)
114  : parent_model_(parent_model)
115  , name_(group_name)
116  , common_root_(nullptr)
117  , variable_count_(0)
118  , active_variable_count_(0)
119  , is_contiguous_index_list_(true)
120  , is_chain_(false)
121  , is_single_dof_(true)
122  , config_(config)
123 {
124  // sort joints in Depth-First order
125  joint_model_vector_ = unsorted_group_joints;
126  std::sort(joint_model_vector_.begin(), joint_model_vector_.end(), OrderJointsByIndex());
128 
129  // figure out active joints, mimic joints, fixed joints
130  // construct index maps, list of variables
131  for (const JointModel* joint_model : joint_model_vector_)
132  {
133  joint_model_name_vector_.push_back(joint_model->getName());
134  joint_model_map_[joint_model->getName()] = joint_model;
135  unsigned int vc = joint_model->getVariableCount();
136  if (vc > 0)
137  {
138  if (vc > 1)
139  is_single_dof_ = false;
140  const std::vector<std::string>& name_order = joint_model->getVariableNames();
141 
142  if (joint_model->getMimic() == nullptr)
143  {
144  active_joint_model_vector_.push_back(joint_model);
145  active_joint_model_name_vector_.push_back(joint_model->getName());
147  active_joint_models_bounds_.push_back(&joint_model->getVariableBounds());
149  }
150  else
151  mimic_joints_.push_back(joint_model);
152  for (const std::string& name : name_order)
153  {
154  variable_names_.push_back(name);
155  variable_names_set_.insert(name);
156  }
157 
158  int first_index = joint_model->getFirstVariableIndex();
159  for (std::size_t j = 0; j < name_order.size(); ++j)
160  {
161  variable_index_list_.push_back(first_index + j);
162  joint_variables_index_map_[name_order[j]] = variable_count_ + j;
163  }
164  joint_variables_index_map_[joint_model->getName()] = variable_count_;
165 
166  if (joint_model->getType() == JointModel::REVOLUTE &&
167  static_cast<const RevoluteJointModel*>(joint_model)->isContinuous())
168  continuous_joint_model_vector_.push_back(joint_model);
169 
170  variable_count_ += vc;
171  }
172  else
173  fixed_joints_.push_back(joint_model);
174  }
175 
176  // now we need to find all the set of joints within this group
177  // that root distinct subtrees
178  for (const JointModel* active_joint_model : active_joint_model_vector_)
179  {
180  // if we find that an ancestor is also in the group, then the joint is not a root
181  if (!includesParent(active_joint_model, this))
182  joint_roots_.push_back(active_joint_model);
183  }
184 
185  // when updating this group within a state, it is useful to know
186  // if the full state of a group is contiguous within the full state of the robot
187  if (variable_index_list_.empty())
188  {
190  }
191  else
192  {
193  for (std::size_t i = 1; i < variable_index_list_.size(); ++i)
194  {
195  if (variable_index_list_[i] != variable_index_list_[i - 1] + 1)
196  {
198  break;
199  }
200  }
201  }
202 
203  // when updating/sampling a group state only, only mimic joints that have their parent within the group get updated.
204  for (const JointModel* mimic_joint : mimic_joints_)
205  {
206  // if the joint we mimic is also in this group, we will need to do updates when sampling
207  if (hasJointModel(mimic_joint->getMimic()->getName()))
208  {
209  int src = joint_variables_index_map_[mimic_joint->getMimic()->getName()];
210  int dest = joint_variables_index_map_[mimic_joint->getName()];
211  GroupMimicUpdate mu(src, dest, mimic_joint->getMimicFactor(), mimic_joint->getMimicOffset());
212  group_mimic_update_.push_back(mu);
213  }
214  }
215 
216  // now we need to make another pass for group links (we include the fixed joints here)
217  std::set<const LinkModel*> group_links_set;
218  for (const JointModel* joint_model : joint_model_vector_)
219  group_links_set.insert(joint_model->getChildLinkModel());
220  for (const LinkModel* group_link : group_links_set)
221  link_model_vector_.push_back(group_link);
222  std::sort(link_model_vector_.begin(), link_model_vector_.end(), OrderLinksByIndex());
223 
224  for (const LinkModel* link_model : link_model_vector_)
225  {
226  link_model_map_[link_model->getName()] = link_model;
227  link_model_name_vector_.push_back(link_model->getName());
228  if (!link_model->getShapes().empty())
229  {
230  link_model_with_geometry_vector_.push_back(link_model);
231  link_model_with_geometry_name_vector_.push_back(link_model->getName());
232  }
233  }
234 
235  // compute the common root of this group
236  if (!joint_roots_.empty())
237  {
239  for (std::size_t i = 1; i < joint_roots_.size(); ++i)
241  }
242 
243  // compute updated links
244  for (const JointModel* joint_root : joint_roots_)
245  {
246  const std::vector<const LinkModel*>& links = joint_root->getDescendantLinkModels();
247  updated_link_model_set_.insert(links.begin(), links.end());
248  }
249  for (const LinkModel* updated_link_model : updated_link_model_set_)
250  {
251  updated_link_model_name_set_.insert(updated_link_model->getName());
252  updated_link_model_vector_.push_back(updated_link_model);
253  if (!updated_link_model->getShapes().empty())
254  {
255  updated_link_model_with_geometry_vector_.push_back(updated_link_model);
256  updated_link_model_with_geometry_set_.insert(updated_link_model);
257  updated_link_model_with_geometry_name_set_.insert(updated_link_model->getName());
258  }
259  }
260  std::sort(updated_link_model_vector_.begin(), updated_link_model_vector_.end(), OrderLinksByIndex());
262  OrderLinksByIndex());
263  for (const LinkModel* updated_link_model : updated_link_model_vector_)
264  updated_link_model_name_vector_.push_back(updated_link_model->getName());
265  for (const LinkModel* updated_link_model_with_geometry : updated_link_model_with_geometry_vector_)
266  updated_link_model_with_geometry_name_vector_.push_back(updated_link_model_with_geometry->getName());
267 
268  // check if this group should actually be a chain
269  if (joint_roots_.size() == 1 && !active_joint_model_vector_.empty())
270  {
271  bool chain = true;
272  // due to our sorting, the joints are sorted in a DF fashion, so looking at them in reverse,
273  // we should always get to the parent.
274  for (std::size_t k = joint_model_vector_.size() - 1; k > 0; --k)
275  {
276  if (!jointPrecedes(joint_model_vector_[k], joint_model_vector_[k - 1]))
277  {
278  chain = false;
279  break;
280  }
281  }
282  if (chain)
283  is_chain_ = true;
284  }
285 }
286 
288 
289 void JointModelGroup::setSubgroupNames(const std::vector<std::string>& subgroups)
290 {
291  subgroup_names_ = subgroups;
292  subgroup_names_set_.clear();
293  for (const std::string& subgroup_name : subgroup_names_)
294  subgroup_names_set_.insert(subgroup_name);
295 }
296 
297 void JointModelGroup::getSubgroups(std::vector<const JointModelGroup*>& sub_groups) const
298 {
299  sub_groups.resize(subgroup_names_.size());
300  for (std::size_t i = 0; i < subgroup_names_.size(); ++i)
301  sub_groups[i] = parent_model_->getJointModelGroup(subgroup_names_[i]);
302 }
303 
304 bool JointModelGroup::hasJointModel(const std::string& joint) const
305 {
306  return joint_model_map_.find(joint) != joint_model_map_.end();
307 }
308 
309 bool JointModelGroup::hasLinkModel(const std::string& link) const
310 {
311  return link_model_map_.find(link) != link_model_map_.end();
312 }
313 
314 const LinkModel* JointModelGroup::getLinkModel(const std::string& name) const
315 {
316  LinkModelMapConst::const_iterator it = link_model_map_.find(name);
317  if (it == link_model_map_.end())
318  {
319  RCLCPP_ERROR(LOGGER, "Link '%s' not found in group '%s'", name.c_str(), name_.c_str());
320  return nullptr;
321  }
322  return it->second;
323 }
324 
325 const JointModel* JointModelGroup::getJointModel(const std::string& name) const
326 {
327  JointModelMapConst::const_iterator it = joint_model_map_.find(name);
328  if (it == joint_model_map_.end())
329  {
330  RCLCPP_ERROR(LOGGER, "Joint '%s' not found in group '%s'", name.c_str(), name_.c_str());
331  return nullptr;
332  }
333  return it->second;
334 }
335 
336 void JointModelGroup::getVariableRandomPositions(random_numbers::RandomNumberGenerator& rng, double* values,
337  const JointBoundsVector& active_joint_bounds) const
338 {
339  assert(active_joint_bounds.size() == active_joint_model_vector_.size());
340  for (std::size_t i = 0; i < active_joint_model_vector_.size(); ++i)
341  {
342  active_joint_model_vector_[i]->getVariableRandomPositions(rng, values + active_joint_model_start_index_[i],
343  *active_joint_bounds[i]);
344  }
345 
346  updateMimicJoints(values);
347 }
348 
349 void JointModelGroup::getVariableRandomPositionsNearBy(random_numbers::RandomNumberGenerator& rng, double* values,
350  const JointBoundsVector& active_joint_bounds, const double* near,
351  double distance) const
352 {
353  assert(active_joint_bounds.size() == active_joint_model_vector_.size());
354  for (std::size_t i = 0; i < active_joint_model_vector_.size(); ++i)
355  {
356  active_joint_model_vector_[i]->getVariableRandomPositionsNearBy(rng, values + active_joint_model_start_index_[i],
357  *active_joint_bounds[i],
359  distance);
360  }
361  updateMimicJoints(values);
362 }
363 
364 void JointModelGroup::getVariableRandomPositionsNearBy(random_numbers::RandomNumberGenerator& rng, double* values,
365  const JointBoundsVector& active_joint_bounds, const double* near,
366  const std::map<JointModel::JointType, double>& distance_map) const
367 {
368  assert(active_joint_bounds.size() == active_joint_model_vector_.size());
369  for (std::size_t i = 0; i < active_joint_model_vector_.size(); ++i)
370  {
371  double distance = 0.0;
372  std::map<JointModel::JointType, double>::const_iterator iter =
373  distance_map.find(active_joint_model_vector_[i]->getType());
374  if (iter != distance_map.end())
375  {
376  distance = iter->second;
377  }
378  else
379  {
380  RCLCPP_WARN(LOGGER, "Did not pass in distance for '%s'", active_joint_model_vector_[i]->getName().c_str());
381  }
382  active_joint_model_vector_[i]->getVariableRandomPositionsNearBy(rng, values + active_joint_model_start_index_[i],
383  *active_joint_bounds[i],
385  distance);
386  }
387  updateMimicJoints(values);
388 }
389 
390 void JointModelGroup::getVariableRandomPositionsNearBy(random_numbers::RandomNumberGenerator& rng, double* values,
391  const JointBoundsVector& active_joint_bounds, const double* near,
392  const std::vector<double>& distances) const
393 {
394  assert(active_joint_bounds.size() == active_joint_model_vector_.size());
395  if (distances.size() != active_joint_model_vector_.size())
396  {
397  throw Exception("When sampling random values nearby for group '" + name_ +
398  "', distances vector should be of size " + std::to_string(active_joint_model_vector_.size()) +
399  ", but it is of size " + std::to_string(distances.size()));
400  }
401  for (std::size_t i = 0; i < active_joint_model_vector_.size(); ++i)
402  {
403  active_joint_model_vector_[i]->getVariableRandomPositionsNearBy(rng, values + active_joint_model_start_index_[i],
404  *active_joint_bounds[i],
406  distances[i]);
407  }
408  updateMimicJoints(values);
409 }
410 
411 bool JointModelGroup::satisfiesPositionBounds(const double* state, const JointBoundsVector& active_joint_bounds,
412  double margin) const
413 {
414  assert(active_joint_bounds.size() == active_joint_model_vector_.size());
415  for (std::size_t i = 0; i < active_joint_model_vector_.size(); ++i)
416  {
418  *active_joint_bounds[i], margin))
419  return false;
420  }
421  return true;
422 }
423 
424 bool JointModelGroup::enforcePositionBounds(double* state, const JointBoundsVector& active_joint_bounds) const
425 {
426  assert(active_joint_bounds.size() == active_joint_model_vector_.size());
427  bool change = false;
428  for (std::size_t i = 0; i < active_joint_model_vector_.size(); ++i)
429  {
431  *active_joint_bounds[i]))
432  change = true;
433  }
434  if (change)
435  updateMimicJoints(state);
436  return change;
437 }
438 
439 double JointModelGroup::getMaximumExtent(const JointBoundsVector& active_joint_bounds) const
440 {
441  double max_distance = 0.0;
442  for (std::size_t j = 0; j < active_joint_model_vector_.size(); ++j)
443  {
444  max_distance += active_joint_model_vector_[j]->getMaximumExtent(*active_joint_bounds[j]) *
445  active_joint_model_vector_[j]->getDistanceFactor();
446  }
447  return max_distance;
448 }
449 
450 double JointModelGroup::distance(const double* state1, const double* state2) const
451 {
452  double d = 0.0;
453  for (std::size_t i = 0; i < active_joint_model_vector_.size(); ++i)
454  {
455  d += active_joint_model_vector_[i]->getDistanceFactor() *
457  state2 + active_joint_model_start_index_[i]);
458  }
459  return d;
460 }
461 
462 void JointModelGroup::interpolate(const double* from, const double* to, double t, double* state) const
463 {
464  // we interpolate values only for active joint models (non-mimic)
465  for (std::size_t i = 0; i < active_joint_model_vector_.size(); ++i)
466  {
470  }
471 
472  // now we update mimic as needed
473  updateMimicJoints(state);
474 }
475 
476 void JointModelGroup::updateMimicJoints(double* values) const
477 {
478  // update mimic (only local joints as we are dealing with a local group state)
479  for (const GroupMimicUpdate& mimic_update : group_mimic_update_)
480  values[mimic_update.dest] = values[mimic_update.src] * mimic_update.factor + mimic_update.offset;
481 }
482 
483 void JointModelGroup::addDefaultState(const std::string& name, const std::map<std::string, double>& default_state)
484 {
485  default_states_[name] = default_state;
486  default_states_names_.push_back(name);
487 }
488 
489 bool JointModelGroup::getVariableDefaultPositions(const std::string& name, std::map<std::string, double>& values) const
490 {
491  std::map<std::string, std::map<std::string, double> >::const_iterator it = default_states_.find(name);
492  if (it == default_states_.end())
493  return false;
494  values = it->second;
495  return true;
496 }
497 
499 {
500  for (std::size_t i = 0; i < active_joint_model_vector_.size(); ++i)
502  updateMimicJoints(values);
503 }
504 
505 void JointModelGroup::getVariableDefaultPositions(std::map<std::string, double>& values) const
506 {
507  std::vector<double> tmp(variable_count_);
509  for (std::size_t i = 0; i < variable_names_.size(); ++i)
510  values[variable_names_[i]] = tmp[i];
511 }
512 
514 {
516 }
517 
518 void JointModelGroup::setEndEffectorParent(const std::string& group, const std::string& link)
519 {
520  end_effector_parent_.first = group;
521  end_effector_parent_.second = link;
522 }
523 
524 void JointModelGroup::attachEndEffector(const std::string& eef_name)
525 {
526  attached_end_effector_names_.push_back(eef_name);
527 }
528 
529 bool JointModelGroup::getEndEffectorTips(std::vector<std::string>& tips) const
530 {
531  // Get a vector of tip links
532  std::vector<const LinkModel*> tip_links;
533  if (!getEndEffectorTips(tip_links))
534  return false;
535 
536  // Convert to string names
537  tips.clear();
538  for (const LinkModel* link_model : tip_links)
539  tips.push_back(link_model->getName());
540  return true;
541 }
542 
543 bool JointModelGroup::getEndEffectorTips(std::vector<const LinkModel*>& tips) const
544 {
545  tips.clear();
546  for (const std::string& name : getAttachedEndEffectorNames())
547  {
549  if (!eef)
550  {
551  RCLCPP_ERROR(LOGGER, "Unable to find joint model group for eef");
552  return false;
553  }
554  const std::string& eef_parent = eef->getEndEffectorParentGroup().second;
555 
556  const LinkModel* eef_link = parent_model_->getLinkModel(eef_parent);
557  if (!eef_link)
558  {
559  RCLCPP_ERROR(LOGGER, "Unable to find end effector link for eef");
560  return false;
561  }
562  // insert eef_link into tips, maintaining a *sorted* vector, thus enabling use of std::lower_bound
563  const auto insert_it = std::lower_bound(tips.cbegin(), tips.cend(), eef_link);
564  if (insert_it == tips.end() || eef_link != *insert_it) // only insert if not a duplicate
565  tips.insert(insert_it, eef_link);
566  }
567  return true;
568 }
569 
571 {
572  std::vector<const LinkModel*> tips;
573  getEndEffectorTips(tips);
574  if (tips.size() == 1)
575  {
576  return tips.front();
577  }
578  else if (tips.size() > 1)
579  {
580  RCLCPP_ERROR(LOGGER, "More than one end effector tip found for joint model group, so cannot return only one");
581  }
582  else
583  {
584  RCLCPP_ERROR(LOGGER, "No end effector tips found in joint model group");
585  }
586  return nullptr;
587 }
588 
589 int JointModelGroup::getVariableGroupIndex(const std::string& variable) const
590 {
591  VariableIndexMap::const_iterator it = joint_variables_index_map_.find(variable);
592  if (it == joint_variables_index_map_.end())
593  {
594  RCLCPP_ERROR(LOGGER, "Variable '%s' is not part of group '%s'", variable.c_str(), name_.c_str());
595  return -1;
596  }
597  return it->second;
598 }
599 
601 {
602  group_kinematics_.first.default_ik_timeout_ = ik_timeout;
603  if (group_kinematics_.first.solver_instance_)
604  group_kinematics_.first.solver_instance_->setDefaultTimeout(ik_timeout);
605  for (std::pair<const JointModelGroup* const, KinematicsSolver>& it : group_kinematics_.second)
606  it.second.default_ik_timeout_ = ik_timeout;
607 }
608 
609 bool JointModelGroup::computeJointVariableIndices(const std::vector<std::string>& joint_names,
610  std::vector<size_t>& joint_bijection) const
611 {
612  joint_bijection.clear();
613  for (const std::string& joint_name : joint_names)
614  {
615  VariableIndexMap::const_iterator it = joint_variables_index_map_.find(joint_name);
616  if (it == joint_variables_index_map_.end())
617  {
618  // skip reported fixed joints
619  if (hasJointModel(joint_name) && getJointModel(joint_name)->getType() == JointModel::FIXED)
620  continue;
621  RCLCPP_ERROR(LOGGER,
622  "Looking for variables for joint '%s', "
623  "but group '%s' does not contain such a joint.",
624  joint_name.c_str(), getName().c_str());
625  return false;
626  }
627  const JointModel* jm = getJointModel(joint_name);
628  for (size_t k = 0; k < jm->getVariableCount(); ++k)
629  joint_bijection.push_back(it->second + k);
630  }
631  return true;
632 }
633 
634 void JointModelGroup::setSolverAllocators(const std::pair<SolverAllocatorFn, SolverAllocatorMapFn>& solvers)
635 {
636  if (solvers.first)
637  {
638  group_kinematics_.first.allocator_ = solvers.first;
639  group_kinematics_.first.solver_instance_ = solvers.first(this);
640  if (group_kinematics_.first.solver_instance_)
641  {
642  group_kinematics_.first.solver_instance_->setDefaultTimeout(group_kinematics_.first.default_ik_timeout_);
643  if (!computeJointVariableIndices(group_kinematics_.first.solver_instance_->getJointNames(),
644  group_kinematics_.first.bijection_))
645  group_kinematics_.first.reset();
646  }
647  }
648  else
649  {
650  // we now compute a joint bijection only if we have a solver map
651  for (const std::pair<const JointModelGroup* const, SolverAllocatorFn>& it : solvers.second)
652  {
653  if (it.first->getSolverInstance())
654  {
655  KinematicsSolver& ks = group_kinematics_.second[it.first];
656  ks.allocator_ = it.second;
657  ks.solver_instance_ = const_cast<JointModelGroup*>(it.first)->getSolverInstance();
658  ks.default_ik_timeout_ = group_kinematics_.first.default_ik_timeout_;
659  if (!computeJointVariableIndices(ks.solver_instance_->getJointNames(), ks.bijection_))
660  {
661  group_kinematics_.second.clear();
662  break;
663  }
664  }
665  }
666  }
667 }
668 
669 bool JointModelGroup::canSetStateFromIK(const std::string& tip) const
670 {
671  const kinematics::KinematicsBaseConstPtr& solver = getSolverInstance();
672  if (!solver || tip.empty())
673  return false;
674 
675  const std::vector<std::string>& tip_frames = solver->getTipFrames();
676 
677  if (tip_frames.empty())
678  {
679  RCLCPP_WARN(LOGGER, "Group %s has no tip frame(s)", name_.c_str());
680  return false;
681  }
682 
683  // loop through all tip frames supported by the JMG
684  for (const std::string& tip_frame : tip_frames)
685  {
686  // remove frame reference, if specified
687  const std::string& tip_local = tip[0] == '/' ? tip.substr(1) : tip;
688  const std::string& tip_frame_local = tip_frame[0] == '/' ? tip_frame.substr(1) : tip_frame;
689  RCLCPP_DEBUG(LOGGER, "comparing input tip: %s to this groups tip: %s ", tip_local.c_str(), tip_frame_local.c_str());
690 
691  // Check if the IK solver's tip is the same as the frame of inquiry
692  if (tip_local != tip_frame_local)
693  {
694  // If not the same, check if this planning group includes the frame of inquiry
695  if (hasLinkModel(tip_frame_local))
696  {
697  const LinkModel* lm = getLinkModel(tip_frame_local);
698  const LinkTransformMap& fixed_links = lm->getAssociatedFixedTransforms();
699  // Check if our frame of inquiry is located anywhere further down the chain (towards the tip of the arm)
700  for (const std::pair<const LinkModel* const, Eigen::Isometry3d>& fixed_link : fixed_links)
701  {
702  if (fixed_link.first->getName() == tip_local)
703  return true;
704  }
705  }
706  }
707  else
708  return true;
709  }
710 
711  // Did not find any valid tip frame links to use
712  return false;
713 }
714 
715 void JointModelGroup::printGroupInfo(std::ostream& out) const
716 {
717  out << "Group '" << name_ << "' using " << variable_count_ << " variables\n";
718  out << " * Joints:\n";
719  for (const JointModel* joint_model : joint_model_vector_)
720  out << " '" << joint_model->getName() << "' (" << joint_model->getTypeName() << ")\n";
721  out << " * Variables:\n";
722  for (const std::string& variable_name : variable_names_)
723  {
724  int local_idx = joint_variables_index_map_.find(variable_name)->second;
725  const JointModel* jm = parent_model_->getJointOfVariable(variable_name);
726  out << " '" << variable_name << "', index "
727  << (jm->getFirstVariableIndex() + jm->getLocalVariableIndex(variable_name)) << " in full state, index "
728  << local_idx << " in group state";
729  if (jm->getMimic())
730  out << ", mimic '" << jm->getMimic()->getName() << '\'';
731  out << '\n';
732  out << " " << parent_model_->getVariableBounds(variable_name) << '\n';
733  }
734  out << " * Variables Index List:\n ";
735  for (int variable_index : variable_index_list_)
736  out << variable_index << ' ';
738  {
739  out << "(contiguous)";
740  }
741  else
742  {
743  out << "(non-contiguous)";
744  }
745  out << '\n';
746  if (group_kinematics_.first)
747  {
748  out << " * Kinematics solver bijection:\n";
749  out << " ";
750  for (unsigned int index : group_kinematics_.first.bijection_)
751  out << index << ' ';
752  out << '\n';
753  }
754  if (!group_kinematics_.second.empty())
755  {
756  out << " * Compound kinematics solver:\n";
757  for (const std::pair<const JointModelGroup* const, KinematicsSolver>& it : group_kinematics_.second)
758  {
759  out << " " << it.first->getName() << ':';
760  for (unsigned int index : it.second.bijection_)
761  out << ' ' << index;
762  out << '\n';
763  }
764  }
765 
766  if (!group_mimic_update_.empty())
767  {
768  out << " * Local Mimic Updates:\n";
769  for (const GroupMimicUpdate& mimic_update : group_mimic_update_)
770  {
771  out << " [" << mimic_update.dest << "] = " << mimic_update.factor << " * [" << mimic_update.src << "] + "
772  << mimic_update.offset << '\n';
773  }
774  }
775  out << '\n';
776 }
777 
778 bool JointModelGroup::isValidVelocityMove(const std::vector<double>& from_joint_pose,
779  const std::vector<double>& to_joint_pose, double dt) const
780 {
781  // Check for equal sized arrays
782  if (from_joint_pose.size() != to_joint_pose.size())
783  {
784  RCLCPP_ERROR(LOGGER, "To and from joint poses are of different sizes.");
785  return false;
786  }
787 
788  return isValidVelocityMove(&from_joint_pose[0], &to_joint_pose[0], from_joint_pose.size(), dt);
789 }
790 
791 bool JointModelGroup::isValidVelocityMove(const double* from_joint_pose, const double* to_joint_pose,
792  std::size_t array_size, double dt) const
793 {
794  const std::vector<const JointModel::Bounds*>& bounds = getActiveJointModelsBounds();
795  const std::vector<size_t>& bij = getKinematicsSolverJointBijection();
796 
797  for (std::size_t i = 0; i < array_size; ++i)
798  {
799  double dtheta = std::abs(from_joint_pose[i] - to_joint_pose[i]);
800  const std::vector<moveit::core::VariableBounds>* var_bounds = bounds[bij[i]];
801 
802  if (var_bounds->size() != 1)
803  {
804  // TODO(davetcoleman) Support multiple variables
805  RCLCPP_ERROR(LOGGER, "Attempting to check velocity bounds for waypoint move with joints that have multiple "
806  "variables");
807  return false;
808  }
809  const double max_velocity = (*var_bounds)[0].max_velocity_;
810 
811  double max_dtheta = dt * max_velocity;
812  if (dtheta > max_dtheta)
813  {
814  RCLCPP_DEBUG(LOGGER, "Not valid velocity move because of joint %lu", i);
815  return false;
816  }
817  }
818 
819  return true;
820 }
821 } // end of namespace core
822 } // end of namespace moveit
This may be thrown if unrecoverable errors occur.
Definition: exceptions.h:53
std::set< const LinkModel * > updated_link_model_with_geometry_set_
The list of downstream link models in the order they should be updated (may include links that are no...
std::vector< int > variable_index_list_
The list of index values this group includes, with respect to a full robot state; this includes mimic...
std::string name_
Name of group.
std::vector< const JointModel * > joint_model_vector_
Joint instances in the order they appear in the group state.
bool is_contiguous_index_list_
True if the state of this group is contiguous within the full robot state; this also means that the i...
std::vector< const JointModel * > continuous_joint_model_vector_
The set of continuous joints this group contains.
bool getEndEffectorTips(std::vector< const LinkModel * > &tips) const
Get the unique set of end effector tips included in a particular joint model group as defined by the ...
std::vector< const JointModel * > active_joint_model_vector_
Active joint instances in the order they appear in the group state.
std::vector< const JointModel * > joint_roots_
The list of active joint models that are roots in this group.
void setDefaultIKTimeout(double ik_timeout)
Set the default IK timeout.
std::vector< const LinkModel * > link_model_vector_
The links that are on the direct lineage between joints and joint_roots_, as well as the children of ...
std::vector< const LinkModel * > updated_link_model_with_geometry_vector_
The list of downstream link models in the order they should be updated (may include links that are no...
const std::string & getName() const
Get the name of the joint group.
std::map< std::string, std::map< std::string, double > > default_states_
The set of default states specified for this group in the SRDF.
void getVariableRandomPositions(random_numbers::RandomNumberGenerator &rng, double *values) const
Compute random values for the state of the joint group.
std::vector< std::string > updated_link_model_name_vector_
The list of downstream link names in the order they should be updated (may include links that are not...
double distance(const double *state1, const double *state2) const
void getVariableRandomPositionsNearBy(random_numbers::RandomNumberGenerator &rng, double *values, const double *near, const double distance) const
Compute random values for the state of the joint group.
void attachEndEffector(const std::string &eef_name)
Notify this group that there is an end-effector attached to it.
void setSolverAllocators(const SolverAllocatorFn &solver, const SolverAllocatorMapFn &solver_map=SolverAllocatorMapFn())
std::vector< std::string > variable_names_
The names of the DOF that make up this group (this is just a sequence of joint variable names; not ne...
bool computeJointVariableIndices(const std::vector< std::string > &joint_names, std::vector< size_t > &joint_bijection) const
Computes the indices of joint variables given a vector of joint names to look up.
std::vector< std::string > default_states_names_
The names of the default states specified for this group in the SRDF.
const JointModel * common_root_
The joint that is a common root for all joints in this group (not necessarily part of this group)
void setEndEffectorName(const std::string &name)
Set the name of the end-effector, and remember this group is indeed an end-effector.
LinkModelMapConst link_model_map_
A map from link names to their instances.
std::vector< std::string > attached_end_effector_names_
If an end-effector is attached to this group, the name of that end-effector is stored in this variabl...
JointBoundsVector active_joint_models_bounds_
The bounds for all the active joint models.
JointModelGroup(const std::string &name, const srdf::Model::Group &config, const std::vector< const JointModel * > &joint_vector, const RobotModel *parent_model)
const std::pair< std::string, std::string > & getEndEffectorParentGroup() const
Get the name of the group this end-effector attaches to (first) and the name of the link in that grou...
JointModelMapConst joint_model_map_
A map from joint names to their instances. This includes all joints in the group.
const JointModel * getJointModel(const std::string &joint) const
Get a joint by its name. Throw an exception if the joint is not part of this group.
std::vector< const LinkModel * > updated_link_model_vector_
The list of downstream link models in the order they should be updated (may include links that are no...
std::set< std::string > variable_names_set_
The names of the DOF that make up this group (this is just a sequence of joint variable names; not ne...
bool enforcePositionBounds(double *state) const
const std::vector< std::string > & getAttachedEndEffectorNames() const
Get the names of the end effectors attached to this group.
const kinematics::KinematicsBaseConstPtr getSolverInstance() const
bool hasJointModel(const std::string &joint) const
Check if a joint is part of this group.
std::vector< int > active_joint_model_start_index_
For each active joint model in this group, hold the index at which the corresponding joint state star...
std::set< std::string > updated_link_model_with_geometry_name_set_
The list of downstream link names in the order they should be updated (may include links that are not...
const LinkModel * getLinkModel(const std::string &link) const
Get a link by its name. Throw an exception if the link is not part of this group.
void interpolate(const double *from, const double *to, double t, double *state) const
const moveit::core::LinkModel * getOnlyOneEndEffectorTip() const
Get one end effector tip, throwing an error if there ends up being more in the joint model group This...
std::vector< std::string > subgroup_names_
The set of labelled subgroups that are included in this group.
std::set< const LinkModel * > updated_link_model_set_
The list of downstream link models in the order they should be updated (may include links that are no...
bool isValidVelocityMove(const std::vector< double > &from_joint_pose, const std::vector< double > &to_joint_pose, double dt) const
Check that the time to move between two waypoints is sufficient given velocity limits.
unsigned int active_variable_count_
The number of variables necessary to describe the active joints in this group of joints.
bool hasLinkModel(const std::string &link) const
Check if a link is part of this group.
void updateMimicJoints(double *values) const
Update the variable values for the state of a group with respect to the mimic joints....
std::vector< GroupMimicUpdate > group_mimic_update_
bool satisfiesPositionBounds(const double *state, double margin=0.0) const
unsigned int variable_count_
The number of variables necessary to describe this group of joints.
const JointBoundsVector & getActiveJointModelsBounds() const
Get the bounds for all the active joints.
void addDefaultState(const std::string &name, const std::map< std::string, double > &default_state)
std::pair< KinematicsSolver, KinematicsSolverMap > group_kinematics_
void getSubgroups(std::vector< const JointModelGroup * > &sub_groups) const
Get the groups that are subsets of this one (in terms of joints set)
bool canSetStateFromIK(const std::string &tip) const
const std::vector< size_t > & getKinematicsSolverJointBijection() const
Return the mapping between the order of the joints in this group and the order of the joints in the k...
std::vector< const LinkModel * > link_model_with_geometry_vector_
std::set< std::string > updated_link_model_name_set_
The list of downstream link names in the order they should be updated (may include links that are not...
std::vector< const JointModel * > mimic_joints_
Joints that mimic other joints.
std::vector< std::string > link_model_name_vector_
The names of the links in this group.
VariableIndexMap joint_variables_index_map_
The group includes all the joint variables that make up the joints the group consists of....
bool getVariableDefaultPositions(const std::string &name, std::map< std::string, double > &values) const
Get the values that correspond to a named state as read from the URDF. Return false on failure.
std::vector< std::string > updated_link_model_with_geometry_name_vector_
The list of downstream link names in the order they should be updated (may include links that are not...
int getVariableGroupIndex(const std::string &variable) const
Get the index of a variable within the group. Return -1 on error.
std::pair< std::string, std::string > end_effector_parent_
First: name of the group that is parent to this end-effector group; Second: the link this in the pare...
std::vector< std::string > active_joint_model_name_vector_
Names of active joints in the order they appear in the group state.
const RobotModel * parent_model_
Owner model.
std::vector< std::string > joint_model_name_vector_
Names of joints in the order they appear in the group state.
void setSubgroupNames(const std::vector< std::string > &subgroups)
Set the names of the subgroups for this group.
void setEndEffectorParent(const std::string &group, const std::string &link)
If this group is an end-effector, specify the parent group (e.g., the arm holding the eef) and the li...
std::set< std::string > subgroup_names_set_
The set of labelled subgroups that are included in this group.
std::vector< std::string > link_model_with_geometry_name_vector_
The names of the links in this group that also have geometry.
std::vector< const JointModel * > fixed_joints_
The joints that have no DOF (fixed)
void printGroupInfo(std::ostream &out=std::cout) const
Print information about the constructed model.
std::string end_effector_name_
The name of the end effector, if this group is an end-effector.
A joint from the robot. Models the transform that this joint applies in the kinematic chain....
Definition: joint_model.h:117
size_t getLocalVariableIndex(const std::string &variable) const
Get the index of the variable within this joint.
Definition: joint_model.cpp:85
size_t getFirstVariableIndex() const
Get the index of this joint's first variable within the full robot state.
Definition: joint_model.h:216
std::size_t getVariableCount() const
Get the number of variables that describe this joint.
Definition: joint_model.h:210
const std::string & getName() const
Get the name of the joint.
Definition: joint_model.h:145
const std::vector< const LinkModel * > & getDescendantLinkModels() const
Get all the link models that descend from this joint, in the kinematic tree.
Definition: joint_model.h:422
const JointModel * getMimic() const
Get the joint this one is mimicking.
Definition: joint_model.h:390
A link from the robot. Contains the constant transform applied to the link and its geometry.
Definition: link_model.h:72
const LinkTransformMap & getAssociatedFixedTransforms() const
Get the set of links that are attached to this one via fixed transforms. The returned transforms are ...
Definition: link_model.h:199
bool isContinuous() const
Check if this joint wraps around.
Definition of a kinematic model. This class is not thread safe, however multiple instances can be cre...
Definition: robot_model.h:76
const VariableBounds & getVariableBounds(const std::string &variable) const
Get the bounds for a specific variable. Throw an exception of variable is not found.
Definition: robot_model.h:433
const JointModelGroup * getEndEffector(const std::string &name) const
Get the joint group that corresponds to a given end-effector name.
const JointModelGroup * getJointModelGroup(const std::string &name) const
Get a joint group from this model (by name)
const JointModel * getJointOfVariable(int variable_index) const
Definition: robot_model.h:213
const LinkModel * getLinkModel(const std::string &link, bool *has_link=nullptr) const
Get a link by its name. Output error and return nullptr when the link is missing.
const JointModel * getCommonRoot(const JointModel *a, const JointModel *b) const
Get the deepest joint in the kinematic tree that is a common parent of both joints passed as argument...
Definition: robot_model.h:451
std::map< const LinkModel *, Eigen::Isometry3d, std::less< const LinkModel * >, Eigen::aligned_allocator< std::pair< const LinkModel *const, Eigen::Isometry3d > > > LinkTransformMap
Map from link model instances to Eigen transforms.
Definition: link_model.h:68
std::vector< const JointModel::Bounds * > JointBoundsVector
Main namespace for MoveIt.
Definition: exceptions.h:43
double distance(const urdf::Pose &transform)
Definition: pr2_arm_ik.h:55
d
Definition: setup.py:4
name
Definition: setup.py:7
FilterFn chain(const std::vector< FilterFn > &filter_functions)
std::vector< size_t > bijection_
The mapping between the order of the joints in the group and the order of the joints in the kinematic...
SolverAllocatorFn allocator_
Function type that allocates a kinematics solver for a particular group.