kdl_inv_kin_chain_nr_jl.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>(
57  ik_solver_ = std::make_unique<KDL::ChainIkSolverPos_NR_JL>(kdl_data_.robot_chain,
60  *fk_solver_,
64 }
65 
67  const std::string& base_link,
68  const std::string& tip_link,
69  Config kdl_config,
70  std::string solver_name)
71  : KDLInvKinChainNR_JL(scene_graph, { std::make_pair(base_link, tip_link) }, kdl_config, std::move(solver_name))
72 {
73 }
74 
75 InverseKinematics::UPtr KDLInvKinChainNR_JL::clone() const { return std::make_unique<KDLInvKinChainNR_JL>(*this); }
76 
78 
80 {
81  kdl_data_ = other.kdl_data_;
82  kdl_config_ = other.kdl_config_;
83  fk_solver_ = std::make_unique<KDL::ChainFkSolverPos_recursive>(kdl_data_.robot_chain);
84  ik_vel_solver_ = std::make_unique<KDL::ChainIkSolverVel_pinv>(
86  ik_solver_ = std::make_unique<KDL::ChainIkSolverPos_NR_JL>(kdl_data_.robot_chain,
89  *fk_solver_,
93  solver_name_ = other.solver_name_;
94 
95  return *this;
96 }
97 
98 IKSolutions KDLInvKinChainNR_JL::calcInvKinHelper(const Eigen::Isometry3d& pose,
99  const Eigen::Ref<const Eigen::VectorXd>& seed,
100  int /*segment_num*/) const
101 {
102  assert(std::abs(1.0 - pose.matrix().determinant()) < 1e-6); // NOLINT
103  KDL::JntArray kdl_seed;
104  KDL::JntArray kdl_solution;
105  EigenToKDL(seed, kdl_seed);
106  kdl_solution.resize(static_cast<unsigned>(seed.size()));
107  Eigen::VectorXd solution(seed.size());
108 
109  // run IK solver
110  // TODO: Need to update to handle seg number. Need to create an IK solver for each seg.
111  KDL::Frame kdl_pose;
112  EigenToKDL(pose, kdl_pose);
113  int status{ -1 };
114  {
115  std::lock_guard<std::mutex> guard(mutex_);
116  status = ik_solver_->CartToJnt(kdl_seed, kdl_pose, kdl_solution);
117  }
118 
119  if (status < 0)
120  {
121  // LCOV_EXCL_START
122  if (status == KDL::ChainIkSolverPos_NR_JL::E_DEGRADED)
123  {
124  CONSOLE_BRIDGE_logDebug("KDL NR_JL Failed to calculate IK, solution converged to <eps in maxiter, but solution "
125  "is degraded in quality (e.g. pseudo-inverse in iksolver is singular)");
126  }
127  else if (status == KDL::ChainIkSolverPos_NR_JL::E_IKSOLVERVEL_FAILED)
128  {
129  CONSOLE_BRIDGE_logDebug("KDL NR_JL Failed to calculate IK, velocity IK solver failed");
130  }
131  else if (status == KDL::ChainIkSolverPos_NR_JL::E_FKSOLVERPOS_FAILED)
132  {
133  CONSOLE_BRIDGE_logDebug("KDL NR_JL Failed to calculate IK, position FK solver failed");
134  }
135  else if (status == KDL::ChainIkSolverPos_NR_JL::E_NO_CONVERGE)
136  {
137  CONSOLE_BRIDGE_logDebug("KDL NR_JL Failed to calculate IK, no solution found");
138  }
139 #ifndef KDL_LESS_1_4_0
140  else if (status == KDL::ChainIkSolverPos_NR_JL::E_MAX_ITERATIONS_EXCEEDED)
141  {
142  CONSOLE_BRIDGE_logDebug("KDL NR_JL Failed to calculate IK, max iteration exceeded");
143  }
144 #endif
145  else
146  {
147  CONSOLE_BRIDGE_logDebug("KDL NR_JL Failed to calculate IK");
148  }
149  // LCOV_EXCL_STOP
150  return {};
151  }
152 
153  KDLToEigen(kdl_solution, solution);
154 
155  return { solution };
156 }
157 
159  const Eigen::Ref<const Eigen::VectorXd>& seed) const
160 {
161  assert(tip_link_poses.find(kdl_data_.tip_link_name) != tip_link_poses.end());
162  return calcInvKinHelper(tip_link_poses.at(kdl_data_.tip_link_name), seed);
163 }
164 
165 std::vector<std::string> KDLInvKinChainNR_JL::getJointNames() const { return kdl_data_.joint_names; }
166 
167 Eigen::Index KDLInvKinChainNR_JL::numJoints() const { return kdl_data_.robot_chain.getNrOfJoints(); }
168 
170 
172 
173 std::vector<std::string> KDLInvKinChainNR_JL::getTipLinkNames() const { return { kdl_data_.tip_link_name }; }
174 
175 std::string KDLInvKinChainNR_JL::getSolverName() const { return solver_name_; }
176 
177 } // 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::KDLInvKinChainNR_JL::ik_vel_solver_
std::unique_ptr< KDL::ChainIkSolverVel_pinv > ik_vel_solver_
KDL Inverse kinematic velocity solver.
Definition: kdl_inv_kin_chain_nr_jl.h:122
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_JL::kdl_config_
Config kdl_config_
KDL configuration data parsed from YAML.
Definition: kdl_inv_kin_chain_nr_jl.h:120
tesseract_kinematics::KDLChainData::q_min
KDL::JntArray q_min
Lower joint limits.
Definition: kdl_utils.h:100
tesseract_kinematics::KDLInvKinChainNR_JL::mutex_
std::mutex mutex_
KDL is not thread safe due to mutable variables in Joint Class.
Definition: kdl_inv_kin_chain_nr_jl.h:125
tesseract_kinematics::KDLInvKinChainNR::Config::pos_iterations
int pos_iterations
Definition: kdl_inv_kin_chain_nr.h:72
tesseract_kinematics::KDLInvKinChainNR_JL::KDLInvKinChainNR_JL
KDLInvKinChainNR_JL(const KDLInvKinChainNR_JL &other)
Definition: kdl_inv_kin_chain_nr_jl.cpp:77
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_JL::Config::pos_eps
double pos_eps
Definition: kdl_inv_kin_chain_nr_jl.h:71
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
tesseract_kinematics::KDLInvKinChainNR_JL::operator=
KDLInvKinChainNR_JL & operator=(const KDLInvKinChainNR_JL &other)
Definition: kdl_inv_kin_chain_nr_jl.cpp:79
tesseract_kinematics::KDLInvKinChainNR_JL::solver_name_
std::string solver_name_
Name of this solver.
Definition: kdl_inv_kin_chain_nr_jl.h:124
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::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_JL::Config
The Config struct.
Definition: kdl_inv_kin_chain_nr_jl.h:67
kdl_parser.h
tesseract_kinematics::KDLInvKinChainNR_JL::getJointNames
std::vector< std::string > getJointNames() const override final
Get list of joint names for kinematic object.
Definition: kdl_inv_kin_chain_nr_jl.cpp:165
tesseract_kinematics::KDLInvKinChainNR_JL::ik_solver_
std::unique_ptr< KDL::ChainIkSolverPos_NR_JL > ik_solver_
KDL Inverse kinematic solver.
Definition: kdl_inv_kin_chain_nr_jl.h:123
tesseract_kinematics::KDLInvKinChainNR_JL::numJoints
Eigen::Index numJoints() const override final
Number of joints in robot.
Definition: kdl_inv_kin_chain_nr_jl.cpp:167
tesseract_kinematics::KDLInvKinChainNR_JL::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_jl.cpp:158
tesseract_kinematics::KDLInvKinChainNR_JL::fk_solver_
std::unique_ptr< KDL::ChainFkSolverPos_recursive > fk_solver_
KDL Forward Kinematic Solver.
Definition: kdl_inv_kin_chain_nr_jl.h:121
tesseract_kinematics::KDLChainData::joint_names
std::vector< std::string > joint_names
List of joint names.
Definition: kdl_utils.h:95
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::Config::vel_iterations
int vel_iterations
Definition: kdl_inv_kin_chain_nr.h:70
tesseract_kinematics::KDLInvKinChainNR_JL::kdl_data_
KDLChainData kdl_data_
KDL data parsed from Scene Graph.
Definition: kdl_inv_kin_chain_nr_jl.h:119
tesseract_kinematics::KDLInvKinChainNR_JL
KDL Inverse kinematic chain implementation.
Definition: kdl_inv_kin_chain_nr_jl.h:46
tesseract_kinematics::KDLInvKinChainNR_JL::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_jl.cpp:175
tesseract_kinematics::KDLInvKinChainNR_JL::getBaseLinkName
std::string getBaseLinkName() const override final
Get the robot base link name.
Definition: kdl_inv_kin_chain_nr_jl.cpp:169
tesseract_kinematics::KDLInvKinChainNR::Config::pos_eps
double pos_eps
Definition: kdl_inv_kin_chain_nr.h:71
tesseract_kinematics::KDLInvKinChainNR_JL::Config::vel_iterations
int vel_iterations
Definition: kdl_inv_kin_chain_nr_jl.h:70
tesseract_kinematics::EigenToKDL
void EigenToKDL(const Eigen::Isometry3d &transform, KDL::Frame &frame)
Convert Eigen::Isometry3d to KDL::Frame.
Definition: kdl_utils.cpp:52
TESSERACT_COMMON_IGNORE_WARNINGS_POP
#define TESSERACT_COMMON_IGNORE_WARNINGS_POP
tesseract_scene_graph::SceneGraph::getRoot
const std::string & getRoot() const
tesseract_kinematics::KDLInvKinChainNR_JL::Config::vel_eps
double vel_eps
Definition: kdl_inv_kin_chain_nr_jl.h:69
tesseract_kinematics
Definition: forward_kinematics.h:40
tesseract_scene_graph::SceneGraph::getLink
std::shared_ptr< const Link > getLink(const std::string &name) const
tesseract_kinematics::KDLInvKinChainNR_JL::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_jl.cpp:98
tesseract_kinematics::KDLInvKinChainNR_JL::Config::pos_iterations
int pos_iterations
Definition: kdl_inv_kin_chain_nr_jl.h:72
tesseract_kinematics::KDLChainData::q_max
KDL::JntArray q_max
Upper joint limits.
Definition: kdl_utils.h:101
tesseract_kinematics::IKSolutions
std::vector< Eigen::VectorXd > IKSolutions
The inverse kinematics solutions container.
Definition: types.h:38
tesseract_kinematics::KDLInvKinChainNR_JL::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_jl.cpp:173
macros.h
tesseract_kinematics::KDLChainData::robot_chain
KDL::Chain robot_chain
KDL Chain object.
Definition: kdl_utils.h:93
tesseract_kinematics::KDLInvKinChainNR_JL::clone
InverseKinematics::UPtr clone() const override final
Clone the forward kinematics object.
Definition: kdl_inv_kin_chain_nr_jl.cpp:75
tesseract_kinematics::KDLInvKinChainNR_JL::getWorkingFrame
std::string getWorkingFrame() const override final
Get the inverse kinematics working frame.
Definition: kdl_inv_kin_chain_nr_jl.cpp:171
kdl_inv_kin_chain_nr_jl.h
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
Author(s): Levi Armstrong
autogenerated on Sun May 18 2025 03:02:14