Franky  0.9.1
A High-Level Motion API for Franka
kinematics.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <optional>
4 
5 #include <Eigen/Core>
6 #include <Eigen/Dense>
7 
8 #include "franky/types.hpp"
9 
10 namespace franky {
11 
12 struct Kinematics {
13  template<class MatT>
14  static Eigen::Matrix<typename MatT::Scalar, MatT::ColsAtCompileTime, MatT::RowsAtCompileTime>
15  pseudoinverse(const MatT &mat, typename MatT::Scalar tolerance = typename MatT::Scalar{1e-4}) {
16  typedef typename MatT::Scalar Scalar;
17  auto svd = mat.jacobiSvd(Eigen::ComputeFullU | Eigen::ComputeFullV);
18  const auto &singularValues = svd.singularValues();
19  Eigen::Matrix<Scalar, MatT::ColsAtCompileTime, MatT::RowsAtCompileTime> singularValuesInv(mat.cols(), mat.rows());
20  singularValuesInv.setZero();
21  for (unsigned int i = 0; i < singularValues.size(); ++i) {
22  if (singularValues(i) > tolerance) {
23  singularValuesInv(i, i) = Scalar{1} / singularValues(i);
24 
25  } else {
26  singularValuesInv(i, i) = Scalar{0};
27  }
28  }
29  return svd.matrixV() * singularValuesInv * svd.matrixU().adjoint();
30  }
31 
33  size_t joint_index;
34  double value;
35 
37  };
38 
39  static Affine forward(const Eigen::Matrix<double, 7, 1> &q);
40 
41  static Affine forwardElbow(const Eigen::Matrix<double, 7, 1> &q);
42 
43  static Eigen::Matrix<double, 6, 1> forwardEuler(const Eigen::Matrix<double, 7, 1> &q);
44 
45  static Eigen::Matrix<double, 6, 7> jacobian(const Eigen::Matrix<double, 7, 1> &q);
46 
47  static Eigen::Matrix<double, 7, 1>
48  inverse(const Eigen::Matrix<double, 6, 1> &x_target, const Eigen::Matrix<double, 7, 1> &q0,
49  std::optional<NullSpaceHandling> null_space = std::nullopt);
50 };
51 
52 template<size_t DoFs>
54  struct DenavitHartenbergParameter {
55  double alpha; // [rad]
56  double d, a; // [m]
57 
58  Affine get_transformation(double theta) const {
59  Affine rot1;
60  rot1.linear() = Euler(theta, 0, 0).toRotationMatrix();
61  Affine trans1;
62  trans1.affine() = Eigen::Vector3d(0, 0, d);
63  Affine trans2;
64  trans2.affine() = Eigen::Vector3d(a, 0, 0);
65  Affine rot2;
66  rot2.linear() = Euler(0, 0, alpha).toRotationMatrix();
67  return rot2 * trans2 * rot1 * trans1;
68  }
69  };
70 
71  std::array<DenavitHartenbergParameter, DoFs> parameters;
72  Affine base;
73 
74  public:
75  explicit KinematicChain(const std::array<DenavitHartenbergParameter, DoFs> &parameters, const Affine &base)
76  : parameters(parameters), base(base) {}
77 
78  Affine forward_chain(const std::array<double, DoFs> &q) const {
79  Affine result;
80  for (size_t i = 0; i < DoFs; ++i) {
81  result = result * parameters[i].get_transformation(q[i]);
82  }
83  return result * base;
84  }
85 };
86 
87 }
Definition: kinematics.hpp:53
KinematicChain(const std::array< DenavitHartenbergParameter, DoFs > &parameters, const Affine &base)
Definition: kinematics.hpp:75
Affine forward_chain(const std::array< double, DoFs > &q) const
Definition: kinematics.hpp:78
Definition: gripper.cpp:3
Eigen::EulerAngles< double, Eigen::EulerSystemXYZ > Euler
Definition: types.hpp:11
Eigen::Affine3d Affine
Definition: types.hpp:13
Definition: kinematics.hpp:32
size_t joint_index
Definition: kinematics.hpp:33
double value
Definition: kinematics.hpp:34
NullSpaceHandling(size_t joint_index, double value)
Definition: kinematics.hpp:36
Definition: kinematics.hpp:12
static Affine forward(const Eigen::Matrix< double, 7, 1 > &q)
Definition: kinematics.cpp:5
static Eigen::Matrix< double, 6, 1 > forwardEuler(const Eigen::Matrix< double, 7, 1 > &q)
Definition: kinematics.cpp:107
static Eigen::Matrix< typename MatT::Scalar, MatT::ColsAtCompileTime, MatT::RowsAtCompileTime > pseudoinverse(const MatT &mat, typename MatT::Scalar tolerance=typename MatT::Scalar{1e-4})
Definition: kinematics.hpp:15
static Eigen::Matrix< double, 7, 1 > inverse(const Eigen::Matrix< double, 6, 1 > &x_target, const Eigen::Matrix< double, 7, 1 > &q0, std::optional< NullSpaceHandling > null_space=std::nullopt)
Definition: kinematics.cpp:344
static Affine forwardElbow(const Eigen::Matrix< double, 7, 1 > &q)
Definition: kinematics.cpp:73
static Eigen::Matrix< double, 6, 7 > jacobian(const Eigen::Matrix< double, 7, 1 > &q)
Definition: kinematics.cpp:163