26 #ifndef TESSERACT_KINEMATICS_UTILS_H
27 #define TESSERACT_KINEMATICS_UTILS_H
32 #include <Eigen/Geometry>
33 #include <console_bridge/console.h>
41 template <
typename FloatType>
42 using VectorX = Eigen::Matrix<FloatType, Eigen::Dynamic, 1>;
56 const Eigen::Isometry3d& change_base,
58 const Eigen::Ref<const Eigen::VectorXd>& joint_values,
59 const std::string& link_name,
60 const Eigen::Ref<const Eigen::Vector3d>& link_point);
71 const Eigen::Isometry3d& change_base,
73 const Eigen::Ref<const Eigen::VectorXd>& joint_values,
74 const std::string& link_name,
75 const Eigen::Ref<const Eigen::Vector3d>& link_point);
85 bool solvePInv(
const Eigen::Ref<const Eigen::MatrixXd>& A,
86 const Eigen::Ref<const Eigen::VectorXd>& b,
87 Eigen::Ref<Eigen::VectorXd> x);
98 bool dampedPInv(
const Eigen::Ref<const Eigen::MatrixXd>& A,
99 Eigen::Ref<Eigen::MatrixXd> P,
101 double lambda = 0.01);
111 bool isNearSingularity(
const Eigen::Ref<const Eigen::MatrixXd>& jacobian,
double threshold = 0.01);
117 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
147 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
180 template <
typename FloatType>
182 const Eigen::Ref<const Eigen::VectorXd>& sol,
183 const Eigen::MatrixX2d& limits,
184 std::vector<Eigen::Index>::const_iterator current_index,
185 std::vector<Eigen::Index>::const_iterator end_index)
188 for (; current_index != end_index; ++current_index)
190 if (std::isinf(limits(*current_index, 0)))
192 std::stringstream ss;
193 ss <<
"Lower limit of joint " << *current_index <<
" is infinite; no redundant solutions will be generated\n";
194 CONSOLE_BRIDGE_logWarn(ss.str().c_str());
198 val = sol[*current_index];
199 while ((val -= (2.0 * M_PI)) > limits(*current_index, 0) ||
203 if (val < limits(*current_index, 1) ||
206 Eigen::VectorXd new_sol = sol;
207 new_sol[*current_index] = val;
212 redundant_sols.push_back(new_sol.template cast<FloatType>());
215 getRedundantSolutionsHelper<FloatType>(redundant_sols, new_sol, limits, current_index + 1, end_index);
220 if (std::isinf(limits(*current_index, 1)))
222 std::stringstream ss;
223 ss <<
"Upper limit of joint " << *current_index <<
" is infinite; no redundant solutions will be generated\n";
224 CONSOLE_BRIDGE_logWarn(ss.str().c_str());
228 val = sol[*current_index];
229 while ((val += (2.0 * M_PI)) < limits(*current_index, 1) ||
233 if (val > limits(*current_index, 0) ||
236 Eigen::VectorXd new_sol = sol;
237 new_sol[*current_index] = val;
242 redundant_sols.push_back(new_sol.template cast<FloatType>());
245 getRedundantSolutionsHelper<FloatType>(redundant_sols, new_sol, limits, current_index + 1, end_index);
259 template <
typename FloatType>
261 const Eigen::MatrixX2d& limits,
262 const std::vector<Eigen::Index>& redundancy_capable_joints)
264 if (redundancy_capable_joints.empty())
267 for (
const Eigen::Index& idx : redundancy_capable_joints)
269 if (idx >= sol.size())
271 std::stringstream ss;
272 ss <<
"Redundant joint index " << idx <<
" is greater than or equal to the joint state size (" << sol.size()
274 throw std::runtime_error(ss.str());
278 std::vector<VectorX<FloatType>> redundant_sols;
279 getRedundantSolutionsHelper<FloatType>(redundant_sols,
280 sol.template cast<double>(),
282 redundancy_capable_joints.begin(),
283 redundancy_capable_joints.end());
284 return redundant_sols;
296 template <
typename FloatType>
297 inline bool isValid(
const std::array<FloatType, 6>& qs)
299 for (
const auto& q : qs)
300 if (!std::isfinite(q))
311 template <
typename FloatType>
313 const std::vector<Eigen::Index>& redundancy_capable_joints)
315 const static auto pi = FloatType(M_PI);
316 const static auto two_pi = FloatType(2.0 * M_PI);
318 for (
const auto& i : redundancy_capable_joints)
320 const auto diff = std::fmod(qs[i] + pi, two_pi);
321 qs[i] = (diff < 0) ? (diff + pi) : (diff - pi);
330 template <
typename FloatType>
332 const std::vector<Eigen::Index>& redundancy_capable_joints,
333 const Eigen::Ref<
const Eigen::Matrix<FloatType, Eigen::Dynamic, 2>>& position_limits)
335 const static auto pi = FloatType(M_PI);
336 const static auto two_pi = FloatType(2.0 * M_PI);
338 for (
const auto& i : redundancy_capable_joints)
340 const auto mean = (position_limits(i, 0) + position_limits(i, 1)) / 2.0;
341 const auto diff = std::fmod((qs[i] - mean) + pi, two_pi);
342 const auto vv = (diff < 0) ? (diff + pi) : (diff - pi);
348 #endif // TESSERACT_KINEMATICS_UTILS_H