Blender  V2.59
WDLSSolver.cpp
Go to the documentation of this file.
00001 
00004 /* $Id: WDLSSolver.cpp 35155 2011-02-25 11:45:16Z jesterking $
00005  * WDLSSolver.hpp.cpp
00006  *
00007  *  Created on: Jan 8, 2009
00008  *      Author: rubensmits
00009  */
00010 
00011 #include "WDLSSolver.hpp"
00012 #include "kdl/utilities/svd_eigen_HH.hpp"
00013 
00014 namespace iTaSC {
00015 
00016 WDLSSolver::WDLSSolver() : m_lambda(0.5), m_epsilon(0.1) 
00017 {
00018         // maximum joint velocity
00019         m_qmax = 50.0;
00020 }
00021 
00022 WDLSSolver::~WDLSSolver() {
00023 }
00024 
00025 bool WDLSSolver::init(unsigned int nq, unsigned int nc, const std::vector<bool>& gc)
00026 {
00027         m_ns = std::min(nc,nq);
00028     m_AWq = e_zero_matrix(nc,nq);
00029     m_WyAWq = e_zero_matrix(nc,nq);
00030     m_WyAWqt = e_zero_matrix(nq,nc);
00031         m_S = e_zero_vector(std::max(nc,nq));
00032         m_Wy_ydot = e_zero_vector(nc);
00033         if (nq > nc) {
00034                 m_transpose = true;
00035             m_temp = e_zero_vector(nc);
00036             m_U = e_zero_matrix(nc,nc);
00037                 m_V = e_zero_matrix(nq,nc);
00038             m_WqV = e_zero_matrix(nq,nc);
00039         } else {
00040                 m_transpose = false;
00041             m_temp = e_zero_vector(nq);
00042             m_U = e_zero_matrix(nc,nq);
00043                 m_V = e_zero_matrix(nq,nq);
00044             m_WqV = e_zero_matrix(nq,nq);
00045         }
00046     return true;
00047 }
00048 
00049 bool WDLSSolver::solve(const e_matrix& A, const e_vector& Wy, const e_vector& ydot, const e_matrix& Wq, e_vector& qdot, e_scalar& nlcoef)
00050 {
00051         double alpha, vmax, norm;
00052         // Create the Weighted jacobian
00053     m_AWq = A*Wq;
00054         for (int i=0; i<Wy.size(); i++)
00055                 m_WyAWq.row(i) = Wy(i)*m_AWq.row(i);
00056 
00057     // Compute the SVD of the weighted jacobian
00058         int ret;
00059         if (m_transpose) {
00060                 m_WyAWqt = m_WyAWq.transpose();
00061                 ret = KDL::svd_eigen_HH(m_WyAWqt,m_V,m_S,m_U,m_temp);
00062         } else {
00063                 ret = KDL::svd_eigen_HH(m_WyAWq,m_U,m_S,m_V,m_temp);
00064         }
00065     if(ret<0)
00066         return false;
00067 
00068     m_WqV = (Wq*m_V).lazy();
00069 
00070     //Wy*ydot
00071         m_Wy_ydot = Wy.cwise() * ydot;
00072     //S^-1*U'*Wy*ydot
00073         e_scalar maxDeltaS = e_scalar(0.0);
00074         e_scalar prevS = e_scalar(0.0);
00075         e_scalar maxS = e_scalar(1.0);
00076         e_scalar S, lambda;
00077         qdot.setZero();
00078         for(int i=0;i<m_ns;++i) {
00079                 S = m_S(i);
00080                 if (S <= KDL::epsilon)
00081                         break;
00082                 if (i > 0 && (prevS-S) > maxDeltaS) {
00083                         maxDeltaS = (prevS-S);
00084                         maxS = prevS;
00085                 }
00086                 lambda = (S < m_epsilon) ? (e_scalar(1.0)-KDL::sqr(S/m_epsilon))*m_lambda*m_lambda : e_scalar(0.0);
00087                 alpha = m_U.col(i).dot(m_Wy_ydot)*S/(S*S+lambda);
00088                 vmax = m_WqV.col(i).cwise().abs().maxCoeff();
00089                 norm = fabs(alpha*vmax);
00090                 if (norm > m_qmax) {
00091                         qdot += m_WqV.col(i)*(alpha*m_qmax/norm);
00092                 } else {
00093                         qdot += m_WqV.col(i)*alpha;
00094                 }
00095                 prevS = S;
00096         }
00097         if (maxDeltaS == e_scalar(0.0))
00098                 nlcoef = e_scalar(KDL::epsilon);
00099         else
00100                 nlcoef = (maxS-maxDeltaS)/maxS;
00101     return true;
00102 }
00103 
00104 }