kdl_inv_kin_chain_nr.cpp
Go to the documentation of this file.
1 
28 #include <console_bridge/console.h>
31 #include <memory>
33 
35 
36 namespace tesseract_kinematics
37 {
38 using Eigen::MatrixXd;
39 using Eigen::VectorXd;
40 
42  const std::vector<std::pair<std::string, std::string>>& chains,
43  Config kdl_config,
44  std::string solver_name)
45  : kdl_config_(kdl_config), solver_name_(std::move(solver_name))
46 {
47  if (!scene_graph.getLink(scene_graph.getRoot()))
48  throw std::runtime_error("The scene graph has an invalid root.");
49 
50  if (!parseSceneGraph(kdl_data_, scene_graph, chains))
51  throw std::runtime_error("Failed to parse KDL data from Scene Graph");
52 
53  // Create KDL FK and IK Solver
54  fk_solver_ = std::make_unique<KDL::ChainFkSolverPos_recursive>(kdl_data_.robot_chain);
55  ik_vel_solver_ = std::make_unique<KDL::ChainIkSolverVel_pinv>(
56  kdl_data_.robot_chain, kdl_config_.vel_eps, kdl_config_.vel_iterations);
57  ik_solver_ = std::make_unique<KDL::ChainIkSolverPos_NR>(
58  kdl_data_.robot_chain, *fk_solver_, *ik_vel_solver_, kdl_config_.pos_iterations, kdl_config_.pos_eps);
59 }
60 
62  const std::string& base_link,
63  const std::string& tip_link,
64  Config kdl_config,
65  std::string solver_name)
66  : KDLInvKinChainNR(scene_graph, { std::make_pair(base_link, tip_link) }, kdl_config, std::move(solver_name))
67 {
68 }
69 
70 InverseKinematics::UPtr KDLInvKinChainNR::clone() const { return std::make_unique<KDLInvKinChainNR>(*this); }
71 
72 KDLInvKinChainNR::KDLInvKinChainNR(const KDLInvKinChainNR& other) { *this = other; }
73 
75 {
76  kdl_data_ = other.kdl_data_;
77  kdl_config_ = other.kdl_config_;
78  fk_solver_ = std::make_unique<KDL::ChainFkSolverPos_recursive>(kdl_data_.robot_chain);
79  ik_vel_solver_ = std::make_unique<KDL::ChainIkSolverVel_pinv>(
81  ik_solver_ = std::make_unique<KDL::ChainIkSolverPos_NR>(
83  solver_name_ = other.solver_name_;
84 
85  return *this;
86 }
87 
88 IKSolutions KDLInvKinChainNR::calcInvKinHelper(const Eigen::Isometry3d& pose,
89  const Eigen::Ref<const Eigen::VectorXd>& seed,
90  int /*segment_num*/) const
91 {
92  assert(std::abs(1.0 - pose.matrix().determinant()) < 1e-6); // NOLINT
93  KDL::JntArray kdl_seed;
94  KDL::JntArray kdl_solution;
95  EigenToKDL(seed, kdl_seed);
96  kdl_solution.resize(static_cast<unsigned>(seed.size()));
97  Eigen::VectorXd solution(seed.size());
98 
99  // run IK solver
100  // TODO: Need to update to handle seg number. Need to create an IK solver for each seg.
101  KDL::Frame kdl_pose;
102  EigenToKDL(pose, kdl_pose);
103  int status{ -1 };
104  {
105  std::lock_guard<std::mutex> guard(mutex_);
106  status = ik_solver_->CartToJnt(kdl_seed, kdl_pose, kdl_solution);
107  }
108 
109  if (status < 0)
110  {
111  // LCOV_EXCL_START
112  if (status == KDL::ChainIkSolverPos_NR::E_DEGRADED)
113  {
114  CONSOLE_BRIDGE_logDebug("KDL NR Failed to calculate IK, solution converged to <eps in maxiter, but solution is "
115  "degraded in quality (e.g. pseudo-inverse in iksolver is singular)");
116  }
117  else if (status == KDL::ChainIkSolverPos_NR::E_IKSOLVER_FAILED)
118  {
119  CONSOLE_BRIDGE_logDebug("KDL NR Failed to calculate IK, velocity solver failed");
120  }
121  else if (status == KDL::ChainIkSolverPos_NR::E_NO_CONVERGE)
122  {
123  CONSOLE_BRIDGE_logDebug("KDL NR Failed to calculate IK, no solution found");
124  }
125 #ifndef KDL_LESS_1_4_0
126  else if (status == KDL::ChainIkSolverPos_NR::E_MAX_ITERATIONS_EXCEEDED)
127  {
128  CONSOLE_BRIDGE_logDebug("KDL NR Failed to calculate IK, max iteration exceeded");
129  }
130 #endif
131  else
132  {
133  CONSOLE_BRIDGE_logDebug("KDL NR Failed to calculate IK");
134  }
135  // LCOV_EXCL_STOP
136  return {};
137  }
138 
139  KDLToEigen(kdl_solution, solution);
140 
141  return { solution };
142 }
143 
145  const Eigen::Ref<const Eigen::VectorXd>& seed) const
146 {
147  assert(tip_link_poses.find(kdl_data_.tip_link_name) != tip_link_poses.end());
148  return calcInvKinHelper(tip_link_poses.at(kdl_data_.tip_link_name), seed);
149 }
150 
151 std::vector<std::string> KDLInvKinChainNR::getJointNames() const { return kdl_data_.joint_names; }
152 
153 Eigen::Index KDLInvKinChainNR::numJoints() const { return kdl_data_.robot_chain.getNrOfJoints(); }
154 
156 
158 
159 std::vector<std::string> KDLInvKinChainNR::getTipLinkNames() const { return { kdl_data_.tip_link_name }; }
160 
161 std::string KDLInvKinChainNR::getSolverName() const { return solver_name_; }
162 
163 } // namespace tesseract_kinematics
graph.h
tesseract_kinematics::KDLInvKinChainNR::kdl_data_
KDLChainData kdl_data_
KDL data parsed from Scene Graph.
Definition: kdl_inv_kin_chain_nr.h:119
tesseract_kinematics::KDLInvKinChainNR::fk_solver_
std::unique_ptr< KDL::ChainFkSolverPos_recursive > fk_solver_
KDL Forward Kinematic Solver.
Definition: kdl_inv_kin_chain_nr.h:121
tesseract_kinematics::KDLInvKinChainNR::ik_solver_
std::unique_ptr< KDL::ChainIkSolverPos_NR > ik_solver_
KDL Inverse kinematic solver.
Definition: kdl_inv_kin_chain_nr.h:123
tesseract_kinematics::KDLInvKinChainLMA::kdl_config_
Config kdl_config_
KDL configuration data parsed from YAML.
Definition: kdl_inv_kin_chain_lma.h:118
tesseract_kinematics::KDLChainData::base_link_name
std::string base_link_name
Link name of first link in the kinematic object.
Definition: kdl_utils.h:96
tesseract_kinematics::KDLInvKinChainNR::Config::pos_iterations
int pos_iterations
Definition: kdl_inv_kin_chain_nr.h:72
tesseract_kinematics::KDLChainData::tip_link_name
std::string tip_link_name
Link name of last kink in the kinematic object.
Definition: kdl_utils.h:97
tesseract_common::TransformMap
AlignedMap< std::string, Eigen::Isometry3d > TransformMap
tesseract_kinematics::KDLInvKinChainNR::calcInvKinHelper
IKSolutions calcInvKinHelper(const Eigen::Isometry3d &pose, const Eigen::Ref< const Eigen::VectorXd > &seed, int segment_num=-1) const
calcFwdKin helper function
Definition: kdl_inv_kin_chain_nr.cpp:88
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
tesseract_kinematics::KDLInvKinChainNR::clone
InverseKinematics::UPtr clone() const override final
Clone the forward kinematics object.
Definition: kdl_inv_kin_chain_nr.cpp:70
tesseract_scene_graph::SceneGraph
tesseract_kinematics::KDLInvKinChainNR::ik_vel_solver_
std::unique_ptr< KDL::ChainIkSolverVel_pinv > ik_vel_solver_
KDL Inverse kinematic velocity solver.
Definition: kdl_inv_kin_chain_nr.h:122
tesseract_kinematics::KDLInvKinChainNR::getTipLinkNames
std::vector< std::string > getTipLinkNames() const override final
Get the names of the tip links of the kinematics group.
Definition: kdl_inv_kin_chain_nr.cpp:159
tesseract_kinematics::KDLInvKinChainNR::Config::vel_eps
double vel_eps
Definition: kdl_inv_kin_chain_nr.h:69
tesseract_kinematics::InverseKinematics::UPtr
std::unique_ptr< InverseKinematics > UPtr
Definition: inverse_kinematics.h:52
tesseract_kinematics::KDLInvKinChainNR::calcInvKin
IKSolutions calcInvKin(const tesseract_common::TransformMap &tip_link_poses, const Eigen::Ref< const Eigen::VectorXd > &seed) const override final
Calculates joint solutions given a pose for each tip link.
Definition: kdl_inv_kin_chain_nr.cpp:144
tesseract_kinematics::KDLInvKinChainNR::KDLInvKinChainNR
KDLInvKinChainNR(const KDLInvKinChainNR &other)
Definition: kdl_inv_kin_chain_nr.cpp:72
kdl_parser.h
tesseract_kinematics::KDLChainData::joint_names
std::vector< std::string > joint_names
List of joint names.
Definition: kdl_utils.h:95
tesseract_kinematics::KDLInvKinChainLMA::kdl_data_
KDLChainData kdl_data_
KDL data parsed from Scene Graph.
Definition: kdl_inv_kin_chain_lma.h:117
tesseract_kinematics::KDLInvKinChainNR::kdl_config_
Config kdl_config_
KDL configuration data parsed from YAML.
Definition: kdl_inv_kin_chain_nr.h:120
tesseract_kinematics::KDLInvKinChainNR::operator=
KDLInvKinChainNR & operator=(const KDLInvKinChainNR &other)
Definition: kdl_inv_kin_chain_nr.cpp:74
tesseract_kinematics::KDLInvKinChainNR::Config::vel_iterations
int vel_iterations
Definition: kdl_inv_kin_chain_nr.h:70
tesseract_kinematics::KDLInvKinChainNR::Config
The Config struct.
Definition: kdl_inv_kin_chain_nr.h:67
kdl_inv_kin_chain_nr.h
tesseract_kinematics::KDLInvKinChainNR::getWorkingFrame
std::string getWorkingFrame() const override final
Get the inverse kinematics working frame.
Definition: kdl_inv_kin_chain_nr.cpp:157
tesseract_kinematics::KDLInvKinChainNR::getSolverName
std::string getSolverName() const override final
Get the name of the solver. Recommend using the name of the class.
Definition: kdl_inv_kin_chain_nr.cpp:161
tesseract_kinematics::KDLInvKinChainNR::Config::pos_eps
double pos_eps
Definition: kdl_inv_kin_chain_nr.h:71
tesseract_kinematics::EigenToKDL
void EigenToKDL(const Eigen::Isometry3d &transform, KDL::Frame &frame)
Convert Eigen::Isometry3d to KDL::Frame.
Definition: kdl_utils.cpp:52
tesseract_kinematics::KDLInvKinChainNR::solver_name_
std::string solver_name_
Name of this solver.
Definition: kdl_inv_kin_chain_nr.h:124
TESSERACT_COMMON_IGNORE_WARNINGS_POP
#define TESSERACT_COMMON_IGNORE_WARNINGS_POP
tesseract_scene_graph::SceneGraph::getRoot
const std::string & getRoot() const
tesseract_kinematics::KDLInvKinChainNR::getBaseLinkName
std::string getBaseLinkName() const override final
Get the robot base link name.
Definition: kdl_inv_kin_chain_nr.cpp:155
tesseract_kinematics
Definition: forward_kinematics.h:40
tesseract_kinematics::KDLInvKinChainLMA::ik_solver_
std::unique_ptr< KDL::ChainIkSolverPos_LMA > ik_solver_
KDL Inverse kinematic solver.
Definition: kdl_inv_kin_chain_lma.h:119
tesseract_scene_graph::SceneGraph::getLink
std::shared_ptr< const Link > getLink(const std::string &name) const
tesseract_kinematics::KDLInvKinChainNR
KDL Inverse kinematic chain implementation.
Definition: kdl_inv_kin_chain_nr.h:46
tesseract_kinematics::IKSolutions
std::vector< Eigen::VectorXd > IKSolutions
The inverse kinematics solutions container.
Definition: types.h:38
macros.h
tesseract_kinematics::KDLChainData::robot_chain
KDL::Chain robot_chain
KDL Chain object.
Definition: kdl_utils.h:93
tesseract_kinematics::KDLInvKinChainNR::mutex_
std::mutex mutex_
KDL is not thread safe due to mutable variables in Joint Class.
Definition: kdl_inv_kin_chain_nr.h:125
tesseract_kinematics::KDLInvKinChainNR::getJointNames
std::vector< std::string > getJointNames() const override final
Get list of joint names for kinematic object.
Definition: kdl_inv_kin_chain_nr.cpp:151
tesseract_kinematics::parseSceneGraph
bool parseSceneGraph(KDLChainData &results, const tesseract_scene_graph::SceneGraph &scene_graph, const std::vector< std::pair< std::string, std::string >> &chains)
Parse KDL chain data from the scene graph.
Definition: kdl_utils.cpp:85
tesseract_kinematics::KDLToEigen
void KDLToEigen(const KDL::Frame &frame, Eigen::Isometry3d &transform)
Convert KDL::Frame to Eigen::Isometry3d.
Definition: kdl_utils.cpp:39
tesseract_kinematics::KDLInvKinChainNR::numJoints
Eigen::Index numJoints() const override final
Number of joints in robot.
Definition: kdl_inv_kin_chain_nr.cpp:153


tesseract_kinematics
Author(s): Levi Armstrong
autogenerated on Sun May 18 2025 03:02:14