chainidsolver_vereshchagin.cpp
Go to the documentation of this file.
00001 // Copyright  (C)  2009, 2011
00002 
00003 // Version: 1.0
00004 // Author: Ruben Smits, Herman Bruyninckx, Azamat Shakhimardanov
00005 // Maintainer: Ruben Smits, Azamat Shakhimardanov
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 "chainidsolver_vereshchagin.hpp"
00023 #include "frames_io.hpp"
00024 #include "utilities/svd_eigen_HH.hpp"
00025 
00026 
00027 namespace KDL
00028 {
00029 using namespace Eigen;
00030 
00031 ChainIdSolver_Vereshchagin::ChainIdSolver_Vereshchagin(const Chain& chain_, Twist root_acc, unsigned int _nc) :
00032     chain(chain_), nj(chain.getNrOfJoints()), ns(chain.getNrOfSegments()), nc(_nc),
00033     results(ns + 1, segment_info(nc))
00034 {
00035     acc_root = root_acc;
00036 
00037     //Provide the necessary memory for computing the inverse of M0
00038     nu_sum.resize(nc);
00039     M_0_inverse.resize(nc, nc);
00040     Um = MatrixXd::Identity(nc, nc);
00041     Vm = MatrixXd::Identity(nc, nc);
00042     Sm = VectorXd::Ones(nc);
00043     tmpm = VectorXd::Ones(nc);
00044 }
00045 
00046 int ChainIdSolver_Vereshchagin::CartToJnt(const JntArray &q, const JntArray &q_dot, JntArray &q_dotdot, const Jacobian& alfa, const JntArray& beta, const Wrenches& f_ext, JntArray &torques)
00047 {
00048     //Check sizes always
00049     if (q.rows() != nj || q_dot.rows() != nj || q_dotdot.rows() != nj || torques.rows() != nj || f_ext.size() != ns)
00050         return -1;
00051     if (alfa.columns() != nc || beta.rows() != nc)
00052         return -2;
00053     //do an upward recursion for position and velocities
00054     this->initial_upwards_sweep(q, q_dot, q_dotdot, f_ext);
00055     //do an inward recursion for inertia, forces and constraints
00056     this->downwards_sweep(alfa, torques);
00057     //Solve for the constraint forces
00058     this->constraint_calculation(beta);
00059     //do an upward recursion to propagate the result
00060     this->final_upwards_sweep(q_dotdot, torques);
00061     return 0;
00062 }
00063 
00064 void ChainIdSolver_Vereshchagin::initial_upwards_sweep(const JntArray &q, const JntArray &qdot, const JntArray &qdotdot, const Wrenches& f_ext)
00065 {
00066     //if (q.rows() != nj || qdot.rows() != nj || qdotdot.rows() != nj || f_ext.size() != ns)
00067     //        return -1;
00068 
00069     unsigned int j = 0;
00070     F_total = Frame::Identity();
00071     for (unsigned int i = 0; i < ns; i++)
00072     {
00073         //Express everything in the segments reference frame (body coordinates)
00074         //which is at the segments tip, i.e. where the next joint is attached.
00075 
00076         //Calculate segment properties: X,S,vj,cj
00077         const Segment& segment = chain.getSegment(i);
00078         segment_info& s = results[i + 1];
00079         //The pose between the joint root and the segment tip (tip expressed in joint root coordinates)
00080         s.F = segment.pose(q(j)); //X pose of each link in link coord system
00081 
00082         F_total = F_total * s.F; //X pose of the each link in root coord system
00083         s.F_base = F_total; //X pose of the each link in root coord system for getter functions
00084 
00085         //The velocity due to the joint motion of the segment expressed in the segments reference frame (tip)
00086         Twist vj = s.F.M.Inverse(segment.twist(q(j), qdot(j))); //XDot of each link
00087         Twist aj = s.F.M.Inverse(segment.twist(q(j), qdotdot(j))); //XDotDot of each link
00088 
00089         //The unit velocity due to the joint motion of the segment expressed in the segments reference frame (tip)
00090         s.Z = s.F.M.Inverse(segment.twist(q(j), 1.0));
00091         //Put Z in the joint root reference frame:
00092         s.Z = s.F * s.Z;
00093 
00094         //The total velocity of the segment expressed in the the segments reference frame (tip)
00095         if (i != 0)
00096         {
00097             s.v = s.F.Inverse(results[i].v) + vj; // recursive velocity of each link in segment frame
00098             //s.A=s.F.Inverse(results[i].A)+aj;
00099             s.A = s.F.M.Inverse(results[i].A);
00100         }
00101         else
00102         {
00103             s.v = vj;
00104             s.A = s.F.M.Inverse(acc_root);
00105         }
00106         //c[i] = cj + v[i]xvj (remark: cj=0, since our S is not time dependent in local coordinates)
00107         //The velocity product acceleration
00108         //std::cout << i << " Initial upward" << s.v << std::endl;
00109         s.C = s.v*vj; //This is a cross product: cartesian space BIAS acceleration in local link coord.
00110         //Put C in the joint root reference frame
00111         s.C = s.F * s.C; //+F_total.M.Inverse(acc_root));
00112         //The rigid body inertia of the segment, expressed in the segments reference frame (tip)
00113         s.H = segment.getInertia();
00114 
00115         //wrench of the rigid body bias forces and the external forces on the segment (in body coordinates, tip)
00116         //external forces are taken into account through s.U.
00117         Wrench FextLocal = F_total.M.Inverse() * f_ext[i];
00118         s.U = s.v * (s.H * s.v) - FextLocal; //f_ext[i];
00119         if (segment.getJoint().getType() != Joint::None)
00120             j++;
00121     }
00122 
00123 }
00124 
00125 void ChainIdSolver_Vereshchagin::downwards_sweep(const Jacobian& alfa, const JntArray &torques)
00126 {
00127     unsigned int j = nj - 1;
00128     for (int i = ns; i >= 0; i--)
00129     {
00130         //Get a handle for the segment we are working on.
00131         segment_info& s = results[i];
00132         //For segment N,
00133         //tilde is in the segment refframe (tip, not joint root)
00134         //without tilde is at the joint root (the childs tip!!!)
00135         //P_tilde is the articulated body inertia
00136         //R_tilde is the sum of external and coriolis/centrifugal forces
00137         //M is the (unit) acceleration energy already generated at link i
00138         //G is the (unit) magnitude of the constraint forces at link i
00139         //E are the (unit) constraint forces due to the constraints
00140         if (i == ns)
00141         {
00142             s.P_tilde = s.H;
00143             s.R_tilde = s.U;
00144             s.M.setZero();
00145             s.G.setZero();
00146             //changeBase(alfa_N,F_total.M.Inverse(),alfa_N2);
00147             for (unsigned int r = 0; r < 3; r++)
00148                 for (unsigned int c = 0; c < nc; c++)
00149                 {
00150                     //copy alfa constrain force matrix in E~
00151                     s.E_tilde(r, c) = alfa(r + 3, c);
00152                     s.E_tilde(r + 3, c) = alfa(r, c);
00153                 }
00154             //Change the reference frame of alfa to the segmentN tip frame
00155             //F_Total holds end effector frame, if done per segment bases then constraints could be extended to all segments
00156             Rotation base_to_end = F_total.M.Inverse();
00157             for (unsigned int c = 0; c < nc; c++)
00158             {
00159                 Wrench col(Vector(s.E_tilde(3, c), s.E_tilde(4, c), s.E_tilde(5, c)),
00160                            Vector(s.E_tilde(0, c), s.E_tilde(1, c), s.E_tilde(2, c)));
00161                 col = base_to_end*col;
00162                 s.E_tilde.col(c) << Vector3d::Map(col.torque.data), Vector3d::Map(col.force.data);
00163             }
00164         }
00165         else
00166         {
00167             //For all others:
00168             //Everything should expressed in the body coordinates of segment i
00169             segment_info& child = results[i + 1];
00170             //Copy PZ into a vector so we can do matrix manipulations, put torques above forces
00171             Vector6d vPZ;
00172             vPZ << Vector3d::Map(child.PZ.torque.data), Vector3d::Map(child.PZ.force.data);
00173             Matrix6d PZDPZt = (vPZ * vPZ.transpose()).lazy();
00174             PZDPZt /= child.D;
00175 
00176             //equation a) (see Vereshchagin89) PZDPZt=[I,H;H',M]
00177             //Azamat:articulated body inertia as in Featherstone (7.19)
00178             s.P_tilde = s.H + child.P - ArticulatedBodyInertia(PZDPZt.corner < 3, 3 > (BottomRight), PZDPZt.corner < 3, 3 > (TopRight), PZDPZt.corner < 3, 3 > (TopLeft));
00179             //equation b) (see Vereshchagin89)
00180             //Azamat: bias force as in Featherstone (7.20)
00181             s.R_tilde = s.U + child.R + child.PC + (child.PZ / child.D) * child.u;
00182             //equation c) (see Vereshchagin89)
00183             s.E_tilde = child.E;
00184 
00185             //Azamat: equation (c) right side term
00186             s.E_tilde -= (vPZ * child.EZ.transpose()).lazy() / child.D;
00187 
00188             //equation d) (see Vereshchagin89)
00189             s.M = child.M;
00190             //Azamat: equation (d) right side term
00191             s.M -= (child.EZ * child.EZ.transpose()).lazy() / child.D;
00192 
00193             //equation e) (see Vereshchagin89)
00194             s.G = child.G;
00195             Twist CiZDu = child.C + (child.Z / child.D) * child.u;
00196             Vector6d vCiZDu;
00197             vCiZDu << Vector3d::Map(CiZDu.rot.data), Vector3d::Map(CiZDu.vel.data);
00198             s.G += (child.E.transpose() * vCiZDu).lazy();
00199         }
00200         if (i != 0)
00201         {
00202             //Transform all results to joint root coordinates of segment i (== body coordinates segment i-1)
00203             //equation a)
00204             s.P = s.F * s.P_tilde;
00205             //equation b)
00206             s.R = s.F * s.R_tilde;
00207             //equation c), in matrix: torques above forces, so switch and switch back
00208             for (unsigned int c = 0; c < nc; c++)
00209             {
00210                 Wrench col(Vector(s.E_tilde(3, c), s.E_tilde(4, c), s.E_tilde(5, c)),
00211                            Vector(s.E_tilde(0, c), s.E_tilde(1, c), s.E_tilde(2, c)));
00212                 col = s.F*col;
00213                 s.E.col(c) << Vector3d::Map(col.torque.data), Vector3d::Map(col.force.data);
00214             }
00215 
00216             //needed for next recursion
00217             s.PZ = s.P * s.Z;
00218             s.D = dot(s.Z, s.PZ);
00219             s.PC = s.P * s.C;
00220 
00221             //u=(Q-Z(R+PC)=sum of external forces along the joint axes,
00222             //R are the forces comming from the children,
00223             //Q is taken zero (do we need to take the previous calculated torques?
00224 
00225             //projection of coriolis and centrepital forces into joint subspace (0 0 Z)
00226             s.totalBias = -dot(s.Z, s.R + s.PC);
00227             s.u = torques(j) + s.totalBias;
00228 
00229             //Matrix form of Z, put rotations above translations
00230             Vector6d vZ;
00231             vZ << Vector3d::Map(s.Z.rot.data), Vector3d::Map(s.Z.vel.data);
00232             s.EZ = (s.E.transpose() * vZ).lazy();
00233 
00234             if (chain.getSegment(i - 1).getJoint().getType() != Joint::None)
00235                 j--;
00236         }
00237     }
00238 }
00239 
00240 void ChainIdSolver_Vereshchagin::constraint_calculation(const JntArray& beta)
00241 {
00242     //equation f) nu = M_0_inverse*(beta_N - E0_tilde`*acc0 - G0)
00243     //M_0_inverse, always nc*nc symmetric matrix
00244     //std::cout<<"M0: "<<results[0].M<<std::endl;
00245     //results[0].M-=MatrixXd::Identity(nc,nc);
00246     //std::cout<<"augmented M0: "<<results[0].M<<std::endl;
00247 
00248 
00249     //ToDo: Need to check ill conditions
00250 
00251     //M_0_inverse=results[0].M.inverse();
00252     svd_eigen_HH(results[0].M, Um, Sm, Vm, tmpm);
00253     //truncated svd, what would sdls, dls physically mean?
00254     for (unsigned int i = 0; i < nc; i++)
00255         if (Sm(i) < 1e-14)
00256             Sm(i) = 0.0;
00257         else
00258             Sm(i) = 1 / Sm(i);
00259 
00260     results[0].M = (Vm * Sm.asDiagonal()).lazy();
00261     M_0_inverse = (results[0].M * Um.transpose()).lazy();
00262     //results[0].M.ldlt().solve(MatrixXd::Identity(nc,nc),&M_0_inverse);
00263     //results[0].M.computeInverse(&M_0_inverse);
00264     Vector6d acc;
00265     acc << Vector3d::Map(acc_root.rot.data), Vector3d::Map(acc_root.vel.data);
00266     nu_sum = -(results[0].E_tilde.transpose() * acc).lazy();
00267     //nu_sum.setZero();
00268     nu_sum += beta.data;
00269     nu_sum -= results[0].G;
00270 
00271     //equation f) nu = M_0_inverse*(beta_N - E0_tilde`*acc0 - G0)
00272     nu = (M_0_inverse * nu_sum).lazy();
00273 }
00274 
00275 void ChainIdSolver_Vereshchagin::final_upwards_sweep(JntArray &q_dotdot, JntArray &torques)
00276 {
00277     unsigned int j = 0;
00278 
00279     for (unsigned int i = 1; i <= ns; i++)
00280     {
00281         segment_info& s = results[i];
00282         //Calculation of joint and segment accelerations
00283         //equation g) qdotdot[i] = D^-1*(Q - Z'(R + P(C + acc[i-1]) + E*nu))
00284         // = D^-1(u - Z'(P*acc[i-1] + E*nu)
00285         Twist a_g;
00286         Twist a_p;
00287         if (i == 1)
00288         {
00289             a_p = acc_root;
00290         }
00291         else
00292         {
00293             a_p = results[i - 1].acc;
00294         }
00295 
00296         //The contribution of the constraint forces at segment i
00297         Vector6d tmp = s.E*nu;
00298         Wrench constraint_force = Wrench(Vector(tmp(3), tmp(4), tmp(5)),
00299                                          Vector(tmp(0), tmp(1), tmp(2)));
00300 
00301         //acceleration components are also computed
00302         //Contribution of the acceleration of the parent (i-1)
00303         Wrench parent_force = s.P*a_p;
00304         double parent_forceProjection = -dot(s.Z, parent_force);
00305         double parentAccComp = parent_forceProjection / s.D;
00306 
00307         //The constraint force and acceleration force projected on the joint axes -> axis torque/force
00308         double constraint_torque = -dot(s.Z, constraint_force);
00309         //The result should be the torque at this joint
00310 
00311         torques(j) = constraint_torque;
00312         //s.constAccComp = torques(j) / s.D;
00313         s.constAccComp = constraint_torque / s.D;
00314         s.nullspaceAccComp = s.u / s.D;
00315         //total joint space acceleration resulting from accelerations of parent joints, constraint forces and
00316         // nullspace forces.
00317         q_dotdot(j) = (s.nullspaceAccComp + parentAccComp + s.constAccComp);
00318         s.acc = s.F.Inverse(a_p + s.Z * q_dotdot(j) + s.C);//returns acceleration in link distal tip coordinates. For use needs to be transformed
00319         if (chain.getSegment(i - 1).getJoint().getType() != Joint::None)
00320             j++;
00321     }
00322 }
00323 
00324 /*
00325 void ChainIdSolver_Vereshchagin::getLinkCartesianPose(Frames& x_base)
00326 {
00327     for (int i = 0; i < ns; i++)
00328     {
00329         x_base[i] = results[i + 1].F_base;
00330     }
00331     return;
00332 }
00333 
00334 void ChainIdSolver_Vereshchagin::getLinkCartesianVelocity(Twists& xDot_base)
00335 {
00336 
00337     for (int i = 0; i < ns; i++)
00338     {
00339         xDot_base[i] = results[i + 1].F_base.M * results[i + 1].v;
00340     }
00341     return;
00342 }
00343 
00344 void ChainIdSolver_Vereshchagin::getLinkCartesianAcceleration(Twists& xDotDot_base)
00345 {
00346 
00347     for (int i = 0; i < ns; i++)
00348     {
00349         xDotDot_base[i] = results[i + 1].F_base.M * results[i + 1].acc;
00350         //std::cout << "XDotDot_base[i] " << xDotDot_base[i] << std::endl;
00351     }
00352     return;
00353 }
00354 
00355 void ChainIdSolver_Vereshchagin::getLinkPose(Frames& x_local)
00356 {
00357     for (int i = 0; i < ns; i++)
00358     {
00359         x_local[i] = results[i + 1].F;
00360     }
00361     return;
00362 }
00363 
00364 void ChainIdSolver_Vereshchagin::getLinkVelocity(Twists& xDot_local)
00365 {
00366     for (int i = 0; i < ns; i++)
00367     {
00368         xDot_local[i] = results[i + 1].v;
00369     }
00370     return;
00371 
00372 }
00373 
00374 void ChainIdSolver_Vereshchagin::getLinkAcceleration(Twists& xDotdot_local)
00375 {
00376      for (int i = 0; i < ns; i++)
00377     {
00378         xDotdot_local[i] = results[i + 1].acc;
00379     }
00380     return;
00381 
00382 }
00383 
00384 void ChainIdSolver_Vereshchagin::getJointBiasAcceleration(JntArray& bias_q_dotdot)
00385 {
00386     for (int i = 0; i < ns; i++)
00387     {
00388         //this is only force
00389         double tmp = results[i + 1].totalBias;
00390         //this is accelleration
00391         bias_q_dotdot(i) = tmp / results[i + 1].D;
00392 
00393         //s.totalBias = - dot(s.Z, s.R + s.PC);
00394         //std::cout << "totalBiasAccComponent" << i << ": " << bias_q_dotdot(i) << std::endl;
00395         //bias_q_dotdot(i) = s.totalBias/s.D
00396 
00397     }
00398     return;
00399 
00400 }
00401 
00402 void ChainIdSolver_Vereshchagin::getJointConstraintAcceleration(JntArray& constraint_q_dotdot)
00403 {
00404     for (int i = 0; i < ns; i++)
00405     {
00406         constraint_q_dotdot(i) = results[i + 1].constAccComp;
00407         //double tmp = results[i + 1].u;
00408         //s.u = torques(j) + s.totalBias;
00409         // std::cout << "s.constraintAccComp" << i << ": " << results[i+1].constAccComp << std::endl;
00410         //nullspace_q_dotdot(i) = s.u/s.D
00411 
00412     }
00413     return;
00414 
00415 
00416 }
00417 
00418 //Check the name it does not seem to be appropriate
00419 
00420 void ChainIdSolver_Vereshchagin::getJointNullSpaceAcceleration(JntArray& nullspace_q_dotdot)
00421 {
00422     for (int i = 0; i < ns; i++)
00423     {
00424         nullspace_q_dotdot(i) = results[i + 1].nullspaceAccComp;
00425         //double tmp = results[i + 1].u;
00426         //s.u = torques(j) + s.totalBias;
00427         //std::cout << "s.nullSpaceAccComp" << i << ": " << results[i+1].nullspaceAccComp << std::endl;
00428         //nullspace_q_dotdot(i) = s.u/s.D
00429 
00430     }
00431     return;
00432 
00433 
00434 }
00435 
00436 //This is not only a bias force energy but also includes generalized forces
00437 //change type of parameter G
00438 //this method should retur array of G's
00439 
00440 void ChainIdSolver_Vereshchagin::getLinkBiasForceAcceleratoinEnergy(Eigen::VectorXd& G)
00441 {
00442     for (int i = 0; i < ns; i++)
00443     {
00444         G = results[i + 1].G;
00445         //double tmp = results[i + 1].u;
00446         //s.u = torques(j) + s.totalBias;
00447         //std::cout << "s.G " << i << ":  " << results[i+1].G << std::endl;
00448         //nullspace_q_dotdot(i) = s.u/s.D
00449 
00450     }
00451     return;
00452 
00453 }
00454 
00455 //this method should retur array of R's
00456 
00457 void ChainIdSolver_Vereshchagin::getLinkBiasForceMatrix(Wrenches& R_tilde)
00458 {
00459     for (int i = 0; i < ns; i++)
00460     {
00461         R_tilde[i] = results[i + 1].R_tilde;
00462         //Azamat: bias force as in Featherstone (7.20)
00463         //s.R_tilde = s.U + child.R + child.PC + child.PZ / child.D * child.u;
00464         std::cout << "s.R_tilde " << i << ":  " << results[i + 1].R_tilde << std::endl;
00465     }
00466     return;
00467 }
00468 
00469 */
00470 
00471 }//namespace


orocos_kdl
Author(s): Ruben Smits, Erwin Aertbelien, Orocos Developers
autogenerated on Sat Dec 28 2013 17:17:25