KdlChainIdRne.cpp
Go to the documentation of this file.
00001 #include "robodyn_controllers/KdlChainIdRne.h"
00002 
00003 using namespace KDL;
00004 /***************************************************************************/
00012 KdlChainIdRne::KdlChainIdRne(const Chain& chain_, const Twist& v_base, const Twist& a_base):
00013     chain(chain_), nj(chain.getNrOfJoints()), ns(chain.getNrOfSegments()),
00014     X(ns), S(ns), v(ns), a(ns), Ii(ns), vb(v_base), ab(a_base)
00015 {
00016 }
00017 /***************************************************************************/
00027 int KdlChainIdRne::KinematicsPass(const JntArray& q, const JntArray& q_dot, const JntArray& q_dotdot, Twist& velOut, Twist& accelOut)
00028 {
00029     //Check sizes when in debug mode
00030     if (q.rows() != nj || q_dot.rows() != nj || q_dotdot.rows() != nj )
00031     {
00032         return -1;
00033     }
00034 
00035     unsigned int j = 0;
00036 
00037     //Sweep from root to leaf
00038     for (unsigned int i = 0; i < ns; i++)
00039     {
00040         double q_, qdot_, qdotdot_;
00041 
00042         if (chain.getSegment(i).getJoint().getType() != Joint::None)
00043         {
00044             q_       = q(j);
00045             qdot_    = q_dot(j);
00046             qdotdot_ = q_dotdot(j);
00047             j++;
00048         }
00049         else
00050         {
00051             q_ = qdot_ = qdotdot_ = 0.0;
00052         }
00053 
00054         //Calculate segment properties: X,S,vj,cj
00055         X[i] = chain.getSegment(i).pose(q_);//Remark this is the inverse of the
00056         //frame for transformations from
00057         //the parent to the current coord frame
00058         //Transform velocity and unit velocity to segment frame
00059         Twist vj = X[i].M.Inverse(chain.getSegment(i).twist(q_, qdot_));
00060         S[i]     = X[i].M.Inverse(chain.getSegment(i).twist(q_, 1.0));
00061 
00062         //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
00063         //calculate velocity and acceleration of the segment (in segment coordinates)
00064         if (i == 0)
00065         {
00066             v[i] = X[i].Inverse(vb) + vj;
00067             a[i] = X[i].Inverse(ab) + S[i] * qdotdot_ + v[i] * vj;
00068         }
00069         else
00070         {
00071             v[i] = X[i].Inverse(v[i - 1]) + vj;
00072             a[i] = X[i].Inverse(a[i - 1]) + S[i] * qdotdot_ + v[i] * vj;
00073         }
00074     }
00075 
00076     if (ns > 0)
00077     {
00078         velOut   = v[ns - 1];
00079         accelOut = a[ns - 1];
00080     }
00081 
00082     return 0;
00083 }
00084 /***************************************************************************/
00095 int KdlChainIdRne::DynamicsPass(const Wrenches& forceExt, JntArray& torques, Wrench& forceBase, std::vector<KDL::Wrench>& f, const KDL::RigidBodyInertia& inertiaExt, const KDL::RigidBodyInertia& inertiaSeg, KDL::JntArray& Hv, KDL::RigidBodyInertia& inertiaBase)
00096 {
00097     if (torques.rows() != nj || forceExt.size() != ns || Hv.rows() != nj || f.size() != ns || ns == 0)
00098     {
00099         return -1;
00100     }
00101 
00102     unsigned int j = 0;
00103 
00104     for (unsigned int i = 0; i < ns; i++)
00105     {
00106         //Calculate the force for the joint
00107         //Collect RigidBodyInertia and external forces
00108         Ii[i] = chain.getSegment(i).getInertia();
00109     }
00110 
00111     Ii[ns - 1] = Ii[ns - 1] + inertiaSeg; // adding seg masses not compensated with other joints
00112 
00113     for (unsigned int i = 0; i < ns; i++)
00114     {
00115         f[i] = Ii[i] * a[i] + v[i] * (Ii[i] * v[i]) + forceExt[i];
00116     }
00117 
00118     Ii[ns - 1] = Ii[ns - 1] + inertiaExt; // for Hv calc only
00119 
00120     //Sweep from leaf to root
00121     j = nj - 1;
00122 
00123     for (int i = ns - 1; i >= 0; i--)
00124     {
00125 
00126         if (i != 0)
00127         {
00128             f[i - 1]  = f[i - 1]  + X[i] * f[i];
00129             Ii[i - 1] = Ii[i - 1] + X[i] * Ii[i];
00130         }
00131 
00132         Fis = Ii[i] * S[i];
00133 
00134         if (chain.getSegment(i).getJoint().getType() != Joint::None)
00135         {
00136             Hv(j) = dot(S[i], Fis);
00137             torques(j--) = dot(S[i], f[i]);
00138         }
00139     }
00140 
00141     forceBase   = X[0] * f[0];
00142     inertiaBase = X[0] * Ii[0];
00143 
00144     return 0;
00145 }


robodyn_controllers
Author(s):
autogenerated on Sat Jun 8 2019 20:20:53