chainiksolvervel_pinv_nso.cpp
Go to the documentation of this file.
00001 // Copyright  (C)  2007  Ruben Smits <ruben dot smits at mech dot kuleuven dot be>
00002 
00003 // Version: 1.0
00004 // Author: Ruben Smits <ruben dot smits at mech dot kuleuven dot be>
00005 // Maintainer: Ruben Smits <ruben dot smits at mech dot kuleuven dot be>
00006 // URL: http://www.orocos.org/kdl
00007 
00008 // This library is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU Lesser General Public
00010 // License as published by the Free Software Foundation; either
00011 // version 2.1 of the License, or (at your option) any later version.
00012 
00013 // This library is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 // Lesser General Public License for more details.
00017 
00018 // You should have received a copy of the GNU Lesser General Public
00019 // License along with this library; if not, write to the Free Software
00020 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00021 
00022 #include "chainiksolvervel_pinv_nso.hpp"
00023 #include "utilities/svd_eigen_HH.hpp"
00024 
00025 namespace KDL
00026 {
00027     ChainIkSolverVel_pinv_nso::ChainIkSolverVel_pinv_nso(const Chain& _chain, const JntArray& _opt_pos, const JntArray& _weights, double _eps, int _maxiter, double _alpha):
00028         chain(_chain),
00029         jnt2jac(chain),
00030         nj(chain.getNrOfJoints()),
00031         jac(nj),
00032         U(MatrixXd::Zero(6,nj)),
00033         S(VectorXd::Zero(nj)),
00034         Sinv(VectorXd::Zero(nj)),
00035         V(MatrixXd::Zero(nj,nj)),
00036         tmp(VectorXd::Zero(nj)),
00037         tmp2(VectorXd::Zero(nj)),
00038         eps(_eps),
00039         maxiter(_maxiter),
00040         svdResult(0),
00041         alpha(_alpha),
00042         weights(_weights),
00043         opt_pos(_opt_pos)
00044     {
00045     }
00046 
00047     ChainIkSolverVel_pinv_nso::ChainIkSolverVel_pinv_nso(const Chain& _chain, double _eps, int _maxiter, double _alpha):
00048         chain(_chain),
00049         jnt2jac(chain),
00050         nj(chain.getNrOfJoints()),
00051         jac(nj),
00052         U(MatrixXd::Zero(6,nj)),
00053         S(VectorXd::Zero(nj)),
00054         Sinv(VectorXd::Zero(nj)),
00055         V(MatrixXd::Zero(nj,nj)),
00056         tmp(VectorXd::Zero(nj)),
00057         tmp2(VectorXd::Zero(nj)),
00058         eps(_eps),
00059         maxiter(_maxiter),
00060         svdResult(0),
00061         alpha(_alpha)
00062     {
00063     }
00064 
00065     ChainIkSolverVel_pinv_nso::~ChainIkSolverVel_pinv_nso()
00066     {
00067     }
00068 
00069 
00070     int ChainIkSolverVel_pinv_nso::CartToJnt(const JntArray& q_in, const Twist& v_in, JntArray& qdot_out)
00071     {
00072         if (nj != q_in.rows() || nj != qdot_out.rows() || nj != opt_pos.rows() || nj != weights.rows())
00073             return (error = E_SIZE_MISMATCH);
00074         //Let the ChainJntToJacSolver calculate the jacobian "jac" for
00075         //the current joint positions "q_in" 
00076         error = jnt2jac.JntToJac(q_in,jac);
00077         if (error < E_NOERROR) return error;
00078 
00079         //Do a singular value decomposition of "jac" with maximum
00080         //iterations "maxiter", put the results in "U", "S" and "V"
00081         //jac = U*S*Vt
00082         svdResult = svd_eigen_HH(jac.data,U,S,V,tmp,maxiter);
00083         if (0 != svdResult)
00084         {
00085             qdot_out.data.setZero() ;
00086             return error = E_SVD_FAILED;
00087         }
00088 
00089         unsigned int i;
00090 
00091         // We have to calculate qdot_out = jac_pinv*v_in
00092         // Using the svd decomposition this becomes(jac_pinv=V*S_pinv*Ut):
00093         // qdot_out = V*S_pinv*Ut*v_in
00094 
00095         // S^-1
00096         for (i = 0; i < nj; ++i) {
00097             Sinv(i) = fabs(S(i))<eps ? 0.0 : 1.0/S(i);
00098         }
00099         for (i = 0; i < 6; ++i) {
00100             tmp(i) = v_in(i);
00101         }
00102 
00103         qdot_out.data = V * Sinv.asDiagonal() * U.transpose() * tmp.head(6);
00104 
00105         // Now onto NULL space
00106         // Given the cost function g, and the current joints q, desired joints qd, and weights w:
00107         // t = g(q) = 1/2 * Sum( w_i * (q_i - qd_i)^2 )
00108         //
00109         // The jacobian Jc is:
00110         //  t_dot = Jc(q) * q_dot
00111         //  Jc = dt/dq = w_j * (q_i - qd_i) [1 x nj vector]
00112         //
00113         // The pseudo inverse (^-1) is
00114         // Jc^-1 = w_j * (q_i - qd_i) / A [nj x 1 vector]
00115         // A = Sum( w_i^2 * (q_i - qd_i)^2 )
00116         //
00117         // We can set the change as the step needed to remove the error times a value alpha:
00118         // t_dot = -2 * alpha * t
00119         //
00120         // When we put it together and project into the nullspace, the final result is
00121         // q'_out += (I_n - J^-1 * J) * Jc^-1 * (-2 * alpha * g(q))
00122 
00123         double g = 0; // g(q)
00124         double A = 0; // normalizing term
00125         for (i = 0; i < nj; ++i) {
00126             double qd = q_in(i) - opt_pos(i);
00127             g += 0.5 * qd*qd * weights(i);
00128             A += qd*qd * weights(i)*weights(i);
00129         }
00130 
00131         if (A > 1e-9) {
00132           // Calculate inverse Jacobian Jc^-1
00133           for (i = 0; i < nj; ++i) {
00134               tmp(i) = weights(i)*(q_in(i) - opt_pos(i)) / A;
00135           }
00136 
00137           // Calcualte J^-1 * J * Jc^-1 = V*S^-1*U' * U*S*V' * tmp
00138           tmp2 = V * Sinv.asDiagonal() * U.transpose() * U * S.asDiagonal() * V.transpose() * tmp;
00139 
00140           for (i = 0; i < nj; ++i) {
00141               //std::cerr << i <<": "<< qdot_out(i) <<", "<< -2*alpha*g * (tmp(i) - tmp2(i)) << std::endl;
00142               qdot_out(i) += -2*alpha*g * (tmp(i) - tmp2(i));
00143           }
00144         }
00145         //return the return value of the svd decomposition
00146         return (error = E_NOERROR);
00147     }
00148 
00149     int ChainIkSolverVel_pinv_nso::setWeights(const JntArray & _weights)
00150     {
00151         if (nj != _weights.rows())
00152             return (error = E_SIZE_MISMATCH);
00153       weights = _weights;
00154       return (error = E_NOERROR);
00155     }
00156 
00157     int ChainIkSolverVel_pinv_nso::setOptPos(const JntArray & _opt_pos)
00158     {
00159         if (nj != _opt_pos.rows())
00160             return (error = E_SIZE_MISMATCH);
00161       opt_pos = _opt_pos;
00162       return (error = E_NOERROR);
00163     }
00164 
00165     int ChainIkSolverVel_pinv_nso::setAlpha(const double _alpha)
00166     {
00167       alpha = _alpha;
00168       return 0;
00169     }
00170 
00171 
00172 }


orocos_kdl
Author(s):
autogenerated on Sat Oct 7 2017 03:04:28