pr2_arm_ik.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 */
36 
37 #ifndef PR2_ARM_IK_H
38 #define PR2_ARM_IK_H
39 
40 #include <urdf_model/model.h>
41 #include <urdf/model.h>
42 #include <Eigen/Geometry>
43 #include <Eigen/LU> // provides LU decomposition
44 #include <kdl/chainiksolver.hpp>
45 #include <moveit_msgs/GetPositionFK.h>
46 #include <moveit_msgs/GetPositionIK.h>
47 #include <moveit_msgs/KinematicSolverInfo.h>
48 
50 {
51 static const int NUM_JOINTS_ARM7DOF = 7;
52 
53 static const double IK_EPS = 1e-5;
54 
55 inline double distance(const urdf::Pose& transform)
56 {
57  return sqrt(transform.position.x * transform.position.x + transform.position.y * transform.position.y +
58  transform.position.z * transform.position.z);
59 }
60 
61 inline bool solveQuadratic(const double& a, const double& b, const double& c, double* x1, double* x2)
62 {
63  double discriminant = b * b - 4 * a * c;
64  if (fabs(a) < IK_EPS)
65  {
66  *x1 = -c / b;
67  *x2 = *x1;
68  return true;
69  }
70  // ROS_DEBUG("Discriminant: %f\n",discriminant);
71  if (discriminant >= 0)
72  {
73  *x1 = (-b + sqrt(discriminant)) / (2 * a);
74  *x2 = (-b - sqrt(discriminant)) / (2 * a);
75  return true;
76  }
77  else if (fabs(discriminant) < IK_EPS)
78  {
79  *x1 = -b / (2 * a);
80  *x2 = -b / (2 * a);
81  return true;
82  }
83  else
84  {
85  *x1 = -b / (2 * a);
86  *x2 = -b / (2 * a);
87  return false;
88  }
89 }
90 
91 inline bool solveCosineEqn(const double& a, const double& b, const double& c, double& soln1, double& soln2)
92 {
93  double theta1 = atan2(b, a);
94  double denom = sqrt(a * a + b * b);
95 
96  if (fabs(denom) < IK_EPS) // should never happen, wouldn't make sense but make sure it is checked nonetheless
97  {
98 #ifdef DEBUG
99  std::cout << "denom: " << denom << std::endl;
100 #endif
101  return false;
102  }
103  double rhs_ratio = c / denom;
104  if (rhs_ratio < -1 || rhs_ratio > 1)
105  {
106 #ifdef DEBUG
107  std::cout << "rhs_ratio: " << rhs_ratio << std::endl;
108 #endif
109  return false;
110  }
111  double acos_term = acos(rhs_ratio);
112  soln1 = theta1 + acos_term;
113  soln2 = theta1 - acos_term;
114 
115  return true;
116 }
117 
118 class PR2ArmIK
119 {
120 public:
126  PR2ArmIK();
128 
136  bool init(const urdf::ModelInterface& robot_model, const std::string& root_name, const std::string& tip_name);
137 
143  void computeIKShoulderPan(const Eigen::Affine3f& g_in, const double& shoulder_pan_initial_guess,
144  std::vector<std::vector<double> >& solution) const;
145 
151  void computeIKShoulderRoll(const Eigen::Affine3f& g_in, const double& shoulder_roll_initial_guess,
152  std::vector<std::vector<double> >& solution) const;
153 
154  // std::vector<std::vector<double> > solution_ik_;/// a vector of ik solutions
155 
161  void getSolverInfo(moveit_msgs::KinematicSolverInfo& info);
162 
166  moveit_msgs::KinematicSolverInfo solver_info_;
167 
169 
170 private:
171  void addJointToChainInfo(urdf::JointConstSharedPtr joint, moveit_msgs::KinematicSolverInfo& info);
172 
173  bool checkJointLimits(const std::vector<double>& joint_values) const;
174 
175  bool checkJointLimits(const double& joint_value, const int& joint_num) const;
176 
177  Eigen::Affine3f grhs_, gf_, home_inv_;
178 
179  std::vector<double> angle_multipliers_;
180 
181  std::vector<double> solution_;
182 
185 
186  std::vector<double> min_angles_;
187 
188  std::vector<double> max_angles_;
189 
190  std::vector<bool> continuous_joint_;
191 };
192 }
193 #endif // PR2_ARM_IK_H
std::vector< double > max_angles_
Definition: pr2_arm_ik.h:188
Core components of MoveIt!
Eigen::Affine3f home_inv_
Definition: pr2_arm_ik.h:177
std::vector< bool > continuous_joint_
Definition: pr2_arm_ik.h:190
static const int NUM_JOINTS_ARM7DOF
Definition: pr2_arm_ik.h:51
bool solveQuadratic(const double &a, const double &b, const double &c, double *x1, double *x2)
Definition: pr2_arm_ik.h:61
void computeIKShoulderRoll(const Eigen::Affine3f &g_in, const double &shoulder_roll_initial_guess, std::vector< std::vector< double > > &solution) const
compute IK based on an initial guess for the shoulder roll angle. h
Definition: pr2_arm_ik.cpp:464
void addJointToChainInfo(urdf::JointConstSharedPtr joint, moveit_msgs::KinematicSolverInfo &info)
Definition: pr2_arm_ik.cpp:154
ROS/KDL based interface for the inverse kinematics of the PR2 arm.
INLINE Rall1d< T, V, S > sqrt(const Rall1d< T, V, S > &arg)
void getSolverInfo(moveit_msgs::KinematicSolverInfo &info)
get chain information about the arm. This populates the IK query response, filling in joint level inf...
Definition: pr2_arm_ik.cpp:192
std::vector< double > min_angles_
Definition: pr2_arm_ik.h:186
std::vector< double > solution_
Definition: pr2_arm_ik.h:181
INLINE Rall1d< T, V, S > acos(const Rall1d< T, V, S > &x)
void computeIKShoulderPan(const Eigen::Affine3f &g_in, const double &shoulder_pan_initial_guess, std::vector< std::vector< double > > &solution) const
compute IK based on an initial guess for the shoulder pan angle.
Definition: pr2_arm_ik.cpp:197
INLINE Rall1d< T, V, S > atan2(const Rall1d< T, V, S > &y, const Rall1d< T, V, S > &x)
bool init(const urdf::ModelInterface &robot_model, const std::string &root_name, const std::string &tip_name)
Initialize the solver by providing a urdf::Model and a root and tip name.
Definition: pr2_arm_ik.cpp:56
static const double IK_EPS
Definition: pr2_arm_ik.h:53
moveit_msgs::KinematicSolverInfo solver_info_
get chain information about the arm.
Definition: pr2_arm_ik.h:166
std::vector< double > angle_multipliers_
Definition: pr2_arm_ik.h:179
bool checkJointLimits(const std::vector< double > &joint_values) const
Definition: pr2_arm_ik.cpp:768
double distance(const urdf::Pose &transform)
Definition: pr2_arm_ik.h:55
bool solveCosineEqn(const double &a, const double &b, const double &c, double &soln1, double &soln2)
Definition: pr2_arm_ik.h:91


moveit_core
Author(s): Ioan Sucan , Sachin Chitta , Acorn Pooley
autogenerated on Wed Dec 12 2018 03:48:59