29 #include <Eigen/Eigenvalues>
39 const Eigen::Isometry3d& change_base,
41 const Eigen::Ref<const Eigen::VectorXd>& joint_values,
42 const std::string& link_name,
43 const Eigen::Ref<const Eigen::Vector3d>& link_point)
45 Eigen::VectorXd njvals;
48 Eigen::Isometry3d pose{ change_base * poses[link_name] };
50 for (
int i = 0; i < static_cast<int>(joint_values.size()); ++i)
52 njvals = joint_values;
54 Eigen::Isometry3d updated_pose = kin.
calcFwdKin(njvals)[link_name];
55 updated_pose = change_base * updated_pose;
57 Eigen::Vector3d temp{ pose * link_point };
58 Eigen::Vector3d temp2{ updated_pose * link_point };
59 jacobian(0, i) = (temp2.x() - temp.x()) / delta;
60 jacobian(1, i) = (temp2.y() - temp.y()) / delta;
61 jacobian(2, i) = (temp2.z() - temp.z()) / delta;
64 updated_pose.rotation())) /
66 jacobian(3, i) = omega(0);
67 jacobian(4, i) = omega(1);
68 jacobian(5, i) = omega(2);
73 const Eigen::Isometry3d& change_base,
75 const Eigen::Ref<const Eigen::VectorXd>& joint_values,
76 const std::string& link_name,
77 const Eigen::Ref<const Eigen::Vector3d>& link_point)
79 Eigen::VectorXd njvals;
80 constexpr
double delta = 1e-8;
82 Eigen::Isometry3d pose = change_base * poses[link_name];
84 for (
int i = 0; i < static_cast<int>(joint_values.size()); ++i)
86 njvals = joint_values;
89 Eigen::Isometry3d updated_pose = change_base * updated_poses[link_name];
91 Eigen::Vector3d temp = pose * link_point;
92 Eigen::Vector3d temp2 = updated_pose * link_point;
93 jacobian(0, i) = (temp2.x() - temp.x()) / delta;
94 jacobian(1, i) = (temp2.y() - temp.y()) / delta;
95 jacobian(2, i) = (temp2.z() - temp.z()) / delta;
98 updated_pose.rotation())) /
100 jacobian(3, i) = omega(0);
101 jacobian(4, i) = omega(1);
102 jacobian(5, i) = omega(2);
106 bool solvePInv(
const Eigen::Ref<const Eigen::MatrixXd>& A,
107 const Eigen::Ref<const Eigen::VectorXd>& b,
108 Eigen::Ref<Eigen::VectorXd> x)
110 const double eps = 0.00001;
111 const double lambda = 0.01;
113 if ((A.rows() == 0) || (A.cols() == 0))
115 CONSOLE_BRIDGE_logError(
"Empty matrices not supported in solvePinv()");
119 if (A.rows() != b.size())
121 CONSOLE_BRIDGE_logError(
"Matrix size mismatch: A(%d, %d), b(%d)", A.rows(), A.cols(), b.size());
128 Eigen::JacobiSVD<Eigen::MatrixXd> svd(A, Eigen::ComputeThinU | Eigen::ComputeThinV);
129 const Eigen::MatrixXd& U = svd.matrixU();
130 const Eigen::VectorXd& Sv = svd.singularValues();
131 const Eigen::MatrixXd& V = svd.matrixV();
135 long int nSv = Sv.size();
136 Eigen::VectorXd inv_Sv(nSv);
137 for (
long int i = 0; i < nSv; ++i)
139 if (fabs(Sv(i)) > eps)
140 inv_Sv(i) = 1 / Sv(i);
142 inv_Sv(i) = Sv(i) / (Sv(i) * Sv(i) + lambda * lambda);
144 x = V * inv_Sv.asDiagonal() * U.transpose() * b;
148 bool dampedPInv(
const Eigen::Ref<const Eigen::MatrixXd>& A, Eigen::Ref<Eigen::MatrixXd> P,
double eps,
double lambda)
150 if ((A.rows() == 0) || (A.cols() == 0))
152 CONSOLE_BRIDGE_logError(
"Empty matrices not supported in dampedPInv()");
159 Eigen::JacobiSVD<Eigen::MatrixXd> svd(A, Eigen::ComputeThinU | Eigen::ComputeThinV);
160 const Eigen::MatrixXd& U = svd.matrixU();
161 const Eigen::VectorXd& Sv = svd.singularValues();
162 const Eigen::MatrixXd& V = svd.matrixV();
166 long int nSv = Sv.size();
167 Eigen::VectorXd inv_Sv(nSv);
168 for (
long int i = 0; i < nSv; ++i)
170 if (fabs(Sv(i)) > eps)
171 inv_Sv(i) = 1 / Sv(i);
174 inv_Sv(i) = Sv(i) / (Sv(i) * Sv(i) + lambda * lambda);
177 P = V * inv_Sv.asDiagonal() * U.transpose();
183 Eigen::JacobiSVD<Eigen::MatrixXd> svd(jacobian, Eigen::ComputeThinU | Eigen::ComputeThinV);
184 const Eigen::VectorXd& sv = svd.singularValues();
185 return (sv.tail(1).value() < threshold);
191 Eigen::MatrixXd jacob_linear = jacobian.topRows(3);
192 Eigen::MatrixXd jacob_angular = jacobian.bottomRows(3);
194 auto fn = [](
const Eigen::MatrixXd& m) {
196 Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> sm(m, Eigen::DecompositionOptions::EigenvaluesOnly);
200 for (Eigen::Index i = 0; i < data.
eigen_values.size(); ++i)
209 data.
measure = std::numeric_limits<double>::max();
210 data.
condition = std::numeric_limits<double>::max();
223 Eigen::MatrixXd a = jacobian * jacobian.transpose();
224 Eigen::MatrixXd a_linear = jacob_linear * jacob_linear.transpose();
225 Eigen::MatrixXd a_angular = jacob_angular * jacob_angular.transpose();
231 Eigen::MatrixXd a_inv = a.inverse();
232 Eigen::MatrixXd a_linear_inv = a_linear.inverse();
233 Eigen::MatrixXd a_angular_inv = a_angular.inverse();