moveit2
The MoveIt Motion Planning Framework for ROS 2.
kinematics_base.cpp
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 
39 #include <rclcpp/logger.hpp>
40 
41 namespace kinematics
42 {
43 // Logger
44 const rclcpp::Logger KinematicsBase::LOGGER = rclcpp::get_logger("moveit_kinematics_base.kinematics_base");
46 const double KinematicsBase::DEFAULT_TIMEOUT = 1.0;
47 
48 static void noDeleter(const moveit::core::RobotModel* /*unused*/)
49 {
50 }
51 
52 void KinematicsBase::storeValues(const moveit::core::RobotModel& robot_model, const std::string& group_name,
53  const std::string& base_frame, const std::vector<std::string>& tip_frames,
54  double search_discretization)
55 {
56  // The RobotModel is passed in as a borrowed reference from the JointModelGroup belonging to this RobotModel.
57  // Hence, it is ensured that the RobotModel will not be destroyed before the JMG and its associated
58  // kinematics solvers. To keep RobotModelPtr API (instead of storing the reference here only), but break
59  // the circular reference (RM => JMG => KS -> RM), here we create a new shared_ptr that doesn't delete the RM.
60  robot_model_ = moveit::core::RobotModelConstPtr(&robot_model, &noDeleter);
61  robot_description_ = "";
62  group_name_ = group_name;
63  base_frame_ = removeSlash(base_frame);
64  tip_frames_.clear();
65  for (const std::string& name : tip_frames)
66  tip_frames_.push_back(removeSlash(name));
67  setSearchDiscretization(search_discretization);
68 }
69 
70 void KinematicsBase::setValues(const std::string& robot_description, const std::string& group_name,
71  const std::string& base_frame, const std::vector<std::string>& tip_frames,
72  double search_discretization)
73 {
74  robot_model_.reset();
75  robot_description_ = robot_description;
76  group_name_ = group_name;
77  base_frame_ = removeSlash(base_frame);
78  tip_frames_.clear();
79  for (const std::string& name : tip_frames)
80  tip_frames_.push_back(removeSlash(name));
81  setSearchDiscretization(search_discretization);
82 }
83 
84 bool KinematicsBase::initialize(const rclcpp::Node::SharedPtr& /*node*/,
85  const moveit::core::RobotModel& /*robot_model*/, const std::string& group_name,
86  const std::string& /*base_frame*/, const std::vector<std::string>& /*tip_frames*/,
87  double /*search_discretization*/)
88 {
89  RCLCPP_ERROR(LOGGER,
90  "IK plugin for group '%s' relies on deprecated API. "
91  "Please implement initialize(rclcpp::Node::SharedPtr, RobotModel, ...).",
92  group_name.c_str());
93  return false;
94 }
95 
96 bool KinematicsBase::setRedundantJoints(const std::vector<unsigned int>& redundant_joint_indices)
97 {
98  for (const unsigned int& redundant_joint_index : redundant_joint_indices)
99  {
100  if (redundant_joint_index >= getJointNames().size())
101  {
102  return false;
103  }
104  }
105  redundant_joint_indices_ = redundant_joint_indices;
107 
108  return true;
109 }
110 
111 bool KinematicsBase::setRedundantJoints(const std::vector<std::string>& redundant_joint_names)
112 {
113  const std::vector<std::string>& jnames = getJointNames();
114  std::vector<unsigned int> redundant_joint_indices;
115  for (const std::string& redundant_joint_name : redundant_joint_names)
116  for (std::size_t j = 0; j < jnames.size(); ++j)
117  if (jnames[j] == redundant_joint_name)
118  {
119  redundant_joint_indices.push_back(j);
120  break;
121  }
122  return redundant_joint_indices.size() == redundant_joint_names.size() ? setRedundantJoints(redundant_joint_indices) :
123  false;
124 }
125 
126 std::string KinematicsBase::removeSlash(const std::string& str) const
127 {
128  return (!str.empty() && str[0] == '/') ? removeSlash(str.substr(1)) : str;
129 }
130 
131 bool KinematicsBase::supportsGroup(const moveit::core::JointModelGroup* jmg, std::string* error_text_out) const
132 {
133  // Default implementation for legacy solvers:
134  if (!jmg->isChain())
135  {
136  if (error_text_out)
137  {
138  *error_text_out = "This plugin only supports joint groups which are chains";
139  }
140  return false;
141  }
142 
143  return true;
144 }
145 
146 KinematicsBase::KinematicsBase() : default_timeout_(DEFAULT_TIMEOUT)
147 {
149 }
150 
152 
153 bool KinematicsBase::getPositionIK(const std::vector<geometry_msgs::msg::Pose>& ik_poses,
154  const std::vector<double>& ik_seed_state,
155  std::vector<std::vector<double> >& solutions, KinematicsResult& result,
156  const KinematicsQueryOptions& options) const
157 {
158  std::vector<double> solution;
159  result.solution_percentage = 0.0;
160 
161  if (std::find(supported_methods_.begin(), supported_methods_.end(), options.discretization_method) ==
162  supported_methods_.end())
163  {
165  return false;
166  }
167 
168  if (ik_poses.size() != 1)
169  {
170  RCLCPP_ERROR(LOGGER, "This kinematic solver does not support getPositionIK for multiple tips");
172  return false;
173  }
174 
175  if (ik_poses.empty())
176  {
177  RCLCPP_ERROR(LOGGER, "Input ik_poses array is empty");
179  return false;
180  }
181 
182  moveit_msgs::msg::MoveItErrorCodes error_code;
183  if (getPositionIK(ik_poses[0], ik_seed_state, solution, error_code, options))
184  {
185  solutions.resize(1);
186  solutions[0] = solution;
188  result.solution_percentage = 1.0f;
189  }
190  else
191  {
193  return false;
194  }
195 
196  return true;
197 }
198 } // end of namespace kinematics
virtual const std::vector< std::string > & getJointNames() const =0
Return all the joint names in the order they are used internally.
void setSearchDiscretization(double sd)
Set the search discretization value for all the redundant joints.
void storeValues(const moveit::core::RobotModel &robot_model, const std::string &group_name, const std::string &base_frame, const std::vector< std::string > &tip_frames, double search_discretization)
virtual void setValues(const std::string &robot_description, const std::string &group_name, const std::string &base_frame, const std::vector< std::string > &tip_frames, double search_discretization)
Set the parameters for the solver, for use with non-chain IK solvers.
std::vector< unsigned int > redundant_joint_indices_
static const rclcpp::Logger LOGGER
std::vector< DiscretizationMethod > supported_methods_
virtual bool initialize(const rclcpp::Node::SharedPtr &node, const moveit::core::RobotModel &robot_model, const std::string &group_name, const std::string &base_frame, const std::vector< std::string > &tip_frames, double search_discretization)
Initialization function for the kinematics, for use with kinematic chain IK solvers.
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.
virtual ~KinematicsBase()
Virtual destructor for the interface.
std::vector< std::string > tip_frames_
virtual bool supportsGroup(const moveit::core::JointModelGroup *jmg, std::string *error_text_out=nullptr) const
Check if this solver supports a given JointModelGroup.
static const double DEFAULT_TIMEOUT
moveit::core::RobotModelConstPtr robot_model_
static const double DEFAULT_SEARCH_DISCRETIZATION
virtual bool setRedundantJoints(const std::vector< unsigned int > &redundant_joint_indices)
Set a set of redundant joints for the kinematics solver to use. This can fail, depending on the IK so...
bool isChain() const
Check if this group is a linear chain.
Definition of a kinematic model. This class is not thread safe, however multiple instances can be cre...
Definition: robot_model.h:76
API for forward and inverse kinematics.
name
Definition: setup.py:7
A set of options for the kinematics solver.