kdl_inv_kin_chain_lma.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 IK Solver
54  ik_solver_ =
55  std::make_unique<KDL::ChainIkSolverPos_LMA>(kdl_data_.robot_chain,
56  Eigen::Matrix<double, 6, 1>{ kdl_config_.task_weights.data() },
57  kdl_config_.eps,
58  kdl_config_.max_iterations,
59  kdl_config_.eps_joints);
60 }
61 
63  const std::string& base_link,
64  const std::string& tip_link,
65  Config kdl_config,
66  std::string solver_name)
67  : KDLInvKinChainLMA(scene_graph, { std::make_pair(base_link, tip_link) }, kdl_config, std::move(solver_name))
68 {
69 }
70 
71 InverseKinematics::UPtr KDLInvKinChainLMA::clone() const { return std::make_unique<KDLInvKinChainLMA>(*this); }
72 
74 
76 {
77  kdl_data_ = other.kdl_data_;
78  kdl_config_ = other.kdl_config_;
79  ik_solver_ =
80  std::make_unique<KDL::ChainIkSolverPos_LMA>(kdl_data_.robot_chain,
81  Eigen::Matrix<double, 6, 1>{ kdl_config_.task_weights.data() },
85  solver_name_ = other.solver_name_;
86 
87  return *this;
88 }
89 
90 IKSolutions KDLInvKinChainLMA::calcInvKinHelper(const Eigen::Isometry3d& pose,
91  const Eigen::Ref<const Eigen::VectorXd>& seed,
92  int /*segment_num*/) const
93 {
94  assert(std::abs(1.0 - pose.matrix().determinant()) < 1e-6); // NOLINT
95  KDL::JntArray kdl_seed;
96  KDL::JntArray kdl_solution;
97  EigenToKDL(seed, kdl_seed);
98  kdl_solution.resize(static_cast<unsigned>(seed.size()));
99  Eigen::VectorXd solution(seed.size());
100 
101  // run IK solver
102  KDL::Frame kdl_pose;
103  EigenToKDL(pose, kdl_pose);
104  int status{ -1 };
105  {
106  std::lock_guard<std::mutex> guard(mutex_);
107  status = ik_solver_->CartToJnt(kdl_seed, kdl_pose, kdl_solution);
108  }
109  if (status < 0)
110  {
111  // LCOV_EXCL_START
112 #ifndef KDL_LESS_1_4_0
113  if (status == KDL::ChainIkSolverPos_LMA::E_GRADIENT_JOINTS_TOO_SMALL)
114  {
115  CONSOLE_BRIDGE_logDebug("KDL LMA Failed to calculate IK, gradient joints are tool small");
116  }
117  else if (status == KDL::ChainIkSolverPos_LMA::E_INCREMENT_JOINTS_TOO_SMALL)
118  {
119  CONSOLE_BRIDGE_logDebug("KDL LMA Failed to calculate IK, increment joints are tool small");
120  }
121  else if (status == KDL::ChainIkSolverPos_LMA::E_MAX_ITERATIONS_EXCEEDED)
122  {
123  CONSOLE_BRIDGE_logDebug("KDL LMA Failed to calculate IK, max iteration exceeded");
124  }
125 #else
126  CONSOLE_BRIDGE_logDebug("KDL LMA Failed to calculate IK");
127 #endif
128  // LCOV_EXCL_STOP
129  return {};
130  }
131 
132  KDLToEigen(kdl_solution, solution);
133 
134  return { solution };
135 }
136 
138  const Eigen::Ref<const Eigen::VectorXd>& seed) const
139 {
140  assert(tip_link_poses.find(kdl_data_.tip_link_name) != tip_link_poses.end());
141  return calcInvKinHelper(tip_link_poses.at(kdl_data_.tip_link_name), seed);
142 }
143 
144 std::vector<std::string> KDLInvKinChainLMA::getJointNames() const { return kdl_data_.joint_names; }
145 
146 Eigen::Index KDLInvKinChainLMA::numJoints() const { return kdl_data_.robot_chain.getNrOfJoints(); }
147 
149 
151 
152 std::vector<std::string> KDLInvKinChainLMA::getTipLinkNames() const { return { kdl_data_.tip_link_name }; }
153 
154 std::string KDLInvKinChainLMA::getSolverName() const { return solver_name_; }
155 
156 } // namespace tesseract_kinematics
graph.h
tesseract_kinematics::KDLInvKinChainLMA::KDLInvKinChainLMA
KDLInvKinChainLMA(const KDLInvKinChainLMA &other)
Definition: kdl_inv_kin_chain_lma.cpp:73
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::KDLFwdKinChain::kdl_data_
KDLChainData kdl_data_
Definition: kdl_fwd_kin_chain.h:104
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_kinematics::KDLInvKinChainLMA::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_lma.cpp:152
tesseract_common::TransformMap
AlignedMap< std::string, Eigen::Isometry3d > TransformMap
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
tesseract_scene_graph::SceneGraph
tesseract_kinematics::KDLInvKinChainLMA::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_lma.cpp:154
tesseract_kinematics::KDLInvKinChainLMA::getWorkingFrame
std::string getWorkingFrame() const override final
Get the inverse kinematics working frame.
Definition: kdl_inv_kin_chain_lma.cpp:150
tesseract_kinematics::InverseKinematics::UPtr
std::unique_ptr< InverseKinematics > UPtr
Definition: inverse_kinematics.h:52
tesseract_kinematics::KDLInvKinChainLMA::Config::eps_joints
double eps_joints
Definition: kdl_inv_kin_chain_lma.h:70
kdl_parser.h
tesseract_kinematics::KDLInvKinChainLMA::numJoints
Eigen::Index numJoints() const override final
Number of joints in robot.
Definition: kdl_inv_kin_chain_lma.cpp:146
tesseract_kinematics::KDLChainData::joint_names
std::vector< std::string > joint_names
List of joint names.
Definition: kdl_utils.h:95
tesseract_kinematics::KDLInvKinChainLMA::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_lma.cpp:90
tesseract_kinematics::KDLInvKinChainLMA
KDL Inverse kinematic chain implementation.
Definition: kdl_inv_kin_chain_lma.h:45
tesseract_kinematics::KDLInvKinChainLMA::kdl_data_
KDLChainData kdl_data_
KDL data parsed from Scene Graph.
Definition: kdl_inv_kin_chain_lma.h:117
tesseract_kinematics::KDLInvKinChainLMA::Config
The Config struct.
Definition: kdl_inv_kin_chain_lma.h:65
tesseract_kinematics::KDLInvKinChainLMA::Config::max_iterations
int max_iterations
Definition: kdl_inv_kin_chain_lma.h:69
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_kinematics::KDLInvKinChainLMA::mutex_
std::mutex mutex_
KDL is not thread safe due to mutable variables in Joint Class.
Definition: kdl_inv_kin_chain_lma.h:121
tesseract_scene_graph::SceneGraph::getRoot
const std::string & getRoot() const
tesseract_kinematics::KDLInvKinChainLMA::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_lma.cpp:137
tesseract_kinematics::KDLInvKinChainLMA::clone
InverseKinematics::UPtr clone() const override final
Clone the forward kinematics object.
Definition: kdl_inv_kin_chain_lma.cpp:71
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::KDLInvKinChainLMA::solver_name_
std::string solver_name_
Name of this solver.
Definition: kdl_inv_kin_chain_lma.h:120
tesseract_kinematics::KDLInvKinChainLMA::getJointNames
std::vector< std::string > getJointNames() const override final
Get list of joint names for kinematic object.
Definition: kdl_inv_kin_chain_lma.cpp:144
tesseract_kinematics::KDLInvKinChainLMA::operator=
KDLInvKinChainLMA & operator=(const KDLInvKinChainLMA &other)
Definition: kdl_inv_kin_chain_lma.cpp:75
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::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::KDLInvKinChainLMA::getBaseLinkName
std::string getBaseLinkName() const override final
Get the robot base link name.
Definition: kdl_inv_kin_chain_lma.cpp:148
kdl_inv_kin_chain_lma.h
Tesseract KDL Inverse kinematics chain Levenberg-Marquardt implementation.
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::KDLInvKinChainLMA::Config::eps
double eps
Definition: kdl_inv_kin_chain_lma.h:68


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