Tesseract
Motion Planning Environment
Loading...
Searching...
No Matches
ikfast_inv_kin.hpp
Go to the documentation of this file.
1
26#ifndef TESSERACT_KINEMATICS_IMPL_IKFAST_INV_KIN_HPP
27#define TESSERACT_KINEMATICS_IMPL_IKFAST_INV_KIN_HPP
28
29#ifndef IKFAST_HAS_LIBRARY
30#define IKFAST_HAS_LIBRARY
31#define IKFAST_NO_MAIN
32#endif
33
36#include <stdexcept>
37#include <console_bridge/console.h>
40
43
45{
47 std::string tip_link_name,
48 std::vector<std::string> joint_names,
49 std::vector<Eigen::Index> redundancy_capable_joints,
50 std::string solver_name,
51 std::vector<std::vector<double>> free_joint_states)
52 : base_link_name_(std::move(base_link_name))
53 , tip_link_name_(std::move(tip_link_name))
54 , joint_names_(std::move(joint_names))
55 , redundancy_capable_joints_(std::move(redundancy_capable_joints))
56 , solver_name_(std::move(solver_name))
57 , free_joint_states_(std::move(free_joint_states))
58{
59}
60
61inline InverseKinematics::UPtr IKFastInvKin::clone() const { return std::make_unique<IKFastInvKin>(*this); }
62
63inline IKFastInvKin::IKFastInvKin(const IKFastInvKin& other) { *this = other; }
64
66{
73
74 return *this;
75}
76
78 const Eigen::Ref<const Eigen::VectorXd>& /*seed*/) const
79{
80 assert(tip_link_poses.size() == 1);
81 assert(tip_link_poses.find(tip_link_name_) != tip_link_poses.end());
82 assert(std::abs(1.0 - tip_link_poses.at(tip_link_name_).matrix().determinant()) < 1e-6);
83
84 const Eigen::Isometry3d& pose = tip_link_poses.at(tip_link_name_);
85
86 // Convert to ikfast data type
87 Eigen::Transform<IkReal, 3, Eigen::Isometry> ikfast_tcp = pose.cast<IkReal>();
88
89 // Decompose
90 const Eigen::Matrix<IkReal, 3, 1> translation = ikfast_tcp.translation();
91
92 // Note the row major ordering here: IkFast expects the matrix in r00, r01, r02, ..., r11, r12, r13
93 // ordering
94 const Eigen::Matrix<IkReal, 3, 3, Eigen::RowMajor> rotation = ikfast_tcp.rotation();
95
96 auto ikfast_dof = static_cast<std::size_t>(numJoints());
97
98 // Call IK (TODO: Make a better solution list class? One that uses vector instead of list)
99 ikfast::IkSolutionList<IkReal> ikfast_solution_set;
100 std::vector<double> sols;
101
102 // Lambda to add all possible solutions for a given combination of free joints, or nullptr if no free joints
103 auto addSols = [&](const double* pfree) {
104 ComputeIk(translation.data(), rotation.data(), pfree, ikfast_solution_set);
105
106 // Unpack the solutions into the output vector
107 const auto n_sols = ikfast_solution_set.GetNumSolutions();
108
109 std::vector<IkReal> ikfast_output;
110 ikfast_output.resize(n_sols * ikfast_dof);
111
112 for (std::size_t i = 0; i < n_sols; ++i)
113 {
114 // This actually walks the list EVERY time from the start of i.
115 const auto& sol = ikfast_solution_set.GetSolution(i);
116 auto* out = ikfast_output.data() + i * ikfast_dof;
117 sol.GetSolution(out, pfree);
118 }
119
120 sols.insert(
121 end(sols), std::make_move_iterator(ikfast_output.begin()), std::make_move_iterator(ikfast_output.end()));
122 };
123
124 if (!free_joint_states_.empty())
125 {
126 for (auto j_combo : free_joint_states_)
127 {
128 addSols(j_combo.data());
129 }
130 }
131 else
132 {
133 addSols(nullptr);
134 }
135
136 // Check the output
137 std::size_t num_sol = sols.size() / ikfast_dof;
138 IKSolutions solution_set;
139 solution_set.reserve(sols.size());
140 for (std::size_t i = 0; i < num_sol; i++)
141 {
142 Eigen::Map<Eigen::VectorXd> eigen_sol(sols.data() + ikfast_dof * i, static_cast<Eigen::Index>(ikfast_dof));
143 if (eigen_sol.array().allFinite())
144 {
145 harmonizeTowardZero<double>(eigen_sol, redundancy_capable_joints_); // Modifies 'sol' in place
146 solution_set.push_back(eigen_sol);
147 }
148 }
149
150 return solution_set;
151}
152
153inline Eigen::Index IKFastInvKin::numJoints() const { return static_cast<Eigen::Index>(GetNumJoints()); }
154inline std::vector<std::string> IKFastInvKin::getJointNames() const { return joint_names_; }
155inline std::string IKFastInvKin::getBaseLinkName() const { return base_link_name_; }
156inline std::string IKFastInvKin::getWorkingFrame() const { return base_link_name_; }
157inline std::vector<std::string> IKFastInvKin::getTipLinkNames() const { return { tip_link_name_ }; }
158inline std::string IKFastInvKin::getSolverName() const { return solver_name_; }
159
160inline std::vector<std::vector<double>>
161IKFastInvKin::generateAllFreeJointStateCombinations(const std::vector<std::vector<double>>& free_joint_samples)
162{
163 std::vector<std::vector<double>> free_joint_states;
164 std::vector<std::size_t> curr_joint_indices(free_joint_samples.size(), 0);
165 while (curr_joint_indices.front() < free_joint_samples.front().size())
166 {
167 std::vector<double> curr_joint_values;
168 for (std::size_t i = 0; i < curr_joint_indices.size(); ++i)
169 curr_joint_values.push_back(free_joint_samples[i][curr_joint_indices[i]]);
170 free_joint_states.push_back(curr_joint_values);
171 curr_joint_indices.back()++;
172 for (std::size_t i = curr_joint_indices.size() - 1; i > 0; --i)
173 {
174 if (curr_joint_indices[i] == free_joint_samples[i].size())
175 {
176 curr_joint_indices[i] = 0;
177 curr_joint_indices[i - 1]++;
178 }
179 }
180 }
181 return free_joint_states;
182}
183
184} // namespace tesseract_kinematics
185
186#endif // TESSERACT_KINEMATICS_IMPL_IKFAST_INV_KIN_HPP
IKFAST_API int GetNumJoints()
Definition: abb_irb2400_ikfast_solver.hpp:381
IKFAST_API bool ComputeIk(const IkReal *eetrans, const IkReal *eerot, const IkReal *pfree, IkSolutionListBase< IkReal > &solutions)
Definition: abb_irb2400_ikfast_solver.hpp:3210
Default implementation of IkSolutionListBase.
Definition: ikfast.h:259
const IkSolutionBase< T > & GetSolution(size_t index) const override
returns the solution pointer
Definition: ikfast.h:268
size_t GetNumSolutions() const override
returns the number of solutions stored
Definition: ikfast.h:279
IKFast Inverse Kinematics Implmentation.
Definition: ikfast_inv_kin.h:78
std::string getWorkingFrame() const override
Get the inverse kinematics working frame.
Definition: ikfast_inv_kin.hpp:156
std::vector< std::vector< double > > free_joint_states_
Definition: ikfast_inv_kin.h:139
std::vector< Eigen::Index > redundancy_capable_joints_
Definition: ikfast_inv_kin.h:135
static std::vector< std::vector< double > > generateAllFreeJointStateCombinations(const std::vector< std::vector< double > > &free_joint_samples)
Generates all possible combinations of joint states and stores it to the free_joint_states_ class mem...
Definition: ikfast_inv_kin.hpp:161
std::vector< std::string > joint_names_
Joint names for the kinematic object.
Definition: ikfast_inv_kin.h:134
Eigen::Index numJoints() const override
Number of joints in robot.
Definition: ikfast_inv_kin.hpp:153
std::string base_link_name_
Link name of first link in the kinematic object.
Definition: ikfast_inv_kin.h:132
IKSolutions calcInvKin(const tesseract_common::TransformMap &tip_link_poses, const Eigen::Ref< const Eigen::VectorXd > &seed) const override
Calculates joint solutions given a pose for each tip link.
Definition: ikfast_inv_kin.hpp:77
IKFastInvKin(const IKFastInvKin &other)
Definition: ikfast_inv_kin.hpp:63
std::string solver_name_
Redundancy capable joints.
Definition: ikfast_inv_kin.h:136
std::vector< std::string > getJointNames() const override
Get list of joint names for kinematic object.
Definition: ikfast_inv_kin.hpp:154
IKFastInvKin & operator=(const IKFastInvKin &other)
Definition: ikfast_inv_kin.hpp:65
std::string getBaseLinkName() const override
Get the robot base link name.
Definition: ikfast_inv_kin.hpp:155
InverseKinematics::UPtr clone() const override
Clone the forward kinematics object.
Definition: ikfast_inv_kin.hpp:61
std::vector< std::string > getTipLinkNames() const override
Get the names of the tip links of the kinematics group.
Definition: ikfast_inv_kin.hpp:157
std::string getSolverName() const override
Get the name of the solver. Recommend using the name of the class.
Definition: ikfast_inv_kin.hpp:158
std::string tip_link_name_
Link name of last kink in the kinematic object.
Definition: ikfast_inv_kin.h:133
std::unique_ptr< InverseKinematics > UPtr
Definition: inverse_kinematics.h:55
Tesseract IKFast Inverse kinematics Wrapper.
std::string base_link_name
Definition: ikfast_kinematics_7dof_unit.cpp:52
std::string tip_link_name
Definition: ikfast_kinematics_7dof_unit.cpp:53
std::vector< std::vector< double > > free_joint_states
Definition: ikfast_kinematics_7dof_unit.cpp:58
Common Tesseract Macros.
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
Definition: macros.h:71
Definition: create_convex_hull.cpp:36
AlignedMap< std::string, Eigen::Isometry3d > TransformMap
Definition: types.h:66
Definition: forward_kinematics.h:44
std::vector< Eigen::VectorXd > IKSolutions
The inverse kinematics solutions container.
Definition: types.h:41
joint_state joint_names
Definition: tesseract_common_serialization_unit.cpp:75
pose translation()
auto pose
Definition: tesseract_environment_collision.cpp:118
Kinematics utility functions.