chainiksolvervel_pinv.cpp
Go to the documentation of this file.
1 // Copyright (C) 2007 Ruben Smits <ruben dot smits at mech dot kuleuven dot be>
2 
3 // Version: 1.0
4 // Author: Ruben Smits <ruben dot smits at mech dot kuleuven dot be>
5 // Maintainer: Ruben Smits <ruben dot smits at mech dot kuleuven dot be>
6 // URL: http://www.orocos.org/kdl
7 
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 
23 
24 namespace KDL
25 {
26  ChainIkSolverVel_pinv::ChainIkSolverVel_pinv(const Chain& _chain,double _eps,int _maxiter):
27  chain(_chain),
28  jnt2jac(chain),
29  nj(chain.getNrOfJoints()),
30  jac(nj),
31  svd(jac),
32  U(6,JntArray(nj)),
33  S(nj),
34  V(nj,JntArray(nj)),
35  tmp(nj),
36  eps(_eps),
37  maxiter(_maxiter),
38  nrZeroSigmas(0),
39  svdResult(0)
40  {
41  }
42 
46  jac.resize(nj);
47  svd = SVD_HH(jac);
48  for(unsigned int i = 0 ; i < U.size(); i++)
49  U[i].resize(nj);
50  S.resize(nj);
51  V.resize(nj);
52  for(unsigned int i = 0 ; i < V.size(); i++)
53  V[i].resize(nj);
54  tmp.resize(nj);
55  }
56 
58  {
59  }
60 
61 
62  int ChainIkSolverVel_pinv::CartToJnt(const JntArray& q_in, const Twist& v_in, JntArray& qdot_out)
63  {
64  if (nj != chain.getNrOfJoints())
65  return (error = E_NOT_UP_TO_DATE);
66 
67  if (nj != q_in.rows() || nj != qdot_out.rows())
68  return (error = E_SIZE_MISMATCH);
69 
70  //Let the ChainJntToJacSolver calculate the jacobian "jac" for
71  //the current joint positions "q_in"
72  error = jnt2jac.JntToJac(q_in,jac);
73  if (error < E_NOERROR) return error;
74 
75  double sum;
76  unsigned int i,j;
77 
78  // Initialize near zero singular value counter
79  nrZeroSigmas = 0 ;
80 
81  //Do a singular value decomposition of "jac" with maximum
82  //iterations "maxiter", put the results in "U", "S" and "V"
83  //jac = U*S*Vt
85  if (0 != svdResult)
86  {
87  qdot_out.data.setZero();
88  return (error = E_SVD_FAILED);
89  }
90 
91  // We have to calculate qdot_out = jac_pinv*v_in
92  // Using the svd decomposition this becomes(jac_pinv=V*S_pinv*Ut):
93  // qdot_out = V*S_pinv*Ut*v_in
94 
95  //first we calculate Ut*v_in
96  for (i=0;i<jac.columns();i++) {
97  sum = 0.0;
98  for (j=0;j<jac.rows();j++) {
99  sum+= U[j](i)*v_in(j);
100  }
101  //If the singular value is too small (<eps), don't invert it but
102  //set the inverted singular value to zero (truncated svd)
103  if ( fabs(S(i))<eps ) {
104  tmp(i) = 0.0 ;
105  // Count number of singular values near zero
106  ++nrZeroSigmas ;
107  }
108  else {
109  tmp(i) = sum/S(i) ;
110  }
111  }
112  //tmp is now: tmp=S_pinv*Ut*v_in, we still have to premultiply
113  //it with V to get qdot_out
114  for (i=0;i<jac.columns();i++) {
115  sum = 0.0;
116  for (j=0;j<jac.columns();j++) {
117  sum+=V[i](j)*tmp(j);
118  }
119  //Put the result in qdot_out
120  qdot_out(i)=sum;
121  }
122 
123  // Note if the solution is singular, i.e. if number of near zero
124  // singular values is greater than the full rank of jac
125  if ( nrZeroSigmas > (jac.columns()-jac.rows()) ) {
126  return (error = E_CONVERGE_PINV_SINGULAR); // converged but pinv singular
127  } else {
128  return (error = E_NOERROR); // have converged
129  }
130  }
131 
132  const char* ChainIkSolverVel_pinv::strError(const int error) const
133  {
134  if (E_CONVERGE_PINV_SINGULAR == error) return "Converged put pseudo inverse of jacobian is singular.";
135  else return SolverI::strError(error);
136  }
137 }
unsigned int rows() const
Definition: jntarray.cpp:72
void resize(unsigned int newNrOfColumns)
Allocates memory for new size (can break realtime behavior)
Definition: jacobian.cpp:56
This class encapsulates a serial kinematic interconnection structure. It is built out of segments...
Definition: chain.hpp:35
Chain size changed.
Definition: solveri.hpp:97
virtual const char * strError(const int error) const
Definition: solveri.hpp:125
This class represents an fixed size array containing joint values of a KDL::Chain.
Definition: jntarray.hpp:69
virtual int CartToJnt(const JntArray &q_in, const Twist &v_in, JntArray &qdot_out)
ChainIkSolverVel_pinv(const Chain &chain, double eps=0.00001, int maxiter=150)
Input size does not match internal state.
Definition: solveri.hpp:99
unsigned int columns() const
Definition: jacobian.cpp:76
represents both translational and rotational velocities.
Definition: frames.hpp:720
virtual void updateInternalDataStructures()
static const int E_CONVERGE_PINV_SINGULAR
solution converged but (pseudo)inverse is singular
Internal svd calculation failed.
Definition: solveri.hpp:107
Eigen::VectorXd data
Definition: jntarray.hpp:72
unsigned int getNrOfJoints() const
Definition: chain.hpp:71
void resize(unsigned int newSize)
Definition: jntarray.cpp:55
int calculate(const Jacobian &jac, std::vector< JntArray > &U, JntArray &w, std::vector< JntArray > &v, int maxiter)
Definition: svd_HH.cpp:60
virtual const char * strError(const int error) const
virtual int JntToJac(const JntArray &q_in, Jacobian &jac, int seg_nr=-1)
int error
Latest error, initialized to E_NOERROR in constructor.
Definition: solveri.hpp:149
unsigned int rows() const
Definition: jacobian.cpp:71


orocos_kdl
Author(s):
autogenerated on Fri Mar 12 2021 03:05:43