Go to the documentation of this file.00001 #include "robodyn_controllers/DynamicsUtilities.h"
00002 #include <iostream>
00003
00004 using namespace Eigen;
00005
00010 DynamicsUtilities::DynamicsUtilities()
00011 {
00012 }
00013
00014 DynamicsUtilities::~DynamicsUtilities()
00015 {
00016 }
00017
00025 bool DynamicsUtilities::isBaseFrame(const std::vector<std::string> baseFrames, const std::string frame)
00026 {
00027 for (unsigned int i = 0; i < baseFrames.size(); i++)
00028 {
00029 if (frame == baseFrames[i])
00030 {
00031 return true;
00032 }
00033 }
00034
00035 return false;
00036 }
00037
00045 void DynamicsUtilities::MultiplyJacobianTranspose(const KDL::Jacobian& jac, const KDL::Wrench& src, KDL::JntArray& dest)
00046 {
00047 Eigen::Matrix<double, 6, 1> w;
00048 w(0) = src.force(0);
00049 w(1) = src.force(1);
00050 w(2) = src.force(2);
00051 w(3) = src.torque(0);
00052 w(4) = src.torque(1);
00053 w(5) = src.torque(2);
00054
00055 Eigen::MatrixXd j = jac.data;
00056 j.transposeInPlace();
00057
00058 Eigen::VectorXd t(jac.columns());
00059 t = j * w;
00060
00061 dest.resize(jac.columns());
00062
00063 for (unsigned int i = 0; i < jac.columns(); i++)
00064 {
00065 dest(i) = t(i);
00066 }
00067 }
00068
00076 int DynamicsUtilities::MultiplyJacobianTransposeInverse(const KDL::Jacobian& jac, const KDL::JntArray& src, KDL::Wrench& dest)
00077 {
00078
00079 unsigned int nj = jac.columns();
00080 unsigned int nc = jac.rows();
00081 int critMaxIter = 100;
00082 double eps = 0.1;
00083
00084 Eigen::MatrixXd jt = jac.data;
00085 jt.transposeInPlace();
00086
00087 if (src.rows() != nj)
00088 {
00089 return -1;
00090 }
00091
00092 Eigen::VectorXd w(nj);
00093 Eigen::VectorXd out(nc);
00094
00095 for (unsigned int j = 0; j < nj; j++)
00096 {
00097 w(j) = src(j);
00098 }
00099
00100
00101 VectorXd tmp(nc);
00102 VectorXd S(VectorXd::Zero(nc));
00103 MatrixXd U(MatrixXd::Identity(nj, nc));
00104 MatrixXd V(MatrixXd::Identity(nc, nc));
00105
00106 if (KDL::svd_eigen_HH(jt, U, S, V, tmp, critMaxIter) < 0)
00107 {
00108 std::cerr << "SVD calculation failed" << std::endl;
00109 return -1;
00110 }
00111
00112
00113 double sum;
00114
00115 for (unsigned int i = 0; i < nc; ++i)
00116 {
00117 sum = 0.0;
00118
00119 for (unsigned int j = 0; j < nj; ++j)
00120 {
00121 sum += U(j, i) * w(j);
00122 }
00123
00124
00125 tmp[i] = sum * (fabs(S[i]) < eps ? 0.0 : 1.0 / S[i]);
00126 }
00127
00128
00129
00130 for (unsigned int i = 0; i < nc; ++i)
00131 {
00132 sum = 0.0;
00133
00134 for (unsigned int j = 0; j < nc; ++j)
00135 {
00136 sum += V(i, j) * tmp[j];
00137 }
00138
00139
00140 out(i) = sum;
00141 }
00142
00143 dest.force.x(out(0));
00144 dest.force.y(out(1));
00145 dest.force.z(out(2));
00146 dest.torque.x(out(3));
00147 dest.torque.y(out(4));
00148 dest.torque.z(out(5));
00149
00150 return 0;
00151 }
00152
00153 double DynamicsUtilities::mag(const KDL::Vector& vec)
00154 {
00155 double val = 0.0;
00156
00157 for (unsigned int i = 0; i < 3; i++)
00158 {
00159 val += vec(i) * vec(i);
00160 }
00161
00162 return sqrt(val);
00163 }
00164
00171 KDL::Wrench DynamicsUtilities::SumWrenches(const std::vector<KDL::Wrench>& forceCenter)
00172 {
00173 KDL::Wrench forceOut;
00174
00175 for (unsigned int i = 0; i < forceCenter.size(); i++)
00176 {
00177 forceOut += forceCenter[i];
00178 }
00179
00180 return forceOut;
00181 }
00182
00183