moveit2
The MoveIt Motion Planning Framework for ROS 2.
kinematics_base.h
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2008, 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: Sachin Chitta, Dave Coleman */
36 
37 #pragma once
38 
39 #include <geometry_msgs/msg/pose.hpp>
40 #include <moveit_msgs/msg/move_it_error_codes.hpp>
42 #include <rclcpp/logging.hpp>
43 #include <rclcpp/node.hpp>
44 #include <string>
45 #include <functional>
46 
47 #include <moveit_kinematics_base_export.h>
48 
49 namespace moveit
50 {
51 namespace core
52 {
56 } // namespace core
57 } // namespace moveit
58 
60 namespace kinematics
61 {
62 /*
63  * @enum DiscretizationMethods
64  *
65  * @brief Flags for choosing the type discretization method applied on the redundant joints during an ik query
66  */
67 namespace DiscretizationMethods
68 {
70 {
79 };
80 } // namespace DiscretizationMethods
82 
83 /*
84  * @enum KinematicErrors
85  * @brief Kinematic error codes that occur in a ik query
86  */
87 namespace KinematicErrors
88 {
90 {
91  OK = 1,
101 };
102 } // namespace KinematicErrors
104 
110 {
112  : lock_redundant_joints(false)
114  , discretization_method(DiscretizationMethods::NO_DISCRETIZATION)
115  {
116  }
117 
122 };
123 
124 /*
125  * @struct KinematicsResult
126  * @brief Reports result details of an ik query
127  *
128  * This struct is used as an output argument of the getPositionIK(...) method that returns multiple joint solutions.
129  * It contains the type of error that led to a failure or KinematicErrors::OK when a set of joint solutions is found.
130  * The solution percentage shall provide a ratio of solutions found over solutions searched.
131  *
132  */
134 {
138 };
139 
140 MOVEIT_CLASS_FORWARD(KinematicsBase); // Defines KinematicsBasePtr, ConstPtr, WeakPtr... etc
141 
146 class MOVEIT_KINEMATICS_BASE_EXPORT KinematicsBase
147 {
148 public:
149  static const rclcpp::Logger LOGGER;
150  static const double DEFAULT_SEARCH_DISCRETIZATION; /* = 0.1 */
151  static const double DEFAULT_TIMEOUT; /* = 1.0 */
152 
154  using IKCallbackFn = std::function<void(const geometry_msgs::msg::Pose&, const std::vector<double>&,
155  moveit_msgs::msg::MoveItErrorCodes&)>;
156 
158  using IKCostFn = std::function<double(const geometry_msgs::msg::Pose&, const moveit::core::RobotState&,
159  moveit::core::JointModelGroup const*, const std::vector<double>&)>;
160 
173  virtual bool
174  getPositionIK(const geometry_msgs::msg::Pose& ik_pose, const std::vector<double>& ik_seed_state,
175  std::vector<double>& solution, moveit_msgs::msg::MoveItErrorCodes& error_code,
177 
199  virtual bool getPositionIK(const std::vector<geometry_msgs::msg::Pose>& ik_poses,
200  const std::vector<double>& ik_seed_state, std::vector<std::vector<double> >& solutions,
202 
215  virtual bool
216  searchPositionIK(const geometry_msgs::msg::Pose& ik_pose, const std::vector<double>& ik_seed_state, double timeout,
217  std::vector<double>& solution, moveit_msgs::msg::MoveItErrorCodes& error_code,
219 
234  virtual bool
235  searchPositionIK(const geometry_msgs::msg::Pose& ik_pose, const std::vector<double>& ik_seed_state, double timeout,
236  const std::vector<double>& consistency_limits, std::vector<double>& solution,
237  moveit_msgs::msg::MoveItErrorCodes& error_code,
239 
253  virtual bool
254  searchPositionIK(const geometry_msgs::msg::Pose& ik_pose, const std::vector<double>& ik_seed_state, double timeout,
255  std::vector<double>& solution, const IKCallbackFn& solution_callback,
256  moveit_msgs::msg::MoveItErrorCodes& error_code,
258 
274  virtual bool
275  searchPositionIK(const geometry_msgs::msg::Pose& ik_pose, const std::vector<double>& ik_seed_state, double timeout,
276  const std::vector<double>& consistency_limits, std::vector<double>& solution,
277  const IKCallbackFn& solution_callback, moveit_msgs::msg::MoveItErrorCodes& error_code,
279 
301  virtual bool
302  searchPositionIK(const std::vector<geometry_msgs::msg::Pose>& ik_poses, const std::vector<double>& ik_seed_state,
303  double timeout, const std::vector<double>& consistency_limits, std::vector<double>& solution,
304  const IKCallbackFn& solution_callback, moveit_msgs::msg::MoveItErrorCodes& error_code,
306  const moveit::core::RobotState* context_state = nullptr) const
307  {
308  (void)context_state;
309  // For IK solvers that do not support multiple poses, fall back to single pose call
310  if (ik_poses.size() == 1)
311  {
312  // Check if a solution_callback function was provided and call the corresponding function
313  if (solution_callback)
314  {
315  return searchPositionIK(ik_poses[0], ik_seed_state, timeout, consistency_limits, solution, solution_callback,
316  error_code, options);
317  }
318  else
319  {
320  return searchPositionIK(ik_poses[0], ik_seed_state, timeout, consistency_limits, solution, error_code, options);
321  }
322  }
323 
324  // Otherwise throw error because this function should have been implemented
325  RCLCPP_ERROR(LOGGER, "This kinematic solver does not support searchPositionIK with multiple poses");
326  return false;
327  }
328 
351  virtual bool
352  searchPositionIK(const std::vector<geometry_msgs::msg::Pose>& ik_poses, const std::vector<double>& ik_seed_state,
353  double timeout, const std::vector<double>& consistency_limits, std::vector<double>& solution,
354  const IKCallbackFn& solution_callback, const IKCostFn& cost_function,
355  moveit_msgs::msg::MoveItErrorCodes& error_code,
357  const moveit::core::RobotState* context_state = nullptr) const
358  {
359  // if cost function is empty, omit
360  if (!cost_function)
361  {
362  return searchPositionIK(ik_poses, ik_seed_state, timeout, consistency_limits, solution, solution_callback,
363  error_code, options, context_state);
364  }
365  RCLCPP_ERROR(LOGGER, "This kinematic solver does not support IK solution cost functions");
366  return false;
367  }
368 
376  virtual bool getPositionFK(const std::vector<std::string>& link_names, const std::vector<double>& joint_angles,
377  std::vector<geometry_msgs::msg::Pose>& poses) const = 0;
378 
389  virtual void setValues(const std::string& robot_description, const std::string& group_name,
390  const std::string& base_frame, const std::vector<std::string>& tip_frames,
391  double search_discretization);
392 
406  virtual bool initialize(const rclcpp::Node::SharedPtr& node, const moveit::core::RobotModel& robot_model,
407  const std::string& group_name, const std::string& base_frame,
408  const std::vector<std::string>& tip_frames, double search_discretization);
409 
414  virtual const std::string& getGroupName() const
415  {
416  return group_name_;
417  }
418 
424  virtual const std::string& getBaseFrame() const
425  {
426  return base_frame_;
427  }
428 
434  virtual const std::string& getTipFrame() const
435  {
436  if (tip_frames_.size() > 1)
437  RCLCPP_ERROR(LOGGER, "This kinematic solver has more than one tip frame, "
438  "do not call getTipFrame()");
439 
440  return tip_frames_[0];
441  }
442 
448  virtual const std::vector<std::string>& getTipFrames() const
449  {
450  return tip_frames_;
451  }
452 
461  virtual bool setRedundantJoints(const std::vector<unsigned int>& redundant_joint_indices);
462 
469  bool setRedundantJoints(const std::vector<std::string>& redundant_joint_names);
470 
474  virtual void getRedundantJoints(std::vector<unsigned int>& redundant_joint_indices) const
475  {
476  redundant_joint_indices = redundant_joint_indices_;
477  }
478 
482  virtual const std::vector<std::string>& getJointNames() const = 0;
483 
487  virtual const std::vector<std::string>& getLinkNames() const = 0;
488 
505  virtual bool supportsGroup(const moveit::core::JointModelGroup* jmg, std::string* error_text_out = nullptr) const;
506 
510  void setSearchDiscretization(double sd)
511  {
512  redundant_joint_discretization_.clear();
513  for (unsigned int index : redundant_joint_indices_)
514  redundant_joint_discretization_[index] = sd;
515  }
516 
524  void setSearchDiscretization(const std::map<int, double>& discretization)
525  {
526  redundant_joint_discretization_.clear();
527  redundant_joint_indices_.clear();
528  for (const auto& pair : discretization)
529  {
530  redundant_joint_discretization_.insert(pair);
531  redundant_joint_indices_.push_back(pair.first);
532  }
533  }
534 
538  double getSearchDiscretization(int joint_index = 0) const
539  {
540  if (redundant_joint_discretization_.count(joint_index) > 0)
541  {
542  return redundant_joint_discretization_.at(joint_index);
543  }
544  else
545  {
546  return 0.0; // returned when there aren't any redundant joints
547  }
548  }
549 
554  std::vector<DiscretizationMethod> getSupportedDiscretizationMethods() const
555  {
556  return supported_methods_;
557  }
558 
561  void setDefaultTimeout(double timeout)
562  {
563  default_timeout_ = timeout;
564  }
565 
568  double getDefaultTimeout() const
569  {
570  return default_timeout_;
571  }
572 
576  virtual ~KinematicsBase();
577 
578  KinematicsBase();
579 
580 protected:
581  rclcpp::Node::SharedPtr node_;
582  moveit::core::RobotModelConstPtr robot_model_;
583  std::string robot_description_;
584  std::string group_name_;
585  std::string base_frame_;
586  std::vector<std::string> tip_frames_;
587 
589  std::vector<unsigned int> redundant_joint_indices_;
590  std::map<int, double> redundant_joint_discretization_;
591  std::vector<DiscretizationMethod> supported_methods_;
592 
606  template <typename T>
607  inline bool lookupParam(const rclcpp::Node::SharedPtr& node, const std::string& param, T& val,
608  const T& default_val) const
609  {
610  if (node->has_parameter({ group_name_ + "." + param }))
611  {
612  node->get_parameter(group_name_ + "." + param, val);
613  return true;
614  }
615 
616  if (node->has_parameter({ param }))
617  {
618  node->get_parameter(param, val);
619  return true;
620  }
621 
622  if (node->has_parameter({ "robot_description_kinematics." + group_name_ + "." + param }))
623  {
624  node->get_parameter("robot_description_kinematics." + group_name_ + "." + param, val);
625  return true;
626  }
627 
628  if (node->has_parameter("robot_description_kinematics." + param))
629  {
630  node->get_parameter("robot_description_kinematics." + param, val);
631  return true;
632  }
633 
634  val = default_val;
635  return false;
636  }
637 
646  void storeValues(const moveit::core::RobotModel& robot_model, const std::string& group_name,
647  const std::string& base_frame, const std::vector<std::string>& tip_frames,
648  double search_discretization);
649 
650 private:
651  std::string removeSlash(const std::string& str) const;
652 };
653 } // namespace kinematics
Provides an interface for kinematics solvers.
virtual bool getPositionFK(const std::vector< std::string > &link_names, const std::vector< double > &joint_angles, std::vector< geometry_msgs::msg::Pose > &poses) const =0
Given a set of joint angles and a set of links, compute their pose.
virtual bool searchPositionIK(const std::vector< geometry_msgs::msg::Pose > &ik_poses, const std::vector< double > &ik_seed_state, double timeout, const std::vector< double > &consistency_limits, std::vector< double > &solution, const IKCallbackFn &solution_callback, const IKCostFn &cost_function, moveit_msgs::msg::MoveItErrorCodes &error_code, const kinematics::KinematicsQueryOptions &options=kinematics::KinematicsQueryOptions(), const moveit::core::RobotState *context_state=nullptr) const
Given a set of desired poses for a planning group with multiple end-effectors, search for the joint a...
virtual const std::vector< std::string > & getJointNames() const =0
Return all the joint names in the order they are used internally.
virtual bool searchPositionIK(const geometry_msgs::msg::Pose &ik_pose, const std::vector< double > &ik_seed_state, double timeout, const std::vector< double > &consistency_limits, std::vector< double > &solution, const IKCallbackFn &solution_callback, moveit_msgs::msg::MoveItErrorCodes &error_code, const kinematics::KinematicsQueryOptions &options=kinematics::KinematicsQueryOptions()) const =0
Given a desired pose of the end-effector, search for the joint angles required to reach it....
void setSearchDiscretization(double sd)
Set the search discretization value for all the redundant joints.
std::vector< DiscretizationMethod > getSupportedDiscretizationMethods() const
Returns the set of supported kinematics discretization search types. This implementation only support...
virtual void getRedundantJoints(std::vector< unsigned int > &redundant_joint_indices) const
Get the set of redundant joints.
virtual bool searchPositionIK(const std::vector< geometry_msgs::msg::Pose > &ik_poses, const std::vector< double > &ik_seed_state, double timeout, const std::vector< double > &consistency_limits, std::vector< double > &solution, const IKCallbackFn &solution_callback, moveit_msgs::msg::MoveItErrorCodes &error_code, const kinematics::KinematicsQueryOptions &options=kinematics::KinematicsQueryOptions(), const moveit::core::RobotState *context_state=nullptr) const
Given a set of desired poses for a planning group with multiple end-effectors, search for the joint a...
virtual const std::vector< std::string > & getLinkNames() const =0
Return all the link names in the order they are represented internally.
std::map< int, double > redundant_joint_discretization_
std::vector< unsigned int > redundant_joint_indices_
virtual const std::string & getTipFrame() const
Return the name of the tip frame of the chain on which the solver is operating. This is usually a lin...
static const rclcpp::Logger LOGGER
virtual const std::string & getBaseFrame() const
Return the name of the frame in which the solver is operating. This is usually a link name....
std::vector< DiscretizationMethod > supported_methods_
void setDefaultTimeout(double timeout)
For functions that require a timeout specified but one is not specified using arguments,...
virtual bool searchPositionIK(const geometry_msgs::msg::Pose &ik_pose, const std::vector< double > &ik_seed_state, double timeout, std::vector< double > &solution, moveit_msgs::msg::MoveItErrorCodes &error_code, const kinematics::KinematicsQueryOptions &options=kinematics::KinematicsQueryOptions()) const =0
Given a desired pose of the end-effector, search for the joint angles required to reach it....
bool lookupParam(const rclcpp::Node::SharedPtr &node, const std::string &param, T &val, const T &default_val) const
Enables kinematics plugins access to parameters that are defined for the private namespace and inside...
virtual bool getPositionIK(const geometry_msgs::msg::Pose &ik_pose, const std::vector< double > &ik_seed_state, std::vector< double > &solution, moveit_msgs::msg::MoveItErrorCodes &error_code, const kinematics::KinematicsQueryOptions &options=kinematics::KinematicsQueryOptions()) const =0
Given a desired pose of the end-effector, compute the joint angles to reach it.
double getSearchDiscretization(int joint_index=0) const
Get the value of the search discretization.
virtual bool searchPositionIK(const geometry_msgs::msg::Pose &ik_pose, const std::vector< double > &ik_seed_state, double timeout, const std::vector< double > &consistency_limits, std::vector< double > &solution, moveit_msgs::msg::MoveItErrorCodes &error_code, const kinematics::KinematicsQueryOptions &options=kinematics::KinematicsQueryOptions()) const =0
Given a desired pose of the end-effector, search for the joint angles required to reach it....
rclcpp::Node::SharedPtr node_
virtual ~KinematicsBase()
Virtual destructor for the interface.
virtual const std::vector< std::string > & getTipFrames() const
Return the names of the tip frames of the kinematic tree on which the solver is operating....
std::vector< std::string > tip_frames_
virtual bool searchPositionIK(const geometry_msgs::msg::Pose &ik_pose, const std::vector< double > &ik_seed_state, double timeout, std::vector< double > &solution, const IKCallbackFn &solution_callback, moveit_msgs::msg::MoveItErrorCodes &error_code, const kinematics::KinematicsQueryOptions &options=kinematics::KinematicsQueryOptions()) const =0
Given a desired pose of the end-effector, search for the joint angles required to reach it....
std::function< double(const geometry_msgs::msg::Pose &, const moveit::core::RobotState &, moveit::core::JointModelGroup const *, const std::vector< double > &)> IKCostFn
Signature for a cost function used to evaluate IK solutions.
static const double DEFAULT_TIMEOUT
std::function< void(const geometry_msgs::msg::Pose &, const std::vector< double > &, moveit_msgs::msg::MoveItErrorCodes &)> IKCallbackFn
Signature for a callback to validate an IK solution. Typically used for collision checking.
moveit::core::RobotModelConstPtr robot_model_
double getDefaultTimeout() const
For functions that require a timeout specified but one is not specified using arguments,...
void setSearchDiscretization(const std::map< int, double > &discretization)
Sets individual discretization values for each redundant joint.
static const double DEFAULT_SEARCH_DISCRETIZATION
virtual const std::string & getGroupName() const
Return the name of the group that the solver is operating on.
Definition of a kinematic model. This class is not thread safe, however multiple instances can be cre...
Definition: robot_model.h:76
Representation of a robot's state. This includes position, velocity, acceleration and effort.
Definition: robot_state.h:90
API for forward and inverse kinematics.
MOVEIT_CLASS_FORWARD(KinematicsBase)
MOVEIT_CLASS_FORWARD(JointModelGroup)
Main namespace for MoveIt.
Definition: exceptions.h:43
A set of options for the kinematics solver.
DiscretizationMethod discretization_method