orocos_kdl
chainidsolver_recursive_newton_euler.cpp
Go to the documentation of this file.
1 // Copyright (C) 2009 Ruben Smits <ruben dot smits at intermodalics dot eu>
2 
3 // Version: 1.0
4 // Author: Ruben Smits <ruben dot smits at intermodalics dot eu>
5 // Maintainer: Ruben Smits <ruben dot smits at intermodalics dot eu>
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 #include "frames_io.hpp"
24 
25 namespace KDL{
26 
28  chain(chain_),nj(chain.getNrOfJoints()),ns(chain.getNrOfSegments()),
29  X(ns),S(ns),v(ns),a(ns),f(ns)
30  {
31  ag=-Twist(grav,Vector::Zero());
32  }
33 
37  X.resize(ns);
38  S.resize(ns);
39  v.resize(ns);
40  a.resize(ns);
41  f.resize(ns);
42  }
43 
44  int ChainIdSolver_RNE::CartToJnt(const JntArray &q, const JntArray &q_dot, const JntArray &q_dotdot, const Wrenches& f_ext,JntArray &torques)
45  {
46  if(nj != chain.getNrOfJoints() || ns != chain.getNrOfSegments())
47  return (error = E_NOT_UP_TO_DATE);
48 
49  //Check sizes when in debug mode
50  if(q.rows()!=nj || q_dot.rows()!=nj || q_dotdot.rows()!=nj || torques.rows()!=nj || f_ext.size()!=ns)
51  return (error = E_SIZE_MISMATCH);
52  unsigned int j=0;
53 
54  //Sweep from root to leaf
55  for(unsigned int i=0;i<ns;i++){
56  double q_,qdot_,qdotdot_;
58  q_=q(j);
59  qdot_=q_dot(j);
60  qdotdot_=q_dotdot(j);
61  j++;
62  }else
63  q_=qdot_=qdotdot_=0.0;
64 
65  //Calculate segment properties: X,S,vj,cj
66  X[i]=chain.getSegment(i).pose(q_);//Remark this is the inverse of the
67  //frame for transformations from
68  //the parent to the current coord frame
69  //Transform velocity and unit velocity to segment frame
70  Twist vj=X[i].M.Inverse(chain.getSegment(i).twist(q_,qdot_));
71  S[i]=X[i].M.Inverse(chain.getSegment(i).twist(q_,1.0));
72  //We can take cj=0, see remark section 3.5, page 55 since the unit velocity vector S of our joints is always time constant
73  //calculate velocity and acceleration of the segment (in segment coordinates)
74  if(i==0){
75  v[i]=vj;
76  a[i]=X[i].Inverse(ag)+S[i]*qdotdot_+v[i]*vj;
77  }else{
78  v[i]=X[i].Inverse(v[i-1])+vj;
79  a[i]=X[i].Inverse(a[i-1])+S[i]*qdotdot_+v[i]*vj;
80  }
81  //Calculate the force for the joint
82  //Collect RigidBodyInertia and external forces
84  f[i]=Ii*a[i]+v[i]*(Ii*v[i])-f_ext[i];
85  //std::cout << "a[i]=" << a[i] << "\n f[i]=" << f[i] << "\n S[i]" << S[i] << std::endl;
86  }
87  //Sweep from leaf to root
88  j=nj-1;
89  for(int i=ns-1;i>=0;i--){
91  torques(j)=dot(S[i],f[i]);
92  torques(j)+=chain.getSegment(i).getJoint().getInertia()*q_dotdot(j); // add torque from joint inertia
93  --j;
94  }
95  if(i!=0)
96  f[i-1]=f[i-1]+X[i]*f[i];
97  }
98  return (error = E_NOERROR);
99  }
100 }//namespace
KDL::ChainIdSolver_RNE::S
std::vector< Twist > S
Definition: chainidsolver_recursive_newton_euler.hpp:70
KDL::JntArray
Definition: jntarray.hpp:69
KDL::ChainIdSolver_RNE::CartToJnt
int CartToJnt(const JntArray &q, const JntArray &q_dot, const JntArray &q_dotdot, const Wrenches &f_ext, JntArray &torques)
Definition: chainidsolver_recursive_newton_euler.cpp:44
KDL::ChainIdSolver_RNE::ag
Twist ag
Definition: chainidsolver_recursive_newton_euler.hpp:74
KDL::Vector::Zero
static Vector Zero()
Definition: frames.inl:138
KDL::Segment::getJoint
const Joint & getJoint() const
Definition: segment.hpp:118
std::vector
std::vector::size
T size(T... args)
frames_io.hpp
KDL::ChainIdSolver_RNE::f
std::vector< Wrench > f
Definition: chainidsolver_recursive_newton_euler.hpp:73
chainidsolver_recursive_newton_euler.hpp
KDL::SolverI::E_NOT_UP_TO_DATE
@ E_NOT_UP_TO_DATE
Chain size changed.
Definition: solveri.hpp:97
KDL::Joint::Fixed
@ Fixed
Definition: joint.hpp:47
KDL::RigidBodyInertia
6D Inertia of a rigid body
Definition: rigidbodyinertia.hpp:37
KDL::ChainIdSolver_RNE::X
std::vector< Frame > X
Definition: chainidsolver_recursive_newton_euler.hpp:69
KDL
Definition: kukaLWR_DHnew.cpp:25
KDL::Chain::getNrOfSegments
unsigned int getNrOfSegments() const
Definition: chain.hpp:76
KDL::Vector
A concrete implementation of a 3 dimensional vector class.
Definition: frames.hpp:162
KDL::Twist
represents both translational and rotational velocities.
Definition: frames.hpp:723
KDL::ChainIdSolver_RNE::chain
const Chain & chain
Definition: chainidsolver_recursive_newton_euler.hpp:66
KDL::JntArray::rows
unsigned int rows() const
Definition: jntarray.cpp:70
dot
doubleAcc dot(const VectorAcc &lhs, const VectorAcc &rhs)
Definition: frameacc.inl:137
KDL::Chain::getSegment
const Segment & getSegment(unsigned int nr) const
Definition: chain.cpp:67
KDL::SolverI::E_SIZE_MISMATCH
@ E_SIZE_MISMATCH
Input size does not match internal state.
Definition: solveri.hpp:99
KDL::Segment::pose
Frame pose(const double &q) const
Definition: segment.cpp:57
KDL::ChainIdSolver_RNE::a
std::vector< Twist > a
Definition: chainidsolver_recursive_newton_euler.hpp:72
KDL::Joint::getInertia
const double & getInertia() const
Definition: joint.hpp:220
KDL::Segment::twist
Twist twist(const double &q, const double &qdot) const
Definition: segment.cpp:62
KDL::ChainIdSolver_RNE::ChainIdSolver_RNE
ChainIdSolver_RNE(const Chain &chain, Vector grav)
Definition: chainidsolver_recursive_newton_euler.cpp:27
KDL::Joint::getType
const JointType & getType() const
Definition: joint.hpp:159
KDL::ChainIdSolver_RNE::nj
unsigned int nj
Definition: chainidsolver_recursive_newton_euler.hpp:67
KDL::SolverI::E_NOERROR
@ E_NOERROR
No error.
Definition: solveri.hpp:91
KDL::ChainIdSolver_RNE::v
std::vector< Twist > v
Definition: chainidsolver_recursive_newton_euler.hpp:71
KDL::Chain
This class encapsulates a serial kinematic interconnection structure. It is built out of segments.
Definition: chain.hpp:35
KDL::Segment::getInertia
const RigidBodyInertia & getInertia() const
Definition: segment.hpp:128
KDL::Chain::getNrOfJoints
unsigned int getNrOfJoints() const
Definition: chain.hpp:71
KDL::ChainIdSolver_RNE::ns
unsigned int ns
Definition: chainidsolver_recursive_newton_euler.hpp:68
KDL::SolverI::error
int error
Latest error, initialized to E_NOERROR in constructor.
Definition: solveri.hpp:149
KDL::ChainIdSolver_RNE::updateInternalDataStructures
virtual void updateInternalDataStructures()
Definition: chainidsolver_recursive_newton_euler.cpp:34