diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainfksolver.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainfksolver.hpp new file mode 100644 index 0000000000000000000000000000000000000000..226cd4f67564e54d83d5cb1c9a5861367523e13e --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainfksolver.hpp @@ -0,0 +1,141 @@ +// Copyright (C) 2007 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#ifndef KDL_CHAIN_FKSOLVER_HPP +#define KDL_CHAIN_FKSOLVER_HPP + +#include "chain.hpp" +#include "framevel.hpp" +#include "frameacc.hpp" +#include "jntarray.hpp" +#include "jntarrayvel.hpp" +#include "jntarrayacc.hpp" +#include "solveri.hpp" + +namespace KDL { + /** + * \brief This abstract class encapsulates a + * solver for the forward position kinematics for a KDL::Chain. + * + * @ingroup KinematicFamily + */ + //Forward definition + class ChainFkSolverPos : public KDL::SolverI { + public: + /** + * Calculate forward position kinematics for a KDL::Chain, + * from joint coordinates to cartesian pose. + * + * @param q_in input joint coordinates + * @param p_out reference to output cartesian pose + * + * @return if < 0 something went wrong + */ + virtual int JntToCart(const JntArray& q_in, Frame& p_out,int segmentNr=-1)=0; + /** + * Calculate forward position kinematics for a KDL::Chain, + * from joint coordinates to cartesian pose. + * + * @param q_in input joint coordinates + * @param p_out reference to a vector of output cartesian poses for all segments + * + * @return if < 0 something went wrong + */ + virtual int JntToCart(const JntArray& q_in, std::vector& p_out,int segmentNr=-1)=0; + + virtual void updateInternalDataStructures()=0; + virtual ~ChainFkSolverPos(){}; + }; + + /** + * \brief This abstract class encapsulates a solver + * for the forward velocity kinematics for a KDL::Chain. + * + * @ingroup KinematicFamily + */ + class ChainFkSolverVel : public KDL::SolverI { + public: + /** + * Calculate forward position and velocity kinematics, from + * joint coordinates to cartesian coordinates. + * + * @param q_in input joint coordinates (position and velocity) + * @param out output cartesian coordinates (position and velocity) + * + * @return if < 0 something went wrong + */ + virtual int JntToCart(const JntArrayVel& q_in, FrameVel& out,int segmentNr=-1)=0; + /** + * Calculate forward position and velocity kinematics, from + * joint coordinates to cartesian coordinates. + * + * @param q_in input joint coordinates (position and velocity) + * @param out output cartesian coordinates for all segments (position and velocity) + * + * @return if < 0 something went wrong + */ + virtual int JntToCart(const JntArrayVel& q_in, std::vector& out,int segmentNr=-1)=0; + + virtual void updateInternalDataStructures()=0; + virtual ~ChainFkSolverVel(){}; + }; + + /** + * \brief This abstract class encapsulates a solver + * for the forward acceleration kinematics for a KDL::Chain. + * + * @ingroup KinematicFamily + */ + class ChainFkSolverAcc : public KDL::SolverI { + public: + /** + * Calculate forward position, velocity and acceleration + * kinematics, from joint coordinates to cartesian coordinates + * + * @param q_in input joint coordinates (position, velocity and + * acceleration + @param out output cartesian coordinates (position, velocity + * and acceleration + * + * @return if < 0 something went wrong + */ + virtual int JntToCart(const JntArrayAcc& q_in, FrameAcc& out,int segmentNr=-1)=0; + /** + * Calculate forward position, velocity and acceleration + * kinematics, from joint coordinates to cartesian coordinates + * + * @param q_in input joint coordinates (position, velocity and + * acceleration + * @param out output cartesian coordinates (position, velocity + * and acceleration for all segments + * + * @return if < 0 something went wrong + */ + virtual int JntToCart(const JntArrayAcc& q_in, std::vector& out,int segmentNr=-1)=0; + + virtual void updateInternalDataStructures()=0; + virtual ~ChainFkSolverAcc()=0; + }; + + +}//end of namespace KDL + +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainfksolverpos_recursive.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainfksolverpos_recursive.cpp new file mode 100644 index 0000000000000000000000000000000000000000..487569c8585a00606ecd1eabaccfde8131330094 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainfksolverpos_recursive.cpp @@ -0,0 +1,101 @@ +// Copyright (C) 2007 Francois Cauwe +// Copyright (C) 2007 Ruben Smits + +// Version: 1.0 +// Author: Francois Cauwe +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#include "chainfksolverpos_recursive.hpp" +#include + +namespace KDL { + + ChainFkSolverPos_recursive::ChainFkSolverPos_recursive(const Chain& _chain): + chain(_chain) + { + } + + int ChainFkSolverPos_recursive::JntToCart(const JntArray& q_in, Frame& p_out, int seg_nr) { + unsigned int segmentNr; + if(seg_nr<0) + segmentNr=chain.getNrOfSegments(); + else + segmentNr = seg_nr; + + p_out = Frame::Identity(); + + if(q_in.rows()!=chain.getNrOfJoints()) + return (error = E_SIZE_MISMATCH); + else if(segmentNr>chain.getNrOfSegments()) + return (error = E_OUT_OF_RANGE); + else{ + int j=0; + for(unsigned int i=0;i& p_out, int seg_nr) { + unsigned int segmentNr; + if(seg_nr<0) + segmentNr=chain.getNrOfSegments(); + else + segmentNr = seg_nr; + + if(q_in.rows()!=chain.getNrOfJoints()) + return -1; + else if(segmentNr>chain.getNrOfSegments()) + return -1; + else if(p_out.size() != segmentNr) + return -1; + else if(segmentNr == 0) + return -1; + else{ + int j=0; + // Initialization + if(chain.getSegment(0).getJoint().getType()!=Joint::Fixed) { + p_out[0] = chain.getSegment(0).pose(q_in(j)); + j++; + }else + p_out[0] = chain.getSegment(0).pose(0.0); + + for(unsigned int i=1;i + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#ifndef KDLCHAINFKSOLVERPOS_RECURSIVE_HPP +#define KDLCHAINFKSOLVERPOS_RECURSIVE_HPP + +#include "chainfksolver.hpp" + +namespace KDL { + + /** + * Implementation of a recursive forward position kinematics + * algorithm to calculate the position transformation from joint + * space to Cartesian space of a general kinematic chain (KDL::Chain). + * + * @ingroup KinematicFamily + */ + class ChainFkSolverPos_recursive : public ChainFkSolverPos + { + public: + ChainFkSolverPos_recursive(const Chain& chain); + ~ChainFkSolverPos_recursive(); + + virtual int JntToCart(const JntArray& q_in, Frame& p_out, int segmentNr=-1); + virtual int JntToCart(const JntArray& q_in, std::vector& p_out, int segmentNr=-1); + + virtual void updateInternalDataStructures() {}; + + private: + const Chain& chain; + }; + +} + +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainfksolvervel_recursive.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainfksolvervel_recursive.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8957db0b2bf138e65437ace357c96018dcf99a55 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainfksolvervel_recursive.cpp @@ -0,0 +1,111 @@ +// Copyright (C) 2007 Francois Cauwe +// Copyright (C) 2007 Ruben Smits + +// Version: 1.0 +// Author: Francois Cauwe +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#include "chainfksolvervel_recursive.hpp" + +namespace KDL +{ + ChainFkSolverVel_recursive::ChainFkSolverVel_recursive(const Chain& _chain): + chain(_chain) + { + } + + ChainFkSolverVel_recursive::~ChainFkSolverVel_recursive() + { + } + + int ChainFkSolverVel_recursive::JntToCart(const JntArrayVel& in,FrameVel& out,int seg_nr) + { + unsigned int segmentNr; + if(seg_nr<0) + segmentNr=chain.getNrOfSegments(); + else + segmentNr = seg_nr; + + out=FrameVel::Identity(); + + if(!(in.q.rows()==chain.getNrOfJoints()&&in.qdot.rows()==chain.getNrOfJoints())) + return (error = E_SIZE_MISMATCH); + else if(segmentNr>chain.getNrOfSegments()) + return (error = E_OUT_OF_RANGE); + else{ + int j=0; + for (unsigned int i=0;i& out,int seg_nr) + { + unsigned int segmentNr; + if(seg_nr<0) + segmentNr=chain.getNrOfSegments(); + else + segmentNr = seg_nr; + + + + if(!(in.q.rows()==chain.getNrOfJoints()&&in.qdot.rows()==chain.getNrOfJoints())) + return -1; + else if(segmentNr>chain.getNrOfSegments()) + return -1; + else if(out.size()!=segmentNr) + return -1; + else if(segmentNr == 0) + return -1; + else{ + int j=0; + // Initialization + if(chain.getSegment(0).getJoint().getType()!=Joint::Fixed) { + out[0] = FrameVel(chain.getSegment(0).pose(in.q(0)), + chain.getSegment(0).twist(in.q(0),in.qdot(0))); + j++; + }else + out[0] = FrameVel(chain.getSegment(0).pose(0.0), + chain.getSegment(0).twist(0.0,0.0)); + + for (unsigned int i=1;i + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#ifndef KDL_CHAIN_FKSOLVERVEL_RECURSIVE_HPP +#define KDL_CHAIN_FKSOLVERVEL_RECURSIVE_HPP + +#include "chainfksolver.hpp" + +namespace KDL +{ + /** + * Implementation of a recursive forward position and velocity + * kinematics algorithm to calculate the position and velocity + * transformation from joint space to Cartesian space of a general + * kinematic chain (KDL::Chain). + * + * @ingroup KinematicFamily + */ + class ChainFkSolverVel_recursive : public ChainFkSolverVel + { + public: + ChainFkSolverVel_recursive(const Chain& chain); + ~ChainFkSolverVel_recursive(); + + virtual int JntToCart(const JntArrayVel& q_in,FrameVel& out,int segmentNr=-1); + virtual int JntToCart(const JntArrayVel& q_in,std::vector& out,int segmentNr=-1); + virtual void updateInternalDataStructures() {}; + private: + const Chain& chain; + }; +} + +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainhdsolver_vereshchagin.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainhdsolver_vereshchagin.cpp new file mode 100644 index 0000000000000000000000000000000000000000..75f58e9696946411607774d4fe9a30f6d7ec46d7 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainhdsolver_vereshchagin.cpp @@ -0,0 +1,521 @@ +// Copyright (C) 2009 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Author: Herman Bruyninckx +// Author: Azamat Shakhimardanov +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#include "chainhdsolver_vereshchagin.hpp" +#include "frames_io.hpp" +#include "utilities/svd_eigen_HH.hpp" + +namespace KDL +{ + +ChainHdSolver_Vereshchagin::ChainHdSolver_Vereshchagin(const Chain& chain_, const Twist &root_acc, const unsigned int nc_) : + chain(chain_), nj(chain.getNrOfJoints()), ns(chain.getNrOfSegments()), nc(nc_), + results(ns + 1, segment_info(nc)) +{ + acc_root = root_acc; + + //Provide the necessary memory for computing the inverse of M0 + nu_sum.resize(nc); + M_0_inverse.resize(nc, nc); + Um = Eigen::MatrixXd::Identity(nc, nc); + Vm = Eigen::MatrixXd::Identity(nc, nc); + Sm = Eigen::VectorXd::Ones(nc); + tmpm = Eigen::VectorXd::Ones(nc); + + // Provide the necessary memory for storing the total torque acting on each joint + total_torques = Eigen::VectorXd::Zero(nj); +} + +void ChainHdSolver_Vereshchagin::updateInternalDataStructures() { + ns = chain.getNrOfSegments(); + nj = chain.getNrOfJoints(); + total_torques = Eigen::VectorXd::Zero(nj); + results.resize(ns+1,segment_info(nc)); +} + +int ChainHdSolver_Vereshchagin::CartToJnt(const JntArray &q, const JntArray &q_dot, JntArray &q_dotdot, const Jacobian& alfa, const JntArray& beta, const Wrenches& f_ext, const JntArray &ff_torques, JntArray &constraint_torques) +{ + //Check sizes always + nj = chain.getNrOfJoints(); + if(ns != chain.getNrOfSegments()) + return (error = E_NOT_UP_TO_DATE); + if (q.rows() != nj || q_dot.rows() != nj || q_dotdot.rows() != nj || ff_torques.rows() != nj || constraint_torques.rows() != nj || f_ext.size() != ns) + return (error = E_SIZE_MISMATCH); + if (alfa.columns() != nc || beta.rows() != nc) + return (error = E_SIZE_MISMATCH); + //do an upward recursion for position, velocities and rigid-body bias forces + this->initial_upwards_sweep(q, q_dot, q_dotdot, f_ext); + //do an inward recursion for inertia, articulated bias forces and constraints + this->downwards_sweep(alfa, ff_torques); + //Solve for the constraint forces + this->constraint_calculation(beta); + //do an upward recursion to propagate the result and compute final output + this->final_upwards_sweep(q_dotdot, constraint_torques); + return (error = E_NOERROR); +} + +void ChainHdSolver_Vereshchagin::initial_upwards_sweep(const JntArray &q, const JntArray &qdot, const JntArray &qdotdot, const Wrenches& f_ext) +{ + //if (q.rows() != nj || qdot.rows() != nj || qdotdot.rows() != nj || f_ext.size() != ns) + // return -1; + + unsigned int j = 0; + F_total = Frame::Identity(); + for (unsigned int i = 0; i < ns; i++) + { + //Express everything in the segments reference frame (body coordinates) + //which is at the segments tip, i.e. where the next joint is attached. + + //Calculate segment properties: X,S,vj,cj + const Segment& segment = chain.getSegment(i); + segment_info& s = results[i + 1]; + //The pose between the joint root and the segment tip (tip expressed in joint root coordinates) + s.F = segment.pose(q(j)); //X pose of each link in link coord system + + F_total = F_total * s.F; //X pose of the each link in root coord system + s.F_base = F_total; //X pose of the each link in root coord system for getter functions + + //The velocity due to the joint motion of the segment expressed in the segments reference frame (tip) + Twist vj = s.F.M.Inverse(segment.twist(q(j), qdot(j))); //XDot of each link + //Twist aj = s.F.M.Inverse(segment.twist(q(j), qdotdot(j))); //XDotDot of each link + + //The unit velocity due to the joint motion of the segment expressed in the segments reference frame (tip) + s.Z = s.F.M.Inverse(segment.twist(q(j), 1.0)); + //Put Z in the joint root reference frame: + s.Z = s.F * s.Z; + + //The total velocity of the segment expressed in the segments reference frame (tip) + if (i != 0) + { + s.v = s.F.Inverse(results[i].v) + vj; // recursive velocity of each link in segment frame + //s.A=s.F.Inverse(results[i].A)+aj; + s.A = s.F.M.Inverse(results[i].A); + } + else + { + s.v = vj; + s.A = s.F.M.Inverse(acc_root); + } + //c[i] = cj + v[i]xvj (remark: cj=0, since our S is not time dependent in local coordinates) + //The velocity product acceleration + //std::cout << i << " Initial upward" << s.v << std::endl; + s.C = s.v*vj; //This is a cross product: cartesian space BIAS acceleration in local link coord. + //Put C in the joint root reference frame + s.C = s.F * s.C; //+F_total.M.Inverse(acc_root)); + //The rigid body inertia of the segment, expressed in the segments reference frame (tip) + s.H = segment.getInertia(); + + //wrench of the rigid body bias forces and the external forces on the segment (in body coordinates, tip) + //external forces are taken into account through s.U. + Wrench FextLocal = F_total.M.Inverse() * f_ext[i]; + s.U = s.v * (s.H * s.v) - FextLocal; //f_ext[i]; + if (segment.getJoint().getType() != Joint::Fixed) + j++; + } + +} + +void ChainHdSolver_Vereshchagin::downwards_sweep(const Jacobian& alfa, const JntArray &ff_torques) +{ + int j = nj - 1; + for (int i = ns; i >= 0; i--) + { + //Get a handle for the segment we are working on. + segment_info& s = results[i]; + //For segment N, + //tilde is in the segment refframe (tip, not joint root) + //without tilde is at the joint root (the childs tip!!!) + //P_tilde is the articulated body inertia + //R_tilde is the sum of external and coriolis/centrifugal forces + //M is the (unit) acceleration energy already generated at link i + //G is the (unit) magnitude of the constraint forces at link i + //E are the (unit) constraint forces due to the constraints + if (i == (int)ns) + { + s.P_tilde = s.H; + s.R_tilde = s.U; + s.M.setZero(); + s.G.setZero(); + //changeBase(alfa_N,F_total.M.Inverse(),alfa_N2); + for (unsigned int r = 0; r < 3; r++) + for (unsigned int c = 0; c < nc; c++) + { + //copy alfa constrain force matrix in E~ + s.E_tilde(r, c) = alfa(r + 3, c); + s.E_tilde(r + 3, c) = alfa(r, c); + } + //Change the reference frame of alfa to the segmentN tip frame + //F_Total holds end effector frame, if done per segment bases then constraints could be extended to all segments + Rotation base_to_end = F_total.M.Inverse(); + for (unsigned int c = 0; c < nc; c++) + { + Wrench col(Vector(s.E_tilde(3, c), s.E_tilde(4, c), s.E_tilde(5, c)), + Vector(s.E_tilde(0, c), s.E_tilde(1, c), s.E_tilde(2, c))); + col = base_to_end*col; + s.E_tilde.col(c) << Eigen::Vector3d::Map(col.torque.data), Eigen::Vector3d::Map(col.force.data); + } + } + else + { + //For all others: + //Everything should expressed in the body coordinates of segment i + segment_info& child = results[i + 1]; + //Copy PZ into a vector so we can do matrix manipulations, put torques above forces + Vector6d vPZ; + vPZ << Eigen::Vector3d::Map(child.PZ.torque.data), Eigen::Vector3d::Map(child.PZ.force.data); + Matrix6d PZDPZt; + PZDPZt.noalias() = vPZ * vPZ.transpose(); + PZDPZt /= child.D; + + //equation a) (see Vereshchagin89) PZDPZt=[I,H;H',M] + //Azamat:articulated body inertia as in Featherstone (7.19) + s.P_tilde = s.H + child.P - ArticulatedBodyInertia(PZDPZt.bottomRightCorner<3,3>(), PZDPZt.topRightCorner<3,3>(), PZDPZt.topLeftCorner<3,3>()); + //equation b) (see Vereshchagin89) + //Azamat: bias force as in Featherstone (7.20) + s.R_tilde = s.U + child.R + child.PC + (child.PZ / child.D) * child.u; + //equation c) (see Vereshchagin89) + s.E_tilde = child.E; + + //Azamat: equation (c) right side term + s.E_tilde.noalias() -= (vPZ * child.EZ.transpose()) / child.D; + + //equation d) (see Vereshchagin89) + s.M = child.M; + //Azamat: equation (d) right side term + s.M.noalias() -= (child.EZ * child.EZ.transpose()) / child.D; + + //equation e) (see Vereshchagin89) + s.G = child.G; + Twist CiZDu = child.C + (child.Z / child.D) * child.u; + Vector6d vCiZDu; + vCiZDu << Eigen::Vector3d::Map(CiZDu.rot.data), Eigen::Vector3d::Map(CiZDu.vel.data); + s.G.noalias() += child.E.transpose() * vCiZDu; + } + if (i != 0) + { + //Transform all results to joint root coordinates of segment i (== body coordinates segment i-1) + //equation a) + s.P = s.F * s.P_tilde; + //equation b) + s.R = s.F * s.R_tilde; + //equation c), in matrix: torques above forces, so switch and switch back + for (unsigned int c = 0; c < nc; c++) + { + Wrench col(Vector(s.E_tilde(3, c), s.E_tilde(4, c), s.E_tilde(5, c)), + Vector(s.E_tilde(0, c), s.E_tilde(1, c), s.E_tilde(2, c))); + col = s.F*col; + s.E.col(c) << Eigen::Vector3d::Map(col.torque.data), Eigen::Vector3d::Map(col.force.data); + } + + //needed for next recursion + s.PZ = s.P * s.Z; + + /** + * Additionally adding joint inertia to s.D, see: + * - equation a) in Vereshchagin89 + * - equation 9.28, page 188, Featherstone book 2008 + */ + if (chain.getSegment(i - 1).getJoint().getType() != Joint::Fixed) + s.D = chain.getSegment(i - 1).getJoint().getInertia() + dot(s.Z, s.PZ); + else + s.D = dot(s.Z, s.PZ); + + s.PC = s.P * s.C; + + //u=(Q-Z(R+PC)=sum of external forces along the joint axes, + //R are the forces coming from the children, + //Q is taken zero (do we need to take the previous calculated torques? + + //projection of coriolis and centrepital forces into joint subspace (0 0 Z) + s.totalBias = -dot(s.Z, s.R + s.PC); + s.u = ff_torques(j) + s.totalBias; + + //Matrix form of Z, put rotations above translations + Vector6d vZ; + vZ << Eigen::Vector3d::Map(s.Z.rot.data), Eigen::Vector3d::Map(s.Z.vel.data); + s.EZ.noalias() = s.E.transpose() * vZ; + + if (chain.getSegment(i - 1).getJoint().getType() != Joint::Fixed) + j--; + } + } +} + +void ChainHdSolver_Vereshchagin::constraint_calculation(const JntArray& beta) +{ + //equation f) nu = M_0_inverse*(beta_N - E0_tilde`*acc0 - G0) + //M_0_inverse, always nc*nc symmetric matrix + //std::cout<<"M0: "< axis torque/force + constraint_torques(j) = -dot(s.Z, constraint_force); + //The result should be the torque originating from the end-effector constraints + + // Total torque on the joint resulting from the parent forces, constraint forces and nullspace forces. + total_torques(j) = s.u + parent_forceProjection + constraint_torques(j); + // q_dotdot(j) is also equal to: total_torques(j) / s.D + + s.constAccComp = constraint_torques(j) / s.D; + s.nullspaceAccComp = s.u / s.D; + + // Total joint space acceleration resulting from accelerations of parent joints, constraint forces and nullspace forces. + q_dotdot(j) = (s.nullspaceAccComp + parentAccComp + s.constAccComp); + // Returns segment's spatial acceleration in link distal-tip coordinates. For use needs to be transformed + s.acc = s.F.Inverse(a_p + s.Z * q_dotdot(j) + s.C); + if (chain.getSegment(i - 1).getJoint().getType() != Joint::Fixed) + j++; + } +} + +// Returns Cartesian acceleration of links in robot base coordinates +void ChainHdSolver_Vereshchagin::getTransformedLinkAcceleration(Twists& x_dotdot) +{ + assert(x_dotdot.size() == ns + 1); + x_dotdot[0] = acc_root; + for (unsigned int i = 1; i < ns + 1; i++) + x_dotdot[i] = results[i].F_base.M * results[i].acc; +} + +// Returns total torque acting on each joint (constraints + nature + external forces) +void ChainHdSolver_Vereshchagin::getTotalTorque(JntArray &total_tau) +{ + assert(total_tau.data.size() == total_torques.size()); + total_tau.data = total_torques; +} + +// Returns magnitude of the constraint forces acting on the end-effector: Lagrange Multiplier +void ChainHdSolver_Vereshchagin::getContraintForceMagnitude(Eigen::VectorXd &nu_) +{ + assert(nu_.size() == nu.size()); + nu_ = nu; +} + +/* +void ChainHdSolver_Vereshchagin::getLinkCartesianPose(Frames& x_base) +{ + for (int i = 0; i < ns; i++) + { + x_base[i] = results[i + 1].F_base; + } + return; +} + +void ChainHdSolver_Vereshchagin::getLinkCartesianVelocity(Twists& xDot_base) +{ + + for (int i = 0; i < ns; i++) + { + xDot_base[i] = results[i + 1].F_base.M * results[i + 1].v; + } + return; +} + +void ChainHdSolver_Vereshchagin::getLinkCartesianAcceleration(Twists& xDotDot_base) +{ + + for (int i = 0; i < ns; i++) + { + xDotDot_base[i] = results[i + 1].F_base.M * results[i + 1].acc; + //std::cout << "XDotDot_base[i] " << xDotDot_base[i] << std::endl; + } + return; +} + +void ChainHdSolver_Vereshchagin::getLinkPose(Frames& x_local) +{ + for (int i = 0; i < ns; i++) + { + x_local[i] = results[i + 1].F; + } + return; +} + +void ChainHdSolver_Vereshchagin::getLinkVelocity(Twists& xDot_local) +{ + for (int i = 0; i < ns; i++) + { + xDot_local[i] = results[i + 1].v; + } + return; + +} + +void ChainHdSolver_Vereshchagin::getLinkAcceleration(Twists& xDotdot_local) +{ + for (int i = 0; i < ns; i++) + { + xDotdot_local[i] = results[i + 1].acc; + } + return; + +} + +void ChainHdSolver_Vereshchagin::getJointBiasAcceleration(JntArray& bias_q_dotdot) +{ + for (int i = 0; i < ns; i++) + { + //this is only force + double tmp = results[i + 1].totalBias; + //this is acceleration + bias_q_dotdot(i) = tmp / results[i + 1].D; + + //s.totalBias = - dot(s.Z, s.R + s.PC); + //std::cout << "totalBiasAccComponent" << i << ": " << bias_q_dotdot(i) << std::endl; + //bias_q_dotdot(i) = s.totalBias/s.D + + } + return; + +} + +void ChainHdSolver_Vereshchagin::getJointConstraintAcceleration(JntArray& constraint_q_dotdot) +{ + for (int i = 0; i < ns; i++) + { + constraint_q_dotdot(i) = results[i + 1].constAccComp; + //double tmp = results[i + 1].u; + //s.u = torques(j) + s.totalBias; + // std::cout << "s.constraintAccComp" << i << ": " << results[i+1].constAccComp << std::endl; + //nullspace_q_dotdot(i) = s.u/s.D + + } + return; + + +} + +//Check the name it does not seem to be appropriate + +void ChainHdSolver_Vereshchagin::getJointNullSpaceAcceleration(JntArray& nullspace_q_dotdot) +{ + for (int i = 0; i < ns; i++) + { + nullspace_q_dotdot(i) = results[i + 1].nullspaceAccComp; + //double tmp = results[i + 1].u; + //s.u = torques(j) + s.totalBias; + //std::cout << "s.nullSpaceAccComp" << i << ": " << results[i+1].nullspaceAccComp << std::endl; + //nullspace_q_dotdot(i) = s.u/s.D + + } + return; + + +} + +//This is not only a bias force energy but also includes generalized forces +//change type of parameter G +//this method should return array of G's + +void ChainHdSolver_Vereshchagin::getLinkBiasForceAcceleratoinEnergy(Eigen::VectorXd& G) +{ + for (int i = 0; i < ns; i++) + { + G = results[i + 1].G; + //double tmp = results[i + 1].u; + //s.u = torques(j) + s.totalBias; + //std::cout << "s.G " << i << ": " << results[i+1].G << std::endl; + //nullspace_q_dotdot(i) = s.u/s.D + + } + return; + +} + +//this method should return array of R's + +void ChainHdSolver_Vereshchagin::getLinkBiasForceMatrix(Wrenches& R_tilde) +{ + for (int i = 0; i < ns; i++) + { + R_tilde[i] = results[i + 1].R_tilde; + //Azamat: bias force as in Featherstone (7.20) + //s.R_tilde = s.U + child.R + child.PC + child.PZ / child.D * child.u; + std::cout << "s.R_tilde " << i << ": " << results[i + 1].R_tilde << std::endl; + } + return; +} + +*/ + +}//namespace diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainhdsolver_vereshchagin.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainhdsolver_vereshchagin.hpp new file mode 100644 index 0000000000000000000000000000000000000000..86be6471aab133f20d5e1341da3e72ee2745768e --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainhdsolver_vereshchagin.hpp @@ -0,0 +1,535 @@ +// Copyright (C) 2009 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Author: Herman Bruyninckx +// Author: Azamat Shakhimardanov +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#ifndef KDL_CHAINHDSOLVER_VERESHCHAGIN_HPP +#define KDL_CHAINHDSOLVER_VERESHCHAGIN_HPP + +#include "chainidsolver.hpp" +#include "frames.hpp" +#include "articulatedbodyinertia.hpp" + +#include + +namespace KDL +{ +/** + * \brief **Abstract**: Acceleration constrained hybrid dynamics calculations for a chain, based on Vereshchagin 1989. + * This class creates an instance of the hybrid dynamics solver. The solver analytically calculates the joint space + * constraint torques and acceleration in a chain when a constraint force(s) is applied to the chain's end-effector + * (task space / cartesian space). In the robotics literature, this algorithm is also known under the following names: + * Acceleration Constrained Hybrid Dynamics (ACHD) and Popov-Vereshchagin solver. + * + * ## INTRODUCTION + * + * In 1970', researchers [1], [2] have developed a hybrid dynamics algorithm for evaluating robot behavior + * based on the input specification that is defined by the Cartesian acceleration constraints, + * feed-forward joint torques and external Cartesian wrenches. The solver is derived from a well-known + * principle of mechanics - **Gauss' principle of least constraint** [6] and provides an analytical (closed-form) + * solution to the hybrid dynamics problem with linear-time, **O(n)** complexity [3]. + * + * In general, the Gauss' principle states that the true motion (acceleration) of a system/body is defined + * by the minimum of a quadratic function that is subject to linear geometric motion constraints [7], [6]. + * The result of this Gauss function represents the **acceleration energy** of a body, which is defined by + * the product of its mass and the squared distance between its allowed (constrained) acceleration and its free + * (unconstrained) acceleration [10], [11]. In the case of originally derived Popov-Vereshchagin algorithm [1], + * geometric motion constraints are Cartesian acceleration constraints imposed on the robot's end-effector. + * This domain-specific solver minimizes the acceleration energy by performing computational (outward and inward) + * sweeps along the robot's kinematic chain [3]. Furthermore, by computing the minimum of Gauss function, + * the Popov-Vereshchagin solver resolves the kinematic redundancy of the robot, when a partial motion (task) + * specification is provided [2]. A necessary condition that enables this type of closed-form algorithm + * (an analytical solution to the above-described optimization problem) defines that the robot’s kinematic + * chain does not consist of closed loops, i.e. the robot’s kinematic chain must be constructed in a serial + * or tree structure [11]. However, it is always possible to cut these loops and introduce explicit constrains. + * + * For evaluating robot dynamics, i.e. resolving its constrained motion, the Popov-Vereshchagin solver is + * performing three computational sweeps (recursions), along the kinematic chain [3]. More specifically, + * two sweeps in **outward** and one sweep in **inward** direction. In the case of robot dynamics algorithms, + * the outward sweep refers to a recursion that is covering a kinematic chain from proximal to distal segments, + * while the inward sweep is covering a kinematic chain from distal to proximal segments [5]. Additionally, + * after completing the recursion in the second sweep and before starting the recursion in the last sweep, + * the solver is computing magnitudes of constraint forces, i.e. the Langrage multiplier (noted as **nu** in + * the KDL's solver implementation and original solver's publication[2]). More specifically, this operation is + * performed when the algorithm reaches segment (link) **{0}**, namely the base segment. In this formulation + * of the solver, the gravity effects are taken into account by setting the base-link's acceleration equal + * to gravitational acceleration [3]. + * + * For more detailed description of the algorithm and its representation, the reader can refer to [3], [5], [11]. + * + * ## INTERFACES + * + * ### Solver's Input + * + * For computing solutions to the constrained hybrid dynamics problem, this original derivation of + * the Popov-Vereshchagin solver [3] takes into account the following inputs: + * + * * Robot's **model** defined by: kinematic parameters of the chain, segments' mass and rigid-body + * inertia, and effective inertia of each joint rotor -> **chain** parameter in solver's constructor + * + * * **Root** acceleration of the robot's base segment (usually gravitational) -> **root_acc** parameter + * in solver's constructor + * + * * Current joint configuration (angles) -> **q** parameter in the **CartToJnt** function + * + * * Current joint velocities -> **q_dot** parameter in the **CartToJnt** function + * + * * Motion drivers: + * * - **Cartesian Acceleration Constraints** imposed on the end-effector segment -> **alpha** and + * **beta** parameters in the **CartToJnt** function + * * - **Cartesian External Wrench** acting on each segment -> **f_ext** parameter in the **CartToJnt** function + * * - **Feed-Forward Torque** acting on each joint -> **ff_torques** parameter in the **CartToJnt** function + * + * The following outlines the above-listed task interfaces in more detail. + * + * #### Cartesian Acceleration Constraints: alpha & beta + * + * This first type of motion driver can be used for specifying **physical** constraints such as contacts with environment [3], + * or **artificial** (i.e. task-imposed) constraints defined by the operational space task definition for the end-effector + * (tool-tip) segment. **Note**: the Vereshchagin solver expects that the input Cartesian Acceleration Constraints, i.e. + * unit constraint forces in alpha parameters, are expressed w.r.t. robot's base frame. However, the acceleration energy + * setpoints, i.e. beta parameters, are expressed w.r.t. above-defined unit constraint forces. More specifically, each DOF + * (element) in beta parameter corresponds to its respective DOF (column) of the unit constraint force matrix (alpha) [11]. + * + * To use this interface, a user should define **i)** the active constraint directions via **alpha** parameter, which is + * a **6 x m** matrix of spatial unit constraint forces, and **ii)** acceleration energy setpoints via **beta**, which is + * a **m x 1** vector. Here, the number of constraints **m**, or in another words number of spatial unit constraint forces + * is not required to always be equal to **6**, which means that a human programmer can leave some of the degrees of freedom + * unspecified [2] for this motion driver, and still produce valid joint control commands [5]. For example, if we want to + * constrain the motion of the end-effector segment in only one direction, namely linear **x**-direction, we can define + * the constraint as [3]: + * + * **alpha** = + * | | + * | --| + * | 1 | + * | 0 | + * | 0 | + * | 0 | + * | 0 | + * | 0 | + * + * **beta** = | 0 | + * + * Note that here, the first three rows of matrix **alpha** represent linear elements and the last three rows represent + * angular elements, of the spatial unit force defined in Plücker coordinates [4]. By giving zero value to acceleration + * energy setpoint (**beta**), we are defining that the end-effector is not allowed to have linear acceleration in **x** + * direction. Or in other words, we are restricting the robot from producing any acceleration energy in that specified direction. + * + * Another example includes the specification of constraints in **5 DOFs**. We can constrain the motion of robot's end-effector + * such that it is only allowed to **freely** move in the linear **z**-direction, without performing linear motions in **x** + * and **y** and angular motions in **x**, **y** and **z** directions: + * + * **alpha** = + * | | | | | | + * | --| --| --| --| --| + * | 1 | 0 | 0 | 0 | 0 | + * | 0 | 1 | 0 | 0 | 0 | + * | 0 | 0 | 0 | 0 | 0 | + * | 0 | 0 | 1 | 0 | 0 | + * | 0 | 0 | 0 | 1 | 0 | + * | 0 | 0 | 0 | 0 | 1 | + * + * **beta** = + * | | + * | --| + * | 0 | + * | 0 | + * | 0 | + * | 0 | + * | 0 | + * + * For both above-described task examples, the Acceleration Constrained Hybrid Dynamics (ACHD) solver will compute + * valid control (constraint) joint torques, even though some of the Cartesian DOFs are left unspecified + * (e.g. in the case of the second example that would be end-effector's **z**-direction). More specifically: + * ``Underconstrained motion specifications are naturally resolved using Gauss' principle of least constraint`` [5]. + * This means that in those directions in which the robot is not constrained by the task definition, its motions will + * be controlled by the nature. For instance, in the second example, natural resolution of the robot motion would define + * that the end-effector "**falls**" in the linear **z** direction due to effects of gravity, with the assumption that + * gravity forces are acting along $z$-direction. + * + * Moreover, the motion specification in the second example is equivalent to: + * + * **alpha** = + * | | | | | | | + * | --| --| --| --| --| --| + * | 1 | 0 | 0 | 0 | 0 | 0 | + * | 0 | 1 | 0 | 0 | 0 | 0 | + * | 0 | 0 | 0 | 0 | 0 | 0 | + * | 0 | 0 | 0 | 1 | 0 | 0 | + * | 0 | 0 | 0 | 0 | 1 | 0 | + * | 0 | 0 | 0 | 0 | 0 | 1 | + * (note that elements in the third column are all zeros, meaning z-linear constraint is deactivated) + * + * **beta** = + * | | + * | --| + * | 0 | + * | 0 | + * | 0 | + * | 0 | + * | 0 | + * | 0 | + * + * The last example involves the full specification of the desired end-effector motion (in this case, + * not necessarily zero accelerations), i.e. specification of constraints in all 6 **DOFs**: + * + * **alpha** = + * | | | | | | | + * | --| --| --| --| --| --| + * | 1 | 0 | 0 | 0 | 0 | 0 | + * | 0 | 1 | 0 | 0 | 0 | 0 | + * | 0 | 0 | 1 | 0 | 0 | 0 | + * | 0 | 0 | 0 | 1 | 0 | 0 | + * | 0 | 0 | 0 | 0 | 1 | 0 | + * | 0 | 0 | 0 | 0 | 0 | 1 | + * + * **beta** = **alpha^T * X_dotdot_N** + * + * Here, **N** stands for the index of the last robot's segment, end-effector (tool-tip). The reader should note + * that we can directly assign values (magnitudes) of the desired (task-defined) spatial acceleration **6 x 1** + * vector **X_dotdot_N** to the **6 x 1** vector of acceleration energy (**beta**) [3]. Even though physical + * dimensions (units) of these two vectors are not the same, the property of matrix **alpha** (it contains + * **unit** vectors), permits that we can assign values of desired accelerations to acceleration energy setpoints, + * in respective directions. Namely, each column of matrix **alpha** has the value of **1** in the respective + * direction in which constraint force works, thus it follows that the value of acceleration energy setpoint is + * the same as the value of Cartesian acceleration, in the respective direction. + * + * #### External Forces: f_ext + * + * This type of driver can be used for specifying **physical** (but not artificial, i.e. not task-introduced) + * Cartesian wrenches acting on each of the robot's segments [11]. Examples for a **physical** force on a segment can be: + * **i)** a known weight at the robot's gripper, for instance, a grasped cup or **ii)** a force from a human pushing + * the robot [5]. Note that the implementation of Vereshchagin solver in KDL expects the provided **f_ext** is + * expressed w.r.t. robot's base frame, which is in contrast to the case of KDL's RNE solver. + * + * ### Feed-Forward Joint Torques: ff_torque + * + * This type of motion driver can be used for specifying **physical** (but not artificial, i.e. not task-introduced) + * joint torques, for example, spring and/or damper-based torques (e.g. friction effects) in robot's joints [11]. + * + * Additional examples on using these input interfaces can be found in "../tests/solvertest.cpp": + * + * * VereshchaginTest() function - an example on how to use all interfaces of this solver for computing + * the solution to the Hybrid Dynamics (HD) problem. + * + * * FdAndVereshchaginSolversConsistencyTest() function - an example on how to only use this solver for + * computing the solution to the Featherstone's (i.e. Articulated Body Algorithm (ABA)) version of + * the Forward Dynamics (FD) problem. + * + * ### Solver's Output + * + * This recursive dynamics solver is computing several quantities that represent solutions to both, inverse and + * forward dynamics problems, or in other words solutions to the constrained hybrid dynamics problem. + * More specifically, the output interface of the original Popov-Vereshchagin algorithm consists of [3], [5]: + * + * * Magnitudes of constraint forces that act on the end-effector, denoted by the Lagrange multiplier **nu** + * in the solver's implementation and original solver's publication[2]. + * + * * Joint constraint torques required for achieving the desired (acceleration-constraints-defined) behavior of + * the robot: **constraint_torque**. These torques represent control commands that should be sent to robot's joint drivers. + * + * * Argument that defines the solution to the originally formulated optimization problem in **Gauss' principle**. + * In other words, the joint accelerations **q_dotdot** resulting from the total torque acting on each joint (**total_torque**), + * i.e. from the aforementioned constraint torques and all natural and external forces acting on the system. + * + * * The resulting and complete spatial accelerations of each segment in the kinematic chain: **X_dotdot** + * + * Furthermore, if necessary, a complete spatial vector of imposed constraint forces can be computed [3], + * from the following relation: **alpha * nu**. + * + * The reader should note that this **constraint_torque** is the **necessary** control command that a user is supposed + * to send to robot's joints, to achieve the motion that is computed (resolved) by the Popov-Vereshchagin solver. + * More specifically, here **constraint_torque** represent solution to the **Inverse Dynamics (ID)** problem. + * Nevertheless, the reason why a user is not supposed to use the **total_torque** values as the control commands + * for robot's joints, is the fact that the torque contributions that represent the difference between **total_torque** + * and **constraint_torque** already exist (act) on robot joints. More specifically, these **additional (residual)** + * contributions are produced on the joints by the already existing natural forces that act on the system [11]. + * + * On the other side, joint accelerations, namely **q_dotdot** provide solution to the **Forward Dynamics (FD)** problem + * and these quantities can be used for both control (integrate to joint positions/velocities) and simulation purposes [11]. + * + * ## PRACTICAL INSIGHTS/CONSIDERATIONS + * + * The Popov-Vereshchagin hybrid dynamics solver enables a user to achieve many types of operational space tasks [11]. + * In other words, various controllers can be implemented **around** the aforementioned interfaces of the algorithm. + * Examples can be controllers for hybrid force/position control, impedance control, etc. However, there are some practical + * insights about this algorithm that need to taken into account. + * + * ### Prioritizations between motion drivers (interfaces) + * + * The original derivation of this solver, which is considered in this library, prioritizes Cartesian acceleration constrains + * (specified for the end-effector segment) over other two motion drivers (Cartesian external wrenches and feedforward joint torques) [11]. + * In practice, this means the following: + * + * * If the external wrenches and/or feedforward joint torques contribute positively (i.e. assist) in producing Cartesian accelerations + * (specified via acceleration constraint interface), the Vereshchagin solver will take advantage of these forces to compute + * (acceleration-) energy optimal motions. + * + * * On the other hand, if the aforementioned external wrenches and/or feedforward joint torques contribute negatively (i.e. interfere) + * the Cartesian accelerations, the Vereshchagin solver will compensate all of those forces to correctly produce constrained + * accelerations of the end-effector. More specifically, additional torque commands will be computed under + * **constrained joint torques** (**ctrl_torques** in this implementation), to overcome those "disturbances". + * + * Nevertheless, the above-described prioritization can be changed (see [3] & [5] for more details) but those features are not implemented in KDL. + * + * ### Using the algorithm for solving forward dynamics (FD) problem + * + * The reader should note that the Popov-Vereshchagin solver represents an extension to the well-known forward-dynamics + * Articulated Body Algorithm (ABA) developed by Featherstone and described in [4] (moreover, Featherstone mentioned Vereshchagin solver + * in his book [4], page 117). This means that the Popov-Vereshchagin solver can also be purely used as this Articulated Body Algorithm + * forward-dynamics algorithm. In that case, it is necessary for the user to deactivate all Cartesian acceleration constraints (it is sufficient + * to set all elements in **alpha** matrix to zero) and proceed using other two interfaces as in the case of standard FD solver. More specifically, + * use **f_ext** input to define **physical** external wrenches acting on the robot's body (should be expressed w.r.t. robot's base frame) and + * **ff_torque** input to define command torques acting in robot's joints. The resulting robot's motion can be taken from **q_dotdot** and + * **X_dotdot** solver's outputs. + * + * Nevertheless, the Popov-Vereshchagin solver can also be used for solving more advanced forward dynamics problems, than those solved by ABA [4]. + * More specifically, if this solver is used in a certain simulation environment for the use-case of simulating robot behaviors, all three + * interfaces can be exploited for defining a more descriptive robot's state. Here, a user can exploit the Cartesian acceleration constraint + * interface to specify different constraints imposed on the end-effector, along with other interfaces, and simulate what would be the robot's + * behavior due to these constraints and environmental impacts. Here, the resulting robot's motion can, as well, be taken from **q_dotdot** and + * **X_dotdot** solver's outputs. For example, MuJoCo framework (see MuJoCo's documentation) also uses Gauss' principle of least constraint to + * simulate constraint forces in certain situations, however, there, final derivation of this principle in the software is different. However, in + * the case of this more advanced forward dynamics computations, the user needs to be aware of prioritizations between input interfaces + * (mentioned in "Prioritizations" section above) and internal policies on + * handling singularities (mentioned in "Singularities and matrix inversions" section below). + * + * ### Singularities and matrix inversions + * + * To find the minimal-energy solution to the Inverse Dynamics (ID) problem, i.e. find the Langrage multiplier **nu**, the solver needs to compute + * the inverse of a so-called "acceleration constraint coupling matrix"[3] in the balance equation before starting the third sweep. + * However, the robot's configuration has a direct impact on this matrix and its inversion. Namely, if the robot is in a singular configuration for + * the task specified via acceleration constraints, this matrix will become rank-deficient. This means that it is not possible to find a feasible + * solution for that particular end-effector's DOF that is (or DOFs that are) lost due to the singular configuration. In other words, it is not + * possible to find **constraint torques** that will satisfy imposed acceleration constraints in that DOF/DOFs. Nevertheless, in this situation, + * it is still possible to find the (energy-optimal) solution for other "non-singular" DOFs. For that reason, in KDL's implementation of the solver, + * matrix inverse is found by using the SVD technique to construct a pseudo inverse. Additionally, in this implementation, a control policy is + * introduced via the truncated-SVD method to deactivate, i.e. more specifically **ignore**, acceleration constraints for the DOFs that are lost due + * to robot's singular configuration. Of course, this is a choice (control policy), i.e. only one option for solving the singularity problem and + * producing safe joint commands. It is left for the user to explore other control policies (options) for this particular problem if of course, + * the user is not satisfied with the current control policy. + * + * ### Supported robot models + * + * KDL's current implementation of the Vereshchagin HD solver supports only robot chains that have equal number of joints and segments. + * Moreover, this implementation can only compute dynamics for **serial** type of chains, i.e. currently, **tree** robot structures are not supported + * in this solver. Nevertheless, the original solver's derivation has been extended in [3] to account for multiple motion constraints imposed + * on a **tree** robot structure. This extension does not only account for acceleration constraints imposed on multiple end-effectors but also for + * acceleration constraints imposed on more proximal segments. However, the above-mentioned extensions are currently not implemented in this version of KDL. + * + * ## REFERENCES + * + * [1] E. P. Popov, A. F. Vereshchagin, and S. L. Zenkevich, "Manipulyatsionnye roboty: Dinamika i algoritmy", Nauka, Moscow, 1978. + * + * [2] A. F. Vereshchagin, “Modelling and control of motion of manipulation robots”, Soviet Journal of Computer and Systems Sciences, vol. 27, pp. 29–38, 1989. + * + * [3] A. Shakhimardanov, “Composable robot motion stack: Implementing constrained hybrid dynamics using semantic models of kinematic chains”, PhD thesis, KU Leuven, 2015. + * + * [4] R. Featherstone, Rigid body dynamics algorithms. Springer, 2008. + * + * [5] S. Schneider and H. Bruyninckx, “Exploiting linearity in dynamics solvers for the design of composable robotic manipulation architectures”, in IEEE/RSJ International Conference on Intelligent Robots and Systems (IROS), 2019. + * + * [6] H. Bruyninckx and O. Khatib, "Gauss’ principle and the dynamics of redundant and constrained manipulators", in IEEE International Conference on Robotics and Automation, 2000. + * + * [7] C. F. Gauß, "Über ein neues allgemeines Grundgesetz der Mechanik.", Journal für die reine und angewandte Mathematik, vol. 4, pp. 232–235, 1829. + * + * [8] A. F. Vereshchagin, “Computer simulation of the dynamics of complicated mechanisms of robot-manipulators”, Engineering Cybernetics, 12(6), pp. 65–70, 1974. + * + * [9] E. P. Popov, "Control of robots-manipulators", Engineering Cybernetics, 1974. + * + * [10] E. Ramm, “Principles of least action and of least constraint”, GAMM-Mitteilungen, vol. 34, pp. 164–182, 2011. + * + * [11] D. Vukcevic, "Lazy Robot Control by Relaxation of Motion and Force Constraints." Technical Report/Hochschule Bonn-Rhein-Sieg University of Applied Sciences, Department of Computer Science, 2020. + * + * @ingroup KinematicFamily + */ + +class ChainHdSolver_Vereshchagin : KDL::SolverI +{ + typedef std::vector Twists; + typedef std::vector Frames; + typedef Eigen::Matrix Vector6d; + typedef Eigen::Matrix Matrix6d; + typedef Eigen::Matrix Matrix6Xd; + +public: + /** + * Constructor for the solver, it will allocate all the necessary memory + * \param chain The kinematic chain to calculate the hybrid dynamics for. An internal copy will be made. + * \param root_acc The acceleration twist of the root segment to use during the calculation (usually contains gravity). + * Note: This solver takes gravity acceleration with opposite sign comparead to the KDL's FD and RNE solvers + * \param nc Number of constraints imposed on the robot's end-effector (maximum is 6). + */ + ChainHdSolver_Vereshchagin(const Chain& chain, const Twist &root_acc, const unsigned int nc); + + ~ChainHdSolver_Vereshchagin() + { + }; + + /** + * This method calculates joint space constraint torques and accelerations. + * It returns 0 when it succeeds, otherwise -1 or -2 for nonmatching matrix and array sizes. + * Input parameters: + * \param q The current joint positions + * \param q_dot The current joint velocities + * \param alpha The active constraint directions (unit constraint forces expressed w.r.t. robot's base frame) + * \param beta The acceleration energy setpoints (expressed w.r.t. above-defined unit constraint forces) + * \param f_ext The external forces (no gravity, it is given in root acceleration) on the segments + * \param ff_torques The feed-forward joint space torques + * + * Output parameters: + * \param q_dotdot The resulting joint accelerations + * \param constraint_torques The resulting joint constraint torques (what each joint feels due to the constraint forces acting on the end-effector) + * + * @return error/success code + */ + int CartToJnt(const JntArray &q, const JntArray &q_dot, JntArray &q_dotdot, const Jacobian& alfa, const JntArray& beta, const Wrenches& f_ext, const JntArray &ff_torques, JntArray &constraint_torques); + + /// @copydoc KDL::SolverI::updateInternalDataStructures + virtual void updateInternalDataStructures(); + + //Returns cartesian acceleration of links in base coordinates + void getTransformedLinkAcceleration(Twists& x_dotdot); + + // Returns total torque acting on each joint (constraints + nature + external forces) + void getTotalTorque(JntArray &total_tau); + + // Returns magnitude of the constraint forces acting on the end-effector: Lagrange Multiplier + void getContraintForceMagnitude(Eigen::VectorXd &nu_); + + /* + //Returns cartesian positions of links in base coordinates + void getLinkCartesianPose(Frames& x_base); + //Returns cartesian velocities of links in base coordinates + void getLinkCartesianVelocity(Twists& xDot_base); + //Returns cartesian acceleration of links in base coordinates + void getLinkCartesianAcceleration(Twists& xDotDot_base); + //Returns cartesian positions of links in link tip coordinates + void getLinkPose(Frames& x_local); + //Returns cartesian velocities of links in link tip coordinates + void getLinkVelocity(Twists& xDot_local); + //Returns cartesian acceleration of links in link tip coordinates + void getLinkAcceleration(Twists& xDotdot_local); + //Acceleration energy due to unit constraint forces at the end-effector + void getLinkUnitForceAccelerationEnergy(Eigen::MatrixXd& M); + //Acceleration energy due to arm configuration: bias force plus input joint torques + void getLinkBiasForceAcceleratoinEnergy(Eigen::VectorXd& G); + + void getLinkUnitForceMatrix(Matrix6Xd& E_tilde); + + void getLinkBiasForceMatrix(Wrenches& R_tilde); + + void getJointBiasAcceleration(JntArray &bias_q_dotdot); + */ +private: + /** + * This method calculates all cartesian space poses, twists, bias accelerations. + * External forces are also taken into account in this outward sweep. + */ + void initial_upwards_sweep(const JntArray &q, const JntArray &q_dot, const JntArray &q_dotdot, const Wrenches& f_ext); + /** + * This method is a force balance sweep. It calculates articulated body inertias and bias forces. + * Additionally, acceleration energies generated by bias forces and unit forces are calculated here. + */ + void downwards_sweep(const Jacobian& alfa, const JntArray& ff_torques); + /** + * This method calculates constraint force magnitudes. + * + */ + void constraint_calculation(const JntArray& beta); + /** + * This method puts all acceleration contributions (constraint, bias, nullspace and parent accelerations) together. + * + */ + void final_upwards_sweep(JntArray &q_dotdot, JntArray &constraint_torques); + +private: + const Chain& chain; + unsigned int nj; + unsigned int ns; + unsigned int nc; + Twist acc_root; + Jacobian alfa_N; + Jacobian alfa_N2; + Eigen::MatrixXd M_0_inverse; + Eigen::MatrixXd Um; + Eigen::MatrixXd Vm; + JntArray beta_N; + Eigen::VectorXd nu; + Eigen::VectorXd nu_sum; + Eigen::VectorXd Sm; + Eigen::VectorXd tmpm; + Eigen::VectorXd total_torques; // all the contributions that are felt at the joint: constraints + nature + external forces + Wrench qdotdot_sum; + Frame F_total; + + struct segment_info + { + Frame F; //local pose with respect to previous link in segments coordinates + Frame F_base; // pose of a segment in root coordinates + Twist Z; //Unit twist + Twist v; //twist + Twist acc; //acceleration twist + Wrench U; //wrench p of the bias forces (in cartesian space) + Wrench R; //wrench p of the bias forces + Wrench R_tilde; //vector of wrench p of the bias forces (new) in matrix form + Twist C; //constraint + Twist A; //constraint + ArticulatedBodyInertia H; //I (expressed in 6*6 matrix) + ArticulatedBodyInertia P; //I (expressed in 6*6 matrix) + ArticulatedBodyInertia P_tilde; //I (expressed in 6*6 matrix) + Wrench PZ; //vector U[i] = I_A[i]*S[i] + Wrench PC; //vector E[i] = I_A[i]*c[i] + double D; //vector D[i] = S[i]^T*U[i] + Matrix6Xd E; //matrix with virtual unit constraint force due to acceleration constraints + Matrix6Xd E_tilde; + Eigen::MatrixXd M; //acceleration energy already generated at link i + Eigen::VectorXd G; //magnitude of the constraint forces already generated at link i + Eigen::VectorXd EZ; //K[i] = Etiltde'*Z + double nullspaceAccComp; //Azamat: constribution of joint space u[i] forces to joint space acceleration + double constAccComp; //Azamat: constribution of joint space constraint forces to joint space acceleration + double biasAccComp; //Azamat: constribution of joint space bias forces to joint space acceleration + double totalBias; //Azamat: R+PC (centrepital+coriolis) in joint subspace + double u; //vector u[i] = torques(i) - S[i]^T*(p_A[i] + I_A[i]*C[i]) in joint subspace. Azamat: In code u[i] = torques(i) - s[i].totalBias + + segment_info(unsigned int nc): + D(0),nullspaceAccComp(0),constAccComp(0),biasAccComp(0),totalBias(0),u(0) + { + E.resize(6, nc); + E_tilde.resize(6, nc); + G.resize(nc); + M.resize(nc, nc); + EZ.resize(nc); + E.setZero(); + E_tilde.setZero(); + M.setZero(); + G.setZero(); + EZ.setZero(); + }; + }; + + std::vector > results; + +}; +} + +#endif // KDL_CHAINHDSOLVER_VERESHCHAGIN_HPP diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainidsolver.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainidsolver.hpp new file mode 100644 index 0000000000000000000000000000000000000000..fe489185c6c32b430d99b2e7a5899dd56f374bc4 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainidsolver.hpp @@ -0,0 +1,62 @@ +// Copyright (C) 2009 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#ifndef KDL_CHAIN_IDSOLVER_HPP +#define KDL_CHAIN_IDSOLVER_HPP + +#include "chain.hpp" +#include "frames.hpp" +#include "jntarray.hpp" +#include "solveri.hpp" + +namespace KDL +{ + + typedef std::vector Wrenches; + + /** + * \brief This abstract class encapsulates the inverse + * dynamics solver for a KDL::Chain. + * + */ + class ChainIdSolver : public KDL::SolverI + { + public: + /** + * Calculate inverse dynamics, from joint positions, velocity, acceleration, external forces + * to joint torques/forces. + * + * @param q input joint positions + * @param q_dot input joint velocities + * @param q_dotdot input joint accelerations + * + * @param torque output joint torques + * + * @return if < 0 something went wrong + */ + virtual int CartToJnt(const JntArray &q, const JntArray &q_dot, const JntArray &q_dotdot, const Wrenches& f_ext,JntArray &torques)=0; + + // Need functions to return the manipulator mass, coriolis and gravity matrices - Lagrangian Formulation. + }; + +} + +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainidsolver_recursive_newton_euler.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainidsolver_recursive_newton_euler.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b93a59ce5ed6bacb37838c6b8ba1b37f684f5df9 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainidsolver_recursive_newton_euler.cpp @@ -0,0 +1,100 @@ +// Copyright (C) 2009 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#include "chainidsolver_recursive_newton_euler.hpp" +#include "frames_io.hpp" + +namespace KDL{ + + ChainIdSolver_RNE::ChainIdSolver_RNE(const Chain& chain_,Vector grav): + chain(chain_),nj(chain.getNrOfJoints()),ns(chain.getNrOfSegments()), + X(ns),S(ns),v(ns),a(ns),f(ns) + { + ag=-Twist(grav,Vector::Zero()); + } + + void ChainIdSolver_RNE::updateInternalDataStructures() { + nj = chain.getNrOfJoints(); + ns = chain.getNrOfSegments(); + X.resize(ns); + S.resize(ns); + v.resize(ns); + a.resize(ns); + f.resize(ns); + } + + int ChainIdSolver_RNE::CartToJnt(const JntArray &q, const JntArray &q_dot, const JntArray &q_dotdot, const Wrenches& f_ext,JntArray &torques) + { + if(nj != chain.getNrOfJoints() || ns != chain.getNrOfSegments()) + return (error = E_NOT_UP_TO_DATE); + + //Check sizes when in debug mode + if(q.rows()!=nj || q_dot.rows()!=nj || q_dotdot.rows()!=nj || torques.rows()!=nj || f_ext.size()!=ns) + return (error = E_SIZE_MISMATCH); + unsigned int j=0; + + //Sweep from root to leaf + for(unsigned int i=0;i=0;i--){ + if(chain.getSegment(i).getJoint().getType()!=Joint::Fixed) { + torques(j)=dot(S[i],f[i]); + torques(j)+=chain.getSegment(i).getJoint().getInertia()*q_dotdot(j); // add torque from joint inertia + --j; + } + if(i!=0) + f[i-1]=f[i-1]+X[i]*f[i]; + } + return (error = E_NOERROR); + } +}//namespace diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainidsolver_recursive_newton_euler.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainidsolver_recursive_newton_euler.hpp new file mode 100644 index 0000000000000000000000000000000000000000..d28ef0e7aec1351dc071ec2172723b9d44ad0055 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainidsolver_recursive_newton_euler.hpp @@ -0,0 +1,78 @@ +// Copyright (C) 2009 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#ifndef KDL_CHAIN_IKSOLVER_RECURSIVE_NEWTON_EULER_HPP +#define KDL_CHAIN_IKSOLVER_RECURSIVE_NEWTON_EULER_HPP + +#include "chainidsolver.hpp" + +namespace KDL{ + /** + * \brief Recursive newton euler inverse dynamics solver + * + * The algorithm implementation is based on the book "Rigid Body + * Dynamics Algorithms" of Roy Featherstone, 2008 + * (ISBN:978-0-387-74314-1) See page 96 for the pseudo-code. + * + * It calculates the torques for the joints, given the motion of + * the joints (q,qdot,qdotdot), external forces on the segments + * (expressed in the segments reference frame) and the dynamical + * parameters of the segments. + */ + class ChainIdSolver_RNE : public ChainIdSolver{ + public: + /** + * Constructor for the solver, it will allocate all the necessary memory + * \param chain The kinematic chain to calculate the inverse dynamics for, an internal copy will be made. + * \param grav The gravity vector to use during the calculation. + */ + ChainIdSolver_RNE(const Chain& chain,Vector grav); + ~ChainIdSolver_RNE(){}; + + /** + * Function to calculate from Cartesian forces to joint torques. + * Input parameters; + * \param q The current joint positions + * \param q_dot The current joint velocities + * \param q_dotdot The current joint accelerations + * \param f_ext The external forces (no gravity) on the segments + * Output parameters: + * \param torques the resulting torques for the joints + */ + int CartToJnt(const JntArray &q, const JntArray &q_dot, const JntArray &q_dotdot, const Wrenches& f_ext,JntArray &torques); + + /// @copydoc KDL::SolverI::updateInternalDataStructures + virtual void updateInternalDataStructures(); + + private: + const Chain& chain; + unsigned int nj; + unsigned int ns; + std::vector X; + std::vector S; + std::vector v; + std::vector a; + std::vector f; + Twist ag; + }; +} + +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainidsolver_vereshchagin.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainidsolver_vereshchagin.cpp new file mode 100644 index 0000000000000000000000000000000000000000..31c8f1bae18524cf3b5c1e5591494e018c3e0686 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainidsolver_vereshchagin.cpp @@ -0,0 +1,36 @@ +// Copyright (C) 2009 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Author: Herman Bruyninckx +// Author: Azamat Shakhimardanov +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#include "chainidsolver_vereshchagin.hpp" + +#include + +namespace KDL +{ + +ChainIdSolver_Vereshchagin::ChainIdSolver_Vereshchagin(const Chain& chain_, const Twist &root_acc, const unsigned int nc_) : ChainHdSolver_Vereshchagin(chain_, root_acc, nc_) +{ + std::cout << "ChainIdSolver_Vereshchagin is renamed to ChainHdSolver_Vereshchagin in version 1.5. The old name will be dropped in version 1.6" << std::endl; +} + +} diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainidsolver_vereshchagin.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainidsolver_vereshchagin.hpp new file mode 100644 index 0000000000000000000000000000000000000000..0f5d736f438251b591a68fd2d2c3096599386824 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainidsolver_vereshchagin.hpp @@ -0,0 +1,40 @@ +// Copyright (C) 2009 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Author: Herman Bruyninckx +// Author: Azamat Shakhimardanov +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#ifndef KDL_CHAINIDSOLVER_VERESHCHAGIN_HPP +#define KDL_CHAINIDSOLVER_VERESHCHAGIN_HPP + +#include "chainhdsolver_vereshchagin.hpp" + +namespace KDL +{ + +class ChainIdSolver_Vereshchagin : public ChainHdSolver_Vereshchagin +{ +public: + ChainIdSolver_Vereshchagin(const Chain& chain, const Twist &root_acc, const unsigned int nc); +}; + +} + +#endif // KDL_CHAINIDSOLVER_VERESHCHAGIN_HPP diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolver.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolver.hpp new file mode 100644 index 0000000000000000000000000000000000000000..0494778ebd887b2ce66afae3f4975434ffb2afa5 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolver.hpp @@ -0,0 +1,169 @@ +// Copyright (C) 2007 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#ifndef KDL_CHAIN_IKSOLVER_HPP +#define KDL_CHAIN_IKSOLVER_HPP + +#include "chain.hpp" +#include "frames.hpp" +#include "framevel.hpp" +#include "frameacc.hpp" +#include "jntarray.hpp" +#include "jntarrayvel.hpp" +#include "jntarrayacc.hpp" +#include "solveri.hpp" + +namespace KDL { + + /** + * \brief This abstract class encapsulates the inverse + * position solver for a KDL::Chain. + * + * @ingroup KinematicFamily + */ + class ChainIkSolverPos : public KDL::SolverI { + public: + /** + * Calculate inverse position kinematics, from cartesian + *coordinates to joint coordinates. + * + * @param q_init initial guess of the joint coordinates + * @param p_in input cartesian coordinates + * @param q_out output joint coordinates + * + * @return if < 0 something went wrong + */ + virtual int CartToJnt(const JntArray& q_init, const Frame& p_in, JntArray& q_out)=0; + + virtual ~ChainIkSolverPos(){}; + virtual void updateInternalDataStructures()=0; + }; + + /** + * \brief This abstract class encapsulates the inverse + * velocity solver for a KDL::Chain. + * + * @ingroup KinematicFamily + */ + class ChainIkSolverVel : public KDL::SolverI { + public: + /** + * Calculate inverse velocity kinematics, from joint positions + *and cartesian velocity to joint velocities. + * + * @param q_in input joint positions + * @param v_in input cartesian velocity + * @param qdot_out output joint velocities + * + * @return if < 0 something went wrong + */ + virtual int CartToJnt(const JntArray& q_in, const Twist& v_in, JntArray& qdot_out)=0; + /** + * Calculate inverse position and velocity kinematics, from + *cartesian position and velocity to joint positions and velocities. + * + * @param q_init initial joint positions + * @param v_in input cartesian position and velocity + * @param q_out output joint position and velocity + * + * @return if < 0 something went wrong + */ + virtual int CartToJnt(const JntArray& q_init, const FrameVel& v_in, JntArrayVel& q_out)=0; + + virtual ~ChainIkSolverVel(){}; + virtual void updateInternalDataStructures()=0; + }; + + /** + * \brief This abstract class encapsulates the inverse + * acceleration solver for a KDL::Chain. + * + * @ingroup KinematicFamily + */ + + class ChainIkSolverAcc : public KDL::SolverI { + public: + /** + * Calculate inverse acceleration kinematics from joint + * positions, joint velocities and cartesian acceleration to joint accelerations. + * + * @param q_in input joint positions + * @param qdot_in input joint velocities + * @param a_in input cartesian acceleration + * @param qdotdot_out output joint accelerations + * + * @return if < 0 something went wrong + */ + virtual int CartToJnt(const JntArray& q_in, const JntArray& qdot_in, const Twist a_in, + JntArray& qdotdot_out)=0; + /** + * Calculate inverse position, velocity and acceration + *kinematics from cartesian coordinates to joint coordinates + * + * @param q_init initial guess for joint positions + * @param a_in input cartesian position, velocity and acceleration + * @param q_out output joint position, velocity and acceleration + * + * @return if < 0 something went wrong + */ + virtual int CartTojnt(const JntArray& q_init, const FrameAcc& a_in, + JntArrayAcc& q_out)=0; + + /** + * Calculate inverse velocity and acceleration kinematics from + * joint positions and cartesian velocity and acceleration to + * joint velocities and accelerations. + * + * @param q_in input joint positions + * @param v_in input cartesian velocity + * @param a_in input cartesian acceleration + * @param qdot_out output joint velocities + * @param qdotdot_out output joint accelerations + * + * @return if < 0 something went wrong + */ + virtual int CartToJnt(const JntArray& q_in, const Twist& v_in, const Twist& a_in, + JntArray& qdot_out, JntArray& qdotdot_out)=0; + /** + * Calculate inverse position and acceleration kinematics from + *joint velocities and cartesian position and acceleration to + *joint positions and accelerations + * + * @param q_init initial guess for joint positions + * @param p_in input cartesian position + * @param qdot_in input joint velocities + * @param a_in input cartesian acceleration + * @param q_out output joint positions + * @param qdotdot_out output joint accelerations + * + * @return if < 0 something went wrong + */ + virtual int CartTojnt(const JntArray& q_init, const Frame& p_in, const JntArray& qdot_in, const Twist& a_in, + JntArray& q_out, JntArray& qdotdot_out)=0; + + virtual void updateInternalDataStructures()=0; + virtual ~ChainIkSolverAcc(){}; + }; + + +}//end of namespace KDL + +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolverpos_lma.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolverpos_lma.cpp new file mode 100644 index 0000000000000000000000000000000000000000..71d2ab5e2c249d8dc89a516622f7da5e1ce1fae9 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolverpos_lma.cpp @@ -0,0 +1,324 @@ +/** + \file chainiksolverpos_lma.cpp + \brief computing inverse position kinematics using Levenberg-Marquardt. +*/ + +/************************************************************************** + begin : May 2012 + copyright : (C) 2012 Erwin Aertbelien + email : firstname.lastname@mech.kuleuven.ac.be + + History (only major changes)( AUTHOR-Description ) : + + *************************************************************************** + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; if not, write to the Free Software * + * Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307 USA * + * * + ***************************************************************************/ + +#include "chainiksolverpos_lma.hpp" +#include + +namespace KDL { + + + + +template +inline void Twist_to_Eigen(const KDL::Twist& t,Eigen::MatrixBase& e) { + e(0)=t.vel.data[0]; + e(1)=t.vel.data[1]; + e(2)=t.vel.data[2]; + e(3)=t.rot.data[0]; + e(4)=t.rot.data[1]; + e(5)=t.rot.data[2]; +} + + +ChainIkSolverPos_LMA::ChainIkSolverPos_LMA( + const KDL::Chain& _chain, + const Eigen::Matrix& _l, + double _eps, + int _maxiter, + double _eps_joints +) : + chain(_chain), + nj(chain.getNrOfJoints()), + ns(chain.getNrOfSegments()), + lastNrOfIter(0), + lastDifference(0), + lastTransDiff(0), + lastRotDiff(0), + lastSV(nj), + jac(6, nj), + grad(nj), + display_information(false), + maxiter(_maxiter), + eps(_eps), + eps_joints(_eps_joints), + L(_l.cast()), + T_base_jointroot(nj), + T_base_jointtip(nj), + q(nj), + A(nj, nj), + tmp(nj), + ldlt(nj), + svd(6, nj,Eigen::ComputeThinU | Eigen::ComputeThinV), + diffq(nj), + q_new(nj), + original_Aii(nj) +{} + +ChainIkSolverPos_LMA::ChainIkSolverPos_LMA( + const KDL::Chain& _chain, + double _eps, + int _maxiter, + double _eps_joints +) : + chain(_chain), + nj(chain.getNrOfJoints()), + ns(chain.getNrOfSegments()), + lastNrOfIter(0), + lastDifference(0), + lastTransDiff(0), + lastRotDiff(0), + lastSV(nj>6?6:nj), + jac(6, nj), + grad(nj), + display_information(false), + maxiter(_maxiter), + eps(_eps), + eps_joints(_eps_joints), + T_base_jointroot(nj), + T_base_jointtip(nj), + q(nj), + A(nj, nj), + ldlt(nj), + svd(6, nj,Eigen::ComputeThinU | Eigen::ComputeThinV), + diffq(nj), + q_new(nj), + original_Aii(nj) +{ + L(0)=1; + L(1)=1; + L(2)=1; + L(3)=0.01; + L(4)=0.01; + L(5)=0.01; +} + +void ChainIkSolverPos_LMA::updateInternalDataStructures() { + nj = chain.getNrOfJoints(); + ns = chain.getNrOfSegments(); + lastSV.conservativeResize(nj>6?6:nj); + jac.conservativeResize(Eigen::NoChange, nj); + grad.conservativeResize(nj); + T_base_jointroot.resize(nj); + T_base_jointtip.resize(nj); + q.conservativeResize(nj); + A.conservativeResize(nj, nj); + ldlt = Eigen::LDLT(nj); + svd = Eigen::JacobiSVD(6, nj,Eigen::ComputeThinU | Eigen::ComputeThinV); + diffq.conservativeResize(nj); + q_new.conservativeResize(nj); + original_Aii.conservativeResize(nj); +} + +ChainIkSolverPos_LMA::~ChainIkSolverPos_LMA() {} + +void ChainIkSolverPos_LMA::compute_fwdpos(const VectorXq& q) { + using namespace KDL; + unsigned int jointndx=0; + T_base_head = Frame::Identity(); // frame w.r.t. base of head + for (unsigned int i=0;i(); + compute_fwdpos(q); + compute_jacobian(q); + svd.compute(jac); + std::cout << "Singular values : " << svd.singularValues().transpose()<<"\n"; +} + + +int ChainIkSolverPos_LMA::CartToJnt(const KDL::JntArray& q_init, const KDL::Frame& T_base_goal, KDL::JntArray& q_out) { + if (nj != chain.getNrOfJoints()) + return (error = E_NOT_UP_TO_DATE); + + if (nj != q_init.rows() || nj != q_out.rows()) + return (error = E_SIZE_MISMATCH); + + using namespace KDL; + double v = 2; + double tau = 10; + double rho; + double lambda; + Twist t; + double delta_pos_norm; + Eigen::Matrix delta_pos; + Eigen::Matrix delta_pos_new; + + + q=q_init.data.cast(); + compute_fwdpos(q); + Twist_to_Eigen( diff( T_base_head, T_base_goal), delta_pos ); + delta_pos=L.asDiagonal()*delta_pos; + delta_pos_norm = delta_pos.norm(); + if (delta_pos_norm(); + return (error = E_NOERROR); + } + compute_jacobian(q); + jac = L.asDiagonal()*jac; + + lambda = tau; + double dnorm = 1; + for (unsigned int i=0;i(); + if (dnorm < eps_joints) { + lastDifference = delta_pos_norm; + lastNrOfIter = i; + lastSV = svd.singularValues(); + q_out.data = q.cast(); + compute_fwdpos(q); + Twist_to_Eigen( diff( T_base_head, T_base_goal), delta_pos ); + lastTransDiff = delta_pos.topRows(3).norm(); + lastRotDiff = delta_pos.bottomRows(3).norm(); + return (error = E_INCREMENT_JOINTS_TOO_SMALL); + } + + + if (grad.transpose()*grad < eps_joints*eps_joints ) { + compute_fwdpos(q); + Twist_to_Eigen( diff( T_base_head, T_base_goal), delta_pos ); + lastDifference = delta_pos_norm; + lastTransDiff = delta_pos.topRows(3).norm(); + lastRotDiff = delta_pos.bottomRows(3).norm(); + lastSV = svd.singularValues(); + lastNrOfIter = i; + q_out.data = q.cast(); + return (error = E_GRADIENT_JOINTS_TOO_SMALL); + } + + q_new = q+diffq; + compute_fwdpos(q_new); + Twist_to_Eigen( diff( T_base_head, T_base_goal), delta_pos_new ); + delta_pos_new = L.asDiagonal()*delta_pos_new; + double delta_pos_new_norm = delta_pos_new.norm(); + rho = delta_pos_norm*delta_pos_norm - delta_pos_new_norm*delta_pos_new_norm; + rho /= diffq.transpose()*(lambda*diffq + grad); + if (rho > 0) { + q = q_new; + delta_pos = delta_pos_new; + delta_pos_norm = delta_pos_new_norm; + if (delta_pos_norm(); + return (error = E_NOERROR); + } + compute_jacobian(q_new); + jac = L.asDiagonal()*jac; + double tmp=2*rho-1; + lambda = lambda*max(1/3.0, 1-tmp*tmp*tmp); + v = 2; + } else { + lambda = lambda*v; + v = 2*v; + } + } + lastDifference = delta_pos_norm; + lastTransDiff = delta_pos.topRows(3).norm(); + lastRotDiff = delta_pos.bottomRows(3).norm(); + lastSV = svd.singularValues(); + lastNrOfIter = maxiter; + q_out.data = q.cast(); + return (error = E_MAX_ITERATIONS_EXCEEDED); + +} + +const char* ChainIkSolverPos_LMA::strError(const int error) const + { + if (E_GRADIENT_JOINTS_TOO_SMALL == error) return "The gradient of E towards the joints is to small"; + else if (E_INCREMENT_JOINTS_TOO_SMALL == error) return "The joint position increments are to small"; + else return SolverI::strError(error); + } + +}//namespace KDL diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolverpos_lma.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolverpos_lma.hpp new file mode 100644 index 0000000000000000000000000000000000000000..42d4bbf14023dea8ee158320cb8eace70192b72b --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolverpos_lma.hpp @@ -0,0 +1,256 @@ +#ifndef KDL_CHAINIKSOLVERPOS_GN_HPP +#define KDL_CHAINIKSOLVERPOS_GN_HPP +/** + \file chainiksolverpos_lma.hpp + \brief computing inverse position kinematics using Levenberg-Marquardt. +*/ + +/************************************************************************** + begin : May 2012 + copyright : (C) 2012 Erwin Aertbelien + email : firstname.lastname@mech.kuleuven.ac.be + + History (only major changes)( AUTHOR-Description ) : + + *************************************************************************** + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; if not, write to the Free Software * + * Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307 USA * + * * + ***************************************************************************/ + + +#include "chainiksolver.hpp" +#include "chain.hpp" +#include + +namespace KDL +{ + +/** + * \brief Solver for the inverse position kinematics that uses Levenberg-Marquardt. + * + * The robustness and speed of this solver is improved in several ways: + * - by using a Levenberg-Marquardt method that automatically adapts the damping when + * computing the inverse damped least squares inverse velocity kinematics. + * - by using an internal implementation of forward position kinematics and the + * Jacobian kinematics. This implementation is more numerically robust, + * is able to cache previous computations, and implements an \f$ \mathcal{O}(N) \f$ + * algorithm for the computation of the Jacobian (with \f$N\f$, the number of joints, and for + * a fixed size task space). + * - by providing a way to specify the weights in task space, you can weigh rotations wrt translations. + * This is important e.g. to specify that rotations do not matter for the problem at hand, or to + * specify how important you judge rotations w.r.t. translations, typically in S.I.-units, ([m],[rad]), + * the rotations are over-specified, this can be avoided using the weight matrix. Weights also + * make the solver more robust . + * - only the constructors call memory allocation. + * + * De general principles behind the optimisation is inspired on: + * Jorge Nocedal, Stephen J. Wright, Numerical Optimization,Springer-Verlag New York, 1999. + + * \ingroup KinematicFamily + */ +class ChainIkSolverPos_LMA : public KDL::ChainIkSolverPos +{ +private: + typedef double ScalarType; + typedef Eigen::Matrix MatrixXq; + typedef Eigen::Matrix VectorXq; +public: + + static const int E_GRADIENT_JOINTS_TOO_SMALL = -100; + static const int E_INCREMENT_JOINTS_TOO_SMALL = -101; + + /** + * \brief constructs an ChainIkSolverPos_LMA solver. + * + * The default parameters are chosen to be applicable to industrial-size robots + * (e.g. 0.5 to 3 meters range in task space), with an accuracy that is more then + * sufficient for typical industrial applications. + * + * Weights are applied in task space, i.e. the kinematic solver minimizes: + * \f$ E = \Delta \mathbf{x}^T \mathbf{L} \mathbf{L}^T \Delta \mathbf{x} \f$, with \f$\mathbf{L}\f$ a diagonal matrix. + * + * \param _chain specifies the kinematic chain. + * \param _l specifies the "square root" of the weight (diagonal) matrix in task space. This diagonal matrix is specified as a vector. + * \param _eps specifies the desired accuracy in task space; after weighing with + * the weight matrix, it is applied on \f$E\f$. + * \param _maxiter specifies the maximum number of iterations. + * \param _eps_joints specifies that the algorithm has to stop when the computed joint angle increments are + * smaller then _eps_joints. This is to avoid unnecessary computations up to _maxiter when the joint angle + * increments are so small that they effectively (in floating point) do not change the joint angles any more. The default + * is a few digits above numerical accuracy. + */ + ChainIkSolverPos_LMA( + const KDL::Chain& _chain, + const Eigen::Matrix& _l, + double _eps=1E-5, + int _maxiter=500, + double _eps_joints=1E-15 + ); + + /** + * \brief identical the full constructor for ChainIkSolverPos_LMA, but provides for a default weight matrix. + * + * \f$\mathbf{L} = \mathrm{diag}\left( \begin{bmatrix} 1 & 1 & 1 & 0.01 & 0.01 & 0.01 \end{bmatrix} \right) \f$. + */ + ChainIkSolverPos_LMA( + const KDL::Chain& _chain, + double _eps=1E-5, + int _maxiter=500, + double _eps_joints=1E-15 + ); + + /** + * \brief computes the inverse position kinematics. + * + * \param q_init initial joint position. + * \param T_base_goal goal position expressed with respect to the robot base. + * \param q_out joint position that achieves the specified goal position (if successful). + * \return E_NOERROR if successful, + * E_GRADIENT_JOINTS_TOO_SMALL the gradient of \f$ E \f$ towards the joints is to small, + * E_INCREMENT_JOINTS_TOO_SMALL if joint position increments are to small, + * E_MAX_ITER_EXCEEDED if number of iterations is exceeded. + */ + virtual int CartToJnt(const KDL::JntArray& q_init, const KDL::Frame& T_base_goal, KDL::JntArray& q_out); + + /** + * \brief destructor. + */ + virtual ~ChainIkSolverPos_LMA(); + + /** + * \brief for internal use only. + * + * Only exposed for test and diagnostic purposes. + */ + void compute_fwdpos(const VectorXq& q); + + /** + * \brief for internal use only. + * Only exposed for test and diagnostic purposes. + * compute_fwdpos(q) should always have been called before. + */ + void compute_jacobian(const VectorXq& q); + + /** + * \brief for internal use only. + * Only exposed for test and diagnostic purposes. + */ + void display_jac(const KDL::JntArray& jval); + + /// @copydoc KDL::SolverI::updateInternalDataStructures + void updateInternalDataStructures(); + + /// @copydoc KDL::SolverI::strError() + virtual const char* strError(const int error) const; + +private: + const KDL::Chain& chain; + unsigned int nj; + unsigned int ns; + +public: + + + /** + * \brief contains the last number of iterations for an execution of CartToJnt. + */ + int lastNrOfIter; + + /** + * \brief contains the last value for \f$ E \f$ after an execution of CartToJnt. + */ + double lastDifference; + + /** + * \brief contains the last value for the (unweighted) translational difference after an execution of CartToJnt. + */ + double lastTransDiff; + + /** + * \brief contains the last value for the (unweighted) rotational difference after an execution of CartToJnt. + */ + double lastRotDiff; + + /** + * \brief contains the last values for the singular values of the weighted Jacobian after an execution of CartToJnt. + */ + VectorXq lastSV; + + /** + * \brief for internal use only. + * + * contains the last value for the Jacobian after an execution of compute_jacobian. + */ + MatrixXq jac; + + /** + * \brief for internal use only. + * + * contains the gradient of the error criterion after an execution of CartToJnt. + */ + VectorXq grad; + /** + * \brief for internal use only. + * + * contains the last value for the position of the tip of the robot (head) with respect to the base, after an execution of compute_jacobian. + */ + KDL::Frame T_base_head; + + /** + * \brief display information on each iteration step to the console. + */ + bool display_information; +private: + // additional specification of the inverse position kinematics problem: + unsigned int maxiter; + double eps; + double eps_joints; + Eigen::Matrix L; + + + + // state of compute_fwdpos and compute_jacobian: + std::vector T_base_jointroot; + std::vector T_base_jointtip; + // need 2 vectors because of the somewhat strange definition of segment.hpp + // you could also recompute jointtip out of jointroot, + // but then you'll need more expensive cos/sin functions. + + + // the following are state of CartToJnt that is pre-allocated: + + VectorXq q; + MatrixXq A; + VectorXq tmp; + Eigen::LDLT ldlt; + Eigen::JacobiSVD svd; + VectorXq diffq; + VectorXq q_new; + VectorXq original_Aii; +}; + + + + + +} // namespace KDL + + + + + + +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolverpos_nr.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolverpos_nr.cpp new file mode 100644 index 0000000000000000000000000000000000000000..79747bf771d6ccc5d9767971134480b8e7f88d45 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolverpos_nr.cpp @@ -0,0 +1,81 @@ +// Copyright (C) 2007 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#include "chainiksolverpos_nr.hpp" + +namespace KDL +{ + ChainIkSolverPos_NR::ChainIkSolverPos_NR(const Chain& _chain,ChainFkSolverPos& _fksolver,ChainIkSolverVel& _iksolver, + unsigned int _maxiter, double _eps): + chain(_chain),nj (chain.getNrOfJoints()), + iksolver(_iksolver),fksolver(_fksolver), + delta_q(_chain.getNrOfJoints()), + maxiter(_maxiter),eps(_eps) + { + } + + void ChainIkSolverPos_NR::updateInternalDataStructures() { + nj = chain.getNrOfJoints(); + iksolver.updateInternalDataStructures(); + fksolver.updateInternalDataStructures(); + delta_q.resize(nj); + } + + int ChainIkSolverPos_NR::CartToJnt(const JntArray& q_init, const Frame& p_in, JntArray& q_out) + { + if (nj != chain.getNrOfJoints()) + return (error = E_NOT_UP_TO_DATE); + + if(q_init.rows() != nj || q_out.rows() != nj) + return (error = E_SIZE_MISMATCH); + + q_out = q_init; + + unsigned int i; + for(i=0;i fksolver.JntToCart(q_out,f) ) + return (error = E_FKSOLVERPOS_FAILED); + delta_twist = diff(f,p_in); + const int rc = iksolver.CartToJnt(q_out,delta_twist,delta_q); + if (E_NOERROR > rc) + return (error = E_IKSOLVER_FAILED); + // we chose to continue if the child solver returned a positive + // "error", which may simply indicate a degraded solution + Add(q_out,delta_q,q_out); + if(Equal(delta_twist,Twist::Zero(),eps)) + // converged, but possibly with a degraded solution + return (rc > E_NOERROR ? E_DEGRADED : E_NOERROR); + } + return (error = E_MAX_ITERATIONS_EXCEEDED); // failed to converge + } + + ChainIkSolverPos_NR::~ChainIkSolverPos_NR() + { + } + + const char* ChainIkSolverPos_NR::strError(const int error) const + { + if (E_IKSOLVER_FAILED == error) return "Child IK solver failed"; + else if (E_FKSOLVERPOS_FAILED == error) return "Child FK solver failed"; + else return SolverI::strError(error); + } +} + diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolverpos_nr.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolverpos_nr.hpp new file mode 100644 index 0000000000000000000000000000000000000000..a94c5a471936edc951a4f9f3c89d524fe9bca4bf --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolverpos_nr.hpp @@ -0,0 +1,97 @@ +// Copyright (C) 2007 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#ifndef KDLCHAINIKSOLVERPOS_NR_HPP +#define KDLCHAINIKSOLVERPOS_NR_HPP + +#include "chainiksolver.hpp" +#include "chainfksolver.hpp" + +namespace KDL { + + /** + * Implementation of a general inverse position kinematics + * algorithm based on Newton-Raphson iterations to calculate the + * position transformation from Cartesian to joint space of a general + * KDL::Chain. + * + * @ingroup KinematicFamily + */ + class ChainIkSolverPos_NR : public ChainIkSolverPos + { + public: + static const int E_IKSOLVER_FAILED = -100; //! Child IK solver vel failed + static const int E_FKSOLVERPOS_FAILED = -101; //! Child FK solver failed + + /** + * Constructor of the solver, it needs the chain, a forward + * position kinematics solver and an inverse velocity + * kinematics solver for that chain. + * + * @param chain the chain to calculate the inverse position for + * @param fksolver a forward position kinematics solver + * @param iksolver an inverse velocity kinematics solver + * @param maxiter the maximum Newton-Raphson iterations, + * default: 100 + * @param eps the precision for the position, used to end the + * iterations, default: epsilon (defined in kdl.hpp) + * + * @return + */ + ChainIkSolverPos_NR(const Chain& chain,ChainFkSolverPos& fksolver,ChainIkSolverVel& iksolver, + unsigned int maxiter=100,double eps=1e-6); + ~ChainIkSolverPos_NR(); + + /** + * Find an output joint pose \a q_out, given a starting joint pose + * \a q_init and a desired cartesian pose \a p_in + * + * @return: + * E_NOERROR=solution converged to +// Copyright (C) 2008 Mikael Mayer +// Copyright (C) 2008 Julia Jesse + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#include "chainiksolverpos_nr_jl.hpp" + +#include + +namespace KDL +{ + ChainIkSolverPos_NR_JL::ChainIkSolverPos_NR_JL(const Chain& _chain, const JntArray& _q_min, const JntArray& _q_max, ChainFkSolverPos& _fksolver,ChainIkSolverVel& _iksolver, + unsigned int _maxiter, double _eps): + chain(_chain), nj(chain.getNrOfJoints()), + q_min(_q_min), q_max(_q_max), + iksolver(_iksolver), fksolver(_fksolver), + delta_q(_chain.getNrOfJoints()), + maxiter(_maxiter),eps(_eps) + { + + } + + ChainIkSolverPos_NR_JL::ChainIkSolverPos_NR_JL(const Chain& _chain, ChainFkSolverPos& _fksolver,ChainIkSolverVel& _iksolver, + unsigned int _maxiter, double _eps): + chain(_chain), nj(chain.getNrOfJoints()), + q_min(nj), q_max(nj), + iksolver(_iksolver), fksolver(_fksolver), + delta_q(nj), + maxiter(_maxiter),eps(_eps) + { + q_min.data.setConstant(std::numeric_limits::min()); + q_max.data.setConstant(std::numeric_limits::max()); + } + + void ChainIkSolverPos_NR_JL::updateInternalDataStructures() { + nj = chain.getNrOfJoints(); + q_min.data.conservativeResizeLike(Eigen::VectorXd::Constant(nj,std::numeric_limits::min())); + q_max.data.conservativeResizeLike(Eigen::VectorXd::Constant(nj,std::numeric_limits::max())); + iksolver.updateInternalDataStructures(); + fksolver.updateInternalDataStructures(); + delta_q.resize(nj); + } + + int ChainIkSolverPos_NR_JL::CartToJnt(const JntArray& q_init, const Frame& p_in, JntArray& q_out) + { + if(nj != chain.getNrOfJoints()) + return (error = E_NOT_UP_TO_DATE); + + if(nj != q_init.rows() || nj != q_out.rows() || nj != q_min.rows() || nj != q_max.rows()) + return (error = E_SIZE_MISMATCH); + + q_out = q_init; + + unsigned int i; + for(i=0;i q_max(j)) + q_out(j) = q_max(j); + } + } + + if(i!=maxiter) + return (error = E_NOERROR); + else + return (error = E_MAX_ITERATIONS_EXCEEDED); + } + + int ChainIkSolverPos_NR_JL::setJointLimits(const JntArray& q_min_in, const JntArray& q_max_in) { + if (q_min_in.rows() != nj || q_max_in.rows() != nj) + return (error = E_SIZE_MISMATCH); + q_min = q_min_in; + q_max = q_max_in; + return (error = E_NOERROR); + } + + ChainIkSolverPos_NR_JL::~ChainIkSolverPos_NR_JL() + { + } + + const char* ChainIkSolverPos_NR_JL::strError(const int error) const + { + if (E_FKSOLVERPOS_FAILED == error) return "Internal forward position solver failed."; + else if (E_IKSOLVERVEL_FAILED == error) return "Internal inverse velocity solver failed."; + else return SolverI::strError(error); + } +} + diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolverpos_nr_jl.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolverpos_nr_jl.hpp new file mode 100644 index 0000000000000000000000000000000000000000..b3c6fed27cf0f5ad747748ab402790c93dd77542 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolverpos_nr_jl.hpp @@ -0,0 +1,129 @@ +// Copyright (C) 2007-2008 Ruben Smits +// Copyright (C) 2008 Mikael Mayer +// Copyright (C) 2008 Julia Jesse + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#ifndef KDLCHAINIKSOLVERPOS_NR_JL_HPP +#define KDLCHAINIKSOLVERPOS_NR_JL_HPP + +#include "chainiksolver.hpp" +#include "chainfksolver.hpp" + +namespace KDL { + + /** + * Implementation of a general inverse position kinematics + * algorithm based on Newton-Raphson iterations to calculate the + * position transformation from Cartesian to joint space of a general + * KDL::Chain. Takes joint limits into account. + * + * @ingroup KinematicFamily + */ + class ChainIkSolverPos_NR_JL : public ChainIkSolverPos + { + public: + + static const int E_IKSOLVERVEL_FAILED = -100; //! Child IK solver vel failed + static const int E_FKSOLVERPOS_FAILED = -101; //! Child FK solver failed + + /** + * Constructor of the solver, it needs the chain, a forward + * position kinematics solver and an inverse velocity + * kinematics solver for that chain. + * + * @param chain the chain to calculate the inverse position for + * @param q_min the minimum joint positions + * @param q_max the maximum joint positions + * @param fksolver a forward position kinematics solver + * @param iksolver an inverse velocity kinematics solver + * @param maxiter the maximum Newton-Raphson iterations, + * default: 100 + * @param eps the precision for the position, used to end the + * iterations, default: epsilon (defined in kdl.hpp) + * + * @return + */ + ChainIkSolverPos_NR_JL(const Chain& chain,const JntArray& q_min, const JntArray& q_max, ChainFkSolverPos& fksolver,ChainIkSolverVel& iksolver,unsigned int maxiter=100,double eps=1e-6); + + /** + * Constructor of the solver, it needs the chain, a forward + * position kinematics solver and an inverse velocity + * kinematics solver for that chain. + * + * @param chain the chain to calculate the inverse position for + * @param fksolver a forward position kinematics solver + * @param iksolver an inverse velocity kinematics solver + * @param maxiter the maximum Newton-Raphson iterations, + * default: 100 + * @param eps the precision for the position, used to end the + * iterations, default: epsilon (defined in kdl.hpp) + * + * @return + */ + ChainIkSolverPos_NR_JL(const Chain& chain, ChainFkSolverPos& fksolver,ChainIkSolverVel& iksolver,unsigned int maxiter=100,double eps=1e-6); + + ~ChainIkSolverPos_NR_JL(); + + + /** + * Calculates the joint values that correspond to the input pose given an initial guess. + * @param q_init Initial guess for the joint values. + * @param p_in The input pose of the chain tip. + * @param q_out The resulting output joint values + * @return E_MAX_ITERATIONS_EXCEEDED if the maximum number of iterations was exceeded before a result was found + * E_NOT_UP_TO_DATE if the internal data is not up to date with the chain + * E_SIZE_MISMATCH if the size of the input/output data does not match the chain. + */ + virtual int CartToJnt(const JntArray& q_init, const Frame& p_in, JntArray& q_out); + + /** + * Function to set the joint limits. + * @param q_min minimum values for the joints + * @param q_max maximum values for the joints + * @return E_SIZE_MISMATCH if input sizes do not match the chain + */ + int setJointLimits(const JntArray& q_min, const JntArray& q_max); + + /// @copydoc KDL::SolverI::updateInternalDataStructures + virtual void updateInternalDataStructures(); + + /// @copydoc KDL::SolverI::strError() + const char* strError(const int error) const; + + private: + const Chain& chain; + unsigned int nj; + JntArray q_min; + JntArray q_max; + ChainIkSolverVel& iksolver; + ChainFkSolverPos& fksolver; + JntArray delta_q; + unsigned int maxiter; + double eps; + + Frame f; + Twist delta_twist; + + }; + +} + +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolvervel_pinv.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolvervel_pinv.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4ecc0cc495bc204ac22ff227a0af4fa684e3a2b1 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolvervel_pinv.cpp @@ -0,0 +1,137 @@ +// Copyright (C) 2007 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#include "chainiksolvervel_pinv.hpp" + +namespace KDL +{ + ChainIkSolverVel_pinv::ChainIkSolverVel_pinv(const Chain& _chain,double _eps,int _maxiter): + chain(_chain), + jnt2jac(chain), + nj(chain.getNrOfJoints()), + jac(nj), + svd(jac), + U(6,JntArray(nj)), + S(nj), + V(nj,JntArray(nj)), + tmp(nj), + eps(_eps), + maxiter(_maxiter), + nrZeroSigmas(0), + svdResult(0) + { + } + + void ChainIkSolverVel_pinv::updateInternalDataStructures() { + jnt2jac.updateInternalDataStructures(); + nj = chain.getNrOfJoints(); + jac.resize(nj); + svd = SVD_HH(jac); + for(unsigned int i = 0 ; i < U.size(); i++) + U[i].resize(nj); + S.resize(nj); + V.resize(nj); + for(unsigned int i = 0 ; i < V.size(); i++) + V[i].resize(nj); + tmp.resize(nj); + } + + ChainIkSolverVel_pinv::~ChainIkSolverVel_pinv() + { + } + + + int ChainIkSolverVel_pinv::CartToJnt(const JntArray& q_in, const Twist& v_in, JntArray& qdot_out) + { + if (nj != chain.getNrOfJoints()) + return (error = E_NOT_UP_TO_DATE); + + if (nj != q_in.rows() || nj != qdot_out.rows()) + return (error = E_SIZE_MISMATCH); + + //Let the ChainJntToJacSolver calculate the jacobian "jac" for + //the current joint positions "q_in" + error = jnt2jac.JntToJac(q_in,jac); + if (error < E_NOERROR) return error; + + double sum; + unsigned int i,j; + + // Initialize near zero singular value counter + nrZeroSigmas = 0 ; + + //Do a singular value decomposition of "jac" with maximum + //iterations "maxiter", put the results in "U", "S" and "V" + //jac = U*S*Vt + svdResult = svd.calculate(jac,U,S,V,maxiter); + if (0 != svdResult) + { + qdot_out.data.setZero(); + return (error = E_SVD_FAILED); + } + + // We have to calculate qdot_out = jac_pinv*v_in + // Using the svd decomposition this becomes(jac_pinv=V*S_pinv*Ut): + // qdot_out = V*S_pinv*Ut*v_in + + //first we calculate Ut*v_in + for (i=0;i (jac.columns()-jac.rows()) ) { + return (error = E_CONVERGE_PINV_SINGULAR); // converged but pinv singular + } else { + return (error = E_NOERROR); // have converged + } + } + + const char* ChainIkSolverVel_pinv::strError(const int error) const + { + if (E_CONVERGE_PINV_SINGULAR == error) return "Converged put pseudo inverse of jacobian is singular."; + else return SolverI::strError(error); + } +} diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolvervel_pinv.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolvervel_pinv.hpp new file mode 100644 index 0000000000000000000000000000000000000000..63a6f9c4c4575242e95a808611e4738d63cf8df8 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolvervel_pinv.hpp @@ -0,0 +1,118 @@ +// Copyright (C) 2007 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#ifndef KDL_CHAIN_IKSOLVERVEL_PINV_HPP +#define KDL_CHAIN_IKSOLVERVEL_PINV_HPP + +#include "chainiksolver.hpp" +#include "chainjnttojacsolver.hpp" +#include "utilities/svd_HH.hpp" + +namespace KDL +{ + /** + * Implementation of a inverse velocity kinematics algorithm based + * on the generalize pseudo inverse to calculate the velocity + * transformation from Cartesian to joint space of a general + * KDL::Chain. It uses a svd-calculation based on householders + * rotations. + * + * @ingroup KinematicFamily + */ + class ChainIkSolverVel_pinv : public ChainIkSolverVel + { + public: + /// solution converged but (pseudo)inverse is singular + static const int E_CONVERGE_PINV_SINGULAR = +100; + + /** + * Constructor of the solver + * + * @param chain the chain to calculate the inverse velocity + * kinematics for + * @param eps if a singular value is below this value, its + * inverse is set to zero, default: 0.00001 + * @param maxiter maximum iterations for the svd calculation, + * default: 150 + * + */ + explicit ChainIkSolverVel_pinv(const Chain& chain,double eps=0.00001,int maxiter=150); + ~ChainIkSolverVel_pinv(); + + /** + * Find an output joint velocity \a qdot_out, given a starting joint pose + * \a q_init and a desired cartesian velocity \a v_in + * + * @return + * E_NOERROR=solution converged to jac.col()-jac.row(), + * then the jacobian pseudoinverse is singular + */ + unsigned int getNrZeroSigmas()const {return nrZeroSigmas;}; + + /** + * Retrieve the latest return code from the SVD algorithm + * @return 0 if CartToJnt() not yet called, otherwise latest SVD result code. + */ + int getSVDResult()const {return svdResult;}; + + /// @copydoc KDL::SolverI::strError() + virtual const char* strError(const int error) const; + + /// @copydoc KDL::SolverI::updateInternalDataStructures + virtual void updateInternalDataStructures(); + private: + const Chain& chain; + ChainJntToJacSolver jnt2jac; + unsigned int nj; + Jacobian jac; + SVD_HH svd; + std::vector U; + JntArray S; + std::vector V; + JntArray tmp; + double eps; + int maxiter; + unsigned int nrZeroSigmas; + int svdResult; + + }; +} +#endif + diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolvervel_pinv_givens.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolvervel_pinv_givens.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fbf102b4004c9668a5ea9d13c455340f0000a8b2 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolvervel_pinv_givens.cpp @@ -0,0 +1,122 @@ +// Copyright (C) 2007 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#include "chainiksolvervel_pinv_givens.hpp" +#include "utilities/svd_eigen_Macie.hpp" + +namespace KDL +{ + ChainIkSolverVel_pinv_givens::ChainIkSolverVel_pinv_givens(const Chain& _chain): + chain(_chain), + nj(chain.getNrOfJoints()), + jnt2jac(chain), + jac(nj), + transpose(nj>6),toggle(true), + m(static_cast(max(6,nj))), + n(static_cast(min(6,nj))), + jac_eigen(m,n), + U(Eigen::MatrixXd::Identity(m,m)), + V(Eigen::MatrixXd::Identity(n,n)), + B(m,n), + S(n), + tempi(m), + UY(Eigen::VectorXd::Zero(6)), + SUY(Eigen::VectorXd::Zero(nj)), + qdot_eigen(nj), + v_in_eigen(6) + { + } + + void ChainIkSolverVel_pinv_givens::updateInternalDataStructures() { + nj = chain.getNrOfJoints(); + jnt2jac.updateInternalDataStructures(); + jac.resize(nj); + transpose = (nj > 6); + m = static_cast(max(6,nj)); + n = static_cast(min(6,nj)); + jac_eigen.conservativeResize(m,n); + U.conservativeResizeLike(Eigen::MatrixXd::Identity(m,m)); + V.conservativeResizeLike(Eigen::MatrixXd::Identity(n,n)); + B.conservativeResize(m,n); + S.conservativeResize(n); + tempi.conservativeResize(m); + SUY.conservativeResizeLike(Eigen::VectorXd::Zero(nj)); + qdot_eigen.conservativeResize(nj); + } + + ChainIkSolverVel_pinv_givens::~ChainIkSolverVel_pinv_givens() + { + } + + + int ChainIkSolverVel_pinv_givens::CartToJnt(const JntArray& q_in, const Twist& v_in, JntArray& qdot_out) + { + if (nj != chain.getNrOfJoints()) + return (error = E_NOT_UP_TO_DATE); + + if (nj != q_in.rows() || nj != qdot_out.rows()) + return (error = E_SIZE_MISMATCH); + + toggle=!toggle; + + error = jnt2jac.JntToJac(q_in,jac); + if (E_NOERROR > error ) + return error; + + for(unsigned int i=0;i<6;i++) + v_in_eigen(i)=v_in(i); + + for(unsigned int i=0;i + +#ifndef KDL_CHAIN_IKSOLVERVEL_PINV_GIVENS_HPP +#define KDL_CHAIN_IKSOLVERVEL_PINV_GIVENS_HPP + +#include "chainiksolver.hpp" +#include "chainjnttojacsolver.hpp" + +#include + +namespace KDL +{ + /** + * Implementation of a inverse velocity kinematics algorithm based + * on the generalize pseudo inverse to calculate the velocity + * transformation from Cartesian to joint space of a general + * KDL::Chain. It uses a svd-calculation based on householders + * rotations. + * + * @ingroup KinematicFamily + */ + class ChainIkSolverVel_pinv_givens : public ChainIkSolverVel + { + public: + + /** + * Constructor of the solver + * + * @param chain the chain to calculate the inverse velocity + * kinematics for + * + */ + explicit ChainIkSolverVel_pinv_givens(const Chain& chain); + ~ChainIkSolverVel_pinv_givens(); + + virtual int CartToJnt(const JntArray& q_in, const Twist& v_in, JntArray& qdot_out); + /** + * not (yet) implemented. + * + */ + virtual int CartToJnt(const JntArray& /*q_init*/, const FrameVel& /*v_in*/, JntArrayVel& /*q_out*/){return (error = E_NOT_IMPLEMENTED);}; + + /// @copydoc KDL::SolverI::updateInternalDataStructures + virtual void updateInternalDataStructures(); + + private: + const Chain& chain; + unsigned int nj; + ChainJntToJacSolver jnt2jac; + Jacobian jac; + bool transpose,toggle; + unsigned int m,n; + Eigen::MatrixXd jac_eigen,U,V,B; + Eigen::VectorXd S,tempi,UY,SUY,qdot_eigen,v_in_eigen; + }; +} +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolvervel_pinv_nso.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolvervel_pinv_nso.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1b04795fb9e6a0c2ff9fda00312d9743769752cc --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolvervel_pinv_nso.cpp @@ -0,0 +1,189 @@ +// Copyright (C) 2007 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#include "chainiksolvervel_pinv_nso.hpp" +#include "utilities/svd_eigen_HH.hpp" + +namespace KDL +{ + ChainIkSolverVel_pinv_nso::ChainIkSolverVel_pinv_nso(const Chain& _chain, const JntArray& _opt_pos, const JntArray& _weights, double _eps, int _maxiter, double _alpha): + chain(_chain), + jnt2jac(chain), + nj(chain.getNrOfJoints()), + jac(nj), + U(Eigen::MatrixXd::Zero(6,nj)), + S(Eigen::VectorXd::Zero(nj)), + Sinv(Eigen::VectorXd::Zero(nj)), + V(Eigen::MatrixXd::Zero(nj,nj)), + tmp(Eigen::VectorXd::Zero(nj)), + tmp2(Eigen::VectorXd::Zero(nj)), + eps(_eps), + maxiter(_maxiter), + svdResult(0), + alpha(_alpha), + weights(_weights), + opt_pos(_opt_pos) + { + } + + ChainIkSolverVel_pinv_nso::ChainIkSolverVel_pinv_nso(const Chain& _chain, double _eps, int _maxiter, double _alpha): + chain(_chain), + jnt2jac(chain), + nj(chain.getNrOfJoints()), + jac(nj), + U(Eigen::MatrixXd::Zero(6,nj)), + S(Eigen::VectorXd::Zero(nj)), + Sinv(Eigen::VectorXd::Zero(nj)), + V(Eigen::MatrixXd::Zero(nj,nj)), + tmp(Eigen::VectorXd::Zero(nj)), + tmp2(Eigen::VectorXd::Zero(nj)), + eps(_eps), + maxiter(_maxiter), + svdResult(0), + alpha(_alpha) + { + } + + void ChainIkSolverVel_pinv_nso::updateInternalDataStructures() { + jnt2jac.updateInternalDataStructures(); + nj = chain.getNrOfJoints(); + jac.resize(nj); + U.conservativeResizeLike(Eigen::MatrixXd::Zero(6,nj)); + S.conservativeResizeLike(Eigen::VectorXd::Zero(nj)); + Sinv.conservativeResizeLike(Eigen::VectorXd::Zero(nj)); + V.conservativeResizeLike(Eigen::MatrixXd::Zero(nj,nj)); + tmp.conservativeResizeLike(Eigen::VectorXd::Zero(nj)); + tmp2.conservativeResizeLike(Eigen::VectorXd::Zero(nj)); + opt_pos.data.conservativeResizeLike(Eigen::VectorXd::Zero(nj)); + weights.data.conservativeResizeLike(Eigen::VectorXd::Ones(nj)); + } + + ChainIkSolverVel_pinv_nso::~ChainIkSolverVel_pinv_nso() + { + } + + + int ChainIkSolverVel_pinv_nso::CartToJnt(const JntArray& q_in, const Twist& v_in, JntArray& qdot_out) + { + if (nj != chain.getNrOfJoints()) + return (error = E_NOT_UP_TO_DATE); + + if (nj != q_in.rows() || nj != qdot_out.rows() || nj != opt_pos.rows() || nj != weights.rows()) + return (error = E_SIZE_MISMATCH); + //Let the ChainJntToJacSolver calculate the jacobian "jac" for + //the current joint positions "q_in" + error = jnt2jac.JntToJac(q_in,jac); + if (error < E_NOERROR) return error; + + //Do a singular value decomposition of "jac" with maximum + //iterations "maxiter", put the results in "U", "S" and "V" + //jac = U*S*Vt + svdResult = svd_eigen_HH(jac.data,U,S,V,tmp,maxiter); + if (0 != svdResult) + { + qdot_out.data.setZero() ; + return error = E_SVD_FAILED; + } + + unsigned int i; + + // We have to calculate qdot_out = jac_pinv*v_in + // Using the svd decomposition this becomes(jac_pinv=V*S_pinv*Ut): + // qdot_out = V*S_pinv*Ut*v_in + + // S^-1 + for (i = 0; i < nj; ++i) { + Sinv(i) = fabs(S(i)) 1e-9) { + // Calculate inverse Jacobian Jc^-1 + for (i = 0; i < nj; ++i) { + tmp(i) = weights(i)*(q_in(i) - opt_pos(i)) / A; + } + + // Calculate J^-1 * J * Jc^-1 = V*S^-1*U' * U*S*V' * tmp + tmp2 = V * Sinv.asDiagonal() * U.transpose() * U * S.asDiagonal() * V.transpose() * tmp; + + for (i = 0; i < nj; ++i) { + //std::cerr << i <<": "<< qdot_out(i) <<", "<< -2*alpha*g * (tmp(i) - tmp2(i)) << std::endl; + qdot_out(i) += -2*alpha*g * (tmp(i) - tmp2(i)); + } + } + //return the return value of the svd decomposition + return (error = E_NOERROR); + } + + int ChainIkSolverVel_pinv_nso::setWeights(const JntArray & _weights) + { + if (nj != _weights.rows()) + return (error = E_SIZE_MISMATCH); + weights = _weights; + return (error = E_NOERROR); + } + + int ChainIkSolverVel_pinv_nso::setOptPos(const JntArray & _opt_pos) + { + if (nj != _opt_pos.rows()) + return (error = E_SIZE_MISMATCH); + opt_pos = _opt_pos; + return (error = E_NOERROR); + } + + int ChainIkSolverVel_pinv_nso::setAlpha(const double _alpha) + { + alpha = _alpha; + return 0; + } + + +} diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolvervel_pinv_nso.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolvervel_pinv_nso.hpp new file mode 100644 index 0000000000000000000000000000000000000000..97cd570ba4735b70efa70defebcc73868a5756c6 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolvervel_pinv_nso.hpp @@ -0,0 +1,160 @@ +// Copyright (C) 2007 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#ifndef KDL_CHAIN_IKSOLVERVEL_PINV_NSO_HPP +#define KDL_CHAIN_IKSOLVERVEL_PINV_NSO_HPP + +#include "chainiksolver.hpp" +#include "chainjnttojacsolver.hpp" +#include + +namespace KDL +{ + /** + * Implementation of a inverse velocity kinematics algorithm based + * on the generalize pseudo inverse to calculate the velocity + * transformation from Cartesian to joint space of a general + * KDL::Chain. It uses a svd-calculation based on householders + * rotations. + * + * In case of a redundant robot this solver optimizes the following criterium: + * g=0.5*sum(weight*(Desired_joint_positions - actual_joint_positions))^2 as described in + * A. Liegeois. Automatic supervisory control of the configuration and + * behavior of multibody mechanisms. IEEE Transactions on Systems, Man, and + * Cybernetics, 7(12):868–871, 1977 + * + * @ingroup KinematicFamily + */ + class ChainIkSolverVel_pinv_nso : public ChainIkSolverVel + { + public: + /** + * Constructor of the solver + * + * @param chain the chain to calculate the inverse velocity + * kinematics for + * @param opt_pos the desired positions of the chain used by to resolve the redundancy + * @param weights the weights applied in the joint space + * @param eps if a singular value is below this value, its + * inverse is set to zero, default: 0.00001 + * @param maxiter maximum iterations for the svd calculation, + * default: 150 + * @param alpha the null-space velocity gain + * + */ + ChainIkSolverVel_pinv_nso(const Chain& chain, const JntArray& opt_pos, const JntArray& weights, double eps=0.00001,int maxiter=150, double alpha = 0.25); + explicit ChainIkSolverVel_pinv_nso(const Chain& chain, double eps=0.00001,int maxiter=150, double alpha = 0.25); + ~ChainIkSolverVel_pinv_nso(); + + virtual int CartToJnt(const JntArray& q_in, const Twist& v_in, JntArray& qdot_out); + /** + * not (yet) implemented. + * + */ + virtual int CartToJnt(const JntArray& /*q_init*/, const FrameVel& /*v_in*/, JntArrayVel& /*q_out*/){return (error = E_NOT_IMPLEMENTED);}; + + /** + * Request the joint weights for optimization criterion + * + * + * @return const reference to the joint weights + */ + const JntArray& getWeights()const + { + return weights; + } + + /** + * Request the optimal joint positions + * + * + * @return const reference to the optimal joint positions + */ + const JntArray& getOptPos()const + { + return opt_pos; + } + + /** + * Request null space velocity gain + * + * + * @return const reference to the null space velocity gain + */ + const double& getAlpha()const + { + return alpha; + } + + /** + *Set joint weights for optimization criterion + * + *@param weights the joint weights + * + */ + virtual int setWeights(const JntArray &weights); + + /** + *Set optimal joint positions + * + *@param opt_pos optimal joint positions + * + */ + virtual int setOptPos(const JntArray &opt_pos); + + /** + *Set null space velocity gain + * + *@param alpha NUllspace velocity cgain + * + */ + virtual int setAlpha(const double alpha); + + /** + * Retrieve the latest return code from the SVD algorithm + * @return 0 if CartToJnt() not yet called, otherwise latest SVD result code. + */ + int getSVDResult()const {return svdResult;}; + + /// @copydoc KDL::SolverI::updateInternalDataStructures + virtual void updateInternalDataStructures(); + + private: + const Chain& chain; + ChainJntToJacSolver jnt2jac; + unsigned int nj; + Jacobian jac; + Eigen::MatrixXd U; + Eigen::VectorXd S; + Eigen::VectorXd Sinv; + Eigen::MatrixXd V; + Eigen::VectorXd tmp; + Eigen::VectorXd tmp2; + double eps; + int maxiter; + int svdResult; + double alpha; + JntArray weights; + JntArray opt_pos; + }; +} +#endif + diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolvervel_wdls.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolvervel_wdls.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a2f37aee82b0a1b165eb50eb6d4d1f48a6ed0c63 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolvervel_wdls.cpp @@ -0,0 +1,224 @@ +// Copyright (C) 2007 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#include "chainiksolvervel_wdls.hpp" +#include "utilities/svd_eigen_HH.hpp" + +namespace KDL +{ + + ChainIkSolverVel_wdls::ChainIkSolverVel_wdls(const Chain& _chain,double _eps,int _maxiter): + chain(_chain), + jnt2jac(chain), + nj(chain.getNrOfJoints()), + jac(nj), + U(Eigen::MatrixXd::Zero(6,nj)), + S(Eigen::VectorXd::Zero(nj)), + V(Eigen::MatrixXd::Zero(nj,nj)), + eps(_eps), + maxiter(_maxiter), + tmp(Eigen::VectorXd::Zero(nj)), + tmp_jac(Eigen::MatrixXd::Zero(6,nj)), + tmp_jac_weight1(Eigen::MatrixXd::Zero(6,nj)), + tmp_jac_weight2(Eigen::MatrixXd::Zero(6,nj)), + tmp_ts(Eigen::MatrixXd::Zero(6,6)), + tmp_js(Eigen::MatrixXd::Zero(nj,nj)), + weight_ts(Eigen::MatrixXd::Identity(6,6)), + weight_js(Eigen::MatrixXd::Identity(nj,nj)), + lambda(0.0), + lambda_scaled(0.0), + nrZeroSigmas(0), + svdResult(0), + sigmaMin(0) + { + } + + void ChainIkSolverVel_wdls::updateInternalDataStructures() { + jnt2jac.updateInternalDataStructures(); + nj = chain.getNrOfJoints(); + jac.resize(nj); + Eigen::MatrixXd z6nj = Eigen::MatrixXd::Zero(6,nj); + Eigen::VectorXd znj = Eigen::VectorXd::Zero(nj); + Eigen::MatrixXd znjnj = Eigen::MatrixXd::Zero(nj,nj); + U.conservativeResizeLike(z6nj); + S.conservativeResizeLike(znj); + V.conservativeResizeLike(znjnj); + tmp.conservativeResizeLike(znj); + tmp_jac.conservativeResizeLike(z6nj); + tmp_jac_weight1.conservativeResizeLike(z6nj); + tmp_jac_weight2.conservativeResizeLike(z6nj); + tmp_js.conservativeResizeLike(znjnj); + weight_js.conservativeResizeLike(Eigen::MatrixXd::Identity(nj,nj)); + } + + ChainIkSolverVel_wdls::~ChainIkSolverVel_wdls() + { + } + + int ChainIkSolverVel_wdls::setWeightJS(const Eigen::MatrixXd& Mq){ + if(nj != chain.getNrOfJoints()) + return (error = E_NOT_UP_TO_DATE); + + if (Mq.size() != weight_js.size()) + return (error = E_SIZE_MISMATCH); + weight_js = Mq; + return (error = E_NOERROR); + } + + int ChainIkSolverVel_wdls::setWeightTS(const Eigen::MatrixXd& Mx){ + if (Mx.size() != weight_ts.size()) + return (error = E_SIZE_MISMATCH); + weight_ts = Mx; + return (error = E_NOERROR); + } + + void ChainIkSolverVel_wdls::setLambda(const double lambda_in) + { + lambda=lambda_in; + } + + void ChainIkSolverVel_wdls::setEps(const double eps_in) + { + eps=eps_in; + } + + void ChainIkSolverVel_wdls::setMaxIter(const int maxiter_in) + { + maxiter=maxiter_in; + } + + int ChainIkSolverVel_wdls::getSigma(Eigen::VectorXd& Sout) + { + if (Sout.size() != S.size()) + return (error = E_SIZE_MISMATCH); + Sout=S; + return (error = E_NOERROR); + } + + int ChainIkSolverVel_wdls::CartToJnt(const JntArray& q_in, const Twist& v_in, JntArray& qdot_out) + { + if(nj != chain.getNrOfJoints()) + return (error = E_NOT_UP_TO_DATE); + + if(nj != q_in.rows() || nj != qdot_out.rows()) + return (error = E_SIZE_MISMATCH); + error = jnt2jac.JntToJac(q_in,jac); + if ( error < E_NOERROR) return error; + + double sum; + unsigned int i,j; + + // Initialize (internal) return values + nrZeroSigmas = 0 ; + sigmaMin = 0.; + lambda_scaled = 0.; + + /* + for (i=0;i=6 and 0 for <6 + if ( jac.columns() >= 6 ) { + sigmaMin = S(5); + } + else { + sigmaMin = 0.; + } + + // tmp = (Si*U'*Ly*y), + for (i=0;i eps, then wdls is not active and lambda_scaled = 0 (default value) + // If sigmaMin < eps, then wdls is active and lambda_scaled is scaled from 0 to lambda + // Note: singular values are always positive so sigmaMin >=0 + if ( sigmaMin < eps ) + { + lambda_scaled = sqrt(1.0-(sigmaMin/eps)*(sigmaMin/eps))*lambda ; + } + if(fabs(S(i))=6 due to cols>rows + } + // Count number of singular values near zero + ++nrZeroSigmas ; + } + else { + tmp(i) = sum/S(i); + } + } + + /* + // x = Lx^-1*V*tmp + x + for (i=0;i (jac.columns()-jac.rows()) ) { + return (error = E_CONVERGE_PINV_SINGULAR); // converged but pinv singular + } else { + return (error = E_NOERROR); // have converged + } + } + + const char* ChainIkSolverVel_wdls::strError(const int error) const + { + if (E_CONVERGE_PINV_SINGULAR == error) return "Converged put pseudo inverse of jacobian is singular."; + else return SolverI::strError(error); + } + +} diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolvervel_wdls.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolvervel_wdls.hpp new file mode 100644 index 0000000000000000000000000000000000000000..dbba19b1a39c795673463ca979322c8b13b9a4c9 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainiksolvervel_wdls.hpp @@ -0,0 +1,244 @@ +// Copyright (C) 2007 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#ifndef KDL_CHAIN_IKSOLVERVEL_WDLS_HPP +#define KDL_CHAIN_IKSOLVERVEL_WDLS_HPP + +#include "chainiksolver.hpp" +#include "chainjnttojacsolver.hpp" +#include + +namespace KDL +{ + /** + * Implementation of a inverse velocity kinematics algorithm based + * on the weighted pseudo inverse with damped least-square to calculate the velocity + * transformation from Cartesian to joint space of a general + * KDL::Chain. It uses a svd-calculation based on householders + * rotations. + * + * J# = M_q*Vb*pinv_dls(Db)*Ub'*M_x + * + * where B = Mx*J*Mq + * + * and B = Ub*Db*Vb' is the SVD decomposition of B + * + * Mq and Mx represent, respectively, the joint-space and task-space weighting + * matrices. + * Please refer to the documentation of setWeightJS(const Eigen::MatrixXd& Mq) + * and setWeightTS(const Eigen::MatrixXd& Mx) for details on the effects of + * these matrices. + * + * For more details on Weighted Pseudo Inverse, see : + * 1) [Ben Israel 03] A. Ben Israel & T.N.E. Greville. + * Generalized Inverses : Theory and Applications, + * second edition. Springer, 2003. ISBN 0-387-00293-6. + * + * 2) [Doty 93] K. L. Doty, C. Melchiorri & C. Boniveto. + * A theory of generalized inverses applied to Robotics. + * The International Journal of Robotics Research, + * vol. 12, no. 1, pages 1-19, february 1993. + * + * + * @ingroup KinematicFamily + */ + class ChainIkSolverVel_wdls : public ChainIkSolverVel + { + public: + /// solution converged but (pseudo)inverse is singular + static const int E_CONVERGE_PINV_SINGULAR = +100; + + /** + * Constructor of the solver + * + * @param chain the chain to calculate the inverse velocity + * kinematics for + * @param eps if a singular value is below this value, its + * inverse is set to zero, default: 0.00001 + * @param maxiter maximum iterations for the svd calculation, + * default: 150 + * + */ + + explicit ChainIkSolverVel_wdls(const Chain& chain,double eps=0.00001,int maxiter=150); + //=ublas::identity_matrix + ~ChainIkSolverVel_wdls(); + + /** + * Find an output joint velocity \a qdot_out, given a starting joint pose + * \a q_init and a desired cartesian velocity \a v_in + * + * @return + * E_NOERROR=svd solution converged in maxiter + * E_SVD_FAILED=svd solution failed + * E_CONVERGE_PINV_SINGULAR=svd solution converged but (pseudo)inverse singular + * + * @note if E_CONVERGE_PINV_SINGULAR returned then converged and can + * continue motion, but have degraded solution + * + * @note If E_SVD_FAILED returned, then getSvdResult() returns the error + * code from the SVD algorithm. + */ + virtual int CartToJnt(const JntArray& q_in, const Twist& v_in, JntArray& qdot_out); + /** + * not (yet) implemented. + * + */ + virtual int CartToJnt(const JntArray& /*q_init*/, const FrameVel& /*v_in*/, JntArrayVel& /*q_out*/){return -1;}; + + /** + * Set the joint space weighting matrix + * + * @param weight_js joint space weighting symmetric matrix, + * default : identity. M_q : This matrix being used as a + * weight for the norm of the joint space speed it HAS TO BE + * symmetric and positive definite. We can actually deal with + * matrices containing a symmetric and positive definite block + * and 0s otherwise. Taking a diagonal matrix as an example, a + * 0 on the diagonal means that the corresponding joints will + * not contribute to the motion of the system. On the other + * hand, the bigger the value, the most the corresponding + * joint will contribute to the overall motion. The obtained + * solution q_dot will actually minimize the weighted norm + * sqrt(q_dot'*(M_q^-2)*q_dot). In the special case we deal + * with, it does not make sense to invert M_q but what is + * important is the physical meaning of all this : a joint + * that has a zero weight in M_q will not contribute to the + * motion of the system and this is equivalent to saying that + * it gets an infinite weight in the norm computation. For + * more detailed explanation : vincent.padois@upmc.fr + * + * @return success/error code + */ + int setWeightJS(const Eigen::MatrixXd& Mq); + + /** + * Set the task space weighting matrix + * + * @param weight_ts task space weighting symmetric matrix, + * default: identity M_x : This matrix being used as a weight + * for the norm of the error (in terms of task space speed) it + * HAS TO BE symmetric and positive definite. We can actually + * deal with matrices containing a symmetric and positive + * definite block and 0s otherwise. Taking a diagonal matrix + * as an example, a 0 on the diagonal means that the + * corresponding task coordinate will not be taken into + * account (ie the corresponding error can be really big). If + * the rank of the jacobian is equal to the number of task + * space coordinates which do not have a 0 weight in M_x, the + * weighting will actually not impact the results (ie there is + * an exact solution to the velocity inverse kinematics + * problem). In cases without an exact solution, the bigger + * the value, the most the corresponding task coordinate will + * be taken into account (ie the more the corresponding error + * will be reduced). The obtained solution will minimize the + * weighted norm sqrt(|x_dot-Jq_dot|'*(M_x^2)*|x_dot-Jq_dot|). + * For more detailed explanation : vincent.padois@upmc.fr + * + * @return success/error code + */ + int setWeightTS(const Eigen::MatrixXd& Mx); + + /** + * Set lambda + */ + void setLambda(const double lambda); + /** + * Set eps + */ + void setEps(const double eps_in); + /** + * Set maxIter + */ + void setMaxIter(const int maxiter_in); + + /** + * Request the number of singular values of the jacobian that are < eps; + * if the number of near zero singular values is > jac.col()-jac.row(), + * then the jacobian pseudoinverse is singular + */ + unsigned int getNrZeroSigmas()const {return nrZeroSigmas;}; + + /** + * Request the minimum of the first six singular values + */ + double getSigmaMin()const {return sigmaMin;}; + + /** + * Request the six singular values of the Jacobian + */ + int getSigma(Eigen::VectorXd& Sout); + + /** + * Request the value of eps + */ + double getEps()const {return eps;}; + + /** + * Request the value of lambda for the minimum + */ + double getLambda()const {return lambda;}; + + /** + * Request the scaled value of lambda for the minimum + * singular value 1-6 + */ + double getLambdaScaled()const {return lambda_scaled;}; + + /** + * Retrieve the latest return code from the SVD algorithm + * @return 0 if CartToJnt() not yet called, otherwise latest SVD result code. + */ + int getSVDResult()const {return svdResult;}; + + /// @copydoc KDL::SolverI::strError() + virtual const char* strError(const int error) const; + + /// @copydoc KDL::SolverI::updateInternalDataStructures() + virtual void updateInternalDataStructures(); + + private: + const Chain& chain; + ChainJntToJacSolver jnt2jac; + unsigned int nj; + Jacobian jac; + Eigen::MatrixXd U; + Eigen::VectorXd S; + Eigen::MatrixXd V; + double eps; + int maxiter; + Eigen::VectorXd tmp; + Eigen::MatrixXd tmp_jac; + Eigen::MatrixXd tmp_jac_weight1; + Eigen::MatrixXd tmp_jac_weight2; + Eigen::MatrixXd tmp_ts; + Eigen::MatrixXd tmp_js; + Eigen::MatrixXd weight_ts; + Eigen::MatrixXd weight_js; + double lambda; + double lambda_scaled; + unsigned int nrZeroSigmas ; + int svdResult; + double sigmaMin; + }; +} +#endif + diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainjnttojacdotsolver.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainjnttojacdotsolver.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9bb7e111b95d946d7af4b6c667e276df411f536b --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainjnttojacdotsolver.cpp @@ -0,0 +1,252 @@ +/* + Computes the Jacobian time derivative + Copyright (C) 2015 Antoine Hoarau + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + + +#include "chainjnttojacdotsolver.hpp" + +namespace KDL +{ + +const int ChainJntToJacDotSolver::E_JAC_DOT_FAILED; +const int ChainJntToJacDotSolver::E_JACSOLVER_FAILED; +const int ChainJntToJacDotSolver::E_FKSOLVERPOS_FAILED; + +const int ChainJntToJacDotSolver::HYBRID; +const int ChainJntToJacDotSolver::BODYFIXED; +const int ChainJntToJacDotSolver::INERTIAL; + +ChainJntToJacDotSolver::ChainJntToJacDotSolver(const Chain& _chain): + chain(_chain), + locked_joints_(chain.getNrOfJoints(),false), + nr_of_unlocked_joints_(chain.getNrOfJoints()), + jac_solver_(chain), + jac_(chain.getNrOfJoints()), + jac_dot_(chain.getNrOfJoints()), + representation_(HYBRID), + fk_solver_(chain) +{ +} + +void ChainJntToJacDotSolver::updateInternalDataStructures() { + locked_joints_.resize(chain.getNrOfJoints(),false); + this->setLockedJoints(locked_joints_); + jac_solver_.updateInternalDataStructures(); + jac_.resize(chain.getNrOfJoints()); + jac_dot_.resize(chain.getNrOfJoints()); + fk_solver_.updateInternalDataStructures(); +} + +int ChainJntToJacDotSolver::JntToJacDot(const JntArrayVel& q_in, Twist& jac_dot_q_dot, int seg_nr) +{ + error = JntToJacDot(q_in,jac_dot_,seg_nr); + if (error != E_NOERROR) { + return error; + } + MultiplyJacobian(jac_dot_,q_in.qdot,jac_dot_q_dot); + return (error = E_NOERROR); +} + +int ChainJntToJacDotSolver::JntToJacDot(const JntArrayVel& q_in, Jacobian& jdot, int seg_nr) +{ + if(locked_joints_.size() != chain.getNrOfJoints()) + return (error = E_NOT_UP_TO_DATE); + + unsigned int segmentNr; + if(seg_nr<0) + segmentNr=chain.getNrOfSegments(); + else + segmentNr = seg_nr; + + //Initialize Jacobian to zero since only segmentNr columns are computed + SetToZero(jdot) ; + + if(q_in.q.rows()!=chain.getNrOfJoints() || nr_of_unlocked_joints_!=jdot.columns()) + return (error = E_SIZE_MISMATCH); + else if(segmentNr>chain.getNrOfSegments()) + return (error = E_OUT_OF_RANGE); + + // First compute the jacobian in the Hybrid representation + if (jac_solver_.JntToJac(q_in.q,jac_,segmentNr) != E_NOERROR) + return (error = E_JACSOLVER_FAILED); + + // Change the reference frame and/or the reference point + if (representation_ != HYBRID) // If HYBRID do nothing is this is the default. + { + if (fk_solver_.JntToCart(q_in.q,F_bs_ee_,segmentNr) != E_NOERROR) + return (error = E_FKSOLVERPOS_FAILED); + if (representation_ == BODYFIXED) { + // Ref Frame {ee}, Ref Point {ee} + jac_.changeBase(F_bs_ee_.M.Inverse()); + } else if (representation_ == INERTIAL) { + // Ref Frame {bs}, Ref Point {bs} + jac_.changeRefPoint(-F_bs_ee_.p); + } else { + return (error = E_JAC_DOT_FAILED); + } + } + + // Let's compute Jdot in the corresponding representation + int k=0; + for(unsigned int i=0;it_djdq_); + return t_djdq_; + } +} + +const Twist& ChainJntToJacDotSolver::getPartialDerivativeHybrid(const KDL::Jacobian& bs_J_ee, + const unsigned int& joint_idx, + const unsigned int& column_idx) +{ + int j=joint_idx; + int i=column_idx; + + jac_j_ = bs_J_ee.getColumn(j); + jac_i_ = bs_J_ee.getColumn(i); + + SetToZero(t_djdq_); + + if(j < i) + { + // P_{\Delta}({}_{bs}J^{j}) ref (20) + t_djdq_.vel = jac_j_.rot * jac_i_.vel; + t_djdq_.rot = jac_j_.rot * jac_i_.rot; + }else if(j > i) + { + // M_{\Delta}({}_{bs}J^{j}) ref (23) + SetToZero(t_djdq_.rot); + t_djdq_.vel = -jac_j_.vel * jac_i_.rot; + }else if(j == i) + { + // ref (40) + SetToZero(t_djdq_.rot); + t_djdq_.vel = jac_i_.rot * jac_i_.vel; + } + return t_djdq_; +} + +const Twist& ChainJntToJacDotSolver::getPartialDerivativeBodyFixed(const Jacobian& ee_J_ee, + const unsigned int& joint_idx, + const unsigned int& column_idx) +{ + int j=joint_idx; + int i=column_idx; + + SetToZero(t_djdq_); + + if(j > i) + { + jac_j_ = ee_J_ee.getColumn(j); + jac_i_ = ee_J_ee.getColumn(i); + + // - S_d_(ee_J^j) * ee_J^ee ref (23) + t_djdq_.vel = jac_j_.rot * jac_i_.vel + jac_j_.vel * jac_i_.rot; + t_djdq_.rot = jac_j_.rot * jac_i_.rot; + t_djdq_ = -t_djdq_; + } + + return t_djdq_; +} +const Twist& ChainJntToJacDotSolver::getPartialDerivativeInertial(const KDL::Jacobian& bs_J_bs, + const unsigned int& joint_idx, + const unsigned int& column_idx) +{ + int j=joint_idx; + int i=column_idx; + + SetToZero(t_djdq_); + + if(j < i) + { + jac_j_ = bs_J_bs.getColumn(j); + jac_i_ = bs_J_bs.getColumn(i); + + // S_d_(bs_J^j) * bs_J^bs ref (23) + t_djdq_.vel = jac_j_.rot * jac_i_.vel + jac_j_.vel * jac_i_.rot; + t_djdq_.rot = jac_j_.rot * jac_i_.rot; + } + + return t_djdq_; +} +void ChainJntToJacDotSolver::setRepresentation(const int& representation) +{ + if(representation == HYBRID || + representation == BODYFIXED || + representation == INERTIAL) + this->representation_ = representation; +} + + +int ChainJntToJacDotSolver::setLockedJoints(const std::vector& locked_joints) +{ + if(locked_joints.size()!=locked_joints_.size()) + return E_SIZE_MISMATCH; + locked_joints_=locked_joints; + nr_of_unlocked_joints_=0; + for(unsigned int i=0;i + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + + +#ifndef KDL_CHAINJNTTOJACDOTSOLVER_HPP +#define KDL_CHAINJNTTOJACDOTSOLVER_HPP + +#include "solveri.hpp" +#include "frames.hpp" +#include "jntarrayvel.hpp" +#include "jacobian.hpp" +#include "chain.hpp" +#include "framevel.hpp" +#include "chainjnttojacsolver.hpp" +#include "chainfksolverpos_recursive.hpp" + +namespace KDL +{ + +/** + * @brief Computes the Jacobian time derivative (Jdot) by calculating the + * partial derivatives regarding to a joint angle, in the Hybrid, Body-fixed + * or Inertial representation. + * + * This work is based on : + * Symbolic differentiation of the velocity mapping for a serial kinematic chain + * H. Bruyninckx, J. De Schutter + * doi:10.1016/0094-114X(95)00069-B + * + * url : http://www.sciencedirect.com/science/article/pii/0094114X9500069B + */ +class ChainJntToJacDotSolver : public SolverI +{ +public: + static const int E_JAC_DOT_FAILED = -100; + static const int E_JACSOLVER_FAILED = -101; + static const int E_FKSOLVERPOS_FAILED = -102; + + // Hybrid representation ref Frame: base, ref Point: end-effector + static const int HYBRID = 0; + // Body-fixed representation ref Frame: end-effector, ref Point: end-effector + static const int BODYFIXED = 1; + // Inertial representation ref Frame: base, ref Point: base + static const int INERTIAL = 2; + + explicit ChainJntToJacDotSolver(const Chain& chain); + virtual ~ChainJntToJacDotSolver(); + /** + * @brief Computes \f$ {}_{bs}\dot{J}^{ee}.\dot{q} \f$ + * + * @param q_in Current joint positions and velocities + * @param jac_dot_q_dot The twist representing Jdot*qdot + * @param seg_nr The final segment to compute + * @return int 0 if no errors happened + */ + virtual int JntToJacDot(const KDL::JntArrayVel& q_in, KDL::Twist& jac_dot_q_dot, int seg_nr = -1); + /** + * @brief Computes \f$ {}_{bs}\dot{J}^{ee} \f$ + * + * @param q_in Current joint positions and velocities + * @param jdot The jacobian time derivative in the configured representation + * (HYBRID, BODYFIXED or INERTIAL) + * @param seg_nr The final segment to compute + * @return int 0 if no errors happened + */ + virtual int JntToJacDot(const KDL::JntArrayVel& q_in, KDL::Jacobian& jdot, int seg_nr = -1); + int setLockedJoints(const std::vector& locked_joints); + + /** + * @brief JntToJacDot() will compute in the Hybrid representation (ref Frame: base, ref Point: end-effector) + * + * + * @return void + */ + void setHybridRepresentation(){setRepresentation(HYBRID);} + /** + * @brief JntToJacDot() will compute in the Body-fixed representation (ref Frame: end-effector, ref Point: end-effector) + * + * @return void + */ + void setBodyFixedRepresentation(){setRepresentation(BODYFIXED);} + /** + * @brief JntToJacDot() will compute in the Inertial representation (ref Frame: base, ref Point: base) + * + * @return void + */ + void setInertialRepresentation(){setRepresentation(INERTIAL);} + /** + * @brief Sets the internal variable for the representation (with a check on the value) + * + * @param representation The representation for Jdot : HYBRID,BODYFIXED or INERTIAL + * @return void + */ + void setRepresentation(const int& representation); + + /// @copydoc KDL::SolverI::updateInternalDataStructures + virtual void updateInternalDataStructures(); + + /// @copydoc KDL::SolverI::strError() + virtual const char* strError(const int error) const; + + +protected: + /** + * @brief Computes \f$ \frac{\partial {}_{bs}J^{i,ee}}{\partial q^{j}}.\dot{q}^{j} \f$ + * + * @param bs_J_ee The Jacobian expressed in the base frame with the end effector as the reference point (default in KDL Jacobian Solver) + * @param joint_idx The index of the current joint (j in the formula) + * @param column_idx The index of the current column (i in the formula) + * @return Twist The twist representing dJi/dqj .qdotj + */ + const Twist& getPartialDerivativeHybrid(const Jacobian& bs_J_ee, + const unsigned int& joint_idx, + const unsigned int& column_idx); + /** + * @brief Computes \f$ \frac{\partial {}_{ee}J^{i,ee}}{\partial q^{j}}.\dot{q}^{j} \f$ + * + * @param bs_J_ee The Jacobian expressed in the end effector frame with the end effector as the reference point + * @param joint_idx The indice of the current joint (j in the formula) + * @param column_idx The indice of the current column (i in the formula) + * @return Twist The twist representing dJi/dqj .qdotj + */ + const Twist& getPartialDerivativeBodyFixed(const Jacobian& ee_J_ee, + const unsigned int& joint_idx, + const unsigned int& column_idx); + /** + * @brief Computes \f$ \frac{\partial {}_{bs}J^{i,bs}}{\partial q^{j}}.\dot{q}^{j} \f$ + * + * @param ee_J_ee The Jacobian expressed in the base frame with the base as the reference point + * @param joint_idx The indice of the current joint (j in the formula) + * @param column_idx The indice of the current column (i in the formula) + * @return Twist The twist representing dJi/dqj .qdotj + */ + const Twist& getPartialDerivativeInertial(const Jacobian& bs_J_bs, + const unsigned int& joint_idx, + const unsigned int& column_idx); + /** + * @brief Computes \f$ \frac{\partial J^{i,ee}}{\partial q^{j}}.\dot{q}^{j} \f$ + * + * @param bs_J_bs The Jacobian expressed in the base frame with the end effector as the reference point + * @param joint_idx The indice of the current joint (j in the formula) + * @param column_idx The indice of the current column (i in the formula) + * @param representation The representation (Hybrid,Body-fixed,Inertial) in which you want to get dJ/dqj .qdotj + * @return Twist The twist representing dJi/dqj .qdotj + */ + const Twist& getPartialDerivative(const Jacobian& J, + const unsigned int& joint_idx, + const unsigned int& column_idx, + const int& representation); +private: + + const Chain& chain; + std::vector locked_joints_; + unsigned int nr_of_unlocked_joints_; + ChainJntToJacSolver jac_solver_; + Jacobian jac_; + Jacobian jac_dot_; + int representation_; + ChainFkSolverPos_recursive fk_solver_; + Frame F_bs_ee_; + Twist jac_dot_k_; + Twist jac_j_, jac_i_; + Twist t_djdq_; +}; + +} +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainjnttojacsolver.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainjnttojacsolver.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c24a9a312b0d4275a2a8e97c093e57260adc6142 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/chainjnttojacsolver.cpp @@ -0,0 +1,101 @@ +// Copyright (C) 2007 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#include "chainjnttojacsolver.hpp" + +namespace KDL +{ + ChainJntToJacSolver::ChainJntToJacSolver(const Chain& _chain): + chain(_chain),locked_joints_(chain.getNrOfJoints(),false) + { + } + + void ChainJntToJacSolver::updateInternalDataStructures() { + locked_joints_.resize(chain.getNrOfJoints(),false); + } + ChainJntToJacSolver::~ChainJntToJacSolver() + { + } + + int ChainJntToJacSolver::setLockedJoints(const std::vector locked_joints) + { + if(locked_joints_.size() != chain.getNrOfJoints()) + return (error = E_NOT_UP_TO_DATE); + if(locked_joints.size()!=locked_joints_.size()) + return (error = E_SIZE_MISMATCH); + locked_joints_=locked_joints; + return (error = E_NOERROR); + } + + int ChainJntToJacSolver::JntToJac(const JntArray& q_in, Jacobian& jac, int seg_nr) + { + if(locked_joints_.size() != chain.getNrOfJoints()) + return (error = E_NOT_UP_TO_DATE); + unsigned int segmentNr; + if(seg_nr<0) + segmentNr=chain.getNrOfSegments(); + else + segmentNr = seg_nr; + + //Initialize Jacobian to zero since only segmentNr columns are computed + SetToZero(jac) ; + + if( q_in.rows()!=chain.getNrOfJoints() || jac.columns() != chain.getNrOfJoints()) + return (error = E_SIZE_MISMATCH); + else if(segmentNr>chain.getNrOfSegments()) + return (error = E_OUT_OF_RANGE); + + T_tmp = Frame::Identity(); + SetToZero(t_tmp); + int j=0; + int k=0; + Frame total; + for (unsigned int i=0;i + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#ifndef KDL_CHAINJNTTOJACSOLVER_HPP +#define KDL_CHAINJNTTOJACSOLVER_HPP + +#include "solveri.hpp" +#include "frames.hpp" +#include "jacobian.hpp" +#include "jntarray.hpp" +#include "chain.hpp" + +namespace KDL +{ + /** + * @brief Class to calculate the jacobian of a general + * KDL::Chain, it is used by other solvers. + */ + + class ChainJntToJacSolver : public SolverI + { + public: + + explicit ChainJntToJacSolver(const Chain& chain); + virtual ~ChainJntToJacSolver(); + /** + * Calculate the jacobian expressed in the base frame of the + * chain, with reference point at the end effector of the + * *chain. The algorithm is similar to the one used in + * KDL::ChainFkSolverVel_recursive + * + * @param q_in input joint positions + * @param jac output jacobian + * @param seg_nr The final segment to compute + * @return success/error code + */ + virtual int JntToJac(const JntArray& q_in, Jacobian& jac, int seg_nr=-1); + + /** + * + * @param locked_joints new values for locked joints + * @return success/error code + */ + int setLockedJoints(const std::vector locked_joints); + + /// @copydoc KDL::SolverI::updateInternalDataStructures + virtual void updateInternalDataStructures(); + + private: + const Chain& chain; + Twist t_tmp; + Frame T_tmp; + std::vector locked_joints_; + }; +} +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/config.h.in b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/config.h.in new file mode 100644 index 0000000000000000000000000000000000000000..a63d12a477195274228566372aa7bfb4b93bea56 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/config.h.in @@ -0,0 +1,37 @@ +// Copyright (C) 2014 Ruben Smits + +// Version: 1.0 +// Author: Brian Jensen +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#ifndef KDL_CONFIG_H +#define KDL_CONFIG_H + +#define KDL_VERSION_MAJOR @KDL_VERSION_MAJOR@ +#define KDL_VERSION_MINOR @KDL_VERSION_MINOR@ +#define KDL_VERSION_PATCH @KDL_VERSION_PATCH@ + +#define KDL_VERSION (KDL_VERSION_MAJOR << 16) | (KDL_VERSION_MINOR << 8) | KDL_VERSION_PATCH + +#define KDL_VERSION_STRING "@KDL_VERSION@" + +//Set which version of the Tree Interface to use +#cmakedefine HAVE_STL_CONTAINER_INCOMPLETE_TYPES +#cmakedefine KDL_USE_NEW_TREE_INTERFACE + +#endif //#define KDL_CONFIG_H diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/frameacc.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/frameacc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2ccedf27570c72a4a95a0d19cee4efec47a125c --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/frameacc.cpp @@ -0,0 +1,26 @@ +/***************************************************************************** + * \author + * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven + * + * \version + * ORO_Geometry V0.2 + * + * \par History + * - $log$ + * + * \par Release + * $Id: rrframes.cpp,v 1.1.1.1 2002/08/26 14:14:21 rmoreas Exp $ + * $Name: $ + ****************************************************************************/ + + +#include "frameacc.hpp" + +namespace KDL { + +#ifndef KDL_INLINE + #include "frameacc.inl" +#endif + +} + diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/frameacc.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/frameacc.hpp new file mode 100644 index 0000000000000000000000000000000000000000..a8cd3529bf56fe758ef9ca7f07dd1272aff042a1 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/frameacc.hpp @@ -0,0 +1,315 @@ +/***************************************************************************** + * \file + * This file contains the definition of classes for a + * Rall Algebra of (subset of) the classes defined in frames, + * i.e. classes that contain a set (value,derivative,2nd derivative) + * and define operations on that set + * this classes are useful for automatic differentiation ( <-> symbolic diff , + * <-> numeric diff). + * Defines VectorAcc, RotationAcc, FrameAcc, doubleAcc. + * Look at the corresponding classes Vector Rotation Frame Twist and + * Wrench for the semantics of the methods. + * + * It also contains the 2nd derivative <-> RFrames.h + * + * \author + * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven + * + * \version + * ORO_Geometry V0.2 + * + * \par History + * - $log$ + * + * \par Release + * $Id: rrframes.h,v 1.1.1.1 2002/08/26 14:14:21 rmoreas Exp $ + * $Name: $ + ****************************************************************************/ + +#ifndef RRFRAMES_H +#define RRFRAMES_H + + +#include "utilities/rall2d.h" +#include "frames.hpp" + + +namespace KDL { + +class TwistAcc; +typedef Rall2d doubleAcc; + +// Equal is friend function, but default arguments for friends are forbidden (§8.3.6.4) +class FrameAcc; +class RotationAcc; +class VectorAcc; + +IMETHOD bool Equal(const FrameAcc& r1,const FrameAcc& r2,double eps=epsilon); +IMETHOD bool Equal(const Frame& r1,const FrameAcc& r2,double eps=epsilon); +IMETHOD bool Equal(const FrameAcc& r1,const Frame& r2,double eps=epsilon); +IMETHOD bool Equal(const RotationAcc& r1,const RotationAcc& r2,double eps=epsilon); +IMETHOD bool Equal(const Rotation& r1,const RotationAcc& r2,double eps=epsilon); +IMETHOD bool Equal(const RotationAcc& r1,const Rotation& r2,double eps=epsilon); +IMETHOD bool Equal(const TwistAcc& a,const TwistAcc& b,double eps=epsilon); +IMETHOD bool Equal(const Twist& a,const TwistAcc& b,double eps=epsilon); +IMETHOD bool Equal(const TwistAcc& a,const Twist& b,double eps=epsilon); +IMETHOD bool Equal(const VectorAcc& r1,const VectorAcc& r2,double eps=epsilon); +IMETHOD bool Equal(const Vector& r1,const VectorAcc& r2,double eps=epsilon); +IMETHOD bool Equal(const VectorAcc& r1,const Vector& r2,double eps=epsilon); + + +class VectorAcc +{ +public: + Vector p; //!< position vector + Vector v; //!< velocity vector + Vector dv; //!< acceleration vector +public: + VectorAcc():p(),v(),dv() {} + explicit VectorAcc(const Vector& _p):p(_p),v(Vector::Zero()),dv(Vector::Zero()) {} + VectorAcc(const Vector& _p,const Vector& _v):p(_p),v(_v),dv(Vector::Zero()) {} + VectorAcc(const Vector& _p,const Vector& _v,const Vector& _dv): + p(_p),v(_v),dv(_dv) {} + IMETHOD VectorAcc& operator = (const VectorAcc& arg); + IMETHOD VectorAcc& operator = (const Vector& arg); + IMETHOD VectorAcc& operator += (const VectorAcc& arg); + IMETHOD VectorAcc& operator -= (const VectorAcc& arg); + IMETHOD static VectorAcc Zero(); + IMETHOD void ReverseSign(); + IMETHOD doubleAcc Norm(double eps=epsilon); + IMETHOD friend VectorAcc operator + (const VectorAcc& r1,const VectorAcc& r2); + IMETHOD friend VectorAcc operator - (const VectorAcc& r1,const VectorAcc& r2); + IMETHOD friend VectorAcc operator + (const Vector& r1,const VectorAcc& r2); + IMETHOD friend VectorAcc operator - (const Vector& r1,const VectorAcc& r2); + IMETHOD friend VectorAcc operator + (const VectorAcc& r1,const Vector& r2); + IMETHOD friend VectorAcc operator - (const VectorAcc& r1,const Vector& r2); + IMETHOD friend VectorAcc operator * (const VectorAcc& r1,const VectorAcc& r2); + IMETHOD friend VectorAcc operator * (const VectorAcc& r1,const Vector& r2); + IMETHOD friend VectorAcc operator * (const Vector& r1,const VectorAcc& r2); + IMETHOD friend VectorAcc operator * (const VectorAcc& r1,double r2); + IMETHOD friend VectorAcc operator * (double r1,const VectorAcc& r2); + IMETHOD friend VectorAcc operator * (const doubleAcc& r1,const VectorAcc& r2); + IMETHOD friend VectorAcc operator * (const VectorAcc& r2,const doubleAcc& r1); + IMETHOD friend VectorAcc operator*(const Rotation& R,const VectorAcc& x); + + IMETHOD friend VectorAcc operator / (const VectorAcc& r1,double r2); + IMETHOD friend VectorAcc operator / (const VectorAcc& r2,const doubleAcc& r1); + + + IMETHOD friend bool Equal(const VectorAcc& r1,const VectorAcc& r2,double eps); + IMETHOD friend bool Equal(const Vector& r1,const VectorAcc& r2,double eps); + IMETHOD friend bool Equal(const VectorAcc& r1,const Vector& r2,double eps); + IMETHOD friend VectorAcc operator - (const VectorAcc& r); + IMETHOD friend doubleAcc dot(const VectorAcc& lhs,const VectorAcc& rhs); + IMETHOD friend doubleAcc dot(const VectorAcc& lhs,const Vector& rhs); + IMETHOD friend doubleAcc dot(const Vector& lhs,const VectorAcc& rhs); +}; + + +class RotationAcc +{ +public: + Rotation R; //!< rotation matrix + Vector w; //!< angular velocity vector + Vector dw; //!< angular acceration vector +public: + RotationAcc():R(),w() {} + explicit RotationAcc(const Rotation& _R):R(_R),w(Vector::Zero()){} + RotationAcc(const Rotation& _R,const Vector& _w,const Vector& _dw): + R(_R),w(_w),dw(_dw) {} + IMETHOD RotationAcc& operator = (const RotationAcc& arg); + IMETHOD RotationAcc& operator = (const Rotation& arg); + IMETHOD static RotationAcc Identity(); + IMETHOD RotationAcc Inverse() const; + IMETHOD VectorAcc Inverse(const VectorAcc& arg) const; + IMETHOD VectorAcc Inverse(const Vector& arg) const; + IMETHOD VectorAcc operator*(const VectorAcc& arg) const; + IMETHOD VectorAcc operator*(const Vector& arg) const; + + // Rotations + // The SetRot.. functions set the value of *this to the appropriate rotation matrix. + // The Rot... static functions give the value of the appropriate rotation matrix back. + // The DoRot... functions apply a rotation R to *this,such that *this = *this * R. + // IMETHOD void DoRotX(const doubleAcc& angle); + // IMETHOD void DoRotY(const doubleAcc& angle); + // IMETHOD void DoRotZ(const doubleAcc& angle); + // IMETHOD static RRotation RotX(const doubleAcc& angle); + // IMETHOD static RRotation RotY(const doubleAcc& angle); + // IMETHOD static RRotation RotZ(const doubleAcc& angle); + + // IMETHOD void SetRot(const Vector& rotaxis,const doubleAcc& angle); + // Along an arbitrary axes. The norm of rotvec is neglected. + // IMETHOD static RotationAcc Rot(const Vector& rotvec,const doubleAcc& angle); + // rotvec has arbitrary norm + // rotation around a constant vector ! + // IMETHOD static RotationAcc Rot2(const Vector& rotvec,const doubleAcc& angle); + // rotvec is normalized. + // rotation around a constant vector ! + + IMETHOD friend RotationAcc operator* (const RotationAcc& r1,const RotationAcc& r2); + IMETHOD friend RotationAcc operator* (const Rotation& r1,const RotationAcc& r2); + IMETHOD friend RotationAcc operator* (const RotationAcc& r1,const Rotation& r2); + IMETHOD friend bool Equal(const RotationAcc& r1,const RotationAcc& r2,double eps); + IMETHOD friend bool Equal(const Rotation& r1,const RotationAcc& r2,double eps); + IMETHOD friend bool Equal(const RotationAcc& r1,const Rotation& r2,double eps); + IMETHOD TwistAcc Inverse(const TwistAcc& arg) const; + IMETHOD TwistAcc Inverse(const Twist& arg) const; + IMETHOD TwistAcc operator * (const TwistAcc& arg) const; + IMETHOD TwistAcc operator * (const Twist& arg) const; +}; + + +class FrameAcc +{ +public: + RotationAcc M; //!< Rotation,angular velocity, and angular acceleration of frame. + VectorAcc p; //!< Translation, velocity and acceleration of origin. +public: + FrameAcc(){} + explicit FrameAcc(const Frame& _T):M(_T.M),p(_T.p) {} + FrameAcc(const Frame& _T,const Twist& _t,const Twist& _dt): + M(_T.M,_t.rot,_dt.rot),p(_T.p,_t.vel,_dt.vel) {} + FrameAcc(const RotationAcc& _M,const VectorAcc& _p):M(_M),p(_p) {} + + IMETHOD FrameAcc& operator = (const FrameAcc& arg); + IMETHOD FrameAcc& operator = (const Frame& arg); + IMETHOD static FrameAcc Identity(); + IMETHOD FrameAcc Inverse() const; + IMETHOD VectorAcc Inverse(const VectorAcc& arg) const; + IMETHOD VectorAcc operator*(const VectorAcc& arg) const; + IMETHOD VectorAcc operator*(const Vector& arg) const; + IMETHOD VectorAcc Inverse(const Vector& arg) const; + IMETHOD Frame GetFrame() const; + IMETHOD Twist GetTwist() const; + IMETHOD Twist GetAccTwist() const; + IMETHOD friend FrameAcc operator * (const FrameAcc& f1,const FrameAcc& f2); + IMETHOD friend FrameAcc operator * (const Frame& f1,const FrameAcc& f2); + IMETHOD friend FrameAcc operator * (const FrameAcc& f1,const Frame& f2); + IMETHOD friend bool Equal(const FrameAcc& r1,const FrameAcc& r2,double eps); + IMETHOD friend bool Equal(const Frame& r1,const FrameAcc& r2,double eps); + IMETHOD friend bool Equal(const FrameAcc& r1,const Frame& r2,double eps); + + IMETHOD TwistAcc Inverse(const TwistAcc& arg) const; + IMETHOD TwistAcc Inverse(const Twist& arg) const; + IMETHOD TwistAcc operator * (const TwistAcc& arg) const; + IMETHOD TwistAcc operator * (const Twist& arg) const; +}; + + +//very similar to Wrench class. +class TwistAcc +{ +public: + VectorAcc vel; //!< translational velocity and its 1st and 2nd derivative + VectorAcc rot; //!< rotational velocity and its 1st and 2nd derivative +public: + + TwistAcc():vel(),rot() {}; + TwistAcc(const VectorAcc& _vel,const VectorAcc& _rot):vel(_vel),rot(_rot) {}; + + IMETHOD TwistAcc& operator-=(const TwistAcc& arg); + IMETHOD TwistAcc& operator+=(const TwistAcc& arg); + + IMETHOD friend TwistAcc operator*(const TwistAcc& lhs,double rhs); + IMETHOD friend TwistAcc operator*(double lhs,const TwistAcc& rhs); + IMETHOD friend TwistAcc operator/(const TwistAcc& lhs,double rhs); + + IMETHOD friend TwistAcc operator*(const TwistAcc& lhs,const doubleAcc& rhs); + IMETHOD friend TwistAcc operator*(const doubleAcc& lhs,const TwistAcc& rhs); + IMETHOD friend TwistAcc operator/(const TwistAcc& lhs,const doubleAcc& rhs); + + IMETHOD friend TwistAcc operator+(const TwistAcc& lhs,const TwistAcc& rhs); + IMETHOD friend TwistAcc operator-(const TwistAcc& lhs,const TwistAcc& rhs); + IMETHOD friend TwistAcc operator-(const TwistAcc& arg); + + IMETHOD friend void SetToZero(TwistAcc& v); + + static IMETHOD TwistAcc Zero(); + + IMETHOD void ReverseSign(); + + IMETHOD TwistAcc RefPoint(const VectorAcc& v_base_AB); + // Changes the reference point of the RTwist. + // The RVector v_base_AB is expressed in the same base as the RTwist + // The RVector v_base_AB is a RVector from the old point to + // the new point. + // Complexity : 6M+6A + + IMETHOD friend bool Equal(const TwistAcc& a,const TwistAcc& b,double eps); + IMETHOD friend bool Equal(const Twist& a,const TwistAcc& b,double eps); + IMETHOD friend bool Equal(const TwistAcc& a,const Twist& b,double eps); + + + IMETHOD Twist GetTwist() const; + IMETHOD Twist GetTwistDot() const; + + friend class RotationAcc; + friend class FrameAcc; + +}; + +#ifdef KDL_INLINE +#include "frameacc.inl" +#endif + +} // namespace KDL + +template<> struct std::hash +{ + std::size_t operator()(KDL::doubleAcc const& da) const noexcept + { + size_t seed = 0; + KDL::hash_combine(seed, da.t); + KDL::hash_combine(seed, da.d); + KDL::hash_combine(seed, da.dd); + return seed; + } +}; + +template<> struct std::hash +{ + std::size_t operator()(KDL::VectorAcc const& va) const noexcept + { + size_t seed = 0; + KDL::hash_combine(seed, va.p); + KDL::hash_combine(seed, va.v); + KDL::hash_combine(seed, va.dv); + return seed; + } +}; + +template<> struct std::hash +{ + std::size_t operator()(KDL::RotationAcc const& ra) const noexcept + { + size_t seed = 0; + KDL::hash_combine(seed, ra.R); + KDL::hash_combine(seed, ra.w); + KDL::hash_combine(seed, ra.dw); + return seed; + } +}; + +template<> struct std::hash +{ + std::size_t operator()(KDL::FrameAcc const& fa) const noexcept + { + size_t seed = 0; + KDL::hash_combine(seed, fa.M); + KDL::hash_combine(seed, fa.p); + return seed; + } +}; + +template<> struct std::hash +{ + std::size_t operator()(KDL::TwistAcc const& ta) const noexcept + { + size_t seed = 0; + KDL::hash_combine(seed, ta.vel); + KDL::hash_combine(seed, ta.rot); + return seed; + } +}; + +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/frameacc.inl b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/frameacc.inl new file mode 100644 index 0000000000000000000000000000000000000000..fa9c23ce4837e5109c53f066ea31038c25e43014 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/frameacc.inl @@ -0,0 +1,598 @@ +/***************************************************************************** + * \file + * provides inline functions of rrframes.h + * + * \author + * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven + * + * \version + * ORO_Geometry V0.2 + * + * \par History + * - $log$ + * + * \par Release + * $Id: rrframes.inl,v 1.1.1.1 2002/08/26 14:14:21 rmoreas Exp $ + * $Name: $ + ****************************************************************************/ + + + + +/////////////////// VectorAcc ///////////////////////////////////// + +VectorAcc operator + (const VectorAcc& r1,const VectorAcc& r2) { + return VectorAcc(r1.p+r2.p,r1.v+r2.v,r1.dv+r2.dv); +} + +VectorAcc operator - (const VectorAcc& r1,const VectorAcc& r2) { + return VectorAcc(r1.p-r2.p, r1.v-r2.v, r1.dv-r2.dv); +} +VectorAcc operator + (const Vector& r1,const VectorAcc& r2) { + return VectorAcc(r1+r2.p,r2.v,r2.dv); +} + +VectorAcc operator - (const Vector& r1,const VectorAcc& r2) { + return VectorAcc(r1-r2.p, -r2.v, -r2.dv); +} +VectorAcc operator + (const VectorAcc& r1,const Vector& r2) { + return VectorAcc(r1.p+r2,r1.v,r1.dv); +} + +VectorAcc operator - (const VectorAcc& r1,const Vector& r2) { + return VectorAcc(r1.p-r2, r1.v, r1.dv); +} + +// unary - +VectorAcc operator - (const VectorAcc& r) { + return VectorAcc(-r.p,-r.v,-r.dv); +} + +// cross prod. +VectorAcc operator * (const VectorAcc& r1,const VectorAcc& r2) { + return VectorAcc(r1.p*r2.p, + r1.p*r2.v+r1.v*r2.p, + r1.dv*r2.p+2*r1.v*r2.v+r1.p*r2.dv + ); +} + +VectorAcc operator * (const VectorAcc& r1,const Vector& r2) { + return VectorAcc(r1.p*r2, r1.v*r2, r1.dv*r2 ); +} + +VectorAcc operator * (const Vector& r1,const VectorAcc& r2) { + return VectorAcc(r1*r2.p, r1*r2.v, r1*r2.dv ); +} + + + +// scalar mult. +VectorAcc operator * (double r1,const VectorAcc& r2) { + return VectorAcc(r1*r2.p, r1*r2.v, r1*r2.dv ); +} + +VectorAcc operator * (const VectorAcc& r1,double r2) { + return VectorAcc(r1.p*r2, r1.v*r2, r1.dv*r2 ); +} + +VectorAcc operator * (const doubleAcc& r1,const VectorAcc& r2) { + return VectorAcc(r1.t*r2.p, + r1.t*r2.v + r1.d*r2.p, + r1.t*r2.dv + 2*r1.d*r2.v + r1.dd*r2.p + ); +} + +VectorAcc operator * (const VectorAcc& r2,const doubleAcc& r1) { + return VectorAcc(r1.t*r2.p, + r1.t*r2.v + r1.d*r2.p, + r1.t*r2.dv + 2*r1.d*r2.v + r1.dd*r2.p + ); +} + +VectorAcc& VectorAcc::operator = (const VectorAcc& arg) { + p=arg.p; + v=arg.v; + dv=arg.dv; + return *this; +} + +VectorAcc& VectorAcc::operator = (const Vector& arg) { + p=arg; + v=Vector::Zero(); + dv=Vector::Zero(); + return *this; +} + +VectorAcc& VectorAcc::operator += (const VectorAcc& arg) { + p+=arg.p; + v+=arg.v; + dv+= arg.dv; + return *this; +} +VectorAcc& VectorAcc::operator -= (const VectorAcc& arg) { + p-=arg.p; + v-=arg.v; + dv-=arg.dv; + return *this; +} + +VectorAcc VectorAcc::Zero() { + return VectorAcc(Vector::Zero(),Vector::Zero(),Vector::Zero()); +} + +void VectorAcc::ReverseSign() { + p.ReverseSign(); + v.ReverseSign(); + dv.ReverseSign(); +} + +doubleAcc VectorAcc::Norm(double eps) { + doubleAcc res; + res.t = p.Norm(eps); + res.d = dot(p,v)/res.t; + res.dd = (dot(p,dv)+dot(v,v)-res.d*res.d)/res.t; + return res; +} + +doubleAcc dot(const VectorAcc& lhs,const VectorAcc& rhs) { + return doubleAcc( dot(lhs.p,rhs.p), + dot(lhs.p,rhs.v)+dot(lhs.v,rhs.p), + dot(lhs.p,rhs.dv)+2*dot(lhs.v,rhs.v)+dot(lhs.dv,rhs.p) + ); +} + +doubleAcc dot(const VectorAcc& lhs,const Vector& rhs) { + return doubleAcc( dot(lhs.p,rhs), + dot(lhs.v,rhs), + dot(lhs.dv,rhs) + ); +} + +doubleAcc dot(const Vector& lhs,const VectorAcc& rhs) { + return doubleAcc( dot(lhs,rhs.p), + dot(lhs,rhs.v), + dot(lhs,rhs.dv) + ); +} + + +bool Equal(const VectorAcc& r1,const VectorAcc& r2,double eps) { + return (Equal(r1.p,r2.p,eps) + && Equal(r1.v,r2.v,eps) + && Equal(r1.dv,r2.dv,eps) + ); +} + +bool Equal(const Vector& r1,const VectorAcc& r2,double eps) { + return (Equal(r1,r2.p,eps) + && Equal(Vector::Zero(),r2.v,eps) + && Equal(Vector::Zero(),r2.dv,eps) + ); +} + +bool Equal(const VectorAcc& r1,const Vector& r2,double eps) { + return (Equal(r1.p,r2,eps) + && Equal(r1.v,Vector::Zero(),eps) + && Equal(r1.dv,Vector::Zero(),eps) + ); +} + +VectorAcc operator / (const VectorAcc& r1,double r2) { + return r1*(1.0/r2); +} + +VectorAcc operator / (const VectorAcc& r2,const doubleAcc& r1) { + return r2*(1.0/r1); +} + + + +/////////////////// RotationAcc ///////////////////////////////////// + +RotationAcc operator* (const RotationAcc& r1,const RotationAcc& r2) { + return RotationAcc( r1.R * r2.R, + r1.w + r1.R*r2.w, + r1.dw + r1.w*(r1.R*r2.w) + r1.R*r2.dw + ); +} + +RotationAcc operator* (const Rotation& r1,const RotationAcc& r2) { + return RotationAcc( r1*r2.R, r1*r2.w, r1*r2.dw); +} + +RotationAcc operator* (const RotationAcc& r1,const Rotation& r2) { + return RotationAcc( r1.R*r2, r1.w, r1.dw ); +} + +RotationAcc& RotationAcc::operator = (const RotationAcc& arg) { + R=arg.R; + w=arg.w; + dw=arg.dw; + return *this; +} +RotationAcc& RotationAcc::operator = (const Rotation& arg) { + R = arg; + w = Vector::Zero(); + dw = Vector::Zero(); + return *this; +} + +RotationAcc RotationAcc::Identity() { + return RotationAcc(Rotation::Identity(),Vector::Zero(),Vector::Zero()); +} + +RotationAcc RotationAcc::Inverse() const { + return RotationAcc(R.Inverse(),-R.Inverse(w),-R.Inverse(dw)); +} + +VectorAcc RotationAcc::Inverse(const VectorAcc& arg) const { + VectorAcc tmp; + tmp.p = R.Inverse(arg.p); + tmp.v = R.Inverse(arg.v - w * arg.p); + tmp.dv = R.Inverse(arg.dv - dw*arg.p - w*(arg.v+R*tmp.v)); + return tmp; +} + +VectorAcc RotationAcc::Inverse(const Vector& arg) const { + VectorAcc tmp; + tmp.p = R.Inverse(arg); + tmp.v = R.Inverse(-w*arg); + tmp.dv = R.Inverse(-dw*arg - w*(R*tmp.v)); + return tmp; +} + + +VectorAcc RotationAcc::operator*(const VectorAcc& arg) const { + VectorAcc tmp; + tmp.p = R*arg.p; + tmp.dv = R*arg.v; + tmp.v = w*tmp.p + tmp.dv; + tmp.dv = dw*tmp.p + w*(tmp.v + tmp.dv) + R*arg.dv; + return tmp; +} + +VectorAcc operator*(const Rotation& R,const VectorAcc& x) { + return VectorAcc(R*x.p,R*x.v,R*x.dv); +} + +VectorAcc RotationAcc::operator*(const Vector& arg) const { + VectorAcc tmp; + tmp.p = R*arg; + tmp.v = w*tmp.p; + tmp.dv = dw*tmp.p + w*tmp.v; + return tmp; +} + +/* + // = Rotations + // The Rot... static functions give the value of the appropriate rotation matrix back. + // The DoRot... functions apply a rotation R to *this,such that *this = *this * R. + + void RRotation::DoRotX(const RDouble& angle) { + w+=R*Vector(angle.grad,0,0); + R.DoRotX(angle.t); + } +RotationAcc RotationAcc::RotX(const doubleAcc& angle) { + return RotationAcc(Rotation::RotX(angle.t), + Vector(angle.d,0,0), + Vector(angle.dd,0,0) + ); +} + + void RRotation::DoRotY(const RDouble& angle) { + w+=R*Vector(0,angle.grad,0); + R.DoRotY(angle.t); + } +RotationAcc RotationAcc::RotY(const doubleAcc& angle) { + return RotationAcc( + Rotation::RotX(angle.t), + Vector(0,angle.d,0), + Vector(0,angle.dd,0) + ); +} + + void RRotation::DoRotZ(const RDouble& angle) { + w+=R*Vector(0,0,angle.grad); + R.DoRotZ(angle.t); + } +RotationAcc RotationAcc::RotZ(const doubleAcc& angle) { + return RotationAcc( + Rotation::RotZ(angle.t), + Vector(0,0,angle.d), + Vector(0,0,angle.dd) + ); +} + + + RRotation RRotation::Rot(const Vector& rotvec,const RDouble& angle) + // rotvec has arbitrary norm + // rotation around a constant vector ! + { + Vector v = rotvec.Normalize(); + return RRotation(Rotation::Rot2(v,angle.t),v*angle.grad); + } + + RRotation RRotation::Rot2(const Vector& rotvec,const RDouble& angle) + // rotvec is normalized. + { + return RRotation(Rotation::Rot2(rotvec,angle.t),rotvec*angle.grad); + } + +*/ + +bool Equal(const RotationAcc& r1,const RotationAcc& r2,double eps) { + return (Equal(r1.w,r2.w,eps) && Equal(r1.R,r2.R,eps) && Equal(r1.dw,r2.dw,eps) ); +} +bool Equal(const Rotation& r1,const RotationAcc& r2,double eps) { + return (Equal(Vector::Zero(),r2.w,eps) && Equal(r1,r2.R,eps) && + Equal(Vector::Zero(),r2.dw,eps) ); +} +bool Equal(const RotationAcc& r1,const Rotation& r2,double eps) { + return (Equal(r1.w,Vector::Zero(),eps) && Equal(r1.R,r2,eps) && + Equal(r1.dw,Vector::Zero(),eps) ); +} + + +// Methods and operators related to FrameAcc +// They all delegate most of the work to RotationAcc and VectorAcc +FrameAcc& FrameAcc::operator = (const FrameAcc& arg) { + M=arg.M; + p=arg.p; + return *this; +} + +FrameAcc FrameAcc::Identity() { + return FrameAcc(RotationAcc::Identity(),VectorAcc::Zero()); +} + + +FrameAcc operator *(const FrameAcc& lhs,const FrameAcc& rhs) +{ + return FrameAcc(lhs.M*rhs.M,lhs.M*rhs.p+lhs.p); +} +FrameAcc operator *(const FrameAcc& lhs,const Frame& rhs) +{ + return FrameAcc(lhs.M*rhs.M,lhs.M*rhs.p+lhs.p); +} +FrameAcc operator *(const Frame& lhs,const FrameAcc& rhs) +{ + return FrameAcc(lhs.M*rhs.M,lhs.M*rhs.p+lhs.p); +} + +VectorAcc FrameAcc::operator *(const VectorAcc & arg) const +{ + return M*arg+p; +} +VectorAcc FrameAcc::operator *(const Vector & arg) const +{ + return M*arg+p; +} + +VectorAcc FrameAcc::Inverse(const VectorAcc& arg) const +{ + return M.Inverse(arg-p); +} + +VectorAcc FrameAcc::Inverse(const Vector& arg) const +{ + return M.Inverse(arg-p); +} + +FrameAcc FrameAcc::Inverse() const +{ + return FrameAcc(M.Inverse(),-M.Inverse(p)); +} + +FrameAcc& FrameAcc::operator =(const Frame & arg) +{ + M = arg.M; + p = arg.p; + return *this; +} + +bool Equal(const FrameAcc& r1,const FrameAcc& r2,double eps) { + return (Equal(r1.M,r2.M,eps) && Equal(r1.p,r2.p,eps)); +} +bool Equal(const Frame& r1,const FrameAcc& r2,double eps) { + return (Equal(r1.M,r2.M,eps) && Equal(r1.p,r2.p,eps)); +} +bool Equal(const FrameAcc& r1,const Frame& r2,double eps) { + return (Equal(r1.M,r2.M,eps) && Equal(r1.p,r2.p,eps)); +} + + +Frame FrameAcc::GetFrame() const { + return Frame(M.R,p.p); +} + + +Twist FrameAcc::GetTwist() const { + return Twist(p.v,M.w); +} + + +Twist FrameAcc::GetAccTwist() const { + return Twist(p.dv,M.dw); +} + + + + + + + + + + + + + + + + + +TwistAcc TwistAcc::Zero() +{ + return TwistAcc(VectorAcc::Zero(),VectorAcc::Zero()); +} + + +void TwistAcc::ReverseSign() +{ + vel.ReverseSign(); + rot.ReverseSign(); +} + +TwistAcc TwistAcc::RefPoint(const VectorAcc& v_base_AB) + // Changes the reference point of the TwistAcc. + // The RVector v_base_AB is expressed in the same base as the TwistAcc + // The RVector v_base_AB is a RVector from the old point to + // the new point. + // Complexity : 6M+6A +{ + return TwistAcc(this->vel+this->rot*v_base_AB,this->rot); +} + +TwistAcc& TwistAcc::operator-=(const TwistAcc& arg) +{ + vel-=arg.vel; + rot -=arg.rot; + return *this; +} + +TwistAcc& TwistAcc::operator+=(const TwistAcc& arg) +{ + vel+=arg.vel; + rot +=arg.rot; + return *this; +} + + +TwistAcc operator*(const TwistAcc& lhs,double rhs) +{ + return TwistAcc(lhs.vel*rhs,lhs.rot*rhs); +} + +TwistAcc operator*(double lhs,const TwistAcc& rhs) +{ + return TwistAcc(lhs*rhs.vel,lhs*rhs.rot); +} + +TwistAcc operator/(const TwistAcc& lhs,double rhs) +{ + return TwistAcc(lhs.vel/rhs,lhs.rot/rhs); +} + + +TwistAcc operator*(const TwistAcc& lhs,const doubleAcc& rhs) +{ + return TwistAcc(lhs.vel*rhs,lhs.rot*rhs); +} + +TwistAcc operator*(const doubleAcc& lhs,const TwistAcc& rhs) +{ + return TwistAcc(lhs*rhs.vel,lhs*rhs.rot); +} + +TwistAcc operator/(const TwistAcc& lhs,const doubleAcc& rhs) +{ + return TwistAcc(lhs.vel/rhs,lhs.rot/rhs); +} + + + +// addition of TwistAcc's +TwistAcc operator+(const TwistAcc& lhs,const TwistAcc& rhs) +{ + return TwistAcc(lhs.vel+rhs.vel,lhs.rot+rhs.rot); +} + +TwistAcc operator-(const TwistAcc& lhs,const TwistAcc& rhs) +{ + return TwistAcc(lhs.vel-rhs.vel,lhs.rot-rhs.rot); +} + +// unary - +TwistAcc operator-(const TwistAcc& arg) +{ + return TwistAcc(-arg.vel,-arg.rot); +} + + + + + +TwistAcc RotationAcc::Inverse(const TwistAcc& arg) const +{ + return TwistAcc(Inverse(arg.vel),Inverse(arg.rot)); +} + +TwistAcc RotationAcc::operator * (const TwistAcc& arg) const +{ + return TwistAcc((*this)*arg.vel,(*this)*arg.rot); +} + +TwistAcc RotationAcc::Inverse(const Twist& arg) const +{ + return TwistAcc(Inverse(arg.vel),Inverse(arg.rot)); +} + +TwistAcc RotationAcc::operator * (const Twist& arg) const +{ + return TwistAcc((*this)*arg.vel,(*this)*arg.rot); +} + + +TwistAcc FrameAcc::operator * (const TwistAcc& arg) const +{ + TwistAcc tmp; + tmp.rot = M*arg.rot; + tmp.vel = M*arg.vel+p*tmp.rot; + return tmp; +} + +TwistAcc FrameAcc::operator * (const Twist& arg) const +{ + TwistAcc tmp; + tmp.rot = M*arg.rot; + tmp.vel = M*arg.vel+p*tmp.rot; + return tmp; +} + +TwistAcc FrameAcc::Inverse(const TwistAcc& arg) const +{ + TwistAcc tmp; + tmp.rot = M.Inverse(arg.rot); + tmp.vel = M.Inverse(arg.vel-p*arg.rot); + return tmp; +} + +TwistAcc FrameAcc::Inverse(const Twist& arg) const +{ + TwistAcc tmp; + tmp.rot = M.Inverse(arg.rot); + tmp.vel = M.Inverse(arg.vel-p*arg.rot); + return tmp; +} + +Twist TwistAcc::GetTwist() const { + return Twist(vel.p,rot.p); +} + +Twist TwistAcc::GetTwistDot() const { + return Twist(vel.v,rot.v); +} + +bool Equal(const TwistAcc& a,const TwistAcc& b,double eps) { + return (Equal(a.rot,b.rot,eps)&& + Equal(a.vel,b.vel,eps) ); +} +bool Equal(const Twist& a,const TwistAcc& b,double eps) { + return (Equal(a.rot,b.rot,eps)&& + Equal(a.vel,b.vel,eps) ); +} +bool Equal(const TwistAcc& a,const Twist& b,double eps) { + return (Equal(a.rot,b.rot,eps)&& + Equal(a.vel,b.vel,eps) ); +} + diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/frameacc_io.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/frameacc_io.hpp new file mode 100644 index 0000000000000000000000000000000000000000..ee74fc773636680040138e9695d8cbbe57bae93f --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/frameacc_io.hpp @@ -0,0 +1,58 @@ +/***************************************************************************** + * \file + * Defines I/O related routines to the FrameAccs classes defined in + * FrameAccs.h + * + * \author + * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven + * + * \version + * ORO_Geometry V0.2 + * + * \par History + * - $log$ + * + * \par Release + * $Id: rrframes_io.h,v 1.1.1.1 2002/08/26 14:14:21 rmoreas Exp $ + * $Name: $ + ****************************************************************************/ +#ifndef RRFRAMES_IO +#define RRFRAMES_IO + +#include "utilities/utility_io.h" +#include "utilities/rall2d_io.h" + +#include "frames_io.hpp" +#include "frameacc.hpp" + +namespace KDL { + + +// Output... +inline std::ostream& operator << (std::ostream& os,const VectorAcc& r) { + os << "{" << r.p << "," << r.v << "," << r.dv << "}" << std::endl; + return os; +} + +inline std::ostream& operator << (std::ostream& os,const RotationAcc& r) { + os << "{" << std::endl << r.R << "," << std::endl << r.w << + "," << std::endl << r.dw << std::endl << "}" << std::endl; + return os; +} + + +inline std::ostream& operator << (std::ostream& os,const FrameAcc& r) { + os << "{" << std::endl << r.M << "," << std::endl << r.p << "}" << std::endl; + return os; +} +inline std::ostream& operator << (std::ostream& os,const TwistAcc& r) { + os << "{" << std::endl << r.vel << "," << std::endl << r.rot << std::endl << "}" << std::endl; + return os; +} + + +} // namespace Frame + + + +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/frames.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/frames.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4d478def4f3581598569f85b8912a14d61d18b00 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/frames.cpp @@ -0,0 +1,445 @@ +/*************************************************************************** + frames.cxx - description + ------------------------- + begin : June 2006 + copyright : (C) 2006 Erwin Aertbelien + email : firstname.lastname@mech.kuleuven.ac.be + + History (only major changes)( AUTHOR-Description ) : + + *************************************************************************** + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; if not, write to the Free Software * + * Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307 USA * + * * + ***************************************************************************/ + +#include "frames.hpp" +#include "utilities/utility.h" + +#include + +namespace KDL { + +#ifndef KDL_INLINE +#include "frames.inl" +#endif + + void Frame::Make4x4(double * d) + { + int i; + int j; + for (i=0;i<3;i++) { + for (j=0;j<3;j++) + d[i*4+j]=M(i,j); + d[i*4+3] = p(i); + } + for (j=0;j<3;j++) + d[12+j] = 0.; + d[15] = 1; + } + + Frame Frame::DH_Craig1989(double a,double alpha,double d,double theta) + // returns Modified Denavit-Hartenberg parameters (According to Craig) + { + double ct,st,ca,sa; + ct = cos(theta); + st = sin(theta); + sa = sin(alpha); + ca = cos(alpha); + return Frame(Rotation( + ct, -st, 0, + st*ca, ct*ca, -sa, + st*sa, ct*sa, ca ), + Vector( + a, -sa*d, ca*d ) + ); + } + + Frame Frame::DH(double a,double alpha,double d,double theta) + // returns Denavit-Hartenberg parameters (Non-Modified DH) + { + double ct,st,ca,sa; + ct = cos(theta); + st = sin(theta); + sa = sin(alpha); + ca = cos(alpha); + return Frame(Rotation( + ct, -st*ca, st*sa, + st, ct*ca, -ct*sa, + 0, sa, ca ), + Vector( + a*ct, a*st, d ) + ); + } + + double Vector2::Norm(double eps) const + { + double tmp1 = fabs(data[0]); + double tmp2 = fabs(data[1]); + + if (tmp1 < eps && tmp2 < eps) + return 0; + + if (tmp1 > tmp2) { + return tmp1*sqrt(1+sqr(data[1]/data[0])); + } else { + return tmp2*sqrt(1+sqr(data[0]/data[1])); + } + } + // makes v a unitvector and returns the norm of v. + // if v is smaller than eps, Vector(1,0) is returned with norm 0. + // if this is not good, check the return value of this method. + double Vector2::Normalize(double eps) { + double v = this->Norm(); + if (v < eps) { + *this = Vector2(1,0); + return 0; + } else { + *this = (*this)/v; + return v; + } + } + + + // do some effort not to lose precision + double Vector::Norm(double eps) const + { + double tmp1; + double tmp2; + tmp1 = fabs(data[0]); + tmp2 = fabs(data[1]); + if (tmp1 >= tmp2) { + tmp2=fabs(data[2]); + if (tmp1 >= tmp2) { + if (tmp1 < eps) { + // only to everything exactly zero case, all other are handled correctly + return 0; + } + return tmp1*sqrt(1+sqr(data[1]/data[0])+sqr(data[2]/data[0])); + } else { + return tmp2*sqrt(1+sqr(data[0]/data[2])+sqr(data[1]/data[2])); + } + } else { + tmp1=fabs(data[2]); + if (tmp2 > tmp1) { + return tmp2*sqrt(1+sqr(data[0]/data[1])+sqr(data[2]/data[1])); + } else { + return tmp1*sqrt(1+sqr(data[0]/data[2])+sqr(data[1]/data[2])); + } + } + } + + // makes v a unitvector and returns the norm of v. + // if v is smaller than eps, Vector(1,0,0) is returned with norm 0. + // if this is not good, check the return value of this method. + double Vector::Normalize(double eps) { + double v = this->Norm(); + if (v < eps) { + *this = Vector(1,0,0); + return 0; + } else { + *this = (*this)/v; + return v; + } + } + + + bool Equal(const Rotation& a,const Rotation& b,double eps) { + return (Equal(a.data[0],b.data[0],eps) && + Equal(a.data[1],b.data[1],eps) && + Equal(a.data[2],b.data[2],eps) && + Equal(a.data[3],b.data[3],eps) && + Equal(a.data[4],b.data[4],eps) && + Equal(a.data[5],b.data[5],eps) && + Equal(a.data[6],b.data[6],eps) && + Equal(a.data[7],b.data[7],eps) && + Equal(a.data[8],b.data[8],eps) ); + } + + + + Rotation operator *(const Rotation& lhs,const Rotation& rhs) + // Complexity : 27M+27A + { + return Rotation( + lhs.data[0]*rhs.data[0]+lhs.data[1]*rhs.data[3]+lhs.data[2]*rhs.data[6], + lhs.data[0]*rhs.data[1]+lhs.data[1]*rhs.data[4]+lhs.data[2]*rhs.data[7], + lhs.data[0]*rhs.data[2]+lhs.data[1]*rhs.data[5]+lhs.data[2]*rhs.data[8], + lhs.data[3]*rhs.data[0]+lhs.data[4]*rhs.data[3]+lhs.data[5]*rhs.data[6], + lhs.data[3]*rhs.data[1]+lhs.data[4]*rhs.data[4]+lhs.data[5]*rhs.data[7], + lhs.data[3]*rhs.data[2]+lhs.data[4]*rhs.data[5]+lhs.data[5]*rhs.data[8], + lhs.data[6]*rhs.data[0]+lhs.data[7]*rhs.data[3]+lhs.data[8]*rhs.data[6], + lhs.data[6]*rhs.data[1]+lhs.data[7]*rhs.data[4]+lhs.data[8]*rhs.data[7], + lhs.data[6]*rhs.data[2]+lhs.data[7]*rhs.data[5]+lhs.data[8]*rhs.data[8] + ); + + } + + Rotation Rotation::Quaternion(double x,double y,double z, double w) + { + double x2, y2, z2, w2; + x2 = x*x; y2 = y*y; z2 = z*z; w2 = w*w; + return Rotation(w2+x2-y2-z2, 2*x*y-2*w*z, 2*x*z+2*w*y, + 2*x*y+2*w*z, w2-x2+y2-z2, 2*y*z-2*w*x, + 2*x*z-2*w*y, 2*y*z+2*w*x, w2-x2-y2+z2); + } + + /* From the following sources: + http://web.archive.org/web/20041029003853/http:/www.j3d.org/matrix_faq/matrfaq_latest.html + http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm + RobOOP::quaternion.cpp + */ + void Rotation::GetQuaternion(double& x,double& y,double& z, double& w) const + { + double trace = (*this)(0,0) + (*this)(1,1) + (*this)(2,2); + double epsilon=1E-12; + if( trace > epsilon ){ + double s = 0.5 / sqrt(trace + 1.0); + w = 0.25 / s; + x = ( (*this)(2,1) - (*this)(1,2) ) * s; + y = ( (*this)(0,2) - (*this)(2,0) ) * s; + z = ( (*this)(1,0) - (*this)(0,1) ) * s; + }else{ + if ( (*this)(0,0) > (*this)(1,1) && (*this)(0,0) > (*this)(2,2) ){ + double s = 2.0 * sqrt( 1.0 + (*this)(0,0) - (*this)(1,1) - (*this)(2,2)); + w = ((*this)(2,1) - (*this)(1,2) ) / s; + x = 0.25 * s; + y = ((*this)(0,1) + (*this)(1,0) ) / s; + z = ((*this)(0,2) + (*this)(2,0) ) / s; + } else if ((*this)(1,1) > (*this)(2,2)) { + double s = 2.0 * sqrt( 1.0 + (*this)(1,1) - (*this)(0,0) - (*this)(2,2)); + w = ((*this)(0,2) - (*this)(2,0) ) / s; + x = ((*this)(0,1) + (*this)(1,0) ) / s; + y = 0.25 * s; + z = ((*this)(1,2) + (*this)(2,1) ) / s; + }else { + double s = 2.0 * sqrt( 1.0 + (*this)(2,2) - (*this)(0,0) - (*this)(1,1) ); + w = ((*this)(1,0) - (*this)(0,1) ) / s; + x = ((*this)(0,2) + (*this)(2,0) ) / s; + y = ((*this)(1,2) + (*this)(2,1) ) / s; + z = 0.25 * s; + } + } + } + +Rotation Rotation::RPY(double roll,double pitch,double yaw) + { + double ca1,cb1,cc1,sa1,sb1,sc1; + ca1 = cos(yaw); sa1 = sin(yaw); + cb1 = cos(pitch);sb1 = sin(pitch); + cc1 = cos(roll);sc1 = sin(roll); + return Rotation(ca1*cb1,ca1*sb1*sc1 - sa1*cc1,ca1*sb1*cc1 + sa1*sc1, + sa1*cb1,sa1*sb1*sc1 + ca1*cc1,sa1*sb1*cc1 - ca1*sc1, + -sb1,cb1*sc1,cb1*cc1); + } + +// Gives back a rotation matrix specified with RPY convention +void Rotation::GetRPY(double& roll,double& pitch,double& yaw) const + { + double epsilon=1E-12; + pitch = atan2(-data[6], sqrt( sqr(data[0]) +sqr(data[3]) ) ); + if ( fabs(pitch) > (PI_2-epsilon) ) { + yaw = atan2( -data[1], data[4]); + roll = 0.0 ; + } else { + roll = atan2(data[7], data[8]); + yaw = atan2(data[3], data[0]); + } + } + +Rotation Rotation::EulerZYZ(double Alfa,double Beta,double Gamma) { + double sa,ca,sb,cb,sg,cg; + sa = sin(Alfa);ca = cos(Alfa); + sb = sin(Beta);cb = cos(Beta); + sg = sin(Gamma);cg = cos(Gamma); + return Rotation( ca*cb*cg-sa*sg, -ca*cb*sg-sa*cg, ca*sb, + sa*cb*cg+ca*sg, -sa*cb*sg+ca*cg, sa*sb, + -sb*cg , sb*sg, cb + ); + + } + + +void Rotation::GetEulerZYZ(double& alpha,double& beta,double& gamma) const { + double epsilon = 1E-12; + if (fabs(data[8]) > 1-epsilon ) { + gamma=0.0; + if (data[8]>0) { + beta = 0.0; + alpha= atan2(data[3],data[0]); + } else { + beta = PI; + alpha= atan2(-data[3],-data[0]); + } + } else { + alpha=atan2(data[5], data[2]); + beta=atan2(sqrt( sqr(data[6]) +sqr(data[7]) ),data[8]); + gamma=atan2(data[7], -data[6]); + } + } + +Rotation Rotation::Rot(const Vector& rotaxis,double angle) { + // The formula is + // V.(V.tr) + st*[V x] + ct*(I-V.(V.tr)) + // can be found by multiplying it with an arbitrary vector p + // and noting that this vector is rotated. + Vector rotvec = rotaxis; + rotvec.Normalize(); + return Rotation::Rot2(rotvec,angle); +} + +Rotation Rotation::Rot2(const Vector& rotvec,double angle) { + // rotvec should be normalized ! + // The formula is + // V.(V.tr) + st*[V x] + ct*(I-V.(V.tr)) + // can be found by multiplying it with an arbitrary vector p + // and noting that this vector is rotated. + double ct = cos(angle); + double st = sin(angle); + double vt = 1-ct; + double m_vt_0=vt*rotvec(0); + double m_vt_1=vt*rotvec(1); + double m_vt_2=vt*rotvec(2); + double m_st_0=rotvec(0)*st; + double m_st_1=rotvec(1)*st; + double m_st_2=rotvec(2)*st; + double m_vt_0_1=m_vt_0*rotvec(1); + double m_vt_0_2=m_vt_0*rotvec(2); + double m_vt_1_2=m_vt_1*rotvec(2); + return Rotation( + ct + m_vt_0*rotvec(0), + -m_st_2 + m_vt_0_1, + m_st_1 + m_vt_0_2, + m_st_2 + m_vt_0_1, + ct + m_vt_1*rotvec(1), + -m_st_0 + m_vt_1_2, + -m_st_1 + m_vt_0_2, + m_st_0 + m_vt_1_2, + ct + m_vt_2*rotvec(2) + ); +} + + + +Vector Rotation::GetRot() const + // Returns a vector with the direction of the equiv. axis + // and its norm is angle + { + Vector axis; + double angle; + angle = Rotation::GetRotAngle(axis,epsilon); + return axis * angle; + } + + + +/** Returns the rotation angle around the equiv. axis + * @param axis the rotation axis is returned in this variable + * @param eps : in the case of angle == 0 : rot axis is undefined and chosen + * to be the Z-axis + * in the case of angle == PI : 2 solutions, positive Z-component + * of the axis is chosen. + * @result returns the rotation angle (between [0..PI] ) + * /todo : + * Check corresponding routines in rframes and rrframes + */ +double Rotation::GetRotAngle(Vector& axis,double eps) const { + double angle,x,y,z; // variables for result + double epsilon = eps; // margin to allow for rounding errors + double epsilon2 = eps*10; // margin to distinguish between 0 and 180 degrees + + // optional check that input is pure rotation, 'isRotationMatrix' is defined at: + // http://www.euclideanspace.com/maths/algebra/matrix/orthogonal/rotation/ + + if ((std::abs(data[1] - data[3]) < epsilon) + && (std::abs(data[2] - data[6])< epsilon) + && (std::abs(data[5] - data[7]) < epsilon)) + { + // singularity found + // first check for identity matrix which must have +1 for all terms + // in leading diagonal and zero in other terms + if ((std::abs(data[1] + data[3]) < epsilon2) + && (std::abs(data[2] + data[6]) < epsilon2) + && (std::abs(data[5] + data[7]) < epsilon2) + && (std::abs(data[0] + data[4] + data[8]-3) < epsilon2)) + { + // this singularity is identity matrix so angle = 0, axis is arbitrary + // Choose 0, 0, 1 to pass orocos tests + axis = KDL::Vector(0,0,1); + angle = 0.0; + return angle; + } + + // otherwise this singularity is angle = 180 + angle = PI; + double xx = (data[0] + 1) / 2; + double yy = (data[4] + 1) / 2; + double zz = (data[8] + 1) / 2; + double xy = (data[1] + data[3]) / 4; + double xz = (data[2] + data[6]) / 4; + double yz = (data[5] + data[7]) / 4; + + if ((xx > yy) && (xx > zz)) + { + // data[0] is the largest diagonal term + x = sqrt(xx); + y = xy/x; + z = xz/x; + } + else if (yy > zz) + { + // data[4] is the largest diagonal term + y = sqrt(yy); + x = xy/y; + z = yz/y; + } + else + { + // data[8] is the largest diagonal term so base result on this + z = sqrt(zz); + x = xz/z; + y = yz/z; + } + axis = KDL::Vector(x, y, z); + return angle; // return 180 deg rotation + } + + double f = (data[0] + data[4] + data[8] - 1) / 2; + + x = (data[7] - data[5]); + y = (data[2] - data[6]); + z = (data[3] - data[1]); + axis = KDL::Vector(x, y, z); + angle = atan2(axis.Norm()/2,f); + axis.Normalize(); + return angle; +} + +bool operator==(const Rotation& a,const Rotation& b) { +#ifdef KDL_USE_EQUAL + return Equal(a,b); +#else + return ( a.data[0]==b.data[0] && + a.data[1]==b.data[1] && + a.data[2]==b.data[2] && + a.data[3]==b.data[3] && + a.data[4]==b.data[4] && + a.data[5]==b.data[5] && + a.data[6]==b.data[6] && + a.data[7]==b.data[7] && + a.data[8]==b.data[8] ); +#endif +} +} diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/frames.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/frames.hpp new file mode 100644 index 0000000000000000000000000000000000000000..d816f0da23ac15857d5ea845ebf34df865b97aab --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/frames.hpp @@ -0,0 +1,1354 @@ +/*************************************************************************** + frames.hpp `- description + ------------------------- + begin : June 2006 + copyright : (C) 2006 Erwin Aertbelien + email : firstname.lastname@mech.kuleuven.be + + History (only major changes)( AUTHOR-Description ) : + + *************************************************************************** + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; if not, write to the Free Software * + * Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307 USA * + * * + ***************************************************************************/ + +/** + * \file + * \warning + * Efficienty can be improved by writing p2 = A*(B*(C*p1))) instead of + * p2=A*B*C*p1 + * + * \par PROPOSED NAMING CONVENTION FOR FRAME-like OBJECTS + * + * \verbatim + * A naming convention of objects of the type defined in this file : + * (1) Frame : F... + * Rotation : R ... + * (2) Twist : T ... + * Wrench : W ... + * Vector : V ... + * This prefix is followed by : + * for category (1) : + * F_A_B : w.r.t. frame A, frame B expressed + * ( each column of F_A_B corresponds to an axis of B, + * expressed w.r.t. frame A ) + * in mathematical convention : + * A + * F_A_B == F + * B + * + * for category (2) : + * V_B : a vector expressed w.r.t. frame B + * + * This can also be prepended by a name : + * e.g. : temporaryV_B + * + * With this convention one can write : + * + * F_A_B = F_B_A.Inverse(); + * F_A_C = F_A_B * F_B_C; + * V_B = F_B_C * V_C; // both translation and rotation + * V_B = R_B_C * V_C; // only rotation + * \endverbatim + * + * \par CONVENTIONS FOR WHEN USED WITH ROBOTS : + * + * \verbatim + * world : represents the frame ([1 0 0,0 1 0,0 0 1],[0 0 0]') + * mp : represents mounting plate of a robot + * (i.e. everything before MP is constructed by robot manufacturer + * everything after MP is tool ) + * tf : represents task frame of a robot + * (i.e. frame in which motion and force control is expressed) + * sf : represents sensor frame of a robot + * (i.e. frame at which the forces measured by the force sensor + * are expressed ) + * + * Frame F_world_mp=...; + * Frame F_mp_sf(..) + * Frame F_mp_tf(,.) + * + * Wrench are measured in sensor frame SF, so one could write : + * Wrench_tf = F_mp_tf.Inverse()* ( F_mp_sf * Wrench_sf ); + * \endverbatim + * + * \par CONVENTIONS REGARDING UNITS : + * Any consistent series of units can be used, e.g. N,mm,Nmm,..mm/sec + * + * \par Twist and Wrench transformations + * 3 different types of transformations do exist for the twists + * and wrenches. + * + * \verbatim + * 1) Frame * Twist or Frame * Wrench : + * this transforms both the velocity/force reference point + * and the basis to which the twist/wrench are expressed. + * 2) Rotation * Twist or Rotation * Wrench : + * this transforms the basis to which the twist/wrench are + * expressed, but leaves the reference point intact. + * 3) Twist.RefPoint(v_base_AB) or Wrench.RefPoint(v_base_AB) + * this transforms only the reference point. v is expressed + * in the same base as the twist/wrench and points from the + * old reference point to the new reference point. + * \endverbatim + * + *\par Spatial cross products + * Let m be a 6D motion vector (Twist) and f be a 6D force vector (Wrench) + * attached to a rigid body moving with a certain velocity v (Twist). Then + *\verbatim + * 1) m_dot = v cross m or Twist=Twist*Twist + * 2) f_dot = v cross f or Wrench=Twist*Wrench + *\endverbatim + * + * \par Complexity + * Sometimes the amount of work is given in the documentation + * e.g. 6M+3A means 6 multiplications and 3 additions. + * + * \author + * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven + * + ****************************************************************************/ +#ifndef KDL_FRAMES_H +#define KDL_FRAMES_H + + +#include "utilities/kdl-config.h" +#include "utilities/utility.h" +#include "utilities/hash_combine.h" + +#include + +///////////////////////////////////////////////////////////// + +namespace KDL { + + +class Vector; +class Rotation; +class Frame; +class Wrench; +class Twist; +class Vector2; +class Rotation2; +class Frame2; + + +// Equal is friend function, but default arguments for friends are forbidden (§8.3.6.4) +inline bool Equal(const Vector& a,const Vector& b,double eps=epsilon); +inline bool Equal(const Frame& a,const Frame& b,double eps=epsilon); +inline bool Equal(const Twist& a,const Twist& b,double eps=epsilon); +inline bool Equal(const Wrench& a,const Wrench& b,double eps=epsilon); +inline bool Equal(const Vector2& a,const Vector2& b,double eps=epsilon); +inline bool Equal(const Rotation2& a,const Rotation2& b,double eps=epsilon); +inline bool Equal(const Frame2& a,const Frame2& b,double eps=epsilon); + + +/** + * \brief A concrete implementation of a 3 dimensional vector class + */ +class Vector +{ +public: + double data[3]; + //! Does not initialise the Vector to zero. use Vector::Zero() or SetToZero for that + inline Vector() {data[0]=data[1]=data[2] = 0.0;} + + //! Constructs a vector out of the three values x, y and z + inline Vector(double x,double y, double z); + + //! Assignment operator. The normal copy by value semantics. + inline Vector(const Vector& arg); + + //! Assignment operator. The normal copy by value semantics. + inline Vector& operator = ( const Vector& arg); + + //! Access to elements, range checked when NDEBUG is not set, from 0..2 + inline double operator()(int index) const; + + //! Access to elements, range checked when NDEBUG is not set, from 0..2 + inline double& operator() (int index); + + //! Equivalent to double operator()(int index) const + double operator[] ( int index ) const + { + return this->operator() ( index ); + } + + //! Equivalent to double& operator()(int index) + double& operator[] ( int index ) + { + return this->operator() ( index ); + } + + inline double x() const; + inline double y() const; + inline double z() const; + inline void x(double); + inline void y(double); + inline void z(double); + + //! Reverses the sign of the Vector object itself + inline void ReverseSign(); + + + //! subtracts a vector from the Vector object itself + inline Vector& operator-=(const Vector& arg); + + + //! Adds a vector from the Vector object itself + inline Vector& operator +=(const Vector& arg); + + //! Scalar multiplication is defined + inline friend Vector operator*(const Vector& lhs,double rhs); + //! Scalar multiplication is defined + inline friend Vector operator*(double lhs,const Vector& rhs); + //! Scalar division is defined + + inline friend Vector operator/(const Vector& lhs,double rhs); + inline friend Vector operator+(const Vector& lhs,const Vector& rhs); + inline friend Vector operator-(const Vector& lhs,const Vector& rhs); + inline friend Vector operator*(const Vector& lhs,const Vector& rhs); + inline friend Vector operator-(const Vector& arg); + inline friend double dot(const Vector& lhs,const Vector& rhs); + + //! To have a uniform operator to put an element to zero, for scalar values + //! and for objects. + inline friend void SetToZero(Vector& v); + + //! @return a zero vector + inline static Vector Zero(); + + /** Normalizes this vector and returns it norm + * makes v a unitvector and returns the norm of v. + * if v is smaller than eps, Vector(1,0,0) is returned with norm 0. + * if this is not good, check the return value of this method. + */ + double Normalize(double eps=epsilon); + + //! @return the norm of the vector + double Norm(double eps=epsilon) const; + + + + //! a 3D vector where the 2D vector v is put in the XY plane + inline void Set2DXY(const Vector2& v); + //! a 3D vector where the 2D vector v is put in the YZ plane + inline void Set2DYZ(const Vector2& v); + //! a 3D vector where the 2D vector v is put in the ZX plane + inline void Set2DZX(const Vector2& v); + //! a 3D vector where the 2D vector v_XY is put in the XY plane of the frame F_someframe_XY. + inline void Set2DPlane(const Frame& F_someframe_XY,const Vector2& v_XY); + + + //! do not use operator == because the definition of Equal(.,.) is slightly + //! different. It compares whether the 2 arguments are equal in an eps-interval + inline friend bool Equal(const Vector& a,const Vector& b,double eps); + + //! The literal equality operator==(), also identical. + inline friend bool operator==(const Vector& a,const Vector& b); + //! The literal inequality operator!=(). + inline friend bool operator!=(const Vector& a,const Vector& b); + + friend class Rotation; + friend class Frame; +}; + + +/** + \brief represents rotations in 3 dimensional space. + + This class represents a rotation matrix with the following + conventions : + \verbatim + Suppose V2 = R*V, (1) + V is expressed in frame B + V2 is expressed in frame A + This matrix R consists of 3 columns [ X,Y,Z ], + X,Y, and Z contain the axes of frame B, expressed in frame A + Because of linearity expr(1) is valid. + \endverbatim + This class only represents rotational_interpolation, not translation + Two interpretations are possible for rotation angles. + * if you rotate with angle around X frame A to have frame B, + then the result of SetRotX is equal to frame B expressed wrt A. + In code: + \verbatim + Rotation R; + F_A_B = R.SetRotX(angle); + \endverbatim + * Secondly, if you take the following code : + \verbatim + Vector p,p2; Rotation R; + R.SetRotX(angle); + p2 = R*p; + \endverbatim + then the frame p2 is rotated around X axis with (-angle). + Analogue reasonings can be applyd to SetRotY,SetRotZ,SetRot + \par type + Concrete implementation +*/ +class Rotation +{ +public: + double data[9]; + + inline Rotation() { + *this = Rotation::Identity(); + } + inline Rotation(double Xx,double Yx,double Zx, + double Xy,double Yy,double Zy, + double Xz,double Yz,double Zz); + inline Rotation(const Vector& x,const Vector& y,const Vector& z); + + inline Rotation(const Rotation& arg); + + inline Rotation& operator=(const Rotation& arg); + + //! Defines a multiplication R*V between a Rotation R and a Vector V. + //! Complexity : 9M+6A + inline Vector operator*(const Vector& v) const; + + //! Access to elements 0..2,0..2, bounds are checked when NDEBUG is not set + inline double& operator()(int i,int j); + + //! Access to elements 0..2,0..2, bounds are checked when NDEBUG is not set + inline double operator() (int i,int j) const; + + friend Rotation operator *(const Rotation& lhs,const Rotation& rhs); + + //! Sets the value of *this to its inverse. + inline void SetInverse(); + + //! Gives back the inverse rotation matrix of *this. + inline Rotation Inverse() const; + + //! The same as R.Inverse()*v but more efficient. + inline Vector Inverse(const Vector& v) const; + + //! The same as R.Inverse()*arg but more efficient. + inline Wrench Inverse(const Wrench& arg) const; + + //! The same as R.Inverse()*arg but more efficient. + inline Twist Inverse(const Twist& arg) const; + + //! Gives back an identity rotaton matrix + inline static Rotation Identity(); + + +// = Rotations + //! The Rot... static functions give the value of the appropriate rotation matrix back. + inline static Rotation RotX(double angle); + //! The Rot... static functions give the value of the appropriate rotation matrix back. + inline static Rotation RotY(double angle); + //! The Rot... static functions give the value of the appropriate rotation matrix back. + inline static Rotation RotZ(double angle); + //! The DoRot... functions apply a rotation R to *this,such that *this = *this * Rot.. + //! DoRot... functions are only defined when they can be executed more efficiently + inline void DoRotX(double angle); + //! The DoRot... functions apply a rotation R to *this,such that *this = *this * Rot.. + //! DoRot... functions are only defined when they can be executed more efficiently + inline void DoRotY(double angle); + //! The DoRot... functions apply a rotation R to *this,such that *this = *this * Rot.. + //! DoRot... functions are only defined when they can be executed more efficiently + inline void DoRotZ(double angle); + + //! Along an arbitrary axes. It is not necessary to normalize rotvec. + //! returns identity rotation matrix in the case that the norm of rotvec + //! is to small to be used. + // @see Rot2 if you want to handle this error in another way. + static Rotation Rot(const Vector& rotvec,double angle); + + //! Along an arbitrary axes. rotvec should be normalized. + static Rotation Rot2(const Vector& rotvec,double angle); + + //! Returns a vector with the direction of the equiv. axis + //! and its norm is angle + Vector GetRot() const; + + /** Returns the rotation angle around the equiv. axis + * @param axis the rotation axis is returned in this variable + * @param eps : in the case of angle == 0 : rot axis is undefined and chosen + * to be +/- Z-axis + * in the case of angle == PI : 2 solutions, positive Z-component + * of the axis is chosen. + * @result returns the rotation angle (between [0..PI] ) + */ + double GetRotAngle(Vector& axis,double eps=epsilon) const; + + +/** Gives back a rotation matrix specified with EulerZYZ convention : + * - First rotate around Z with alfa, + * - then around the new Y with beta, + * - then around new Z with gamma. + * Invariants: + * - EulerZYX(alpha,beta,gamma) == EulerZYX(alpha +/- PHI, -beta, gamma +/- PI) + * - (angle + 2*k*PI) + **/ + static Rotation EulerZYZ(double Alfa,double Beta,double Gamma); + + /** Gives back the EulerZYZ convention description of the rotation matrix : + First rotate around Z with alpha, + then around the new Y with beta, then around + new Z with gamma. + + Variables are bound by: + - (-PI < alpha <= PI), + - (0 <= beta <= PI), + - (-PI < gamma <= PI) + + if beta==0 or beta==PI, then alpha and gamma are not unique, in this case gamma is chosen to be zero. + Invariants: + - EulerZYX(alpha,beta,gamma) == EulerZYX(alpha +/- PI, -beta, gamma +/- PI) + - angle + 2*k*PI + */ + void GetEulerZYZ(double& alpha,double& beta,double& gamma) const; + + //! Gives back a rotation matrix specified with Quaternion convention + //! the norm of (x,y,z,w) should be equal to 1 + static Rotation Quaternion(double x,double y,double z, double w); + + //! Get the quaternion of this matrix + //! \post the norm of (x,y,z,w) is 1 + void GetQuaternion(double& x,double& y,double& z, double& w) const; + + /** + * + * Gives back a rotation matrix specified with RPY convention: + * first rotate around X with roll, then around the + * old Y with pitch, then around old Z with yaw + * + * Invariants: + * - RPY(roll,pitch,yaw) == RPY( roll +/- PI, PI-pitch, yaw +/- PI ) + * - angles + 2*k*PI + */ + static Rotation RPY(double roll,double pitch,double yaw); + +/** Gives back a vector in RPY coordinates, variables are bound by + - -PI <= roll <= PI + - -PI <= Yaw <= PI + - -PI/2 <= PITCH <= PI/2 + + convention : + - first rotate around X with roll, + - then around the old Y with pitch, + - then around old Z with yaw + + if pitch == PI/2 or pitch == -PI/2, multiple solutions for gamma and alpha exist. The solution where roll==0 + is chosen. + + Invariants: + - RPY(roll,pitch,yaw) == RPY( roll +/- PI, PI-pitch, yaw +/- PI ) + - angles + 2*k*PI + +**/ + void GetRPY(double& roll,double& pitch,double& yaw) const; + + + /** EulerZYX constructs a Rotation from the Euler ZYX parameters: + * - First rotate around Z with alfa, + * - then around the new Y with beta, + * - then around new X with gamma. + * + * Closely related to RPY-convention. + * + * Invariants: + * - EulerZYX(alpha,beta,gamma) == EulerZYX(alpha +/- PI, PI-beta, gamma +/- PI) + * - (angle + 2*k*PI) + **/ + inline static Rotation EulerZYX(double Alfa,double Beta,double Gamma) { + return RPY(Gamma,Beta,Alfa); + } + + /** GetEulerZYX gets the euler ZYX parameters of a rotation : + * First rotate around Z with alfa, + * then around the new Y with beta, then around + * new X with gamma. + * + * Range of the results of GetEulerZYX : + * - -PI <= alfa <= PI + * - -PI <= gamma <= PI + * - -PI/2 <= beta <= PI/2 + * + * if beta == PI/2 or beta == -PI/2, multiple solutions for gamma and alpha exist. The solution where gamma==0 + * is chosen. + * + * + * Invariants: + * - EulerZYX(alpha,beta,gamma) == EulerZYX(alpha +/- PI, PI-beta, gamma +/- PI) + * - and also (angle + 2*k*PI) + * + * Closely related to RPY-convention. + **/ + inline void GetEulerZYX(double& Alfa,double& Beta,double& Gamma) const { + GetRPY(Gamma,Beta,Alfa); + } + + //! Transformation of the base to which the twist is expressed. + //! Complexity : 18M+12A + //! @see Frame*Twist for a transformation that also transforms + //! the velocity reference point. + inline Twist operator * (const Twist& arg) const; + + //! Transformation of the base to which the wrench is expressed. + //! Complexity : 18M+12A + //! @see Frame*Wrench for a transformation that also transforms + //! the force reference point. + inline Wrench operator * (const Wrench& arg) const; + + //! Access to the underlying unitvectors of the rotation matrix + inline Vector UnitX() const { + return Vector(data[0],data[3],data[6]); + } + + //! Access to the underlying unitvectors of the rotation matrix + inline void UnitX(const Vector& X) { + data[0] = X(0); + data[3] = X(1); + data[6] = X(2); + } + + //! Access to the underlying unitvectors of the rotation matrix + inline Vector UnitY() const { + return Vector(data[1],data[4],data[7]); + } + + //! Access to the underlying unitvectors of the rotation matrix + inline void UnitY(const Vector& X) { + data[1] = X(0); + data[4] = X(1); + data[7] = X(2); + } + + //! Access to the underlying unitvectors of the rotation matrix + inline Vector UnitZ() const { + return Vector(data[2],data[5],data[8]); + } + + //! Access to the underlying unitvectors of the rotation matrix + inline void UnitZ(const Vector& X) { + data[2] = X(0); + data[5] = X(1); + data[8] = X(2); + } + + //! do not use operator == because the definition of Equal(.,.) is slightly + //! different. It compares whether the 2 arguments are equal in an eps-interval + friend bool Equal(const Rotation& a,const Rotation& b,double eps); + + //! The literal equality operator==(), also identical. + friend bool operator==(const Rotation& a,const Rotation& b); + //! The literal inequality operator!=() + friend bool operator!=(const Rotation& a,const Rotation& b); + + friend class Frame; +}; + bool operator==(const Rotation& a,const Rotation& b); + bool Equal(const Rotation& a,const Rotation& b,double eps=epsilon); + + + +/** + \brief represents a frame transformation in 3D space (rotation + translation) + + if V2 = Frame*V1 (V2 expressed in frame A, V1 expressed in frame B) + then V2 = Frame.M*V1+Frame.p + + Frame.M contains columns that represent the axes of frame B wrt frame A + Frame.p contains the origin of frame B expressed in frame A. +*/ +class Frame { +public: + Vector p; //!< origine of the Frame + Rotation M; //!< Orientation of the Frame + +public: + + inline Frame(const Rotation& R,const Vector& V); + + //! The rotation matrix defaults to identity + explicit inline Frame(const Vector& V); + //! The position matrix defaults to zero + explicit inline Frame(const Rotation& R); + + inline Frame() {} + //! The copy constructor. Normal copy by value semantics. + inline Frame(const Frame& arg); + + //! Reads data from an double array + //\TODO should be formulated as a constructor + void Make4x4(double* d); + + //! Treats a frame as a 4x4 matrix and returns element i,j + //! Access to elements 0..3,0..3, bounds are checked when NDEBUG is not set + inline double operator()(int i,int j); + + //! Treats a frame as a 4x4 matrix and returns element i,j + //! Access to elements 0..3,0..3, bounds are checked when NDEBUG is not set + inline double operator() (int i,int j) const; + +// = Inverse + //! Gives back inverse transformation of a Frame + inline Frame Inverse() const; + + //! The same as p2=R.Inverse()*p but more efficient. + inline Vector Inverse(const Vector& arg) const; + + //! The same as p2=R.Inverse()*p but more efficient. + inline Wrench Inverse(const Wrench& arg) const; + + //! The same as p2=R.Inverse()*p but more efficient. + inline Twist Inverse(const Twist& arg) const; + + //! Normal copy-by-value semantics. + inline Frame& operator = (const Frame& arg); + + //! Transformation of the base to which the vector + //! is expressed. + inline Vector operator * (const Vector& arg) const; + + //! Transformation of both the force reference point + //! and of the base to which the wrench is expressed. + //! look at Rotation*Wrench operator for a transformation + //! of only the base to which the twist is expressed. + //! + //! Complexity : 24M+18A + inline Wrench operator * (const Wrench& arg) const; + + //! Transformation of both the velocity reference point + //! and of the base to which the twist is expressed. + //! look at Rotation*Twist for a transformation of only the + //! base to which the twist is expressed. + //! + //! Complexity : 24M+18A + inline Twist operator * (const Twist& arg) const; + + //! Composition of two frames. + inline friend Frame operator *(const Frame& lhs,const Frame& rhs); + + //! @return the identity transformation Frame(Rotation::Identity(),Vector::Zero()). + inline static Frame Identity(); + + //! The twist is expressed wrt the current + //! frame. This frame is integrated into an updated frame with + //! . Very simple first order integration rule. + inline void Integrate(const Twist& t_this,double frequency); + + /* + // DH_Craig1989 : constructs a transformationmatrix + // T_link(i-1)_link(i) with the Denavit-Hartenberg convention as + // described in the Craigs book: Craig, J. J.,Introduction to + // Robotics: Mechanics and Control, Addison-Wesley, + // isbn:0-201-10326-5, 1986. + // + // Note that the frame is a redundant way to express the information + // in the DH-convention. + // \verbatim + // Parameters in full : a(i-1),alpha(i-1),d(i),theta(i) + // + // axis i-1 is connected by link i-1 to axis i numbering axis 1 + // to axis n link 0 (immobile base) to link n + // + // link length a(i-1) length of the mutual perpendicular line + // (normal) between the 2 axes. This normal runs from (i-1) to + // (i) axis. + // + // link twist alpha(i-1): construct plane perpendicular to the + // normal project axis(i-1) and axis(i) into plane angle from + // (i-1) to (i) measured in the direction of the normal + // + // link offset d(i) signed distance between normal (i-1) to (i) + // and normal (i) to (i+1) along axis i joint angle theta(i) + // signed angle between normal (i-1) to (i) and normal (i) to + // (i+1) along axis i + // + // First and last joints : a(0)= a(n) = 0 + // alpha(0) = alpha(n) = 0 + // + // PRISMATIC : theta(1) = 0 d(1) arbitrarily + // + // REVOLUTE : theta(1) arbitrarily d(1) = 0 + // + // Not unique : if intersecting joint axis 2 choices for normal + // Frame assignment of the DH convention : Z(i-1) follows axis + // (i-1) X(i-1) is the normal between axis(i-1) and axis(i) + // Y(i-1) follows out of Z(i-1) and X(i-1) + // + // a(i-1) = distance from Z(i-1) to Z(i) along X(i-1) + // alpha(i-1) = angle between Z(i-1) to Z(i) along X(i-1) + // d(i) = distance from X(i-1) to X(i) along Z(i) + // theta(i) = angle between X(i-1) to X(i) along X(i) + // \endverbatim + */ + static Frame DH_Craig1989(double a,double alpha,double d,double theta); + + // DH : constructs a transformationmatrix T_link(i-1)_link(i) with + // the Denavit-Hartenberg convention as described in the original + // publictation: Denavit, J. and Hartenberg, R. S., A kinematic + // notation for lower-pair mechanisms based on matrices, ASME + // Journal of Applied Mechanics, 23:215-221, 1955. + + static Frame DH(double a,double alpha,double d,double theta); + + + //! do not use operator == because the definition of Equal(.,.) is slightly + //! different. It compares whether the 2 arguments are equal in an eps-interval + inline friend bool Equal(const Frame& a,const Frame& b,double eps); + + //! The literal equality operator==(), also identical. + inline friend bool operator==(const Frame& a,const Frame& b); + //! The literal inequality operator!=(). + inline friend bool operator!=(const Frame& a,const Frame& b); +}; + + +/** + * \brief represents both translational and rotational velocities. + * + * This class represents a twist. A twist is the combination of translational + * velocity and rotational velocity applied at one point. +*/ +class Twist { +public: + Vector vel; //!< The velocity of that point + Vector rot; //!< The rotational velocity of that point. +public: + + //! The default constructor initialises to Zero via the constructor of Vector. + Twist():vel(),rot() {}; + + Twist(const Vector& _vel,const Vector& _rot):vel(_vel),rot(_rot) {}; + + inline Twist& operator-=(const Twist& arg); + inline Twist& operator+=(const Twist& arg); + //! index-based access to components, first vel(0..2), then rot(3..5) + inline double& operator()(int i); + + //! index-based access to components, first vel(0..2), then rot(3..5) + //! For use with a const Twist + inline double operator()(int i) const; + + double operator[] ( int index ) const + { + return this->operator() ( index ); + } + + double& operator[] ( int index ) + { + return this->operator() ( index ); + } + + inline friend Twist operator*(const Twist& lhs,double rhs); + inline friend Twist operator*(double lhs,const Twist& rhs); + inline friend Twist operator/(const Twist& lhs,double rhs); + inline friend Twist operator+(const Twist& lhs,const Twist& rhs); + inline friend Twist operator-(const Twist& lhs,const Twist& rhs); + inline friend Twist operator-(const Twist& arg); + inline friend double dot(const Twist& lhs,const Wrench& rhs); + inline friend double dot(const Wrench& rhs,const Twist& lhs); + inline friend void SetToZero(Twist& v); + /// Spatial cross product for 6d motion vectors, beware all of them have to be expressed in the same reference frame/point + inline friend Twist operator*(const Twist& lhs,const Twist& rhs); + /// Spatial cross product for 6d force vectors, beware all of them have to be expressed in the same reference frame/point + inline friend Wrench operator*(const Twist& lhs,const Wrench& rhs); + + //! @return a zero Twist : Twist(Vector::Zero(),Vector::Zero()) + static inline Twist Zero(); + + //! Reverses the sign of the twist + inline void ReverseSign(); + + //! Changes the reference point of the twist. + //! The vector v_base_AB is expressed in the same base as the twist + //! The vector v_base_AB is a vector from the old point to + //! the new point. + //! + //! Complexity : 6M+6A + inline Twist RefPoint(const Vector& v_base_AB) const; + + + //! do not use operator == because the definition of Equal(.,.) is slightly + //! different. It compares whether the 2 arguments are equal in an eps-interval + inline friend bool Equal(const Twist& a,const Twist& b,double eps); + + //! The literal equality operator==(), also identical. + inline friend bool operator==(const Twist& a,const Twist& b); + //! The literal inequality operator!=(). + inline friend bool operator!=(const Twist& a,const Twist& b); + +// = Friends + friend class Rotation; + friend class Frame; +}; + + +/** + * \brief represents both translational and rotational acceleration. + * + * This class represents an acceleration twist. A acceleration twist is + * the combination of translational + * acceleration and rotational acceleration applied at one point. +*/ +/* +class AccelerationTwist { +public: + Vector trans; //!< The translational acceleration of that point + Vector rot; //!< The rotational acceleration of that point. +public: + + //! The default constructor initialises to Zero via the constructor of Vector. + AccelerationTwist():trans(),rot() {}; + + AccelerationTwist(const Vector& _trans,const Vector& _rot):trans(_trans),rot(_rot) {}; + + inline AccelerationTwist& operator-=(const AccelerationTwist& arg); + inline AccelerationTwist& operator+=(const AccelerationTwist& arg); + //! index-based access to components, first vel(0..2), then rot(3..5) + inline double& operator()(int i); + + //! index-based access to components, first vel(0..2), then rot(3..5) + //! For use with a const AccelerationTwist + inline double operator()(int i) const; + + double operator[] ( int index ) const + { + return this->operator() ( index ); + } + + double& operator[] ( int index ) + { + return this->operator() ( index ); + } + + inline friend AccelerationTwist operator*(const AccelerationTwist& lhs,double rhs); + inline friend AccelerationTwist operator*(double lhs,const AccelerationTwist& rhs); + inline friend AccelerationTwist operator/(const AccelerationTwist& lhs,double rhs); + inline friend AccelerationTwist operator+(const AccelerationTwist& lhs,const AccelerationTwist& rhs); + inline friend AccelerationTwist operator-(const AccelerationTwist& lhs,const AccelerationTwist& rhs); + inline friend AccelerationTwist operator-(const AccelerationTwist& arg); + //inline friend double dot(const AccelerationTwist& lhs,const Wrench& rhs); + //inline friend double dot(const Wrench& rhs,const AccelerationTwist& lhs); + inline friend void SetToZero(AccelerationTwist& v); + + + //! @return a zero AccelerationTwist : AccelerationTwist(Vector::Zero(),Vector::Zero()) + static inline AccelerationTwist Zero(); + + //! Reverses the sign of the AccelerationTwist + inline void ReverseSign(); + + //! Changes the reference point of the AccelerationTwist. + //! The vector v_base_AB is expressed in the same base as the AccelerationTwist + //! The vector v_base_AB is a vector from the old point to + //! the new point. + //! + //! Complexity : 6M+6A + inline AccelerationTwist RefPoint(const Vector& v_base_AB) const; + + + //! do not use operator == because the definition of Equal(.,.) is slightly + //! different. It compares whether the 2 arguments are equal in an eps-interval + inline friend bool Equal(const AccelerationTwist& a,const AccelerationTwist& b,double eps=epsilon); + + //! The literal equality operator==(), also identical. + inline friend bool operator==(const AccelerationTwist& a,const AccelerationTwist& b); + //! The literal inequality operator!=(). + inline friend bool operator!=(const AccelerationTwist& a,const AccelerationTwist& b); + +// = Friends + friend class Rotation; + friend class Frame; + +}; +*/ +/** + * \brief represents the combination of a force and a torque. + * + * This class represents a Wrench. A Wrench is the force and torque applied at a point + */ +class Wrench +{ +public: + Vector force; //!< Force that is applied at the origin of the current ref frame + Vector torque; //!< Torque that is applied at the origin of the current ref frame +public: + + //! Does initialise force and torque to zero via the underlying constructor of Vector + Wrench():force(),torque() {}; + Wrench(const Vector& _force,const Vector& _torque):force(_force),torque(_torque) {}; + +// = Operators + inline Wrench& operator-=(const Wrench& arg); + inline Wrench& operator+=(const Wrench& arg); + + //! index-based access to components, first force(0..2), then torque(3..5) + inline double& operator()(int i); + + //! index-based access to components, first force(0..2), then torque(3..5) + //! for use with a const Wrench + inline double operator()(int i) const; + + double operator[] ( int index ) const + { + return this->operator() ( index ); + } + + double& operator[] ( int index ) + { + return this->operator() ( index ); + } + + //! Scalar multiplication + inline friend Wrench operator*(const Wrench& lhs,double rhs); + //! Scalar multiplication + inline friend Wrench operator*(double lhs,const Wrench& rhs); + //! Scalar division + inline friend Wrench operator/(const Wrench& lhs,double rhs); + + inline friend Wrench operator+(const Wrench& lhs,const Wrench& rhs); + inline friend Wrench operator-(const Wrench& lhs,const Wrench& rhs); + + //! An unary - operator + inline friend Wrench operator-(const Wrench& arg); + + //! Sets the Wrench to Zero, to have a uniform function that sets an object or + //! double to zero. + inline friend void SetToZero(Wrench& v); + + //! @return a zero Wrench + static inline Wrench Zero(); + + //! Reverses the sign of the current Wrench + inline void ReverseSign(); + + //! Changes the reference point of the wrench. + //! The vector v_base_AB is expressed in the same base as the twist + //! The vector v_base_AB is a vector from the old point to + //! the new point. + //! + //! Complexity : 6M+6A + inline Wrench RefPoint(const Vector& v_base_AB) const; + + + //! do not use operator == because the definition of Equal(.,.) is slightly + //! different. It compares whether the 2 arguments are equal in an eps-interval + inline friend bool Equal(const Wrench& a,const Wrench& b,double eps); + + //! The literal equality operator==(), also identical. + inline friend bool operator==(const Wrench& a,const Wrench& b); + //! The literal inequality operator!=(). + inline friend bool operator!=(const Wrench& a,const Wrench& b); + + friend class Rotation; + friend class Frame; +}; + + +//! 2D version of Vector +class Vector2 +{ + double data[2]; +public: + //! Does not initialise to Zero(). + Vector2() {data[0]=data[1] = 0.0;} + inline Vector2(double x,double y); + inline Vector2(const Vector2& arg); + + inline Vector2& operator = ( const Vector2& arg); + + //! Access to elements, range checked when NDEBUG is not set, from 0..1 + inline double operator()(int index) const; + + //! Access to elements, range checked when NDEBUG is not set, from 0..1 + inline double& operator() (int index); + + //! Equivalent to double operator()(int index) const + double operator[] ( int index ) const + { + return this->operator() ( index ); + } + + //! Equivalent to double& operator()(int index) + double& operator[] ( int index ) + { + return this->operator() ( index ); + } + + inline double x() const; + inline double y() const; + inline void x(double); + inline void y(double); + + inline void ReverseSign(); + inline Vector2& operator-=(const Vector2& arg); + inline Vector2& operator +=(const Vector2& arg); + + + inline friend Vector2 operator*(const Vector2& lhs,double rhs); + inline friend Vector2 operator*(double lhs,const Vector2& rhs); + inline friend Vector2 operator/(const Vector2& lhs,double rhs); + inline friend Vector2 operator+(const Vector2& lhs,const Vector2& rhs); + inline friend Vector2 operator-(const Vector2& lhs,const Vector2& rhs); + inline friend Vector2 operator*(const Vector2& lhs,const Vector2& rhs); + inline friend Vector2 operator-(const Vector2& arg); + inline friend void SetToZero(Vector2& v); + + //! @return a zero 2D vector. + inline static Vector2 Zero(); + + /** Normalizes this vector and returns it norm + * makes v a unitvector and returns the norm of v. + * if v is smaller than eps, Vector(1,0,0) is returned with norm 0. + * if this is not good, check the return value of this method. + */ + double Normalize(double eps=epsilon); + + //! @return the norm of the vector + double Norm(double eps=epsilon) const; + + //! projects v in its XY plane, and sets *this to these values + inline void Set3DXY(const Vector& v); + + //! projects v in its YZ plane, and sets *this to these values + inline void Set3DYZ(const Vector& v); + + //! projects v in its ZX plane, and sets *this to these values + inline void Set3DZX(const Vector& v); + + //! projects v_someframe in the XY plane of F_someframe_XY, + //! and sets *this to these values + //! expressed wrt someframe. + inline void Set3DPlane(const Frame& F_someframe_XY,const Vector& v_someframe); + + + //! do not use operator == because the definition of Equal(.,.) is slightly + //! different. It compares whether the 2 arguments are equal in an eps-interval + inline friend bool Equal(const Vector2& a,const Vector2& b,double eps); + + //! The literal equality operator==(), also identical. + inline friend bool operator==(const Vector2& a,const Vector2& b); + //! The literal inequality operator!=(). + inline friend bool operator!=(const Vector2& a,const Vector2& b); + + friend class Rotation2; +}; + + +//! A 2D Rotation class, for conventions see Rotation. For further documentation +//! of the methods see Rotation class. +class Rotation2 +{ + double s,c; + //! c,s represent cos(angle), sin(angle), this also represents first col. of rot matrix + //! from outside, this class behaves as if it would store the complete 2x2 matrix. +public: + //! Default constructor does NOT initialise to Zero(). + Rotation2() {c=1.0;s=0.0;} + + explicit Rotation2(double angle_rad):s(sin(angle_rad)),c(cos(angle_rad)) {} + + Rotation2(double ca,double sa):s(sa),c(ca){} + + Rotation2(const Rotation2& arg); + + inline Rotation2& operator=(const Rotation2& arg); + inline Vector2 operator*(const Vector2& v) const; + //! Access to elements 0..1,0..1, bounds are checked when NDEBUG is not set + inline double operator() (int i,int j) const; + + inline friend Rotation2 operator *(const Rotation2& lhs,const Rotation2& rhs); + + inline void SetInverse(); + inline Rotation2 Inverse() const; + inline Vector2 Inverse(const Vector2& v) const; + + inline void SetIdentity(); + inline static Rotation2 Identity(); + + + //! The SetRot.. functions set the value of *this to the appropriate rotation matrix. + inline void SetRot(double angle); + + //! The Rot... static functions give the value of the appropriate rotation matrix bac + inline static Rotation2 Rot(double angle); + + //! Gets the angle (in radians) + inline double GetRot() const; + + //! do not use operator == because the definition of Equal(.,.) is slightly + //! different. It compares whether the 2 arguments are equal in an eps-interval + inline friend bool Equal(const Rotation2& a,const Rotation2& b,double eps); +}; + + +//! A 2D frame class, for further documentation see the Frames class +//! for methods with unchanged semantics. +class Frame2 + { +public: + Vector2 p; //!< origine of the Frame + Rotation2 M; //!< Orientation of the Frame + +public: + + inline Frame2(const Rotation2& R,const Vector2& V); + explicit inline Frame2(const Vector2& V); + explicit inline Frame2(const Rotation2& R); + inline Frame2(void); + inline Frame2(const Frame2& arg); + inline void Make4x4(double* d); + + //! Treats a frame as a 3x3 matrix and returns element i,j + //! Access to elements 0..2,0..2, bounds are checked when NDEBUG is not set + inline double operator()(int i,int j); + + //! Treats a frame as a 4x4 matrix and returns element i,j + //! Access to elements 0..3,0..3, bounds are checked when NDEBUG is not set + inline double operator() (int i,int j) const; + + inline void SetInverse(); + inline Frame2 Inverse() const; + inline Vector2 Inverse(const Vector2& arg) const; + inline Frame2& operator = (const Frame2& arg); + inline Vector2 operator * (const Vector2& arg) const; + inline friend Frame2 operator *(const Frame2& lhs,const Frame2& rhs); + inline void SetIdentity(); + inline void Integrate(const Twist& t_this,double frequency); + inline static Frame2 Identity() { + Frame2 tmp; + tmp.SetIdentity(); + return tmp; + } + inline friend bool Equal(const Frame2& a,const Frame2& b,double eps); +}; + +/** + * determines the difference of vector b with vector a. + * + * see diff for Rotation matrices for further background information. + * + * \param p_w_a start vector a expressed to some frame w + * \param p_w_b end vector b expressed to some frame w . + * \param dt [optional][obsolete] time interval over which the numerical differentiation takes place. + * \return the difference (b-a) expressed to the frame w. + */ +IMETHOD Vector diff(const Vector& p_w_a,const Vector& p_w_b,double dt=1); + + +/** + * determines the (scaled) rotation axis necessary to rotate from b1 to b2. + * + * This rotation axis is expressed w.r.t. frame a. The rotation axis is scaled + * by the necessary rotation angle. The rotation angle is always in the + * (inclusive) interval \f$ [0 , \pi] \f$. + * + * This definition is chosen in this way to facilitate numerical differentiation. + * With this definition diff(a,b) == -diff(b,a). + * + * The diff() function is overloaded for all classes in frames.hpp and framesvel.hpp, such that + * numerical differentiation, equality checks with tolerances, etc. can be performed + * without caring exactly on which type the operation is performed. + * + * \param R_a_b1: The rotation matrix \f$ _a^{b1} R \f$ of b1 with respect to frame a. + * \param R_a_b2: The Rotation matrix \f$ _a^{b2} R \f$ of b2 with respect to frame a. + * \param dt [optional][obsolete] time interval over which the numerical differentiation takes place. By default this is set to 1.0. + * \return rotation axis to rotate from b1 to b2, scaled by the rotation angle, expressed in frame a. + * \warning - The result is not a rotational vector, i.e. it is not a mathematical vector. + * (no communitative addition). + * + * \warning - When used in the context of numerical differentiation, with the frames b1 and b2 very + * close to each other, the semantics correspond to the twist, scaled by the time. + * + * \warning - For angles equal to \f$ \pi \f$, The negative of the + * return value is equally valid. + */ +IMETHOD Vector diff(const Rotation& R_a_b1,const Rotation& R_a_b2,double dt=1); + +/** + * determines the rotation axis necessary to rotate the frame b1 to the same orientation as frame b2 and the vector + * necessary to translate the origin of b1 to the origin of b2, and stores the result in a Twist datastructure. + * \param F_a_b1 frame b1 expressed with respect to some frame a. + * \param F_a_b2 frame b2 expressed with respect to some frame a. + * \warning The result is not a Twist! + * see diff() for Rotation and Vector arguments for further detail on the semantics. + */ +IMETHOD Twist diff(const Frame& F_a_b1,const Frame& F_a_b2,double dt=1); + +/** + * determines the difference between two twists i.e. the difference between + * the underlying velocity vectors and rotational velocity vectors. + */ +IMETHOD Twist diff(const Twist& a,const Twist& b,double dt=1); + +/** + * determines the difference between two wrenches i.e. the difference between + * the underlying torque vectors and force vectors. + */ +IMETHOD Wrench diff(const Wrench& W_a_p1,const Wrench& W_a_p2,double dt=1); + +/** + * \brief adds vector da to vector a. + * see also the corresponding diff() routine. + * \param p_w_a vector a expressed to some frame w. + * \param p_w_da vector da expressed to some frame w. + * \returns the vector resulting from the displacement of vector a by vector da, expressed in the frame w. + */ +IMETHOD Vector addDelta(const Vector& p_w_a,const Vector& p_w_da,double dt=1); + +/** + * returns the rotation matrix resulting from the rotation of frame a by the axis and angle + * specified with da_w. + * + * see also the corresponding diff() routine. + * + * \param R_w_a Rotation matrix of frame a expressed to some frame w. + * \param da_w axis and angle of the rotation expressed to some frame w. + * \returns the rotation matrix resulting from the rotation of frame a by the axis and angle + * specified with da. The resulting rotation matrix is expressed with respect to + * frame w. + */ +IMETHOD Rotation addDelta(const Rotation& R_w_a,const Vector& da_w,double dt=1); + +/** + * returns the frame resulting from the rotation of frame a by the axis and angle + * specified in da_w and the translation of the origin (also specified in da_w). + * + * see also the corresponding diff() routine. + * \param R_w_a Rotation matrix of frame a expressed to some frame w. + * \param da_w axis and angle of the rotation (da_w.rot), together with a displacement vector for the origin (da_w.vel), expressed to some frame w. + * \returns the frame resulting from the rotation of frame a by the axis and angle + * specified with da.rot, and the translation of the origin da_w.vel . The resulting frame is expressed with respect to frame w. + */ +IMETHOD Frame addDelta(const Frame& F_w_a,const Twist& da_w,double dt=1); + +/** + * \brief adds the twist da to the twist a. + * see also the corresponding diff() routine. + * \param a a twist wrt some frame + * \param da a twist difference wrt some frame + * \returns The twist (a+da) wrt the corresponding frame. + */ +IMETHOD Twist addDelta(const Twist& a,const Twist&da,double dt=1); + + +/** + * \brief adds the wrench da to the wrench w. + * see also the corresponding diff() routine. + * see also the corresponding diff() routine. + * \param a a wrench wrt some frame + * \param da a wrench difference wrt some frame + * \returns the wrench (a+da) wrt the corresponding frame. + */ +IMETHOD Wrench addDelta(const Wrench& a,const Wrench&da,double dt=1); + +#ifdef KDL_INLINE +#include "frames.inl" +#endif + +} + +template<> struct std::hash +{ + std::size_t operator()(KDL::Vector const& v) const noexcept + { + size_t seed = 0; + KDL::hash_combine(seed, v.x()); + KDL::hash_combine(seed, v.y()); + KDL::hash_combine(seed, v.z()); + return seed; + } +}; + +template<> struct std::hash +{ + std::size_t operator()(KDL::Rotation const& r) const noexcept + { + size_t seed = 0; + double x, y, z, w; + r.GetQuaternion(x, y, z, w); + KDL::hash_combine(seed, x); + KDL::hash_combine(seed, y); + KDL::hash_combine(seed, z); + KDL::hash_combine(seed, w); + return seed; + } +}; + +template<> struct std::hash +{ + std::size_t operator()(KDL::Frame const& f) const noexcept + { + size_t seed = 0; + KDL::hash_combine(seed, f.p); + KDL::hash_combine(seed, f.M); + return seed; + } +}; + +template<> struct std::hash +{ + std::size_t operator()(KDL::Wrench const& w) const noexcept + { + size_t seed = 0; + KDL::hash_combine(seed, w.force); + KDL::hash_combine(seed, w.torque); + return seed; + } +}; + +template<> struct std::hash +{ + std::size_t operator()(KDL::Twist const& t) const noexcept + { + size_t seed = 0; + KDL::hash_combine(seed, t.vel); + KDL::hash_combine(seed, t.rot); + return seed; + } +}; + +template<> struct std::hash +{ + std::size_t operator()(KDL::Vector2 const& v) const noexcept + { + size_t seed = 0; + KDL::hash_combine(seed, v.x()); + KDL::hash_combine(seed, v.y()); + return seed; + } +}; + +template<> struct std::hash +{ + std::size_t operator()(KDL::Rotation2 const& r) const noexcept + { + size_t seed = 0; + KDL::hash_combine(seed, r.GetRot()); + return seed; + } +}; + +template<> struct std::hash +{ + std::size_t operator()(KDL::Frame2 const& f) const noexcept + { + size_t seed = 0; + KDL::hash_combine(seed, f.p); + KDL::hash_combine(seed, f.M); + return seed; + } +}; + +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/frames.inl b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/frames.inl new file mode 100644 index 0000000000000000000000000000000000000000..8349ff1084e4680e909beaafae37ac8035393187 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/frames.inl @@ -0,0 +1,1343 @@ +/*************************************************************************** + frames.inl - description + ------------------------- + begin : June 2006 + copyright : (C) 2006 Erwin Aertbelien + email : firstname.lastname@mech.kuleuven.ac.be + + History (only major changes)( AUTHOR-Description ) : + + *************************************************************************** + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; if not, write to the Free Software * + * Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307 USA * + * * + ***************************************************************************/ + +/** + * \file frames.inl + * Inlined member functions and global functions that relate to the classes in frames.cpp + * + */ + + + +IMETHOD Vector::Vector(const Vector & arg) +{ + data[0] = arg.data[0]; + data[1] = arg.data[1]; + data[2] = arg.data[2]; +} + +IMETHOD Vector::Vector(double x,double y, double z) +{ + data[0]=x;data[1]=y;data[2]=z; +} + + +IMETHOD Vector& Vector::operator =(const Vector & arg) +{ + data[0] = arg.data[0]; + data[1] = arg.data[1]; + data[2] = arg.data[2]; + return *this; +} + +IMETHOD Vector operator +(const Vector & lhs,const Vector& rhs) +{ + Vector tmp; + tmp.data[0] = lhs.data[0]+rhs.data[0]; + tmp.data[1] = lhs.data[1]+rhs.data[1]; + tmp.data[2] = lhs.data[2]+rhs.data[2]; + return tmp; +} + +IMETHOD Vector operator -(const Vector & lhs,const Vector& rhs) +{ + Vector tmp; + tmp.data[0] = lhs.data[0]-rhs.data[0]; + tmp.data[1] = lhs.data[1]-rhs.data[1]; + tmp.data[2] = lhs.data[2]-rhs.data[2]; + return tmp; +} + +IMETHOD double Vector::x() const { return data[0]; } +IMETHOD double Vector::y() const { return data[1]; } +IMETHOD double Vector::z() const { return data[2]; } + +IMETHOD void Vector::x( double _x ) { data[0] = _x; } +IMETHOD void Vector::y( double _y ) { data[1] = _y; } +IMETHOD void Vector::z( double _z ) { data[2] = _z; } + +Vector operator *(const Vector& lhs,double rhs) +{ + Vector tmp; + tmp.data[0] = lhs.data[0]*rhs; + tmp.data[1] = lhs.data[1]*rhs; + tmp.data[2] = lhs.data[2]*rhs; + return tmp; +} + +Vector operator *(double lhs,const Vector& rhs) +{ + Vector tmp; + tmp.data[0] = lhs*rhs.data[0]; + tmp.data[1] = lhs*rhs.data[1]; + tmp.data[2] = lhs*rhs.data[2]; + return tmp; +} + +Vector operator /(const Vector& lhs,double rhs) +{ + Vector tmp; + tmp.data[0] = lhs.data[0]/rhs; + tmp.data[1] = lhs.data[1]/rhs; + tmp.data[2] = lhs.data[2]/rhs; + return tmp; +} + +Vector operator *(const Vector & lhs,const Vector& rhs) +// Complexity : 6M+3A +{ + Vector tmp; + tmp.data[0] = lhs.data[1]*rhs.data[2]-lhs.data[2]*rhs.data[1]; + tmp.data[1] = lhs.data[2]*rhs.data[0]-lhs.data[0]*rhs.data[2]; + tmp.data[2] = lhs.data[0]*rhs.data[1]-lhs.data[1]*rhs.data[0]; + return tmp; +} + +Vector& Vector::operator +=(const Vector & arg) +// Complexity : 3A +{ + data[0]+=arg.data[0]; + data[1]+=arg.data[1]; + data[2]+=arg.data[2]; + return *this; +} + +Vector& Vector::operator -=(const Vector & arg) +// Complexity : 3A +{ + data[0]-=arg.data[0]; + data[1]-=arg.data[1]; + data[2]-=arg.data[2]; + return *this; +} + +Vector Vector::Zero() +{ + return Vector(0,0,0); +} + +double Vector::operator()(int index) const { + FRAMES_CHECKI((0<=index)&&(index<=2)); + return data[index]; +} + +double& Vector::operator () (int index) +{ + FRAMES_CHECKI((0<=index)&&(index<=2)); + return data[index]; +} + + + +Wrench Frame::operator * (const Wrench& arg) const +// Complexity : 24M+18A +{ + Wrench tmp; + tmp.force = M*arg.force; + tmp.torque = M*arg.torque + p*tmp.force; + return tmp; +} + +Wrench Frame::Inverse(const Wrench& arg) const +{ + Wrench tmp; + tmp.force = M.Inverse(arg.force); + tmp.torque = M.Inverse(arg.torque-p*arg.force); + return tmp; +} + + + +Wrench Rotation::Inverse(const Wrench& arg) const +{ + return Wrench(Inverse(arg.force),Inverse(arg.torque)); +} + +Twist Rotation::Inverse(const Twist& arg) const +{ + return Twist(Inverse(arg.vel),Inverse(arg.rot)); +} + +Wrench Wrench::Zero() +{ + return Wrench(Vector::Zero(),Vector::Zero()); +} + + +void Wrench::ReverseSign() +{ + torque.ReverseSign(); + force.ReverseSign(); +} + +Wrench Wrench::RefPoint(const Vector& v_base_AB) const + // Changes the reference point of the Wrench. + // The vector v_base_AB is expressed in the same base as the twist + // The vector v_base_AB is a vector from the old point to + // the new point. +{ + return Wrench(this->force, + this->torque+this->force*v_base_AB + ); +} + + +Wrench& Wrench::operator-=(const Wrench& arg) +{ + torque-=arg.torque; + force -=arg.force; + return *this; +} + +Wrench& Wrench::operator+=(const Wrench& arg) +{ + torque+=arg.torque; + force +=arg.force; + return *this; +} + +double& Wrench::operator()(int i) +{ + // assert((0<=i)&&(i<6)); done by underlying routines + if (i<3) + return force(i); + else + return torque(i-3); +} + +double Wrench::operator()(int i) const +{ + // assert((0<=i)&&(i<6)); done by underlying routines + if (i<3) + return force(i); + else + return torque(i-3); +} + + +Wrench operator*(const Wrench& lhs,double rhs) +{ + return Wrench(lhs.force*rhs,lhs.torque*rhs); +} + +Wrench operator*(double lhs,const Wrench& rhs) +{ + return Wrench(lhs*rhs.force,lhs*rhs.torque); +} + +Wrench operator/(const Wrench& lhs,double rhs) +{ + return Wrench(lhs.force/rhs,lhs.torque/rhs); +} + +// addition of Wrench's +Wrench operator+(const Wrench& lhs,const Wrench& rhs) +{ + return Wrench(lhs.force+rhs.force,lhs.torque+rhs.torque); +} + +Wrench operator-(const Wrench& lhs,const Wrench& rhs) +{ + return Wrench(lhs.force-rhs.force,lhs.torque-rhs.torque); +} + +// unary - +Wrench operator-(const Wrench& arg) +{ + return Wrench(-arg.force,-arg.torque); +} + +Twist Frame::operator * (const Twist& arg) const +// Complexity : 24M+18A +{ + Twist tmp; + tmp.rot = M*arg.rot; + tmp.vel = M*arg.vel+p*tmp.rot; + return tmp; +} +Twist Frame::Inverse(const Twist& arg) const +{ + Twist tmp; + tmp.rot = M.Inverse(arg.rot); + tmp.vel = M.Inverse(arg.vel-p*arg.rot); + return tmp; +} + +Twist Twist::Zero() +{ + return Twist(Vector::Zero(),Vector::Zero()); +} + + +void Twist::ReverseSign() +{ + vel.ReverseSign(); + rot.ReverseSign(); +} + +Twist Twist::RefPoint(const Vector& v_base_AB) const + // Changes the reference point of the twist. + // The vector v_base_AB is expressed in the same base as the twist + // The vector v_base_AB is a vector from the old point to + // the new point. + // Complexity : 6M+6A +{ + return Twist(this->vel+this->rot*v_base_AB,this->rot); +} + +Twist& Twist::operator-=(const Twist& arg) +{ + vel-=arg.vel; + rot -=arg.rot; + return *this; +} + +Twist& Twist::operator+=(const Twist& arg) +{ + vel+=arg.vel; + rot +=arg.rot; + return *this; +} + +double& Twist::operator()(int i) +{ + // assert((0<=i)&&(i<6)); done by underlying routines + if (i<3) + return vel(i); + else + return rot(i-3); +} + +double Twist::operator()(int i) const +{ + // assert((0<=i)&&(i<6)); done by underlying routines + if (i<3) + return vel(i); + else + return rot(i-3); +} + + +Twist operator*(const Twist& lhs,double rhs) +{ + return Twist(lhs.vel*rhs,lhs.rot*rhs); +} + +Twist operator*(double lhs,const Twist& rhs) +{ + return Twist(lhs*rhs.vel,lhs*rhs.rot); +} + +Twist operator/(const Twist& lhs,double rhs) +{ + return Twist(lhs.vel/rhs,lhs.rot/rhs); +} + +// addition of Twist's +Twist operator+(const Twist& lhs,const Twist& rhs) +{ + return Twist(lhs.vel+rhs.vel,lhs.rot+rhs.rot); +} + +Twist operator-(const Twist& lhs,const Twist& rhs) +{ + return Twist(lhs.vel-rhs.vel,lhs.rot-rhs.rot); +} + +// unary - +Twist operator-(const Twist& arg) +{ + return Twist(-arg.vel,-arg.rot); +} + + +//Spatial products for twists +Twist operator*(const Twist& lhs,const Twist& rhs) +{ + return Twist(lhs.rot*rhs.vel+lhs.vel*rhs.rot,lhs.rot*rhs.rot); +} +Wrench operator*(const Twist& lhs,const Wrench& rhs) +{ + return Wrench(lhs.rot*rhs.force,lhs.rot*rhs.torque+lhs.vel*rhs.force); +} + +Frame::Frame(const Rotation & R) +{ + M=R; + p=Vector::Zero(); +} + +Frame::Frame(const Vector & V) +{ + M = Rotation::Identity(); + p = V; +} + +Frame::Frame(const Rotation & R, const Vector & V) +{ + M = R; + p = V; +} + + Frame operator *(const Frame& lhs,const Frame& rhs) +// Complexity : 36M+36A +{ + return Frame(lhs.M*rhs.M,lhs.M*rhs.p+lhs.p); +} + +Vector Frame::operator *(const Vector & arg) const +{ + return M*arg+p; +} + +Vector Frame::Inverse(const Vector& arg) const +{ + return M.Inverse(arg-p); +} + +Frame Frame::Inverse() const +{ + return Frame(M.Inverse(),-M.Inverse(p)); +} + + +Frame& Frame::operator =(const Frame & arg) +{ + M = arg.M; + p = arg.p; + return *this; +} + +Frame::Frame(const Frame & arg) : + p(arg.p),M(arg.M) +{} + + +void Vector::ReverseSign() +{ + data[0] = -data[0]; + data[1] = -data[1]; + data[2] = -data[2]; +} + + + +Vector operator-(const Vector & arg) +{ + Vector tmp; + tmp.data[0]=-arg.data[0]; + tmp.data[1]=-arg.data[1]; + tmp.data[2]=-arg.data[2]; + return tmp; +} + +void Vector::Set2DXY(const Vector2& v) +// a 3D vector where the 2D vector v is put in the XY plane +{ + data[0]=v(0); + data[1]=v(1); + data[2]=0; + +} +void Vector::Set2DYZ(const Vector2& v) +// a 3D vector where the 2D vector v is put in the YZ plane +{ + data[1]=v(0); + data[2]=v(1); + data[0]=0; + +} + +void Vector::Set2DZX(const Vector2& v) +// a 3D vector where the 2D vector v is put in the ZX plane +{ + data[2]=v(0); + data[0]=v(1); + data[1]=0; + +} + + + + + +double& Rotation::operator()(int i,int j) { + FRAMES_CHECKI((0<=i)&&(i<=2)&&(0<=j)&&(j<=2)); + return data[i*3+j]; +} + +double Rotation::operator()(int i,int j) const { + FRAMES_CHECKI((0<=i)&&(i<=2)&&(0<=j)&&(j<=2)); + return data[i*3+j]; +} + +Rotation::Rotation( double Xx,double Yx,double Zx, + double Xy,double Yy,double Zy, + double Xz,double Yz,double Zz) +{ + data[0] = Xx;data[1]=Yx;data[2]=Zx; + data[3] = Xy;data[4]=Yy;data[5]=Zy; + data[6] = Xz;data[7]=Yz;data[8]=Zz; +} + + +Rotation::Rotation(const Vector& x,const Vector& y,const Vector& z) +{ + data[0] = x.data[0];data[3] = x.data[1];data[6] = x.data[2]; + data[1] = y.data[0];data[4] = y.data[1];data[7] = y.data[2]; + data[2] = z.data[0];data[5] = z.data[1];data[8] = z.data[2]; +} + +Rotation::Rotation(const Rotation& arg) { + int count=9; + while (count--) data[count] = arg.data[count]; +} + +Rotation& Rotation::operator=(const Rotation& arg) { + int count=9; + while (count--) data[count] = arg.data[count]; + return *this; +} + +Vector Rotation::operator*(const Vector& v) const { +// Complexity : 9M+6A + return Vector( + data[0]*v.data[0] + data[1]*v.data[1] + data[2]*v.data[2], + data[3]*v.data[0] + data[4]*v.data[1] + data[5]*v.data[2], + data[6]*v.data[0] + data[7]*v.data[1] + data[8]*v.data[2] + ); +} + +Twist Rotation::operator * (const Twist& arg) const + // Transformation of the base to which the twist is expressed. + // look at Frame*Twist for a transformation that also transforms + // the velocity reference point. + // Complexity : 18M+12A +{ + return Twist((*this)*arg.vel,(*this)*arg.rot); +} + +Wrench Rotation::operator * (const Wrench& arg) const + // Transformation of the base to which the wrench is expressed. + // look at Frame*Twist for a transformation that also transforms + // the force reference point. +{ + return Wrench((*this)*arg.force,(*this)*arg.torque); +} + +Rotation Rotation::Identity() { + return Rotation(1,0,0,0,1,0,0,0,1); +} +// *this = *this * ROT(X,angle) +void Rotation::DoRotX(double angle) +{ + double cs = cos(angle); + double sn = sin(angle); + double x1,x2,x3; + x1 = cs* (*this)(0,1) + sn* (*this)(0,2); + x2 = cs* (*this)(1,1) + sn* (*this)(1,2); + x3 = cs* (*this)(2,1) + sn* (*this)(2,2); + (*this)(0,2) = -sn* (*this)(0,1) + cs* (*this)(0,2); + (*this)(1,2) = -sn* (*this)(1,1) + cs* (*this)(1,2); + (*this)(2,2) = -sn* (*this)(2,1) + cs* (*this)(2,2); + (*this)(0,1) = x1; + (*this)(1,1) = x2; + (*this)(2,1) = x3; +} + +void Rotation::DoRotY(double angle) +{ + double cs = cos(angle); + double sn = sin(angle); + double x1,x2,x3; + x1 = cs* (*this)(0,0) - sn* (*this)(0,2); + x2 = cs* (*this)(1,0) - sn* (*this)(1,2); + x3 = cs* (*this)(2,0) - sn* (*this)(2,2); + (*this)(0,2) = sn* (*this)(0,0) + cs* (*this)(0,2); + (*this)(1,2) = sn* (*this)(1,0) + cs* (*this)(1,2); + (*this)(2,2) = sn* (*this)(2,0) + cs* (*this)(2,2); + (*this)(0,0) = x1; + (*this)(1,0) = x2; + (*this)(2,0) = x3; +} + +void Rotation::DoRotZ(double angle) +{ + double cs = cos(angle); + double sn = sin(angle); + double x1,x2,x3; + x1 = cs* (*this)(0,0) + sn* (*this)(0,1); + x2 = cs* (*this)(1,0) + sn* (*this)(1,1); + x3 = cs* (*this)(2,0) + sn* (*this)(2,1); + (*this)(0,1) = -sn* (*this)(0,0) + cs* (*this)(0,1); + (*this)(1,1) = -sn* (*this)(1,0) + cs* (*this)(1,1); + (*this)(2,1) = -sn* (*this)(2,0) + cs* (*this)(2,1); + (*this)(0,0) = x1; + (*this)(1,0) = x2; + (*this)(2,0) = x3; +} + + +Rotation Rotation::RotX(double angle) { + double cs=cos(angle); + double sn=sin(angle); + return Rotation(1,0,0,0,cs,-sn,0,sn,cs); +} +Rotation Rotation::RotY(double angle) { + double cs=cos(angle); + double sn=sin(angle); + return Rotation(cs,0,sn,0,1,0,-sn,0,cs); +} +Rotation Rotation::RotZ(double angle) { + double cs=cos(angle); + double sn=sin(angle); + return Rotation(cs,-sn,0,sn,cs,0,0,0,1); +} + + + + +void Frame::Integrate(const Twist& t_this,double samplefrequency) +{ + double n = t_this.rot.Norm()/samplefrequency; + if (n +#include +#include +#include + +namespace KDL { + + +std::ostream& operator << (std::ostream& os,const Vector& v) { + os << "[" << std::setw(KDL_FRAME_WIDTH) << v(0) << "," << std::setw(KDL_FRAME_WIDTH)<> operators. +std::ostream& operator << (std::ostream& os,const Rotation2& R) { + os << "[" << R.GetRot()*rad2deg << "]"; + return os; +} + +std::ostream& operator << (std::ostream& os, const Frame2& T) +{ + os << T.M << T.p; + return os; +} + +std::istream& operator >> (std::istream& is,Vector& v) +{ IOTrace("Stream input Vector (vector or ZERO)"); + char storage[10]; + EatWord(is,"[]",storage,10); + if (strlen(storage)==0) { + Eat(is,'['); + is >> v(0); + Eat(is,','); + is >> v(1); + Eat(is,','); + is >> v(2); + EatEnd(is,']'); + IOTracePop(); + return is; + } + if (strcmp(storage,"ZERO")==0) { + v = Vector::Zero(); + IOTracePop(); + return is; + } + throw Error_Frame_Vector_Unexpected_id(); +} + +std::istream& operator >> (std::istream& is,Twist& v) +{ IOTrace("Stream input Twist"); + Eat(is,'['); + is >> v.vel(0); + Eat(is,','); + is >> v.vel(1); + Eat(is,','); + is >> v.vel(2); + Eat(is,','); + is >> v.rot(0); + Eat(is,','); + is >> v.rot(1); + Eat(is,','); + is >> v.rot(2); + EatEnd(is,']'); + IOTracePop(); + return is; +} + +std::istream& operator >> (std::istream& is,Wrench& v) +{ IOTrace("Stream input Wrench"); + Eat(is,'['); + is >> v.force(0); + Eat(is,','); + is >> v.force(1); + Eat(is,','); + is >> v.force(2); + Eat(is,','); + is >> v.torque(0); + Eat(is,','); + is >> v.torque(1); + Eat(is,','); + is >> v.torque(2); + EatEnd(is,']'); + IOTracePop(); + return is; +} + +std::istream& operator >> (std::istream& is,Rotation& r) +{ IOTrace("Stream input Rotation (Matrix or EULERZYX, EULERZYZ,RPY, ROT, IDENTITY)"); + char storage[10]; + EatWord(is,"[]",storage,10); + if (strlen(storage)==0) { + Eat(is,'['); + for (int i=0;i<3;i++) { + is >> r(i,0); + Eat(is,',') ; + is >> r(i,1); + Eat(is,','); + is >> r(i,2); + if (i<2) + Eat(is,';'); + else + EatEnd(is,']'); + } + IOTracePop(); + return is; + } + Vector v; + if (strcmp(storage,"EULERZYX")==0) { + is >> v; + v=v*deg2rad; + r = Rotation::EulerZYX(v(0),v(1),v(2)); + IOTracePop(); + return is; + } + if (strcmp(storage,"EULERZYZ")==0) { + is >> v; + v=v*deg2rad; + r = Rotation::EulerZYZ(v(0),v(1),v(2)); + IOTracePop(); + return is; + } + if (strcmp(storage,"RPY")==0) { + is >> v; + v=v*deg2rad; + r = Rotation::RPY(v(0),v(1),v(2)); + IOTracePop(); + return is; + } + if (strcmp(storage,"ROT")==0) { + is >> v; + double angle; + Eat(is,'['); + is >> angle; + EatEnd(is,']'); + r = Rotation::Rot(v,angle*deg2rad); + IOTracePop(); + return is; + } + if (strcmp(storage,"IDENTITY")==0) { + r = Rotation::Identity(); + IOTracePop(); + return is; + } + throw Error_Frame_Rotation_Unexpected_id(); + return is; +} + +std::istream& operator >> (std::istream& is,Frame& T) +{ IOTrace("Stream input Frame (Rotation,Vector) or DH[...]"); + char storage[10]; + EatWord(is,"[",storage,10); + if (strlen(storage)==0) { + Eat(is,'['); + is >> T.M; + is >> T.p; + EatEnd(is,']'); + IOTracePop(); + return is; + } + if (strcmp(storage,"DH")==0) { + double a,alpha,d,theta; + Eat(is,'['); + is >> a; + Eat(is,','); + is >> alpha; + Eat(is,','); + is >> d; + Eat(is,','); + is >> theta; + EatEnd(is,']'); + T = Frame::DH(a,alpha*deg2rad,d,theta*deg2rad); + IOTracePop(); + return is; + } + throw Error_Frame_Frame_Unexpected_id(); + return is; +} + +std::istream& operator >> (std::istream& is,Vector2& v) +{ IOTrace("Stream input Vector2"); + Eat(is,'['); + is >> v(0); + Eat(is,','); + is >> v(1); + EatEnd(is,']'); + IOTracePop(); + return is; +} +std::istream& operator >> (std::istream& is,Rotation2& r) +{ IOTrace("Stream input Rotation2"); + Eat(is,'['); + double val; + is >> val; + r.Rot(val*deg2rad); + EatEnd(is,']'); + IOTracePop(); + return is; +} +std::istream& operator >> (std::istream& is,Frame2& T) +{ IOTrace("Stream input Frame2"); + is >> T.M; + is >> T.p; + IOTracePop(); + return is; +} + +} // namespace Frame diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/frames_io.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/frames_io.hpp new file mode 100644 index 0000000000000000000000000000000000000000..a358d27383f1f04b25a761375b94229f5a48e7ae --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/frames_io.hpp @@ -0,0 +1,114 @@ +/*************************************************************************** + frames_io.h - description + ------------------------- + begin : June 2006 + copyright : (C) 2006 Erwin Aertbelien + email : firstname.lastname@mech.kuleuven.ac.be + + History (only major changes)( AUTHOR-Description ) : + + Ruben Smits - Added output for jacobian and jntarray 06/2007 + + *************************************************************************** + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; if not, write to the Free Software * + * Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307 USA * + * * + ***************************************************************************/ +/** +// +// \file +// Defines routines for I/O of Frame and related objects. +// \verbatim +// Spaces, tabs and newlines do not have any importance. +// Comments are allowed C-style,C++-style, make/perl/csh -style +// Description of the I/O : +// Vector : OUTPUT : e.g. [10,20,30] +// INPUT : +// 1) [10,20,30] +// 2) Zero +// Twist : e.g. [1,2,3,4,5,6] +// where [1,2,3] is velocity vector +// where [4,5,6] is rotational velocity vector +// Wrench : e.g. [1,2,3,4,5,6] +// where [1,2,3] represents a force vector +// where [4,5,6] represents a torque vector +// Rotation : output : +// [1,2,3; +// 4,5,6; +// 7,8,9] cfr definition of Rotation object. +// input : +// 1) like the output +// 2) EulerZYX,EulerZYZ,RPY word followed by a vector, e.g. : +// Eulerzyx[10,20,30] +// (ANGLES are always expressed in DEGREES for I/O) +// (ANGELS are always expressed in RADIANS for internal representation) +// 3) Rot [1,2,3] [20] Rotates around axis [1,2,3] with an angle +// of 20 degrees. +// 4) Identity returns identity rotation matrix. +// Frames : output : [ Rotationmatrix positionvector ] +// e.g. [ [1,0,0;0,1,0;0,0,1] [1,2,3] ] +// Input : +// 1) [ Rotationmatrix positionvector ] +// 2) DH [ 10,10,50,30] Denavit-Hartenberg representation +// ( is in fact not the representation of a Frame, but more +// limited, cfr. documentation of Frame object.) +// \endverbatim +// +// \warning +// You can use iostream.h or iostream header files for file I/O, +// if one declares the define WANT_STD_IOSTREAM then the standard C++ +// iostreams headers are included instead of the compiler-dependent version +// + * + ****************************************************************************/ +#ifndef FRAMES_IO_H +#define FRAMES_IO_H + +#include "utilities/utility_io.h" +#include "frames.hpp" +#include "jntarray.hpp" +#include "jacobian.hpp" + +namespace KDL { + + //! width to be used when printing variables out with frames_io.h + //! global variable, can be changed. + + + // I/O to C++ stream. + std::ostream& operator << (std::ostream& os,const Vector& v); + std::ostream& operator << (std::ostream& os,const Rotation& R); + std::ostream& operator << (std::ostream& os,const Frame& T); + std::ostream& operator << (std::ostream& os,const Twist& T); + std::ostream& operator << (std::ostream& os,const Wrench& T); + std::ostream& operator << (std::ostream& os,const Vector2& v); + std::ostream& operator << (std::ostream& os,const Rotation2& R); + std::ostream& operator << (std::ostream& os,const Frame2& T); + + + + std::istream& operator >> (std::istream& is,Vector& v); + std::istream& operator >> (std::istream& is,Rotation& R); + std::istream& operator >> (std::istream& is,Frame& T); + std::istream& operator >> (std::istream& os,Twist& T); + std::istream& operator >> (std::istream& os,Wrench& T); + std::istream& operator >> (std::istream& is,Vector2& v); + std::istream& operator >> (std::istream& is,Rotation2& R); + std::istream& operator >> (std::istream& is,Frame2& T); + + +} // namespace Frame + +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/framevel.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/framevel.cpp new file mode 100644 index 0000000000000000000000000000000000000000..80e157c235dfb9942da596cd26901c15e632ac50 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/framevel.cpp @@ -0,0 +1,27 @@ +/***************************************************************************** + * \author + * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven + * + * \version + * ORO_Geometry V0.2 + * + * \par History + * - $log$ + * + * \par Release + * $Id: rframes.cpp,v 1.1.1.1 2002/08/26 14:14:21 rmoreas Exp $ + * $Name: $ + ****************************************************************************/ + + +#include "framevel.hpp" + +namespace KDL { + +#ifndef KDL_INLINE + #include "framevel.inl" +#endif + + + +} diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/framevel.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/framevel.hpp new file mode 100644 index 0000000000000000000000000000000000000000..b84e69c1fd2820bd627cb30c2d675616dec87bca --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/framevel.hpp @@ -0,0 +1,470 @@ +/***************************************************************************** + * \file + * This file contains the definition of classes for a + * Rall Algebra of (subset of) the classes defined in frames, + * i.e. classes that contain a pair (value,derivative) and define operations on that pair + * this classes are useful for automatic differentiation ( <-> symbolic diff , <-> numeric diff) + * Defines VectorVel, RotationVel, FrameVel. Look at Frames.h for details on how to work + * with Frame objects. + * \author + * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven + * + * \version + * ORO_Geometry V0.2 + * + * \par History + * - $log$ + * + * \par Release + * $Id: rframes.h,v 1.1.1.1 2002/08/26 14:14:21 rmoreas Exp $ + * $Name: $ + ****************************************************************************/ + +#ifndef KDL_FRAMEVEL_H +#define KDL_FRAMEVEL_H + +#include "utilities/utility.h" +#include "utilities/rall1d.h" +#include "utilities/traits.h" + +#include "frames.hpp" + + +namespace KDL { + +typedef Rall1d doubleVel; + +IMETHOD doubleVel diff(const doubleVel& a,const doubleVel& b,double dt=1.0) { + return doubleVel((b.t-a.t)/dt,(b.grad-a.grad)/dt); +} + +IMETHOD doubleVel addDelta(const doubleVel& a,const doubleVel&da,double dt=1.0) { + return doubleVel(a.t+da.t*dt,a.grad+da.grad*dt); +} + +IMETHOD void random(doubleVel& F) { + random(F.t); + random(F.grad); +} +IMETHOD void posrandom(doubleVel& F) { + posrandom(F.t); + posrandom(F.grad); +} + +} + +template <> +struct Traits { + typedef double valueType; + typedef KDL::doubleVel derivType; +}; + +namespace KDL { + +class TwistVel; +class VectorVel; +class FrameVel; +class RotationVel; + +// Equal is friend function, but default arguments for friends are forbidden (§8.3.6.4) +IMETHOD bool Equal(const VectorVel& r1,const VectorVel& r2,double eps=epsilon); +IMETHOD bool Equal(const Vector& r1,const VectorVel& r2,double eps=epsilon); +IMETHOD bool Equal(const VectorVel& r1,const Vector& r2,double eps=epsilon); +IMETHOD bool Equal(const RotationVel& r1,const RotationVel& r2,double eps=epsilon); +IMETHOD bool Equal(const Rotation& r1,const RotationVel& r2,double eps=epsilon); +IMETHOD bool Equal(const RotationVel& r1,const Rotation& r2,double eps=epsilon); +IMETHOD bool Equal(const FrameVel& r1,const FrameVel& r2,double eps=epsilon); +IMETHOD bool Equal(const Frame& r1,const FrameVel& r2,double eps=epsilon); +IMETHOD bool Equal(const FrameVel& r1,const Frame& r2,double eps=epsilon); +IMETHOD bool Equal(const TwistVel& a,const TwistVel& b,double eps=epsilon); +IMETHOD bool Equal(const Twist& a,const TwistVel& b,double eps=epsilon); +IMETHOD bool Equal(const TwistVel& a,const Twist& b,double eps=epsilon); + + +class VectorVel +// = TITLE +// An VectorVel is a Vector and its first derivative +// = CLASS TYPE +// Concrete +{ +public: + Vector p; // position vector + Vector v; // velocity vector +public: + VectorVel():p(),v(){} + VectorVel(const Vector& _p,const Vector& _v):p(_p),v(_v) {} + explicit VectorVel(const Vector& _p):p(_p),v(Vector::Zero()) {} + + Vector value() const { return p;} + Vector deriv() const { return v;} + + IMETHOD VectorVel& operator = (const VectorVel& arg); + IMETHOD VectorVel& operator = (const Vector& arg); + IMETHOD VectorVel& operator += (const VectorVel& arg); + IMETHOD VectorVel& operator -= (const VectorVel& arg); + IMETHOD static VectorVel Zero(); + IMETHOD void ReverseSign(); + IMETHOD doubleVel Norm(double eps=epsilon) const; + IMETHOD friend VectorVel operator + (const VectorVel& r1,const VectorVel& r2); + IMETHOD friend VectorVel operator - (const VectorVel& r1,const VectorVel& r2); + IMETHOD friend VectorVel operator + (const Vector& r1,const VectorVel& r2); + IMETHOD friend VectorVel operator - (const Vector& r1,const VectorVel& r2); + IMETHOD friend VectorVel operator + (const VectorVel& r1,const Vector& r2); + IMETHOD friend VectorVel operator - (const VectorVel& r1,const Vector& r2); + IMETHOD friend VectorVel operator * (const VectorVel& r1,const VectorVel& r2); + IMETHOD friend VectorVel operator * (const VectorVel& r1,const Vector& r2); + IMETHOD friend VectorVel operator * (const Vector& r1,const VectorVel& r2); + IMETHOD friend VectorVel operator * (const VectorVel& r1,double r2); + IMETHOD friend VectorVel operator * (double r1,const VectorVel& r2); + IMETHOD friend VectorVel operator * (const doubleVel& r1,const VectorVel& r2); + IMETHOD friend VectorVel operator * (const VectorVel& r2,const doubleVel& r1); + IMETHOD friend VectorVel operator*(const Rotation& R,const VectorVel& x); + + IMETHOD friend VectorVel operator / (const VectorVel& r1,double r2); + IMETHOD friend VectorVel operator / (const VectorVel& r2,const doubleVel& r1); + IMETHOD friend void SetToZero(VectorVel& v); + + + IMETHOD friend bool Equal(const VectorVel& r1,const VectorVel& r2,double eps); + IMETHOD friend bool Equal(const Vector& r1,const VectorVel& r2,double eps); + IMETHOD friend bool Equal(const VectorVel& r1,const Vector& r2,double eps); + + IMETHOD friend bool operator==(const VectorVel& r1,const VectorVel& r2); + IMETHOD friend bool operator!=(const VectorVel& r1,const VectorVel& r2); + IMETHOD friend bool operator==(const Vector& r1,const VectorVel& r2); + IMETHOD friend bool operator!=(const Vector& r1,const VectorVel& r2); + IMETHOD friend bool operator==(const VectorVel& r1,const Vector& r2); + IMETHOD friend bool operator!=(const VectorVel& r1,const Vector& r2); + + IMETHOD friend VectorVel operator - (const VectorVel& r); + IMETHOD friend doubleVel dot(const VectorVel& lhs,const VectorVel& rhs); + IMETHOD friend doubleVel dot(const VectorVel& lhs,const Vector& rhs); + IMETHOD friend doubleVel dot(const Vector& lhs,const VectorVel& rhs); +}; + + +class RotationVel +// = TITLE +// An RotationVel is a Rotation and its first derivative, a rotation vector +// = CLASS TYPE +// Concrete +{ +public: + Rotation R; // Rotation matrix + Vector w; // rotation vector +public: + RotationVel():R(),w() {} + explicit RotationVel(const Rotation& _R):R(_R),w(Vector::Zero()){} + RotationVel(const Rotation& _R,const Vector& _w):R(_R),w(_w){} + + + Rotation value() const { return R;} + Vector deriv() const { return w;} + + + IMETHOD RotationVel& operator = (const RotationVel& arg); + IMETHOD RotationVel& operator = (const Rotation& arg); + IMETHOD VectorVel UnitX() const; + IMETHOD VectorVel UnitY() const; + IMETHOD VectorVel UnitZ() const; + IMETHOD static RotationVel Identity(); + IMETHOD RotationVel Inverse() const; + IMETHOD VectorVel Inverse(const VectorVel& arg) const; + IMETHOD VectorVel Inverse(const Vector& arg) const; + IMETHOD VectorVel operator*(const VectorVel& arg) const; + IMETHOD VectorVel operator*(const Vector& arg) const; + IMETHOD void DoRotX(const doubleVel& angle); + IMETHOD void DoRotY(const doubleVel& angle); + IMETHOD void DoRotZ(const doubleVel& angle); + IMETHOD static RotationVel RotX(const doubleVel& angle); + IMETHOD static RotationVel RotY(const doubleVel& angle); + IMETHOD static RotationVel RotZ(const doubleVel& angle); + IMETHOD static RotationVel Rot(const Vector& rotvec,const doubleVel& angle); + // rotvec has arbitrary norm + // rotation around a constant vector ! + IMETHOD static RotationVel Rot2(const Vector& rotvec,const doubleVel& angle); + // rotvec is normalized. + // rotation around a constant vector ! + IMETHOD friend RotationVel operator* (const RotationVel& r1,const RotationVel& r2); + IMETHOD friend RotationVel operator* (const Rotation& r1,const RotationVel& r2); + IMETHOD friend RotationVel operator* (const RotationVel& r1,const Rotation& r2); + IMETHOD friend bool Equal(const RotationVel& r1,const RotationVel& r2,double eps); + IMETHOD friend bool Equal(const Rotation& r1,const RotationVel& r2,double eps); + IMETHOD friend bool Equal(const RotationVel& r1,const Rotation& r2,double eps); + + IMETHOD friend bool operator==(const RotationVel& r1,const RotationVel& r2); + IMETHOD friend bool operator!=(const RotationVel& r1,const RotationVel& r2); + IMETHOD friend bool operator==(const Rotation& r1,const RotationVel& r2); + IMETHOD friend bool operator!=(const Rotation& r1,const RotationVel& r2); + IMETHOD friend bool operator==(const RotationVel& r1,const Rotation& r2); + IMETHOD friend bool operator!=(const RotationVel& r1,const Rotation& r2); + + IMETHOD TwistVel Inverse(const TwistVel& arg) const; + IMETHOD TwistVel Inverse(const Twist& arg) const; + IMETHOD TwistVel operator * (const TwistVel& arg) const; + IMETHOD TwistVel operator * (const Twist& arg) const; +}; + + +class FrameVel +// = TITLE +// An FrameVel is a Frame and its first derivative, a Twist vector +// = CLASS TYPE +// Concrete +// = CAVEATS +// +{ +public: + RotationVel M; + VectorVel p; +public: + FrameVel(){} + + explicit FrameVel(const Frame& _T): + M(_T.M),p(_T.p) {} + + FrameVel(const Frame& _T,const Twist& _t): + M(_T.M,_t.rot),p(_T.p,_t.vel) {} + + FrameVel(const RotationVel& _M,const VectorVel& _p): + M(_M),p(_p) {} + + + Frame value() const { return Frame(M.value(),p.value());} + Twist deriv() const { return Twist(p.deriv(),M.deriv());} + + + IMETHOD FrameVel& operator = (const Frame& arg); + IMETHOD FrameVel& operator = (const FrameVel& arg); + IMETHOD static FrameVel Identity(); + IMETHOD FrameVel Inverse() const; + IMETHOD VectorVel Inverse(const VectorVel& arg) const; + IMETHOD VectorVel operator*(const VectorVel& arg) const; + IMETHOD VectorVel operator*(const Vector& arg) const; + IMETHOD VectorVel Inverse(const Vector& arg) const; + IMETHOD Frame GetFrame() const; + IMETHOD Twist GetTwist() const; + IMETHOD friend FrameVel operator * (const FrameVel& f1,const FrameVel& f2); + IMETHOD friend FrameVel operator * (const Frame& f1,const FrameVel& f2); + IMETHOD friend FrameVel operator * (const FrameVel& f1,const Frame& f2); + IMETHOD friend bool Equal(const FrameVel& r1,const FrameVel& r2,double eps); + IMETHOD friend bool Equal(const Frame& r1,const FrameVel& r2,double eps); + IMETHOD friend bool Equal(const FrameVel& r1,const Frame& r2,double eps); + + IMETHOD friend bool operator==(const FrameVel& a,const FrameVel& b); + IMETHOD friend bool operator!=(const FrameVel& a,const FrameVel& b); + IMETHOD friend bool operator==(const Frame& a,const FrameVel& b); + IMETHOD friend bool operator!=(const Frame& a,const FrameVel& b); + IMETHOD friend bool operator==(const FrameVel& a,const Frame& b); + IMETHOD friend bool operator!=(const FrameVel& a,const Frame& b); + + IMETHOD TwistVel Inverse(const TwistVel& arg) const; + IMETHOD TwistVel Inverse(const Twist& arg) const; + IMETHOD TwistVel operator * (const TwistVel& arg) const; + IMETHOD TwistVel operator * (const Twist& arg) const; +}; + + +//very similar to Wrench class. +class TwistVel +// = TITLE +// This class represents a TwistVel. This is a velocity and rotational velocity together +{ +public: + VectorVel vel; + VectorVel rot; +public: + +// = Constructors + TwistVel():vel(),rot() {}; + TwistVel(const VectorVel& _vel,const VectorVel& _rot):vel(_vel),rot(_rot) {}; + TwistVel(const Twist& p,const Twist& v):vel(p.vel, v.vel), rot( p.rot, v.rot) {}; + TwistVel(const Twist& p):vel(p.vel), rot( p.rot) {}; + + Twist value() const { + return Twist(vel.value(),rot.value()); + } + Twist deriv() const { + return Twist(vel.deriv(),rot.deriv()); + } +// = Operators + IMETHOD TwistVel& operator-=(const TwistVel& arg); + IMETHOD TwistVel& operator+=(const TwistVel& arg); + +// = External operators + IMETHOD friend TwistVel operator*(const TwistVel& lhs,double rhs); + IMETHOD friend TwistVel operator*(double lhs,const TwistVel& rhs); + IMETHOD friend TwistVel operator/(const TwistVel& lhs,double rhs); + + IMETHOD friend TwistVel operator*(const TwistVel& lhs,const doubleVel& rhs); + IMETHOD friend TwistVel operator*(const doubleVel& lhs,const TwistVel& rhs); + IMETHOD friend TwistVel operator/(const TwistVel& lhs,const doubleVel& rhs); + + IMETHOD friend TwistVel operator+(const TwistVel& lhs,const TwistVel& rhs); + IMETHOD friend TwistVel operator-(const TwistVel& lhs,const TwistVel& rhs); + IMETHOD friend TwistVel operator-(const TwistVel& arg); + IMETHOD friend void SetToZero(TwistVel& v); + + +// = Zero + static IMETHOD TwistVel Zero(); + +// = Reverse Sign + IMETHOD void ReverseSign(); + +// = Change Reference point + IMETHOD TwistVel RefPoint(const VectorVel& v_base_AB); + // Changes the reference point of the TwistVel. + // The VectorVel v_base_AB is expressed in the same base as the TwistVel + // The VectorVel v_base_AB is a VectorVel from the old point to + // the new point. + // Complexity : 6M+6A + + // = Equality operators + // do not use operator == because the definition of Equal(.,.) is slightly + // different. It compares whether the 2 arguments are equal in an eps-interval + IMETHOD friend bool Equal(const TwistVel& a,const TwistVel& b,double eps); + IMETHOD friend bool Equal(const Twist& a,const TwistVel& b,double eps); + IMETHOD friend bool Equal(const TwistVel& a,const Twist& b,double eps); + + IMETHOD friend bool operator==(const TwistVel& a,const TwistVel& b); + IMETHOD friend bool operator!=(const TwistVel& a,const TwistVel& b); + IMETHOD friend bool operator==(const Twist& a,const TwistVel& b); + IMETHOD friend bool operator!=(const Twist& a,const TwistVel& b); + IMETHOD friend bool operator==(const TwistVel& a,const Twist& b); + IMETHOD friend bool operator!=(const TwistVel& a,const Twist& b); + +// = Conversion to other entities + IMETHOD Twist GetTwist() const; + IMETHOD Twist GetTwistDot() const; +// = Friends + friend class RotationVel; + friend class FrameVel; + +}; + +IMETHOD VectorVel diff(const VectorVel& a,const VectorVel& b,double dt=1.0) { + return VectorVel(diff(a.p,b.p,dt),diff(a.v,b.v,dt)); +} + +IMETHOD VectorVel addDelta(const VectorVel& a,const VectorVel&da,double dt=1.0) { + return VectorVel(addDelta(a.p,da.p,dt),addDelta(a.v,da.v,dt)); +} +IMETHOD VectorVel diff(const RotationVel& a,const RotationVel& b,double dt = 1.0) { + return VectorVel(diff(a.R,b.R,dt),diff(a.w,b.w,dt)); +} + +IMETHOD RotationVel addDelta(const RotationVel& a,const VectorVel&da,double dt=1.0) { + return RotationVel(addDelta(a.R,da.p,dt),addDelta(a.w,da.v,dt)); +} + +IMETHOD TwistVel diff(const FrameVel& a,const FrameVel& b,double dt=1.0) { + return TwistVel(diff(a.M,b.M,dt),diff(a.p,b.p,dt)); +} + +IMETHOD FrameVel addDelta(const FrameVel& a,const TwistVel& da,double dt=1.0) { + return FrameVel( + addDelta(a.M,da.rot,dt), + addDelta(a.p,da.vel,dt) + ); +} + +IMETHOD void random(VectorVel& a) { + random(a.p); + random(a.v); +} +IMETHOD void random(TwistVel& a) { + random(a.vel); + random(a.rot); +} + +IMETHOD void random(RotationVel& R) { + random(R.R); + random(R.w); +} + +IMETHOD void random(FrameVel& F) { + random(F.M); + random(F.p); +} +IMETHOD void posrandom(VectorVel& a) { + posrandom(a.p); + posrandom(a.v); +} +IMETHOD void posrandom(TwistVel& a) { + posrandom(a.vel); + posrandom(a.rot); +} + +IMETHOD void posrandom(RotationVel& R) { + posrandom(R.R); + posrandom(R.w); +} + +IMETHOD void posrandom(FrameVel& F) { + posrandom(F.M); + posrandom(F.p); +} + +#ifdef KDL_INLINE +#include "framevel.inl" +#endif + +} // namespace KDL + +template<> struct std::hash +{ + std::size_t operator()(KDL::doubleVel const& dv) const noexcept + { + size_t seed = 0; + KDL::hash_combine(seed, dv.value()); + KDL::hash_combine(seed, dv.deriv()); + return seed; + } +}; + +template<> struct std::hash +{ + std::size_t operator()(KDL::VectorVel const& vv) const noexcept + { + size_t seed = 0; + KDL::hash_combine(seed, vv.p); + KDL::hash_combine(seed, vv.v); + return seed; + } +}; + +template<> struct std::hash +{ + std::size_t operator()(KDL::RotationVel const& rv) const noexcept + { + size_t seed = 0; + KDL::hash_combine(seed, rv.R); + KDL::hash_combine(seed, rv.w); + return seed; + } +}; + +template<> struct std::hash +{ + std::size_t operator()(KDL::FrameVel const& fv) const noexcept + { + size_t seed = 0; + KDL::hash_combine(seed, fv.M); + KDL::hash_combine(seed, fv.p); + return seed; + } +}; + +template<> struct std::hash +{ + std::size_t operator()(KDL::TwistVel const& tv) const noexcept + { + size_t seed = 0; + KDL::hash_combine(seed, tv.vel); + KDL::hash_combine(seed, tv.rot); + return seed; + } +}; + +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/framevel.inl b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/framevel.inl new file mode 100644 index 0000000000000000000000000000000000000000..05aceb0afd8488980de39c9f352079345264b43f --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/framevel.inl @@ -0,0 +1,661 @@ +/***************************************************************************** + * \file + * provides inline functions of rframes.h + * + * \author + * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven + * + * \version + * ORO_Geometry V0.2 + * + * \par History + * - $log$ + * + * \par Release + * $Id: rframes.inl,v 1.1.1.1 2002/08/26 14:14:21 rmoreas Exp $ + * $Name: $ + ****************************************************************************/ + + +// Methods and operators related to FrameVelVel +// They all delegate most of the work to RotationVelVel and VectorVelVel +FrameVel& FrameVel::operator = (const FrameVel& arg) { + M=arg.M; + p=arg.p; + return *this; +} + +FrameVel FrameVel::Identity() { + return FrameVel(RotationVel::Identity(),VectorVel::Zero()); +} + + +FrameVel operator *(const FrameVel& lhs,const FrameVel& rhs) +{ + return FrameVel(lhs.M*rhs.M,lhs.M*rhs.p+lhs.p); +} +FrameVel operator *(const FrameVel& lhs,const Frame& rhs) +{ + return FrameVel(lhs.M*rhs.M,lhs.M*rhs.p+lhs.p); +} +FrameVel operator *(const Frame& lhs,const FrameVel& rhs) +{ + return FrameVel(lhs.M*rhs.M , lhs.M*rhs.p+lhs.p ); +} + +VectorVel FrameVel::operator *(const VectorVel & arg) const +{ + return M*arg+p; +} +VectorVel FrameVel::operator *(const Vector & arg) const +{ + return M*arg+p; +} + +VectorVel FrameVel::Inverse(const VectorVel& arg) const +{ + return M.Inverse(arg-p); +} + +VectorVel FrameVel::Inverse(const Vector& arg) const +{ + return M.Inverse(arg-p); +} + +FrameVel FrameVel::Inverse() const +{ + return FrameVel(M.Inverse(),-M.Inverse(p)); +} + +FrameVel& FrameVel::operator = (const Frame& arg) { + M = arg.M; + p = arg.p; + return *this; +} +bool Equal(const FrameVel& r1,const FrameVel& r2,double eps) { + return (Equal(r1.M,r2.M,eps) && Equal(r1.p,r2.p,eps)); +} +bool Equal(const Frame& r1,const FrameVel& r2,double eps) { + return (Equal(r1.M,r2.M,eps) && Equal(r1.p,r2.p,eps)); +} +bool Equal(const FrameVel& r1,const Frame& r2,double eps) { + return (Equal(r1.M,r2.M,eps) && Equal(r1.p,r2.p,eps)); +} +bool operator==(const FrameVel& r1,const FrameVel& r2) { +#ifdef KDL_USE_EQUAL + return Equal(r1, r2); +#else + return (r1.p == r2.p && + r1.M == r2.M ); +#endif +} +bool operator!=(const FrameVel& r1,const FrameVel& r2) { + return !operator==(r1,r2); +} +bool operator==(const Frame& r1,const FrameVel& r2) { +#ifdef KDL_USE_EQUAL + return Equal(r1, r2); +#else + return (r1.p == r2.p && + r1.M == r2.M ); +#endif +} +bool operator!=(const Frame& r1,const FrameVel& r2) { + return !operator==(r1,r2); +} +bool operator==(const FrameVel& r1,const Frame& r2) { +#ifdef KDL_USE_EQUAL + return Equal(r1, r2); +#else + return (r1.p == r2.p && + r1.M == r2.M ); +#endif +} +bool operator!=(const FrameVel& r1,const Frame& r2) { + return !operator==(r1,r2); +} + + + +Frame FrameVel::GetFrame() const { + return Frame(M.R,p.p); +} + +Twist FrameVel::GetTwist() const { + return Twist(p.v,M.w); +} + + +RotationVel operator* (const RotationVel& r1,const RotationVel& r2) { + return RotationVel( r1.R*r2.R, r1.w + r1.R*r2.w ); +} + +RotationVel operator* (const Rotation& r1,const RotationVel& r2) { + return RotationVel( r1*r2.R, r1*r2.w ); +} + +RotationVel operator* (const RotationVel& r1,const Rotation& r2) { + return RotationVel( r1.R*r2, r1.w ); +} + +RotationVel& RotationVel::operator = (const RotationVel& arg) { + R=arg.R; + w=arg.w; + return *this; + } +RotationVel& RotationVel::operator = (const Rotation& arg) { + R=arg; + w=Vector::Zero(); + return *this; +} + +VectorVel RotationVel::UnitX() const { + return VectorVel(R.UnitX(),w*R.UnitX()); +} + +VectorVel RotationVel::UnitY() const { + return VectorVel(R.UnitY(),w*R.UnitY()); +} + +VectorVel RotationVel::UnitZ() const { + return VectorVel(R.UnitZ(),w*R.UnitZ()); +} + + + +RotationVel RotationVel::Identity() { + return RotationVel(Rotation::Identity(),Vector::Zero()); +} + +RotationVel RotationVel::Inverse() const { + return RotationVel(R.Inverse(),-R.Inverse(w)); +} + +VectorVel RotationVel::Inverse(const VectorVel& arg) const { + Vector tmp=R.Inverse(arg.p); + return VectorVel(tmp, + R.Inverse(arg.v-w*arg.p) + ); +} + +VectorVel RotationVel::Inverse(const Vector& arg) const { + Vector tmp=R.Inverse(arg); + return VectorVel(tmp, + R.Inverse(-w*arg) + ); +} + + +VectorVel RotationVel::operator*(const VectorVel& arg) const { + Vector tmp=R*arg.p; + return VectorVel(tmp,w*tmp+R*arg.v); +} + +VectorVel RotationVel::operator*(const Vector& arg) const { + Vector tmp=R*arg; + return VectorVel(tmp,w*tmp); +} + + +// = Rotations +// The Rot... static functions give the value of the appropriate rotation matrix back. +// The DoRot... functions apply a rotation R to *this,such that *this = *this * R. + +void RotationVel::DoRotX(const doubleVel& angle) { + w+=R*Vector(angle.grad,0,0); + R.DoRotX(angle.t); +} +RotationVel RotationVel::RotX(const doubleVel& angle) { + return RotationVel(Rotation::RotX(angle.t),Vector(angle.grad,0,0)); +} + +void RotationVel::DoRotY(const doubleVel& angle) { + w+=R*Vector(0,angle.grad,0); + R.DoRotY(angle.t); +} +RotationVel RotationVel::RotY(const doubleVel& angle) { + return RotationVel(Rotation::RotX(angle.t),Vector(0,angle.grad,0)); +} + +void RotationVel::DoRotZ(const doubleVel& angle) { + w+=R*Vector(0,0,angle.grad); + R.DoRotZ(angle.t); +} +RotationVel RotationVel::RotZ(const doubleVel& angle) { + return RotationVel(Rotation::RotZ(angle.t),Vector(0,0,angle.grad)); +} + + +RotationVel RotationVel::Rot(const Vector& rotvec,const doubleVel& angle) +// rotvec has arbitrary norm +// rotation around a constant vector ! +{ + Vector v(rotvec); + v.Normalize(); + return RotationVel(Rotation::Rot2(v,angle.t),v*angle.grad); +} + +RotationVel RotationVel::Rot2(const Vector& rotvec,const doubleVel& angle) + // rotvec is normalized. +{ + return RotationVel(Rotation::Rot2(rotvec,angle.t),rotvec*angle.grad); +} + + +VectorVel operator + (const VectorVel& r1,const VectorVel& r2) { + return VectorVel(r1.p+r2.p,r1.v+r2.v); +} + +VectorVel operator - (const VectorVel& r1,const VectorVel& r2) { + return VectorVel(r1.p-r2.p,r1.v-r2.v); +} + +VectorVel operator + (const VectorVel& r1,const Vector& r2) { + return VectorVel(r1.p+r2,r1.v); +} + +VectorVel operator - (const VectorVel& r1,const Vector& r2) { + return VectorVel(r1.p-r2,r1.v); +} + +VectorVel operator + (const Vector& r1,const VectorVel& r2) { + return VectorVel(r1+r2.p,r2.v); +} + +VectorVel operator - (const Vector& r1,const VectorVel& r2) { + return VectorVel(r1-r2.p,-r2.v); +} + +// unary - +VectorVel operator - (const VectorVel& r) { + return VectorVel(-r.p,-r.v); +} + +void SetToZero(VectorVel& v){ + SetToZero(v.p); + SetToZero(v.v); +} + +// cross prod. +VectorVel operator * (const VectorVel& r1,const VectorVel& r2) { + return VectorVel(r1.p*r2.p, r1.p*r2.v+r1.v*r2.p); +} + +VectorVel operator * (const VectorVel& r1,const Vector& r2) { + return VectorVel(r1.p*r2, r1.v*r2); +} + +VectorVel operator * (const Vector& r1,const VectorVel& r2) { + return VectorVel(r1*r2.p, r1*r2.v); +} + + + +// scalar mult. +VectorVel operator * (double r1,const VectorVel& r2) { + return VectorVel(r1*r2.p, r1*r2.v); +} + +VectorVel operator * (const VectorVel& r1,double r2) { + return VectorVel(r1.p*r2, r1.v*r2); +} + + + +VectorVel operator * (const doubleVel& r1,const VectorVel& r2) { + return VectorVel(r1.t*r2.p, r1.t*r2.v + r1.grad*r2.p); +} + +VectorVel operator * (const VectorVel& r2,const doubleVel& r1) { + return VectorVel(r1.t*r2.p, r1.t*r2.v + r1.grad*r2.p); +} + +VectorVel operator / (const VectorVel& r1,double r2) { + return VectorVel(r1.p/r2, r1.v/r2); +} + +VectorVel operator / (const VectorVel& r2,const doubleVel& r1) { + return VectorVel(r2.p/r1.t, r2.v/r1.t - r2.p*r1.grad/r1.t/r1.t); +} + +VectorVel operator*(const Rotation& R,const VectorVel& x) { + return VectorVel(R*x.p,R*x.v); +} + +VectorVel& VectorVel::operator = (const VectorVel& arg) { + p=arg.p; + v=arg.v; + return *this; +} +VectorVel& VectorVel::operator = (const Vector& arg) { + p=arg; + v=Vector::Zero(); + return *this; +} +VectorVel& VectorVel::operator += (const VectorVel& arg) { + p+=arg.p; + v+=arg.v; + return *this; +} +VectorVel& VectorVel::operator -= (const VectorVel& arg) { + p-=arg.p; + v-=arg.v; + return *this; +} + +VectorVel VectorVel::Zero() { + return VectorVel(Vector::Zero(),Vector::Zero()); +} +void VectorVel::ReverseSign() { + p.ReverseSign(); + v.ReverseSign(); +} +doubleVel VectorVel::Norm(double eps) const { + double n = p.Norm(eps); + if (n < eps) // Setting norm of p and v to 0 in case norm of p is smaller than eps + return doubleVel(0, 0); + return doubleVel(n, dot(p,v)/n); +} + +bool Equal(const VectorVel& r1,const VectorVel& r2,double eps) { + return (Equal(r1.p,r2.p,eps) && Equal(r1.v,r2.v,eps)); +} +bool Equal(const Vector& r1,const VectorVel& r2,double eps) { + return (Equal(r1,r2.p,eps) && Equal(Vector::Zero(),r2.v,eps)); +} +bool Equal(const VectorVel& r1,const Vector& r2,double eps) { + return (Equal(r1.p,r2,eps) && Equal(r1.v,Vector::Zero(),eps)); +} +bool operator==(const VectorVel& r1,const VectorVel& r2) { +#ifdef KDL_USE_EQUAL + return Equal(r1, r2); +#else + return (r1.p == r2.p && + r1.v == r2.v ); +#endif +} +bool operator!=(const VectorVel& r1,const VectorVel& r2) { + return !operator==(r1,r2); +} +bool operator==(const Vector& r1,const VectorVel& r2) { +#ifdef KDL_USE_EQUAL + return Equal(r1, r2); +#else + return (r1 == r2.p && + Vector::Zero() == r2.v); +#endif +} +bool operator!=(const Vector& r1,const VectorVel& r2) { + return !operator==(r1,r2); +} +bool operator==(const VectorVel& r1,const Vector& r2) { +#ifdef KDL_USE_EQUAL + return Equal(r1, r2); +#else + return (r1.p == r2 && + r1.v == Vector::Zero() ); +#endif +} +bool operator!=(const VectorVel& r1,const Vector& r2) { + return !operator==(r1,r2); +} + + +bool Equal(const RotationVel& r1,const RotationVel& r2,double eps) { + return (Equal(r1.w,r2.w,eps) && Equal(r1.R,r2.R,eps)); +} +bool Equal(const Rotation& r1,const RotationVel& r2,double eps) { + return (Equal(Vector::Zero(),r2.w,eps) && Equal(r1,r2.R,eps)); +} +bool Equal(const RotationVel& r1,const Rotation& r2,double eps) { + return (Equal(r1.w,Vector::Zero(),eps) && Equal(r1.R,r2,eps)); +} +bool operator==(const RotationVel& r1,const RotationVel& r2) { +#ifdef KDL_USE_EQUAL + return Equal(r1, r2); +#else + return (r1.w == r2.w && + r1.R == r2.R ); +#endif +} +bool operator!=(const RotationVel& r1,const RotationVel& r2) { + return !operator==(r1,r2); +} +bool operator==(const Rotation& r1,const RotationVel& r2) { +#ifdef KDL_USE_EQUAL + return Equal(r1, r2); +#else + return (Vector::Zero() == r2.w && + r1 == r2.R); +#endif +} +bool operator!=(const Rotation& r1,const RotationVel& r2) { + return !operator==(r1,r2); +} +bool operator==(const RotationVel& r1,const Rotation& r2) { +#ifdef KDL_USE_EQUAL + return Equal(r1, r2); +#else + return (r1.w == Vector::Zero() && + r1.R == r2); +#endif +} +bool operator!=(const RotationVel& r1,const Rotation& r2) { + return !operator==(r1,r2); +} + + +bool Equal(const TwistVel& a,const TwistVel& b,double eps) { + return (Equal(a.rot,b.rot,eps)&& + Equal(a.vel,b.vel,eps) ); +} +bool Equal(const Twist& a,const TwistVel& b,double eps) { + return (Equal(a.rot,b.rot,eps)&& + Equal(a.vel,b.vel,eps) ); +} +bool Equal(const TwistVel& a,const Twist& b,double eps) { + return (Equal(a.rot,b.rot,eps)&& + Equal(a.vel,b.vel,eps) ); +} +bool operator==(const TwistVel& a,const TwistVel& b) { +#ifdef KDL_USE_EQUAL + return Equal(a, b); +#else + return (a.rot == b.rot && + a.vel == b.vel ); +#endif +} +bool operator!=(const TwistVel& a,const TwistVel& b) { + return !operator==(a,b); +} +bool operator==(const Twist& a,const TwistVel& b) { +#ifdef KDL_USE_EQUAL + return Equal(a, b); +#else + return (a.rot == b.rot && + a.vel == b.vel ); +#endif +} +bool operator!=(const Twist& r1,const TwistVel& r2) { + return !operator==(r1,r2); +} +bool operator==(const TwistVel& r1,const Twist& r2) { +#ifdef KDL_USE_EQUAL + return Equal(r1, r2); +#else + return (a.rot == b.rot && + a.vel == b.vel ); +#endif +} +bool operator!=(const TwistVel& r1,const Twist& r2) { + return !operator==(r1,r2); +} + + +IMETHOD doubleVel dot(const VectorVel& lhs,const VectorVel& rhs) { + return doubleVel(dot(lhs.p,rhs.p),dot(lhs.p,rhs.v)+dot(lhs.v,rhs.p)); +} +IMETHOD doubleVel dot(const VectorVel& lhs,const Vector& rhs) { + return doubleVel(dot(lhs.p,rhs),dot(lhs.v,rhs)); +} +IMETHOD doubleVel dot(const Vector& lhs,const VectorVel& rhs) { + return doubleVel(dot(lhs,rhs.p),dot(lhs,rhs.v)); +} + +TwistVel TwistVel::Zero() +{ + return TwistVel(VectorVel::Zero(),VectorVel::Zero()); +} + + +void TwistVel::ReverseSign() +{ + vel.ReverseSign(); + rot.ReverseSign(); +} + +TwistVel TwistVel::RefPoint(const VectorVel& v_base_AB) + // Changes the reference point of the TwistVel. + // The VectorVel v_base_AB is expressed in the same base as the TwistVel + // The VectorVel v_base_AB is a VectorVel from the old point to + // the new point. + // Complexity : 6M+6A +{ + return TwistVel(this->vel+this->rot*v_base_AB,this->rot); +} + +TwistVel& TwistVel::operator-=(const TwistVel& arg) +{ + vel-=arg.vel; + rot -=arg.rot; + return *this; +} + +TwistVel& TwistVel::operator+=(const TwistVel& arg) +{ + vel+=arg.vel; + rot +=arg.rot; + return *this; +} + + +TwistVel operator*(const TwistVel& lhs,double rhs) +{ + return TwistVel(lhs.vel*rhs,lhs.rot*rhs); +} + +TwistVel operator*(double lhs,const TwistVel& rhs) +{ + return TwistVel(lhs*rhs.vel,lhs*rhs.rot); +} + +TwistVel operator/(const TwistVel& lhs,double rhs) +{ + return TwistVel(lhs.vel/rhs,lhs.rot/rhs); +} + + +TwistVel operator*(const TwistVel& lhs,const doubleVel& rhs) +{ + return TwistVel(lhs.vel*rhs,lhs.rot*rhs); +} + +TwistVel operator*(const doubleVel& lhs,const TwistVel& rhs) +{ + return TwistVel(lhs*rhs.vel,lhs*rhs.rot); +} + +TwistVel operator/(const TwistVel& lhs,const doubleVel& rhs) +{ + return TwistVel(lhs.vel/rhs,lhs.rot/rhs); +} + + + +// addition of TwistVel's +TwistVel operator+(const TwistVel& lhs,const TwistVel& rhs) +{ + return TwistVel(lhs.vel+rhs.vel,lhs.rot+rhs.rot); +} + +TwistVel operator-(const TwistVel& lhs,const TwistVel& rhs) +{ + return TwistVel(lhs.vel-rhs.vel,lhs.rot-rhs.rot); +} + +// unary - +TwistVel operator-(const TwistVel& arg) +{ + return TwistVel(-arg.vel,-arg.rot); +} + +void SetToZero(TwistVel& v) +{ + SetToZero(v.vel); + SetToZero(v.rot); +} + + + + + +TwistVel RotationVel::Inverse(const TwistVel& arg) const +{ + return TwistVel(Inverse(arg.vel),Inverse(arg.rot)); +} + +TwistVel RotationVel::operator * (const TwistVel& arg) const +{ + return TwistVel((*this)*arg.vel,(*this)*arg.rot); +} + +TwistVel RotationVel::Inverse(const Twist& arg) const +{ + return TwistVel(Inverse(arg.vel),Inverse(arg.rot)); +} + +TwistVel RotationVel::operator * (const Twist& arg) const +{ + return TwistVel((*this)*arg.vel,(*this)*arg.rot); +} + + +TwistVel FrameVel::operator * (const TwistVel& arg) const +{ + TwistVel tmp; + tmp.rot = M*arg.rot; + tmp.vel = M*arg.vel+p*tmp.rot; + return tmp; +} + +TwistVel FrameVel::operator * (const Twist& arg) const +{ + TwistVel tmp; + tmp.rot = M*arg.rot; + tmp.vel = M*arg.vel+p*tmp.rot; + return tmp; +} + +TwistVel FrameVel::Inverse(const TwistVel& arg) const +{ + TwistVel tmp; + tmp.rot = M.Inverse(arg.rot); + tmp.vel = M.Inverse(arg.vel-p*arg.rot); + return tmp; +} + +TwistVel FrameVel::Inverse(const Twist& arg) const +{ + TwistVel tmp; + tmp.rot = M.Inverse(arg.rot); + tmp.vel = M.Inverse(arg.vel-p*arg.rot); + return tmp; +} + +Twist TwistVel::GetTwist() const { + return Twist(vel.p,rot.p); +} + +Twist TwistVel::GetTwistDot() const { + return Twist(vel.v,rot.v); +} diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/framevel_io.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/framevel_io.hpp new file mode 100644 index 0000000000000000000000000000000000000000..a3dcc07435a050c70a8f5b0c6ec2ce847b1b679a --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/framevel_io.hpp @@ -0,0 +1,55 @@ +/***************************************************************************** + * \file + * provides I/O operations on FrameVels classes + * + * \author + * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven + * + * \version + * ORO_Geometry V2 + * + * \par History + * - $log$ + * + * \par Release + * $Id: rframes_io.h,v 1.1.1.1 2002/08/26 14:14:21 rmoreas Exp $ + * $Name: $ + ****************************************************************************/ +#ifndef KDL_FRAMESVEL_IO +#define KDL_FRAMESVEL_IO + +#include "utilities/utility_io.h" +#include "utilities/rall1d_io.h" + +#include "frames_io.hpp" + +namespace KDL { + +// Output... +inline std::ostream& operator << (std::ostream& os,const VectorVel& r) { + os << "{" << r.p << "," << r.v << "}" << std::endl; + return os; +} + +inline std::ostream& operator << (std::ostream& os,const RotationVel& r) { + os << "{" << std::endl << r.R << "," < + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#include "jacobian.hpp" + +namespace KDL +{ + Jacobian::Jacobian() + { + } + + + Jacobian::Jacobian(unsigned int nr_of_columns): + data(6,nr_of_columns) + { + data.setZero(); + } + + Jacobian::Jacobian(const Jacobian& arg): + data(arg.data) + { + } + + Jacobian& Jacobian::operator = (const Jacobian& arg) + { + this->data=arg.data; + return *this; + } + + + Jacobian::~Jacobian() + { + + } + + void Jacobian::resize(unsigned int new_nr_of_columns) + { + data.conservativeResize(Eigen::NoChange,new_nr_of_columns); + } + + double Jacobian::operator()(unsigned int i,unsigned int j)const + { + return data(i,j); + } + + double& Jacobian::operator()(unsigned int i,unsigned int j) + { + return data(i,j); + } + + unsigned int Jacobian::rows()const + { + return static_cast(data.rows()); + } + + unsigned int Jacobian::columns()const + { + return static_cast(data.cols()); + } + + void SetToZero(Jacobian& jac) + { + jac.data.setZero(); + } + + void Jacobian::changeRefPoint(const Vector& base_AB){ + for(unsigned int i=0;isetColumn(i,this->getColumn(i).RefPoint(base_AB)); + } + + bool changeRefPoint(const Jacobian& src1, const Vector& base_AB, Jacobian& dest) + { + if(src1.columns()!=dest.columns()) + return false; + for(unsigned int i=0;isetColumn(i,rot*this->getColumn(i));; + } + + bool changeBase(const Jacobian& src1, const Rotation& rot, Jacobian& dest) + { + if(src1.columns()!=dest.columns()) + return false; + for(unsigned int i=0;isetColumn(i,frame*this->getColumn(i)); + } + + bool changeRefFrame(const Jacobian& src1,const Frame& frame, Jacobian& dest) + { + if(src1.columns()!=dest.columns()) + return false; + for(unsigned int i=0;i()=Eigen::Map(t.vel.data); + data.col(i).tail<3>()=Eigen::Map(t.rot.data); + } + +} diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/jacobian.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/jacobian.hpp new file mode 100644 index 0000000000000000000000000000000000000000..eff3587f535974491c3f604bb54a113bc9269cea --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/jacobian.hpp @@ -0,0 +1,88 @@ +// Copyright (C) 2007 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#ifndef KDL_JACOBIAN_HPP +#define KDL_JACOBIAN_HPP + +#include "frames.hpp" +#include + +namespace KDL +{ + // Equal is friend function, but default arguments for friends are forbidden (§8.3.6.4) + class Jacobian; + bool Equal(const Jacobian& a,const Jacobian& b,double eps=epsilon); + void SetToZero(Jacobian& jac); + + + class Jacobian + { + public: + + EIGEN_MAKE_ALIGNED_OPERATOR_NEW + Eigen::Matrix data; + Jacobian(); + explicit Jacobian(unsigned int nr_of_columns); + Jacobian(const Jacobian& arg); + + ///Allocates memory for new size (can break realtime behavior) + void resize(unsigned int newNrOfColumns); + + ///Allocates memory if size of this and argument is different + Jacobian& operator=(const Jacobian& arg); + + bool operator ==(const Jacobian& arg)const; + bool operator !=(const Jacobian& arg)const; + + friend bool Equal(const Jacobian& a,const Jacobian& b,double eps); + + + ~Jacobian(); + + double operator()(unsigned int i,unsigned int j)const; + double& operator()(unsigned int i,unsigned int j); + unsigned int rows()const; + unsigned int columns()const; + + friend void SetToZero(Jacobian& jac); + + friend bool changeRefPoint(const Jacobian& src1, const Vector& base_AB, Jacobian& dest); + friend bool changeBase(const Jacobian& src1, const Rotation& rot, Jacobian& dest); + friend bool changeRefFrame(const Jacobian& src1,const Frame& frame, Jacobian& dest); + + Twist getColumn(unsigned int i) const; + void setColumn(unsigned int i,const Twist& t); + + void changeRefPoint(const Vector& base_AB); + void changeBase(const Rotation& rot); + void changeRefFrame(const Frame& frame); + + + }; + + bool changeRefPoint(const Jacobian& src1, const Vector& base_AB, Jacobian& dest); + bool changeBase(const Jacobian& src1, const Rotation& rot, Jacobian& dest); + bool changeRefFrame(const Jacobian& src1,const Frame& frame, Jacobian& dest); + + +} + +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/jntarray.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/jntarray.cpp new file mode 100644 index 0000000000000000000000000000000000000000..32c875d0bbd982e795a803948a466b54426750a3 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/jntarray.cpp @@ -0,0 +1,123 @@ +// Copyright (C) 2007 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#include "jntarray.hpp" + +namespace KDL +{ + JntArray::JntArray() + { + } + + JntArray::JntArray(unsigned int _size): + data(_size) + { + data.setZero(); + } + + + JntArray::JntArray(const JntArray& arg): + data(arg.data) + { + } + + JntArray& JntArray::operator = (const JntArray& arg) + { + data=arg.data; + return *this; + } + + + JntArray::~JntArray() + { + } + + void JntArray::resize(unsigned int newSize) + { + data.conservativeResizeLike(Eigen::VectorXd::Zero(newSize)); + } + + double JntArray::operator()(unsigned int i,unsigned int j)const + { + assert(j==0); + return data(i); + } + + double& JntArray::operator()(unsigned int i,unsigned int j) + { + assert(j==0); + return data(i); + } + + unsigned int JntArray::rows()const + { + return static_cast(data.rows()); + } + + unsigned int JntArray::columns()const + { + return static_cast(data.cols()); + } + + void Add(const JntArray& src1,const JntArray& src2,JntArray& dest) + { + dest.data=src1.data+src2.data; + } + + void Subtract(const JntArray& src1,const JntArray& src2,JntArray& dest) + { + dest.data=src1.data-src2.data; + } + + void Multiply(const JntArray& src,const double& factor,JntArray& dest) + { + dest.data=factor*src.data; + } + + void Divide(const JntArray& src,const double& factor,JntArray& dest) + { + dest.data=src.data/factor; + } + + void MultiplyJacobian(const Jacobian& jac, const JntArray& src, Twist& dest) + { + Eigen::Matrix t=jac.data.lazyProduct(src.data); + dest=Twist(Vector(t(0),t(1),t(2)),Vector(t(3),t(4),t(5))); + } + + void SetToZero(JntArray& array) + { + array.data.setZero(); + } + + bool Equal(const JntArray& src1, const JntArray& src2,double eps) + { + if(src1.rows()!=src2.rows()) + return false; + return (src1.data-src2.data).isZero(eps); + } + + bool operator==(const JntArray& src1,const JntArray& src2){return Equal(src1,src2);} + //bool operator!=(const JntArray& src1,const JntArray& src2){return Equal(src1,src2);} + +} + + diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/jntarray.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/jntarray.hpp new file mode 100644 index 0000000000000000000000000000000000000000..f69fc5317d689ccb28e26951a2fda87448ed7354 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/jntarray.hpp @@ -0,0 +1,227 @@ +// Copyright (C) 2007 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#ifndef KDL_JNTARRAY_HPP +#define KDL_JNTARRAY_HPP + +#include "frames.hpp" +#include "jacobian.hpp" + +#include + +namespace KDL +{ + /** + * @brief This class represents an fixed size array containing + * joint values of a KDL::Chain. + * + * \warning An object constructed with the default constructor provides + * a valid, but inert, object. Many of the member functions will do + * the correct thing and have no affect on this object, but some + * member functions can _NOT_ deal with an inert/empty object. These + * functions will assert() and exit the program instead. The intended use + * case for the default constructor (in an RTT/OCL setting) is outlined in + * code below - the default constructor plus the resize() function allow + * use of JntArray objects whose size is set within a configureHook() call + * (typically based on a size determined from a property). + +\code +class MyTask : public RTT::TaskContext +{ + JntArray j; + MyTask() + {} // invokes j's default constructor + + bool configureHook() + { + unsigned int size = some_property.rvalue(); + j.resize(size) + ... + } + + void updateHook() + { + ** use j here + } +}; +\endcode + + */ + + class JntArray + { + public: + Eigen::VectorXd data; + + /** Construct with _no_ data array + * @post NULL == data + * @post 0 == rows() + * @warning use of an object constructed like this, without + * a resize() first, may result in program exit! See class + * documentation. + */ + JntArray(); + /** + * Constructor of the joint array + * + * @param size size of the array, this cannot be changed + * afterwards. + * @pre 0 < size + * @post NULL != data + * @post 0 < rows() + * @post all elements in data have 0 value + */ + explicit JntArray(unsigned int size); + + /** Copy constructor + * @note Will correctly copy an empty object + */ + JntArray(const JntArray& arg); + ~JntArray(); + /** Resize the array + * @warning This causes a dynamic allocation (and potentially + * also a dynamic deallocation). This _will_ negatively affect + * real-time performance! + * + * @post newSize == rows() + * @post NULL != data + * @post all elements in data have 0 value + */ + void resize(unsigned int newSize); + + JntArray& operator = ( const JntArray& arg); + /** + * get_item operator for the joint array, if a second value is + * given it should be zero, since a JntArray resembles a column. + * + * + * @return the joint value at position i, starting from 0 + * @pre 0 != size (ie non-default constructor or resize() called) + */ + double operator()(unsigned int i,unsigned int j=0)const; + /** + * set_item operator, again if a second value is given it + *should be zero. + * + * @return reference to the joint value at position i,starting + *from zero. + * @pre 0 != size (ie non-default constructor or resize() called) + */ + double& operator()(unsigned int i,unsigned int j=0); + /** + * Returns the number of rows (size) of the array + * + */ + unsigned int rows()const; + /** + * Returns the number of columns of the array, always 1. + */ + unsigned int columns()const; + + friend void Add(const JntArray& src1,const JntArray& src2,JntArray& dest); + friend void Subtract(const JntArray& src1,const JntArray& src2,JntArray& dest); + friend void Multiply(const JntArray& src,const double& factor,JntArray& dest); + friend void Divide(const JntArray& src,const double& factor,JntArray& dest); + friend void MultiplyJacobian(const Jacobian& jac, const JntArray& src, Twist& dest); + friend void SetToZero(JntArray& array); + friend bool Equal(const JntArray& src1,const JntArray& src2,double eps); + + friend bool operator==(const JntArray& src1,const JntArray& src2); + //friend bool operator!=(const JntArray& src1,const JntArray& src2); + }; + + bool operator==(const JntArray& src1,const JntArray& src2); + //bool operator!=(const JntArray& src1,const JntArray& src2); + + /** + * Function to add two joint arrays, all the arguments must + * have the same size: A + B = C. This function is + * aliasing-safe, A or B can be the same array as C. + * + * @param src1 A + * @param src2 B + * @param dest C + */ + void Add(const JntArray& src1,const JntArray& src2,JntArray& dest); + /** + * Function to subtract two joint arrays, all the arguments must + * have the same size: A - B = C. This function is + * aliasing-safe, A or B can be the same array as C. + * + * @param src1 A + * @param src2 B + * @param dest C + */ + void Subtract(const JntArray& src1,const JntArray& src2,JntArray& dest); + /** + * Function to multiply all the array values with a scalar + * factor: A*b=C. This function is aliasing-safe, A can be the + * same array as C. + * + * @param src A + * @param factor b + * @param dest C + */ + void Multiply(const JntArray& src,const double& factor,JntArray& dest); + /** + * Function to divide all the array values with a scalar + * factor: A/b=C. This function is aliasing-safe, A can be the + * same array as C. + * + * @param src A + * @param factor b + * @param dest C + */ + void Divide(const JntArray& src,const double& factor,JntArray& dest); + /** + * Function to multiply a KDL::Jacobian with a KDL::JntArray + * to get a KDL::Twist, it should not be used to calculate the + * forward velocity kinematics, the solver classes are built + * for this purpose. + * J*q = t + * + * @param jac J + * @param src q + * @param dest t + * @post dest==Twist::Zero() if 0==src.rows() (ie src is empty) + */ + void MultiplyJacobian(const Jacobian& jac, const JntArray& src, Twist& dest); + /** + * Function to set all the values of the array to 0 + * + * @param array + */ + void SetToZero(JntArray& array); + /** + * Function to check if two arrays are the same with a + *precision of eps + * + * @param src1 + * @param src2 + * @param eps default: epsilon + * @return true if each element of src1 is within eps of the same + * element in src2, or if both src1 and src2 have no data (ie 0==rows()) + */ + bool Equal(const JntArray& src1,const JntArray& src2,double eps=epsilon); + +} + +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/jntarrayacc.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/jntarrayacc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d0949acd6ce07fd578eaab4d5da61aca27b4307 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/jntarrayacc.cpp @@ -0,0 +1,177 @@ +// Copyright (C) 2007 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#include "jntarrayacc.hpp" + +namespace KDL +{ + JntArrayAcc::JntArrayAcc(unsigned int size): + q(size),qdot(size),qdotdot(size) + { + } + JntArrayAcc::JntArrayAcc(const JntArray& qin, const JntArray& qdotin,const JntArray& qdotdotin): + q(qin),qdot(qdotin),qdotdot(qdotdotin) + { + assert(q.rows()==qdot.rows()&&qdot.rows()==qdotdot.rows()); + } + JntArrayAcc::JntArrayAcc(const JntArray& qin, const JntArray& qdotin): + q(qin),qdot(qdotin),qdotdot(q.rows()) + { + assert(q.rows()==qdot.rows()); + } + JntArrayAcc::JntArrayAcc(const JntArray& qin): + q(qin),qdot(q.rows()),qdotdot(q.rows()) + { + } + + void JntArrayAcc::resize(unsigned int newSize) + { + q.resize(newSize); + qdot.resize(newSize); + qdotdot.resize(newSize); + } + + JntArray JntArrayAcc::value()const + { + return q; + } + + JntArray JntArrayAcc::deriv()const + { + return qdot; + } + JntArray JntArrayAcc::dderiv()const + { + return qdotdot; + } + + void Add(const JntArrayAcc& src1,const JntArrayAcc& src2,JntArrayAcc& dest) + { + Add(src1.q,src2.q,dest.q); + Add(src1.qdot,src2.qdot,dest.qdot); + Add(src1.qdotdot,src2.qdotdot,dest.qdotdot); + } + void Add(const JntArrayAcc& src1,const JntArrayVel& src2,JntArrayAcc& dest) + { + Add(src1.q,src2.q,dest.q); + Add(src1.qdot,src2.qdot,dest.qdot); + dest.qdotdot=src1.qdotdot; + } + void Add(const JntArrayAcc& src1,const JntArray& src2,JntArrayAcc& dest) + { + Add(src1.q,src2,dest.q); + dest.qdot=src1.qdot; + dest.qdotdot=src1.qdotdot; + } + + void Subtract(const JntArrayAcc& src1,const JntArrayAcc& src2,JntArrayAcc& dest) + { + Subtract(src1.q,src2.q,dest.q); + Subtract(src1.qdot,src2.qdot,dest.qdot); + Subtract(src1.qdotdot,src2.qdotdot,dest.qdotdot); + } + void Subtract(const JntArrayAcc& src1,const JntArrayVel& src2,JntArrayAcc& dest) + { + Subtract(src1.q,src2.q,dest.q); + Subtract(src1.qdot,src2.qdot,dest.qdot); + dest.qdotdot=src1.qdotdot; + } + void Subtract(const JntArrayAcc& src1,const JntArray& src2,JntArrayAcc& dest) + { + Subtract(src1.q,src2,dest.q); + dest.qdot=src1.qdot; + dest.qdotdot=src1.qdotdot; + } + + void Multiply(const JntArrayAcc& src,const double& factor,JntArrayAcc& dest) + { + Multiply(src.q,factor,dest.q); + Multiply(src.qdot,factor,dest.qdot); + Multiply(src.qdotdot,factor,dest.qdotdot); + } + void Multiply(const JntArrayAcc& src,const doubleVel& factor,JntArrayAcc& dest) + { + Multiply(src.qdot,factor.grad*2,dest.qdot); + Multiply(src.qdotdot,factor.t,dest.qdotdot); + Add(dest.qdot,dest.qdotdot,dest.qdotdot); + Multiply(src.q,factor.grad,dest.q); + Multiply(src.qdot,factor.t,dest.qdot); + Add(dest.qdot,dest.q,dest.qdot); + Multiply(src.q,factor.t,dest.q); + } + void Multiply(const JntArrayAcc& src,const doubleAcc& factor,JntArrayAcc& dest) + { + Multiply(src.q,factor.dd,dest.q); + Multiply(src.qdot,factor.d*2,dest.qdot); + Multiply(src.qdotdot,factor.t,dest.qdotdot); + Add(dest.qdotdot,dest.qdot,dest.qdotdot); + Add(dest.qdotdot,dest.q,dest.qdotdot); + Multiply(src.q,factor.d,dest.q); + Multiply(src.qdot,factor.t,dest.qdot); + Add(dest.qdot,dest.q,dest.qdot); + Multiply(src.q,factor.t,dest.q); + } + + void Divide(const JntArrayAcc& src,const double& factor,JntArrayAcc& dest) + { + Divide(src.q,factor,dest.q); + Divide(src.qdot,factor,dest.qdot); + Divide(src.qdotdot,factor,dest.qdotdot); + } + void Divide(const JntArrayAcc& src,const doubleVel& factor,JntArrayAcc& dest) + { + Multiply(src.q,(2*factor.grad*factor.grad)/(factor.t*factor.t*factor.t),dest.q); + Multiply(src.qdot,(2*factor.grad)/(factor.t*factor.t),dest.qdot); + Divide(src.qdotdot,factor.t,dest.qdotdot); + Subtract(dest.qdotdot,dest.qdot,dest.qdotdot); + Add(dest.qdotdot,dest.q,dest.qdotdot); + Multiply(src.q,factor.grad/(factor.t*factor.t),dest.q); + Divide(src.qdot,factor.t,dest.qdot); + Subtract(dest.qdot,dest.q,dest.qdot); + Divide(src.q,factor.t,dest.q); + } + void Divide(const JntArrayAcc& src,const doubleAcc& factor,JntArrayAcc& dest) + { + Multiply(src.q,(2*factor.d*factor.d)/(factor.t*factor.t*factor.t)-factor.dd/(factor.t*factor.t),dest.q); + Multiply(src.qdot,(2*factor.d)/(factor.t*factor.t),dest.qdot); + Divide(src.qdotdot,factor.t,dest.qdotdot); + Subtract(dest.qdotdot,dest.qdot,dest.qdotdot); + Add(dest.qdotdot,dest.q,dest.qdotdot); + Multiply(src.q,factor.d/(factor.t*factor.t),dest.q); + Divide(src.qdot,factor.t,dest.qdot); + Subtract(dest.qdot,dest.q,dest.qdot); + Divide(src.q,factor.t,dest.q); + } + + void SetToZero(JntArrayAcc& array) + { + SetToZero(array.q); + SetToZero(array.qdot); + SetToZero(array.qdotdot); + } + + bool Equal(const JntArrayAcc& src1,const JntArrayAcc& src2,double eps) + { + return (Equal(src1.q,src2.q,eps)&&Equal(src1.qdot,src2.qdot,eps)&&Equal(src1.qdotdot,src2.qdotdot,eps)); + } +} + + diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/jntarrayacc.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/jntarrayacc.hpp new file mode 100644 index 0000000000000000000000000000000000000000..141d1534ed2aca9a6bd0771be82f8d9466e056d0 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/jntarrayacc.hpp @@ -0,0 +1,87 @@ +// Copyright (C) 2007 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#ifndef KDL_JNTARRAYACC_HPP +#define KDL_JNTARRAYACC_HPP + +#include "utilities/utility.h" +#include "jntarray.hpp" +#include "jntarrayvel.hpp" +#include "frameacc.hpp" + +namespace KDL +{ + // Equal is friend function, but default arguments for friends are forbidden (§8.3.6.4) + class JntArrayAcc; + bool Equal(const JntArrayAcc& src1,const JntArrayAcc& src2,double eps=epsilon); + void Add(const JntArrayAcc& src1,const JntArrayAcc& src2,JntArrayAcc& dest); + void Add(const JntArrayAcc& src1,const JntArrayVel& src2,JntArrayAcc& dest); + void Add(const JntArrayAcc& src1,const JntArray& src2,JntArrayAcc& dest); + void Subtract(const JntArrayAcc& src1,const JntArrayAcc& src2,JntArrayAcc& dest); + void Subtract(const JntArrayAcc& src1,const JntArrayVel& src2,JntArrayAcc& dest); + void Subtract(const JntArrayAcc& src1,const JntArray& src2,JntArrayAcc& dest); + void Multiply(const JntArrayAcc& src,const double& factor,JntArrayAcc& dest); + void Multiply(const JntArrayAcc& src,const doubleVel& factor,JntArrayAcc& dest); + void Multiply(const JntArrayAcc& src,const doubleAcc& factor,JntArrayAcc& dest); + void Divide(const JntArrayAcc& src,const double& factor,JntArrayAcc& dest); + void Divide(const JntArrayAcc& src,const doubleVel& factor,JntArrayAcc& dest); + void Divide(const JntArrayAcc& src,const doubleAcc& factor,JntArrayAcc& dest); + void SetToZero(JntArrayAcc& array); + + class JntArrayAcc + { + public: + JntArray q; + JntArray qdot; + JntArray qdotdot; + public: + JntArrayAcc(){}; + explicit JntArrayAcc(unsigned int size); + JntArrayAcc(const JntArray& q,const JntArray& qdot,const JntArray& qdotdot); + JntArrayAcc(const JntArray& q,const JntArray& qdot); + explicit JntArrayAcc(const JntArray& q); + + void resize(unsigned int newSize); + + JntArray value()const; + JntArray deriv()const; + JntArray dderiv()const; + + friend void Add(const JntArrayAcc& src1,const JntArrayAcc& src2,JntArrayAcc& dest); + friend void Add(const JntArrayAcc& src1,const JntArrayVel& src2,JntArrayAcc& dest); + friend void Add(const JntArrayAcc& src1,const JntArray& src2,JntArrayAcc& dest); + friend void Subtract(const JntArrayAcc& src1,const JntArrayAcc& src2,JntArrayAcc& dest); + friend void Subtract(const JntArrayAcc& src1,const JntArrayVel& src2,JntArrayAcc& dest); + friend void Subtract(const JntArrayAcc& src1,const JntArray& src2,JntArrayAcc& dest); + friend void Multiply(const JntArrayAcc& src,const double& factor,JntArrayAcc& dest); + friend void Multiply(const JntArrayAcc& src,const doubleVel& factor,JntArrayAcc& dest); + friend void Multiply(const JntArrayAcc& src,const doubleAcc& factor,JntArrayAcc& dest); + friend void Divide(const JntArrayAcc& src,const double& factor,JntArrayAcc& dest); + friend void Divide(const JntArrayAcc& src,const doubleVel& factor,JntArrayAcc& dest); + friend void Divide(const JntArrayAcc& src,const doubleAcc& factor,JntArrayAcc& dest); + friend void SetToZero(JntArrayAcc& array); + friend bool Equal(const JntArrayAcc& src1,const JntArrayAcc& src2,double eps); + + }; + +} + +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/jntarrayvel.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/jntarrayvel.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2a142507e7377c49916da2393dc05a15528e5ecc --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/jntarrayvel.cpp @@ -0,0 +1,117 @@ +// Copyright (C) 2007 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + +#include "jntarrayacc.hpp" + +namespace KDL +{ + JntArrayVel::JntArrayVel(unsigned int size): + q(size),qdot(size) + { + } + JntArrayVel::JntArrayVel(const JntArray& qin, const JntArray& qdotin): + q(qin),qdot(qdotin) + { + assert(q.rows()==qdot.rows()); + } + JntArrayVel::JntArrayVel(const JntArray& qin): + q(qin),qdot(q.rows()) + { + } + + void JntArrayVel::resize(unsigned int newSize) + { + q.resize(newSize); + qdot.resize(newSize); + } + + JntArray JntArrayVel::value()const + { + return q; + } + + JntArray JntArrayVel::deriv()const + { + return qdot; + } + + void Add(const JntArrayVel& src1,const JntArrayVel& src2,JntArrayVel& dest) + { + Add(src1.q,src2.q,dest.q); + Add(src1.qdot,src2.qdot,dest.qdot); + } + void Add(const JntArrayVel& src1,const JntArray& src2,JntArrayVel& dest) + { + Add(src1.q,src2,dest.q); + dest.qdot=src1.qdot; + } + + void Subtract(const JntArrayVel& src1,const JntArrayVel& src2,JntArrayVel& dest) + { + Subtract(src1.q,src2.q,dest.q); + Subtract(src1.qdot,src2.qdot,dest.qdot); + } + void Subtract(const JntArrayVel& src1,const JntArray& src2,JntArrayVel& dest) + { + Subtract(src1.q,src2,dest.q); + dest.qdot=src1.qdot; + } + + void Multiply(const JntArrayVel& src,const double& factor,JntArrayVel& dest) + { + Multiply(src.q,factor,dest.q); + Multiply(src.qdot,factor,dest.qdot); + } + void Multiply(const JntArrayVel& src,const doubleVel& factor,JntArrayVel& dest) + { + Multiply(src.q,factor.grad,dest.q); + Multiply(src.qdot,factor.t,dest.qdot); + Add(dest.qdot,dest.q,dest.qdot); + Multiply(src.q,factor.t,dest.q); + } + + void Divide(const JntArrayVel& src,const double& factor,JntArrayVel& dest) + { + Divide(src.q,factor,dest.q); + Divide(src.qdot,factor,dest.qdot); + } + void Divide(const JntArrayVel& src,const doubleVel& factor,JntArrayVel& dest) + { + Multiply(src.q,(factor.grad/factor.t/factor.t),dest.q); + Divide(src.qdot,factor.t,dest.qdot); + Subtract(dest.qdot,dest.q,dest.qdot); + Divide(src.q,factor.t,dest.q); + } + + void SetToZero(JntArrayVel& array) + { + SetToZero(array.q); + SetToZero(array.qdot); + } + + bool Equal(const JntArrayVel& src1,const JntArrayVel& src2,double eps) + { + return Equal(src1.q,src2.q,eps)&&Equal(src1.qdot,src2.qdot,eps); + } +} + + diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/jntarrayvel.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/jntarrayvel.hpp new file mode 100644 index 0000000000000000000000000000000000000000..cfc27a683f61ab85b8357c706a7b3172267575ed --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/jntarrayvel.hpp @@ -0,0 +1,76 @@ +// Copyright (C) 2007 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#ifndef KDL_JNTARRAYVEL_HPP +#define KDL_JNTARRAYVEL_HPP + +#include "utilities/utility.h" +#include "jntarray.hpp" +#include "framevel.hpp" + +namespace KDL +{ + // Equal is friend function, but default arguments for friends are forbidden (§8.3.6.4) + class JntArrayVel; + bool Equal(const JntArrayVel& src1,const JntArrayVel& src2,double eps=epsilon); + void Add(const JntArrayVel& src1,const JntArrayVel& src2,JntArrayVel& dest); + void Add(const JntArrayVel& src1,const JntArray& src2,JntArrayVel& dest); + void Subtract(const JntArrayVel& src1,const JntArrayVel& src2,JntArrayVel& dest); + void Subtract(const JntArrayVel& src1,const JntArray& src2,JntArrayVel& dest); + void Multiply(const JntArrayVel& src,const double& factor,JntArrayVel& dest); + void Multiply(const JntArrayVel& src,const doubleVel& factor,JntArrayVel& dest); + void Divide(const JntArrayVel& src,const double& factor,JntArrayVel& dest); + void Divide(const JntArrayVel& src,const doubleVel& factor,JntArrayVel& dest); + void SetToZero(JntArrayVel& array); + + + class JntArrayVel + { + public: + JntArray q; + JntArray qdot; + public: + JntArrayVel(){}; + explicit JntArrayVel(unsigned int size); + JntArrayVel(const JntArray& q,const JntArray& qdot); + explicit JntArrayVel(const JntArray& q); + + void resize(unsigned int newSize); + + JntArray value()const; + JntArray deriv()const; + + friend void Add(const JntArrayVel& src1,const JntArrayVel& src2,JntArrayVel& dest); + friend void Add(const JntArrayVel& src1,const JntArray& src2,JntArrayVel& dest); + friend void Subtract(const JntArrayVel& src1,const JntArrayVel& src2,JntArrayVel& dest); + friend void Subtract(const JntArrayVel& src1,const JntArray& src2,JntArrayVel& dest); + friend void Multiply(const JntArrayVel& src,const double& factor,JntArrayVel& dest); + friend void Multiply(const JntArrayVel& src,const doubleVel& factor,JntArrayVel& dest); + friend void Divide(const JntArrayVel& src,const double& factor,JntArrayVel& dest); + friend void Divide(const JntArrayVel& src,const doubleVel& factor,JntArrayVel& dest); + friend void SetToZero(JntArrayVel& array); + friend bool Equal(const JntArrayVel& src1,const JntArrayVel& src2,double eps); + + }; + +} + +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/jntspaceinertiamatrix.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/jntspaceinertiamatrix.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb8a4fed41c0269ab6b2f6764ff3192e7e20e345 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/jntspaceinertiamatrix.cpp @@ -0,0 +1,120 @@ +// Copyright (C) 2009 Dominick Vanthienen + +// Version: 1.0 +// Author: Dominick Vanthienen +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#include "jntspaceinertiamatrix.hpp" + +namespace KDL +{ + JntSpaceInertiaMatrix::JntSpaceInertiaMatrix() + { + } + + JntSpaceInertiaMatrix::JntSpaceInertiaMatrix(int _size): + data(_size, _size) + { + data.setZero(); + } + + + JntSpaceInertiaMatrix::JntSpaceInertiaMatrix(const JntSpaceInertiaMatrix& arg): + data(arg.data) + { + } + + JntSpaceInertiaMatrix& JntSpaceInertiaMatrix::operator = (const JntSpaceInertiaMatrix& arg) + { + data=arg.data; + return *this; + } + + + JntSpaceInertiaMatrix::~JntSpaceInertiaMatrix() + { + } + + void JntSpaceInertiaMatrix::resize(unsigned int newSize) + { + data.resize(newSize,newSize); + } + + double JntSpaceInertiaMatrix::operator()(unsigned int i,unsigned int j)const + { + return data(i, j); + } + + double& JntSpaceInertiaMatrix::operator()(unsigned int i,unsigned int j) + { + return data(i, j); + } + + unsigned int JntSpaceInertiaMatrix::rows()const + { + return static_cast(data.rows()); + } + + unsigned int JntSpaceInertiaMatrix::columns()const + { + return static_cast(data.cols()); + } + + + void Add(const JntSpaceInertiaMatrix& src1,const JntSpaceInertiaMatrix& src2,JntSpaceInertiaMatrix& dest) + { + dest.data=src1.data+src2.data; + } + + void Subtract(const JntSpaceInertiaMatrix& src1,const JntSpaceInertiaMatrix& src2,JntSpaceInertiaMatrix& dest) + { + dest.data=src1.data-src2.data; + } + + void Multiply(const JntSpaceInertiaMatrix& src,const double& factor,JntSpaceInertiaMatrix& dest) + { + dest.data=factor*src.data; + } + + void Divide(const JntSpaceInertiaMatrix& src,const double& factor,JntSpaceInertiaMatrix& dest) + { + dest.data=src.data/factor; + } + + void Multiply(const JntSpaceInertiaMatrix& src, const JntArray& vec, JntArray& dest) + { + dest.data=src.data.lazyProduct(vec.data); + } + + void SetToZero(JntSpaceInertiaMatrix& mat) + { + mat.data.setZero(); + } + + bool Equal(const JntSpaceInertiaMatrix& src1, const JntSpaceInertiaMatrix& src2,double eps) + { + if(src1.rows()!=src2.rows()||src1.columns()!=src2.columns()) + return false; + return (src1.data-src2.data).isZero(eps); + } + + bool operator==(const JntSpaceInertiaMatrix& src1,const JntSpaceInertiaMatrix& src2){return Equal(src1,src2);} + //bool operator!=(const JntSpaceInertiaMatrix& src1,const JntSpaceInertiaMatrix& src2){return Equal(src1,src2);} +} + + diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/jntspaceinertiamatrix.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/jntspaceinertiamatrix.hpp new file mode 100644 index 0000000000000000000000000000000000000000..fc386cf35bb66dd0a90633efd26c96f319a60bed --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/jntspaceinertiamatrix.hpp @@ -0,0 +1,232 @@ +// Copyright (C) 2009 Dominick Vanthienen + +// Version: 1.0 +// Author: Dominick Vanthienen +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#ifndef KDL_JNTSPACEINERTIAMATRIX_HPP +#define KDL_JNTSPACEINERTIAMATRIX_HPP + +#include "frames.hpp" +#include "jacobian.hpp" +#include "jntarray.hpp" + +#include + +namespace KDL +{ + /** + * @brief This class represents an fixed size matrix containing + * the Joint-Space Inertia Matrix of a KDL::Chain. + * + * \warning An object constructed with the default constructor provides + * a valid, but inert, object. Many of the member functions will do + * the correct thing and have no affect on this object, but some + * member functions can _NOT_ deal with an inert/empty object. These + * functions will assert() and exit the program instead. The intended use + * case for the default constructor (in an RTT/OCL setting) is outlined in + * code below - the default constructor plus the resize() function allow + * use of JntSpaceInertiaMatrix objects whose size is set within a configureHook() call + * (typically based on a size determined from a property). + +\code +class MyTask : public RTT::TaskContext +{ + JntSpaceInertiaMatrix j; + MyTask() + {} // invokes j's default constructor + + bool configureHook() + { + unsigned int size = some_property.rvalue(); + j.resize(size) + ... + } + + void updateHook() + { + ** use j here + } +}; +/endcode + + */ + + class JntSpaceInertiaMatrix + { + public: + Eigen::MatrixXd data; + + /** Construct with _no_ data array + * @post NULL == data + * @post 0 == rows() + * @warning use of an object constructed like this, without + * a resize() first, may result in program exit! See class + * documentation. + */ + JntSpaceInertiaMatrix(); + /** + * Constructor of the Joint-Space Inertia Matrix + * + * @param size of the matrix, this cannot be changed + * afterwards. Size rows and size columns. + * @pre 0 < size + * @post NULL != data + * @post 0 < rows() + * @post all elements in data have 0 value + */ + explicit JntSpaceInertiaMatrix(int size); + /** Copy constructor + * @note Will correctly copy an empty object + */ + JntSpaceInertiaMatrix(const JntSpaceInertiaMatrix& arg); + ~JntSpaceInertiaMatrix(); + /** Resize the array + * @warning This causes a dynamic allocation (and potentially + * also a dynamic deallocation). This _will_ negatively affect + * real-time performance! + * + * @post newSize == rows() + * @post NULL != data + * @post all elements in data have 0 value + */ + void resize(unsigned int newSize); + + JntSpaceInertiaMatrix& operator = ( const JntSpaceInertiaMatrix& arg); + /** + * get_item operator for the joint matrix + * + * + * @return the joint value at position i, starting from 0 + * @pre 0 != size (ie non-default constructor or resize() called) + */ + double operator()(unsigned int i,unsigned int j)const; + /** + * set_item operator + * + * @return reference to the joint value at position i,starting + *from zero. + * @pre 0 != size (ie non-default constructor or resize() called) + */ + double& operator()(unsigned int i,unsigned int j); + /** + * Returns the number of rows and columns of the matrix + * + */ + unsigned int rows()const; + /** + * Returns the number of columns of the matrix. + */ + unsigned int columns()const; + + friend void Add(const JntSpaceInertiaMatrix& src1,const JntSpaceInertiaMatrix& src2,JntSpaceInertiaMatrix& dest); + friend void Subtract(const JntSpaceInertiaMatrix& src1,const JntSpaceInertiaMatrix& src2,JntSpaceInertiaMatrix& dest); + friend void Multiply(const JntSpaceInertiaMatrix& src,const double& factor,JntSpaceInertiaMatrix& dest); + friend void Divide(const JntSpaceInertiaMatrix& src,const double& factor,JntSpaceInertiaMatrix& dest); + friend void Multiply(const JntSpaceInertiaMatrix& src, const JntArray& vec, JntArray& dest); + friend void SetToZero(JntSpaceInertiaMatrix& matrix); + friend bool Equal(const JntSpaceInertiaMatrix& src1,const JntSpaceInertiaMatrix& src2,double eps); + friend bool operator==(const JntSpaceInertiaMatrix& src1,const JntSpaceInertiaMatrix& src2); + //friend bool operator!=(const JntSpaceInertiaMatrix& src1,const JntSpaceInertiaMatrix& src2); + }; + + bool operator==(const JntSpaceInertiaMatrix& src1,const JntSpaceInertiaMatrix& src2); + //bool operator!=(const JntSpaceInertiaMatrix& src1,const JntSpaceInertiaMatrix& src2); + + /** + * Function to add two joint matrix, all the arguments must + * have the same size: A + B = C. This function is + * aliasing-safe, A or B can be the same array as C. + * + * @param src1 A + * @param src2 B + * @param dest C + */ + void Add(const JntSpaceInertiaMatrix& src1,const JntSpaceInertiaMatrix& src2,JntSpaceInertiaMatrix& dest); + + /** + * Function to subtract two joint matrix, all the arguments must + * have the same size: A - B = C. This function is + * aliasing-safe, A or B can be the same array as C. + * + * @param src1 A + * @param src2 B + * @param dest C + */ + void Subtract(const JntSpaceInertiaMatrix& src1,const JntSpaceInertiaMatrix& src2,JntSpaceInertiaMatrix& dest); + + /** + * Function to multiply all the array values with a scalar + * factor: A*b=C. This function is aliasing-safe, A can be the + * same array as C. + * + * @param src A + * @param factor b + * @param dest C + */ + void Multiply(const JntSpaceInertiaMatrix& src,const double& factor,JntSpaceInertiaMatrix& dest); + + /** + * Function to divide all the array values with a scalar + * factor: A/b=C. This function is aliasing-safe, A can be the + * same array as C. + * + * @param src A + * @param factor b + * @param dest C + */ + void Divide(const JntSpaceInertiaMatrix& src,const double& factor,JntSpaceInertiaMatrix& dest); + + /** + * Function to multiply a KDL::Jacobian with a KDL::JntSpaceInertiaMatrix + * to get a KDL::Twist, it should not be used to calculate the + * forward velocity kinematics, the solver classes are built + * for this purpose. + * J*q = t + * + * @param jac J + * @param src q + * @param dest t + * @post dest==Twist::Zero() if 0==src.rows() (ie src is empty) + */ + void Multiply(const JntSpaceInertiaMatrix& src, const JntArray& vec, JntArray& dest); + + /** + * Function to set all the values of the array to 0 + * + * @param array + */ + void SetToZero(JntSpaceInertiaMatrix& matrix); + + /** + * Function to check if two matrices are the same with a + *precision of eps + * + * @param src1 + * @param src2 + * @param eps default: epsilon + * @return true if each element of src1 is within eps of the same + * element in src2, or if both src1 and src2 have no data (ie 0==rows()) + */ + bool Equal(const JntSpaceInertiaMatrix& src1,const JntSpaceInertiaMatrix& src2,double eps=epsilon); + + bool operator==(const JntSpaceInertiaMatrix& src1,const JntSpaceInertiaMatrix& src2); + +} + +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/joint.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/joint.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e27bf2516e4fef498ed6b97bd9f710fe66cc510b --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/joint.cpp @@ -0,0 +1,150 @@ +// Copyright (C) 2007 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#include "joint.hpp" + +namespace KDL { + + // constructor for joint along x,y or z axis, at origin of reference frame + Joint::Joint(const std::string& _name, const JointType& _type, const double& _scale, const double& _offset, + const double& _inertia, const double& _damping, const double& _stiffness): + name(_name),type(_type),scale(_scale),offset(_offset),inertia(_inertia),damping(_damping),stiffness(_stiffness) + { + if (type == RotAxis || type == TransAxis) + throw joint_type_ex; + } + + // constructor for joint along x,y or z axis, at origin of reference frame + Joint::Joint(const JointType& _type, const double& _scale, const double& _offset, + const double& _inertia, const double& _damping, const double& _stiffness): + name("NoName"),type(_type),scale(_scale),offset(_offset),inertia(_inertia),damping(_damping),stiffness(_stiffness) + { + if (type == RotAxis || type == TransAxis) + throw joint_type_ex; + } + + // constructor for joint along arbitrary axis, at arbitrary origin + Joint::Joint(const std::string& _name, const Vector& _origin, const Vector& _axis, const JointType& _type, const double& _scale, + const double& _offset, const double& _inertia, const double& _damping, const double& _stiffness): + name(_name), type(_type),scale(_scale),offset(_offset),inertia(_inertia),damping(_damping),stiffness(_stiffness) + , axis(_axis / _axis.Norm()), origin(_origin) + { + if (type != RotAxis && type != TransAxis) + throw joint_type_ex; + } + + // constructor for joint along arbitrary axis, at arbitrary origin + Joint::Joint(const Vector& _origin, const Vector& _axis, const JointType& _type, const double& _scale, + const double& _offset, const double& _inertia, const double& _damping, const double& _stiffness): + name("NoName"), type(_type),scale(_scale),offset(_offset),inertia(_inertia),damping(_damping),stiffness(_stiffness), + axis(_axis / _axis.Norm()),origin(_origin) + { + if (type != RotAxis && type != TransAxis) + throw joint_type_ex; + } + + Joint::~Joint() + { + } + + Frame Joint::pose(const double& q)const + { + switch(type){ + case RotAxis: + return Frame(Rotation::Rot2(axis, scale*q+offset), origin); + case RotX: + return Frame(Rotation::RotX(scale*q+offset)); + case RotY: + return Frame(Rotation::RotY(scale*q+offset)); + case RotZ: + return Frame(Rotation::RotZ(scale*q+offset)); + case TransAxis: + return Frame(origin + (axis * (scale*q+offset))); + case TransX: + return Frame(Vector(scale*q+offset,0.0,0.0)); + case TransY: + return Frame(Vector(0.0,scale*q+offset,0.0)); + case TransZ: + return Frame(Vector(0.0,0.0,scale*q+offset)); + case Fixed: + return Frame::Identity(); + } + return Frame::Identity(); + } + + Twist Joint::twist(const double& qdot)const + { + switch(type){ + case RotAxis: + return Twist(Vector(0,0,0), axis * ( scale * qdot)); + case RotX: + return Twist(Vector(0.0,0.0,0.0),Vector(scale*qdot,0.0,0.0)); + case RotY: + return Twist(Vector(0.0,0.0,0.0),Vector(0.0,scale*qdot,0.0)); + case RotZ: + return Twist(Vector(0.0,0.0,0.0),Vector(0.0,0.0,scale*qdot)); + case TransAxis: + return Twist(axis * (scale * qdot), Vector(0,0,0)); + case TransX: + return Twist(Vector(scale*qdot,0.0,0.0),Vector(0.0,0.0,0.0)); + case TransY: + return Twist(Vector(0.0,scale*qdot,0.0),Vector(0.0,0.0,0.0)); + case TransZ: + return Twist(Vector(0.0,0.0,scale*qdot),Vector(0.0,0.0,0.0)); + case Fixed: + return Twist::Zero(); + } + return Twist::Zero(); + } + + Vector Joint::JointAxis() const + { + switch(type) + { + case RotAxis: + return axis; + case RotX: + return Vector(1.,0.,0.); + case RotY: + return Vector(0.,1.,0.); + case RotZ: + return Vector(0.,0.,1.); + case TransAxis: + return axis; + case TransX: + return Vector(1.,0.,0.); + case TransY: + return Vector(0.,1.,0.); + case TransZ: + return Vector(0.,0.,1.); + case Fixed: + return Vector::Zero(); + } + return Vector::Zero(); + } + + Vector Joint::JointOrigin() const + { + return origin; + } + +} // end of namespace KDL + diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/joint.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/joint.hpp new file mode 100644 index 0000000000000000000000000000000000000000..c7fccd09b7858b819585064741cf7a4018748146 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/joint.hpp @@ -0,0 +1,290 @@ +// Copyright (C) 2007 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#ifndef KDL_JOINT_HPP +#define KDL_JOINT_HPP + +#include "frames.hpp" +#include +#include + + +namespace KDL { + + /** + * \brief This class encapsulates a simple joint, that is with one + * parameterized degree of freedom and with scalar dynamic properties. + * + * A simple joint is described by the following properties : + * - scale: ratio between motion input and motion output + * - offset: between the "physical" and the "logical" zero position. + * - type: revolute or translational, along one of the basic frame axes + * - inertia, stiffness and damping: scalars representing the physical + * effects along/about the joint axis only. + * + * @ingroup KinematicFamily + */ + class Joint { + public: + typedef enum { RotAxis,RotX,RotY,RotZ,TransAxis,TransX,TransY,TransZ,Fixed,None=Fixed} JointType; + /** + * Constructor of a joint. + * + * @param name of the joint + * @param type type of the joint, default: Joint::Fixed + * @param scale scale between joint input and actual geometric + * movement, default: 1 + * @param offset offset between joint input and actual + * geometric input, default: 0 + * @param inertia 1D inertia along the joint axis, default: 0 + * @param damping 1D damping along the joint axis, default: 0 + * @param stiffness 1D stiffness along the joint axis, + * default: 0 + */ + explicit Joint(const std::string& name, const JointType& type=Fixed,const double& scale=1,const double& offset=0, + const double& inertia=0,const double& damping=0,const double& stiffness=0); + /** + * Constructor of a joint. + * + * @param type type of the joint, default: Joint::Fixed + * @param scale scale between joint input and actual geometric + * movement, default: 1 + * @param offset offset between joint input and actual + * geometric input, default: 0 + * @param inertia 1D inertia along the joint axis, default: 0 + * @param damping 1D damping along the joint axis, default: 0 + * @param stiffness 1D stiffness along the joint axis, + * default: 0 + */ + explicit Joint(const JointType& type=Fixed,const double& scale=1,const double& offset=0, + const double& inertia=0,const double& damping=0,const double& stiffness=0); + /** + * Constructor of a joint. + * + * @param name of the joint + * @param origin the origin of the joint + * @param axis the axis of the joint + * @param scale scale between joint input and actual geometric + * movement, default: 1 + * @param offset offset between joint input and actual + * geometric input, default: 0 + * @param inertia 1D inertia along the joint axis, default: 0 + * @param damping 1D damping along the joint axis, default: 0 + * @param stiffness 1D stiffness along the joint axis, + * default: 0 + */ + Joint(const std::string& name, const Vector& _origin, const Vector& _axis, const JointType& type, const double& _scale=1, const double& _offset=0, + const double& _inertia=0, const double& _damping=0, const double& _stiffness=0); + /** + * Constructor of a joint. + * + * @param origin the origin of the joint + * @param axis the axis of the joint + * @param scale scale between joint input and actual geometric + * movement, default: 1 + * @param offset offset between joint input and actual + * geometric input, default: 0 + * @param inertia 1D inertia along the joint axis, default: 0 + * @param damping 1D damping along the joint axis, default: 0 + * @param stiffness 1D stiffness along the joint axis, + * default: 0 + */ + Joint(const Vector& _origin, const Vector& _axis, const JointType& type, const double& _scale=1, const double& _offset=0, + const double& _inertia=0, const double& _damping=0, const double& _stiffness=0); + + /** + * Request the 6D-pose between the beginning and the end of + * the joint at joint position q + * + * @param q the 1D joint position + * + * @return the resulting 6D-pose + */ + Frame pose(const double& q)const; + /** + * Request the resulting 6D-velocity with a joint velocity qdot + * + * @param qdot the 1D joint velocity + * + * @return the resulting 6D-velocity + */ + Twist twist(const double& qdot)const; + + /** + * Request the Vector corresponding to the axis of a revolute joint. + * + * @return Vector. e.g (1,0,0) for RotX etc. + */ + Vector JointAxis() const; + + /** + * Request the Vector corresponding to the origin of a revolute joint. + * + * @return Vector + */ + Vector JointOrigin() const; + /** + * Request the name of the joint + * + * + * @return const reference to the name of the joint + */ + const std::string& getName()const + { + return name; + } + /** + * Request the type of the joint. + * + * @return const reference to the type + */ + const JointType& getType() const + { + return type; + }; + + /** + * Request the stringified type of the joint. + * + * @return const string + */ + const std::string getTypeName() const + { + switch (type) { + case RotAxis: + return "RotAxis"; + case TransAxis: + return "TransAxis"; + case RotX: + return "RotX"; + case RotY: + return "RotY"; + case RotZ: + return "RotZ"; + case TransX: + return "TransX"; + case TransY: + return "TransY"; + case TransZ: + return "TransZ"; + case Fixed: + return "Fixed"; + default: + return "Fixed"; + } + }; + + /** + * Request the scale of the joint. + * + * @return const reference to the scale of the joint + */ + const double& getScale() const + { + return scale; + } + + /** + * Request the offset of the joint. + * + * @return const reference to the offset of the joint + */ + const double& getOffset() const + { + return offset; + } + + /** + * Request the inertia of the joint. + * + * @return const reference to the inertia of the joint + */ + const double& getInertia() const + { + return inertia; + }; + + /** + * Request the damping of the joint. + * + * @return const reference to the damping of the joint + */ + const double& getDamping() const + { + return damping; + }; + + /** + * Request the stiffness of the joint. + * + * @return const reference to the stiffness of the joint + */ + const double& getStiffness() const + { + return stiffness; + }; + + /** + * Request the axis of the joint. + * + * @return const reference to the axis of the joint + */ + const Vector& getAxis() const + { + return axis; + } + + /** + * Request the origin of the joint. + * + * @return const reference to the origin of the joint + */ + const Vector& getOrigin() const + { + return origin; + } + + virtual ~Joint(); + + private: + std::string name; + Joint::JointType type; + double scale; + double offset; + double inertia; + double damping; + double stiffness; + + // variables for RotAxis joint + Vector axis, origin; + mutable Frame joint_pose; // Deprecated, but keeping for ABI compatibility + mutable double q_previous; // Deprecated, but keeping for ABI compatibility + + class joint_type_exception: public std::exception{ + virtual const char* what() const throw(){ + return "Joint Type excption";} + } joint_type_ex; + + }; + +} // end of namespace KDL + +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/kdl.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/kdl.hpp new file mode 100644 index 0000000000000000000000000000000000000000..c74ede6d7121f873b93497fea530d7f506428a20 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/kdl.hpp @@ -0,0 +1,129 @@ +// Copyright (C) 2007 Ruben Smits +// Copyright (C) 2011 Erwin Aertbelien + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +/** + * \mainpage KDL + * + * This is the API reference of the + * Kinematics and Dynamics + * Library (KDL), a sub-project of Orocos, but that can be used + * independently of Orocos. KDL offers different kinds of + * functionality, grouped in the following Modules: + * - \subpage geomprim + * - \ref KinematicFamily : functionality to build kinematic chains and access their kinematic and dynamic properties, such as e.g. Forward and Inverse kinematics and dynamics. + * - \ref Motion : functionality to specify motion trajectories of frames and kinematic chains, such as e.g. Trapezoidal Velocity profiles. + * - \ref KDLTK : the interface code to integrate KDL into the Orocos Real-Time Toolkit (RTT). + * + * +**/ +/** + * \page geomprim Geometric Primitives + * \section Introduction + * Geometric primitives are represented by the following classes. + * - KDL::Vector + * - KDL::Rotation + * - KDL::Frame + * - KDL::Twist + * - KDL::Wrench + * + * \par Twist and Wrench transformations + * 3 different types of transformations do exist for the twists + * and wrenches. + * + * \verbatim + * 1) Frame * Twist or Frame * Wrench : + * this transforms both the velocity/force reference point + * and the basis to which the twist/wrench are expressed. + * 2) Rotation * Twist or Rotation * Wrench : + * this transforms the basis to which the twist/wrench are + * expressed, but leaves the reference point intact. + * 3) Twist.RefPoint(v_base_AB) or Wrench.RefPoint(v_base_AB) + * this transforms only the reference point. v is expressed + * in the same base as the twist/wrench and points from the + * old reference point to the new reference point. + * \endverbatim + * + *\warning + * Efficienty can be improved by writing p2 = A*(B*(C*p1))) instead of + * p2=A*B*C*p1 + * + * \par PROPOSED NAMING CONVENTION FOR FRAME-like OBJECTS + * + * \verbatim + * A naming convention of objects of the type defined in this file : + * (1) Frame : F... + * Rotation : R ... + * (2) Twist : T ... + * Wrench : W ... + * Vector : V ... + * This prefix is followed by : + * for category (1) : + * F_A_B : w.r.t. frame A, frame B expressed + * ( each column of F_A_B corresponds to an axis of B, + * expressed w.r.t. frame A ) + * in mathematical convention : + * A + * F_A_B == F + * B + * + * for category (2) : + * V_B : a vector expressed w.r.t. frame B + * + * This can also be prepended by a name : + * e.g. : temporaryV_B + * + * With this convention one can write : + * + * F_A_B = F_B_A.Inverse(); + * F_A_C = F_A_B * F_B_C; + * V_B = F_B_C * V_C; // both translation and rotation + * V_B = R_B_C * V_C; // only rotation + * \endverbatim + * + * \par CONVENTIONS FOR WHEN USED WITH ROBOTS : + * + * \verbatim + * world : represents the frame ([1 0 0,0 1 0,0 0 1],[0 0 0]') + * mp : represents mounting plate of a robot + * (i.e. everything before MP is constructed by robot manufacturer + * everything after MP is tool ) + * tf : represents task frame of a robot + * (i.e. frame in which motion and force control is expressed) + * sf : represents sensor frame of a robot + * (i.e. frame at which the forces measured by the force sensor + * are expressed ) + * + * Frame F_world_mp=...; + * Frame F_mp_sf(..) + * Frame F_mp_tf(,.) + * + * Wrench are measured in sensor frame SF, so one could write : + * Wrench_tf = F_mp_tf.Inverse()* ( F_mp_sf * Wrench_sf ); + * \endverbatim + * + * \par CONVENTIONS REGARDING UNITS : + * Typically we use the standard S.I. units: N, m, sec. + * + */ + + diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/kinfam.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/kinfam.hpp new file mode 100644 index 0000000000000000000000000000000000000000..1800c21d8da59fef4a159011b15278a2fccccafb --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/kinfam.hpp @@ -0,0 +1,52 @@ +// Copyright (C) 2007 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +/** + * @defgroup KinematicFamily Kinematic Families + * @brief All classes to support kinematic families. + * + * The Kinematic Families classes range from the basic building blocks + * (KDL::Joint and KDL::Segment) and their interconnected kinematic + * structures (KDL::Chain and KDL::Tree), to the solver + * algorithms for the kinematics and dynamics of particular kinematic + * families. + * + * A kinematic family is a set of kinematic structures that have + * similar properties, such as the same interconnection topology, the same + * numerical or analytical solver algorithms, etc. Different members of the + * same kinematic family differ only by the concrete values of their + * kinematic and dynamic properties (link lengths, mass, etc.). + * + * Each kinematic structure is built from one or more Segments + * (KDL::Segment). A KDL::Chain is a serial connection of + * these segments; a KDL:Tree is a tree-structured + * interconnection; and a KDL:Graph is a kinematic structure with a + * general graph topology. (The current implementation + * supports only KDL::Chain.) + * + * A KDL::Segment contains a KDL::Joint and an offset frame ("link length", + * defined by a KDL::Frame), that represents the geometric pose + * between the KDL::Joint on the previous segment and its own KDL::Joint. + * + * A list of all the classes is available on the modules page: \ref KinematicFamily + * + * + */ diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/kinfam_io.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/kinfam_io.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d8eb4d31bbcf5fefbdafa398bb3d944ce5fee06a --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/kinfam_io.cpp @@ -0,0 +1,132 @@ +// Copyright (C) 2007 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#include "kinfam_io.hpp" +#include "frames_io.hpp" +#include + +namespace KDL { +std::ostream& operator <<(std::ostream& os, const Joint& joint) { + return os << joint.getName()<<":["<>(std::istream& is, Joint& joint) { + return is; +} + +std::ostream& operator <<(std::ostream& os, const Segment& segment) { + os << segment.getName()<<":[" << segment.getJoint() << ",\n tip: \n" << segment.getFrameToTip() << "]"; + return os; +} + +std::istream& operator >>(std::istream& is, Segment& segment) { + return is; +} + +std::ostream& operator <<(std::ostream& os, const Chain& chain) { + os << "["; + for (unsigned int i = 0; i < chain.getNrOfSegments(); i++) + os << chain.getSegment(i) << "\n"; + os << "]"; + return os; +} + +std::istream& operator >>(std::istream& is, Chain& chain) { + return is; +} + +std::ostream& operator <<(std::ostream& os, const Tree& tree) { + SegmentMap::const_iterator root = tree.getRootSegment(); + return os << root; +} + +std::ostream& operator <<(std::ostream& os, SegmentMap::const_iterator root) { + //os<first<<": "<second.segment<<"\n"; + os << root->first<<"(q_nr: "<< GetTreeElementQNr(root->second) << ")" << "\n \t"; + for (unsigned int i = 0; i < GetTreeElementChildren(root->second).size(); i++) { + os << ( GetTreeElementChildren(root->second)[i] ) << "\t"; + } + return os << "\n"; +} + +std::istream& operator >>(std::istream& is, Tree& tree) { + return is; +} + +std::ostream& operator <<(std::ostream& os, const JntArray& array) { + os << "["; + for (unsigned int i = 0; i < array.rows(); i++) + os << std::setw(KDL_FRAME_WIDTH) << array(i); + os << "]"; + return os; +} + +std::istream& operator >>(std::istream& is, JntArray& array) { + return is; +} + +std::ostream& operator <<(std::ostream& os, const Jacobian& jac) { + os << "["; + for (unsigned int i = 0; i < jac.rows(); i++) { + for (unsigned int j = 0; j < jac.columns(); j++) + os << std::setw(KDL_FRAME_WIDTH) << jac(i, j); + os << std::endl; + } + os << "]"; + return os; +} + +std::istream& operator >>(std::istream& is, Jacobian& jac) { + return is; +} +std::ostream& operator <<(std::ostream& os, const JntSpaceInertiaMatrix& jntspaceinertiamatrix) { + os << "["; + for (unsigned int i = 0; i < jntspaceinertiamatrix.rows(); i++) { + for (unsigned int j = 0; j < jntspaceinertiamatrix.columns(); j++) + os << std::setw(KDL_FRAME_WIDTH) << jntspaceinertiamatrix(i, j); + os << std::endl; + } + os << "]"; + return os; +} + +std::istream& operator >>(std::istream& is, JntSpaceInertiaMatrix& jntspaceinertiamatrix) { + return is; +} + + +std::string tree2str(const SegmentMap::const_iterator it, const std::string& separator, const std::string& preamble, unsigned int level) { + std::stringstream out; + out << preamble; + for(unsigned int i=0; ifirst << "(q_nr: " << GetTreeElementQNr(it->second) << ")\n"; + for (unsigned int i = 0; i < GetTreeElementChildren(it->second).size(); i++) + out << tree2str(GetTreeElementChildren(it->second)[i], separator, preamble, level+1); + return out.str(); +} + +std::string tree2str(const Tree& tree, const std::string& separator, const std::string& preamble) { + return tree2str(tree.getRootSegment(), separator, preamble, 0); +} + +} diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/kinfam_io.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/kinfam_io.hpp new file mode 100644 index 0000000000000000000000000000000000000000..da0ec1e26f00760ecd59226f9099e8882bf8dc14 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/kinfam_io.hpp @@ -0,0 +1,81 @@ +// Copyright (C) 2007 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#ifndef KDL_KINFAM_IO_HPP +#define KDL_KINFAM_IO_HPP + +#include +#include + +#include "joint.hpp" +#include "segment.hpp" +#include "chain.hpp" +#include "jntarray.hpp" +#include "jacobian.hpp" +#include "tree.hpp" +#include "jntspaceinertiamatrix.hpp" + +namespace KDL { +std::ostream& operator <<(std::ostream& os, const Joint& joint); +std::istream& operator >>(std::istream& is, Joint& joint); +std::ostream& operator <<(std::ostream& os, const Segment& segment); +std::istream& operator >>(std::istream& is, Segment& segment); +std::ostream& operator <<(std::ostream& os, const Chain& chain); +std::istream& operator >>(std::istream& is, Chain& chain); + +std::ostream& operator <<(std::ostream& os, const Tree& tree); +std::istream& operator >>(std::istream& is, Tree& tree); + +std::ostream& operator <<(std::ostream& os, SegmentMap::const_iterator it); + +std::ostream& operator <<(std::ostream& os, const JntArray& array); +std::istream& operator >>(std::istream& is, JntArray& array); +std::ostream& operator <<(std::ostream& os, const Jacobian& jac); +std::istream& operator >>(std::istream& is, Jacobian& jac); +std::ostream& operator <<(std::ostream& os, const JntSpaceInertiaMatrix& jntspaceinertiamatrix); +std::istream& operator >>(std::istream& is, JntSpaceInertiaMatrix& jntspaceinertiamatrix); + +//Builds a string containing the "branches" of a Tree using indentation or another +//user-supplied pattern, so that it is easier to visualize its structure. It is +//also possible to specify a "preamble", ie, a string to be included at the +//beginning of each new line. +std::string tree2str(const Tree& tree, const std::string& separator=" ", const std::string& preamble=""); +std::string tree2str(const SegmentMap::const_iterator it, const std::string& separator=" ", const std::string& preamble="", unsigned int level=0); + + /* +template +std::ostream& operator<<(std::ostream& os, const std::vector& vec) { + os << "["; + for (unsigned int i = 0; i < vec.size(); i++) + os << vec[i] << " "; + os << "]"; + return os; +} +; + +template +std::istream& operator >>(std::istream& is, std::vector& vec) { + return is; +} +; + */ +} +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/motion.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/motion.hpp new file mode 100644 index 0000000000000000000000000000000000000000..7d427f72df0811f1596e78803670969d2d4bcbdb --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/motion.hpp @@ -0,0 +1,28 @@ +// Copyright (C) 2007 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +/** + * @defgroup Motion Motion + * @brief All classes related to the non-instantaneous motion of rigid + * bodies and kinematic structures, e.g., path and trajecory definitions + * and their building blocks. + * + */ diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b51a490802207d6445ee0ed1049af8ba9598ab68 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path.cpp @@ -0,0 +1,167 @@ +/*************************************************************************** + tag: Erwin Aertbelien Mon May 10 19:10:36 CEST 2004 path.cxx + + path.cxx - description + ------------------- + begin : Mon May 10 2004 + copyright : (C) 2004 Erwin Aertbelien + email : erwin.aertbelien@mech.kuleuven.ac.be + + *************************************************************************** + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; if not, write to the Free Software * + * Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307 USA * + * * + ***************************************************************************/ + +/***************************************************************************** + * \author + * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven + * + * \version + * ORO_Geometry V0.2 + * + * \par History + * - $log$ + * + * \par Release + * $Id: path.cpp,v 1.1.1.1.2.4 2003/07/18 14:49:50 psoetens Exp $ + * $Name: $ + ****************************************************************************/ + + +#include "utilities/error.h" +#include "utilities/error_stack.h" +#include "utilities/scoped_ptr.hpp" +#include "path.hpp" +#include "path_line.hpp" +#include "path_point.hpp" +#include "path_circle.hpp" +#include "path_composite.hpp" +#include "path_roundedcomposite.hpp" +#include "path_cyclic_closed.hpp" +#include +#include + +namespace KDL { + + +Path* Path::Read(std::istream& is) { + // auto_ptr because exception can be thrown ! + IOTrace("Path::Read"); + char storage[64]; + EatWord(is,"[",storage,sizeof(storage)); + Eat(is,'['); + if (strcmp(storage,"POINT")==0) { + IOTrace("POINT"); + Frame startpos; + is >> startpos; + EatEnd(is,']'); + IOTracePop(); + IOTracePop(); + return new Path_Point(startpos); + } else if (strcmp(storage,"LINE")==0) { + IOTrace("LINE"); + Frame startpos; + Frame endpos; + is >> startpos; + is >> endpos; + scoped_ptr orient( RotationalInterpolation::Read(is) ); + double eqradius; + is >> eqradius; + EatEnd(is,']'); + IOTracePop(); + IOTracePop(); + return new Path_Line(startpos,endpos,orient.release(),eqradius); + } else if (strcmp(storage,"CIRCLE")==0) { + IOTrace("CIRCLE"); + Frame F_base_start; + Vector V_base_center; + Vector V_base_p; + Rotation R_base_end; + double alpha; + double eqradius; + is >> F_base_start; + is >> V_base_center; + is >> V_base_p; + is >> R_base_end; + is >> alpha; + alpha *= deg2rad; + scoped_ptr orient( RotationalInterpolation::Read(is) ); + is >> eqradius; + EatEnd(is,']'); + IOTracePop(); + IOTracePop(); + return new Path_Circle( + F_base_start, + V_base_center, + V_base_p, + R_base_end, + alpha, + orient.release() , + eqradius + ); + } else if (strcmp(storage,"ROUNDEDCOMPOSITE")==0) { + IOTrace("ROUNDEDCOMPOSITE"); + double radius; + is >> radius; + double eqradius; + is >> eqradius; + scoped_ptr orient( RotationalInterpolation::Read(is) ); + scoped_ptr tr( + new Path_RoundedComposite(radius,eqradius,orient.release()) + ); + int size; + is >> size; + int i; + for (i=0;i> f; + tr->Add(f); + } + tr->Finish(); + EatEnd(is,']'); + IOTracePop(); + IOTracePop(); + return tr.release(); + } else if (strcmp(storage,"COMPOSITE")==0) { + IOTrace("COMPOSITE"); + int size; + scoped_ptr tr( new Path_Composite() ); + is >> size; + int i; + for (i=0;iAdd(Path::Read(is)); + } + EatEnd(is,']'); + IOTracePop(); + IOTracePop(); + return tr.release(); + } else if (strcmp(storage,"CYCLIC_CLOSED")==0) { + IOTrace("CYCLIC_CLOSED"); + int times; + scoped_ptr tr( Path::Read(is) ); + is >> times; + EatEnd(is,']'); + IOTracePop(); + IOTracePop(); + return new Path_Cyclic_Closed(tr.release(),times); + } else { + throw Error_MotionIO_Unexpected_Traj(); + } + return NULL; // just to avoid the warning; +} + +} + diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path.hpp new file mode 100644 index 0000000000000000000000000000000000000000..6cffae4c1d3a28006fdc84f73c86d015d5271fb2 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path.hpp @@ -0,0 +1,136 @@ +/*************************************************************************** + tag: Erwin Aertbelien Mon Jan 10 16:38:38 CET 2005 path.h + + path.h - description + ------------------- + begin : Mon January 10 2005 + copyright : (C) 2005 Erwin Aertbelien + email : erwin.aertbelien@mech.kuleuven.ac.be + + *************************************************************************** + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; if not, write to the Free Software * + * Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307 USA * + * * + ***************************************************************************/ + + + /***************************************************************************** + * \author + * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven + * + * \version + * ORO_Geometry V0.2 + * + * \par History + * - $log$ + * + * \par Release + * $Id: path.h,v 1.1.1.1.2.3 2003/07/24 13:26:15 psoetens Exp $ + * $Name: $ + ****************************************************************************/ + +#ifndef KDL_MOTION_PATH_H +#define KDL_MOTION_PATH_H + +#include "frames.hpp" + +#include + +#include "frames_io.hpp" + +namespace KDL { + +/** + * The specification of the path of a trajectory. + */ +class Path + { + public: + enum IdentifierType { + ID_LINE=1, + ID_CIRCLE=2, + ID_COMPOSITE=3, + ID_ROUNDED_COMPOSITE=4, + ID_POINT=5, + ID_CYCLIC_CLOSED=6 + }; + /** + * LengthToS() converts a physical length along the trajectory + * to the parameter s used in Pos, Vel and Acc. This is used because + * in cases with large rotations the parameter s does NOT correspond to + * the lineair length along the trajectory. + * User should be sure that the lineair distance travelled by this + * path object is NOT zero, when using this method ! + * (e.g. the case of only rotational change) + * throws Error_MotionPlanning_Not_Applicable if used on composed + * path objects. + * @ingroup Motion + */ + virtual double LengthToS(double length) = 0; + + /** + * Returns the total path length of the trajectory + * (has dimension LENGTH) + * This is not always a physical length , ie when dealing with rotations + * that are dominant. + */ + virtual double PathLength() = 0; + + /** + * Returns the Frame at the current path length s + */ + virtual Frame Pos(double s) const = 0; + + /** + * Returns the velocity twist at path length s theta and with + * derivative of s == sd + */ + virtual Twist Vel(double s,double sd) const = 0; + + /** + * Returns the acceleration twist at path length s and with + * derivative of s == sd, and 2nd derivative of s == sdd + */ + virtual Twist Acc(double s,double sd,double sdd) const = 0; + + /** + * Writes one of the derived objects to the stream + */ + virtual void Write(std::ostream& os) = 0; + + /** + * Reads one of the derived objects from the stream and returns a pointer + * (factory method) + */ + static Path* Read(std::istream& is); + + /** + * Virtual constructor, constructing by copying, + * Returns a deep copy of this Path Object + */ + virtual Path* Clone() = 0; + + /** + * gets an identifier indicating the type of this Path object + */ + virtual IdentifierType getIdentifier() const=0; + + virtual ~Path() {} + }; + +} + + +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_circle.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_circle.cpp new file mode 100644 index 0000000000000000000000000000000000000000..53261e83299f1b7c4d7b54f8fb3a7e6f580c168e --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_circle.cpp @@ -0,0 +1,170 @@ +/*************************************************************************** + tag: Erwin Aertbelien Mon May 10 19:10:36 CEST 2004 path_circle.cxx + + path_circle.cxx - description + ------------------- + begin : Mon May 10 2004 + copyright : (C) 2004 Erwin Aertbelien + email : erwin.aertbelien@mech.kuleuven.ac.be + + *************************************************************************** + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; if not, write to the Free Software * + * Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307 USA * + * * + ***************************************************************************/ +/***************************************************************************** + * \author + * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven + * + * \version + * ORO_Geometry V0.2 + * + * \par History + * - $log$ + * + * \par Release + * $Id: path_circle.cpp,v 1.1.1.1.2.5 2003/07/24 13:26:15 psoetens Exp $ + * $Name: $ + ****************************************************************************/ + + +#include "path_circle.hpp" +#include "utilities/error.h" + +namespace KDL { + +Path_Circle::Path_Circle(const Frame& F_base_start, + const Vector& _V_base_center, + const Vector& V_base_p, + const Rotation& R_base_end, + double alpha, + RotationalInterpolation* _orient, + double _eqradius, + bool _aggregate) : + orient(_orient) , + eqradius(_eqradius), + aggregate(_aggregate) +{ + F_base_center.p = _V_base_center; + orient->SetStartEnd(F_base_start.M,R_base_end); + double oalpha = orient->Angle(); + + Vector x(F_base_start.p - F_base_center.p); + radius = x.Normalize(); + if (radius < epsilon) { + if (aggregate) + delete orient; + throw Error_MotionPlanning_Circle_ToSmall(); + } + Vector tmpv(V_base_p-F_base_center.p); + tmpv.Normalize(); + Vector z( x * tmpv); + double n = z.Normalize(); + if (n < epsilon) { + if (aggregate) + delete orient; + throw Error_MotionPlanning_Circle_No_Plane(); + } + F_base_center.M = Rotation(x,z*x,z); + double dist = alpha*radius; + // See what has the slowest eq. motion, and adapt + // the other to this slower motion + // use eqradius to transform between rot and transl. + // the same as for lineair motion + if (oalpha*eqradius > dist) { + // rotational_interpolation is the limitation + pathlength = oalpha*eqradius; + scalerot = 1/eqradius; + scalelin = dist/pathlength; + } else { + // translation is the limitation + pathlength = dist; + scalerot = oalpha/pathlength; + scalelin = 1; + } +} + + +double Path_Circle::LengthToS(double length) { + return length/scalelin; +} + + +double Path_Circle::PathLength() { + return pathlength; +} + +Frame Path_Circle::Pos(double s) const { + double p = s*scalelin / radius; + return Frame(orient->Pos(s*scalerot), + F_base_center*Vector(radius*cos(p),radius*sin(p),0) + ); + +} + +Twist Path_Circle::Vel(double s,double sd) const { + double p = s*scalelin / radius; + double v = sd*scalelin / radius; + return Twist( F_base_center.M*Vector(-radius*sin(p)*v,radius*cos(p)*v,0), + orient->Vel(s*scalerot,sd*scalerot) + ); +} + +Twist Path_Circle::Acc(double s,double sd,double sdd) const { + double p = s*scalelin / radius; + double cp = cos(p); + double sp = sin(p); + double v = sd*scalelin / radius; + double a = sdd*scalelin / radius; + return Twist( F_base_center.M*Vector( + -radius*cp*v*v - radius*sp*a, + -radius*sp*v*v + radius*cp*a, + 0 + ), + orient->Acc(s*scalerot,sd*scalerot,sdd*scalerot) + ); +} + +Path* Path_Circle::Clone() { + return new Path_Circle( + Pos(0), + F_base_center.p, + F_base_center.M.UnitY(), + orient->Pos(pathlength*scalerot), + pathlength*scalelin/radius/deg2rad, + orient->Clone(), + eqradius, + aggregate + ); +} + +Path_Circle::~Path_Circle() { + if (aggregate) + delete orient; +} + +void Path_Circle::Write(std::ostream& os) { + os << "CIRCLE[ "; + os << " " << Pos(0) << std::endl; + os << " " << F_base_center.p << std::endl; + os << " " << F_base_center.M.UnitY() << std::endl; + os << " " << orient->Pos(pathlength*scalerot) << std::endl; + os << " " << pathlength*scalelin/radius/deg2rad << std::endl; + os << " ";orient->Write(os); + os << " " << eqradius; + os << "]"<< std::endl; +} + +} diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_circle.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_circle.hpp new file mode 100644 index 0000000000000000000000000000000000000000..36166cf998298264fbdae3bf269619f9509c8bd1 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_circle.hpp @@ -0,0 +1,118 @@ +/*************************************************************************** + tag: Erwin Aertbelien Mon Jan 10 16:38:39 CET 2005 path_circle.h + + path_circle.h - description + ------------------- + begin : Mon January 10 2005 + copyright : (C) 2005 Erwin Aertbelien + email : erwin.aertbelien@mech.kuleuven.ac.be + + *************************************************************************** + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; if not, write to the Free Software * + * Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307 USA * + * * + ***************************************************************************/ + + +/***************************************************************************** +* ALTERNATIVE FOR trajectory_circle.h/cpp + * \author + * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven + * + * \version + * ORO_Geometry V0.2 + * + * \par History + * - $log$ + * + * \par Release + * $Id: path_circle.h,v 1.1.1.1.2.3 2003/07/24 13:26:15 psoetens Exp $ + * $Name: $ + ****************************************************************************/ + +#ifndef KDL_MOTION_PATHCIRCLE_H +#define KDL_MOTION_PATHCIRCLE_H + +#include "path.hpp" +#include "rotational_interpolation.hpp" + + +namespace KDL { + + + /** + * A circular Path with 'open ends'. Path_Arc would + * have been a better name though. + * @ingroup Motion + */ +class Path_Circle : public Path + { + + // Orientatie gedeelte + RotationalInterpolation* orient; + + // Circular gedeelte + double radius; + Frame F_base_center; + + // equivalent radius + double eqradius; + + // verdeling baanlengte over pos/rot + double pathlength; + double scalelin; + double scalerot; + + bool aggregate; + + public: + + /** + * + * CAN THROW Error_MotionPlanning_Circle_ToSmall + * CAN THROW Error_MotionPlanning_Circle_No_Plane + */ + Path_Circle(const Frame& F_base_start,const Vector& V_base_center, + const Vector& V_base_p, + const Rotation& R_base_end, + double alpha, + RotationalInterpolation* otraj, + double eqradius, + bool _aggregate=true); + + double LengthToS(double length); + + virtual double PathLength(); + virtual Frame Pos(double s) const; + virtual Twist Vel(double s,double sd) const; + virtual Twist Acc(double s,double sd,double sdd) const; + virtual Path* Clone(); + virtual void Write(std::ostream& os); + + /** + * gets an identifier indicating the type of this Path object + */ + virtual IdentifierType getIdentifier() const { + return ID_CIRCLE; + } + + virtual ~Path_Circle(); + }; + + +} + + +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_composite.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_composite.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c93f5a293ec987c2f47edc5eb46a14299905663b --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_composite.cpp @@ -0,0 +1,161 @@ +/*************************************************************************** + tag: Erwin Aertbelien Mon May 10 19:10:36 CEST 2004 path_composite.cxx + + path_composite.cxx - description + ------------------- + begin : Mon May 10 2004 + copyright : (C) 2004 Erwin Aertbelien + email : erwin.aertbelien@mech.kuleuven.ac.be + + *************************************************************************** + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; if not, write to the Free Software * + * Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307 USA * + * * + ***************************************************************************/ +/***************************************************************************** + * \author + * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven + * + * \version + * ORO_Geometry V0.2 + * + * \par History + * - $log$ + * + * \par Release + * $Id: path_composite.cpp,v 1.1.1.1.2.7 2003/07/24 13:49:16 rwaarsin Exp $ + * $Name: $ + ****************************************************************************/ + + +#include "path_composite.hpp" +#include "utilities/error.h" +#include "utilities/scoped_ptr.hpp" +#include + +namespace KDL { + +// s should be in allowable limits, this is not checked +// simple linear search : TODO : make it binary search +// uses cached_... variables +// returns the relative path length within the segment +// you probably want to use the cached_index variable +double Path_Composite::Lookup(double s) const +{ + assert(s>=-1e-12); + assert(s<=pathlength+1e-12); + if ( (cached_starts <=s) && ( s <= cached_ends) ) { + return s - cached_starts; + } + double previous_s=0; + for (unsigned int i=0;iPathLength(); + dv.insert(dv.end(),pathlength); + gv.insert( gv.end(),std::make_pair(geom,aggregate) ); +} + +double Path_Composite::LengthToS(double length) { + throw Error_MotionPlanning_Not_Applicable(); + return 0; +} + +double Path_Composite::PathLength() { + return pathlength; +} + + +Frame Path_Composite::Pos(double s) const { + s = Lookup(s); + return gv[cached_index].first->Pos(s); +} + +Twist Path_Composite::Vel(double s,double sd) const { + s = Lookup(s); + return gv[cached_index].first->Vel(s,sd); +} + +Twist Path_Composite::Acc(double s,double sd,double sdd) const { + s = Lookup(s); + return gv[cached_index].first->Acc(s,sd,sdd); +} + +Path* Path_Composite::Clone() { + scoped_ptr comp( new Path_Composite() ); + for (unsigned int i = 0; i < dv.size(); ++i) { + comp->Add(gv[i].first->Clone(), gv[i].second); + } + return comp.release(); +} + +void Path_Composite::Write(std::ostream& os) { + os << "COMPOSITE[ " << std::endl; + os << " " << dv.size() << std::endl; + for (unsigned int i=0;iWrite(os); + } + os << "]" << std::endl; +} + +int Path_Composite::GetNrOfSegments() { + return static_cast(dv.size()); +} + +Path* Path_Composite::GetSegment(int i) { + assert(i>=0); + assert(i(dv.size())); + return gv[i].first; +} + +double Path_Composite::GetLengthToEndOfSegment(int i) { + assert(i>=0); + assert(i(dv.size())); + return dv[i]; +} + +void Path_Composite::GetCurrentSegmentLocation(double s, int& segment_number, + double& inner_s) +{ + inner_s = Lookup(s); + segment_number= cached_index; +} + +Path_Composite::~Path_Composite() { + PathVector::iterator it; + for (it=gv.begin();it!=gv.end();++it) { + if (it->second) + delete it->first; + } +} + +} // namespace KDL diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_composite.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_composite.hpp new file mode 100644 index 0000000000000000000000000000000000000000..25466eb793a12d7c36ee34c05a5b5960f28cf71a --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_composite.hpp @@ -0,0 +1,171 @@ +/*************************************************************************** + tag: Erwin Aertbelien Mon Jan 10 16:38:38 CET 2005 path_composite.h + + path_composite.h - description + ------------------- + begin : Mon January 10 2005 + copyright : (C) 2005 Erwin Aertbelien + email : erwin.aertbelien@mech.kuleuven.ac.be + + *************************************************************************** + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; if not, write to the Free Software * + * Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307 USA * + * * + ***************************************************************************/ + + + /***************************************************************************** + * \author + * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven + * + * \version + * ORO_Geometry V0.2 + * + * \par History + * - $log$ + * + * \par Release + * $Id: path_composite.h,v 1.1.1.1.2.5 2003/07/24 13:49:16 rwaarsin Exp $ + * $Name: $ + ****************************************************************************/ + +#ifndef KDL_PATHCOMPOSITE_H +#define KDL_PATHCOMPOSITE_H + +#include "frames.hpp" +#include "frames_io.hpp" +#include "path.hpp" +#include + +namespace KDL { + + /** + * A Path being the composition of other Path objects. + * + * For several of its methods, this class needs to lookup the segment corresponding to a value + * of the path variable s. To increase efficiency, this value is cached. + * + * \TODO Currently a linear search is used to look up the segment. A binary search is more efficient. Can STL be used for this ? + * \TODO Increase the efficiency for caching for the common case of a fine grained monotonously increasing path variable s. + * + * \TODO For all Path.., VelocityProfile.., Trajectory... check the bounds on the inputs with asserts. + * + * \TODO explain this routine in the wiki. + * + * @ingroup Motion + */ + class Path_Composite : public Path + { + typedef std::vector< std::pair > PathVector; + typedef std::vector DoubleVector; + + PathVector gv; + DoubleVector dv; + double pathlength; + + // lookup mechanism : + mutable double cached_starts; + mutable double cached_ends; + mutable int cached_index; + double Lookup(double s) const; + public: + + + Path_Composite(); + + /** + * Adds a Path* to this composite + */ + void Add(Path* geom, bool aggregate=true); + + + virtual double LengthToS(double length); + /** + * Returns the total path length of the trajectory + * (has dimension LENGTH) + * This is not always a physical length , ie when dealing with rotations + * that are dominant. + */ + virtual double PathLength(); + + /** + * Returns the Frame at the current path length s + */ + virtual Frame Pos(double s) const; + + /** + * Returns the velocity twist at path length s theta and with + * derivative of s == sd + */ + virtual Twist Vel(double s,double sd) const; + + /** + * Returns the acceleration twist at path length s and with + * derivative of s == sd, and 2nd derivative of s == sdd + */ + virtual Twist Acc(double s,double sd,double sdd) const; + + virtual Path* Clone(); + + /** + * Writes one of the derived objects to the stream + */ + virtual void Write(std::ostream& os); + + /** + * returns the number of underlying segments. + */ + virtual int GetNrOfSegments(); + + /** + * returns a pointer to the underlying Path of the given segment number i. + * \param i segment number + * \return pointer to the underlying Path + * \warning The pointer is still owned by this class and is lifetime depends on the lifetime + * of this class. + */ + virtual Path* GetSegment(int i); + + /** + * gets the length to the end of the given segment. + * \param i segment number + * \return length to the end of the segment, i.e. the value for s corresponding to the end of + * this segment. + */ + virtual double GetLengthToEndOfSegment(int i); + + /** + * \param s [INPUT] path length variable for the composite. + * \param segment_number [OUTPUT] segments that corresponds to the path length variable s. + * \param inner_s [OUTPUT] path length to use within the segment. + */ + virtual void GetCurrentSegmentLocation(double s, int &segment_number, double& inner_s); + + /** + * gets an identifier indicating the type of this Path object + */ + virtual IdentifierType getIdentifier() const { + return ID_COMPOSITE; + } + + virtual ~Path_Composite(); + }; + + + +} + + +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_cyclic_closed.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_cyclic_closed.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fa5c65f30781c0f748a5b90f59b68742058ed461 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_cyclic_closed.cpp @@ -0,0 +1,90 @@ +/*************************************************************************** + tag: Erwin Aertbelien Mon May 10 19:10:36 CEST 2004 path_cyclic_closed.cxx + + path_cyclic_closed.cxx - description + ------------------- + begin : Mon May 10 2004 + copyright : (C) 2004 Erwin Aertbelien + email : erwin.aertbelien@mech.kuleuven.ac.be + + *************************************************************************** + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; if not, write to the Free Software * + * Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307 USA * + * * + ***************************************************************************/ +/***************************************************************************** + * \author + * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven + * + * \version + * ORO_Geometry V0.2 + * + * \par History + * - $log$ + * + * \par Release + * $Id: path_cyclic_closed.cpp,v 1.1.1.1.2.5 2003/07/24 13:26:15 psoetens Exp $ + * $Name: $ + ****************************************************************************/ + + +#include "path_cyclic_closed.hpp" +#include "utilities/error.h" + +namespace KDL { + +Path_Cyclic_Closed::Path_Cyclic_Closed(Path* _geom,int _times, bool _aggregate): +times(_times),geom(_geom), aggregate(_aggregate) {} + +double Path_Cyclic_Closed::LengthToS(double length) { + throw Error_MotionPlanning_Not_Applicable(); + return 0; +} + +double Path_Cyclic_Closed::PathLength(){ + return geom->PathLength()*times; +} + +Frame Path_Cyclic_Closed::Pos(double s) const { + return geom->Pos( fmod(s,geom->PathLength()) ); +} + +Twist Path_Cyclic_Closed::Vel(double s,double sd) const { + return geom->Vel( fmod(s,geom->PathLength()),sd ); +} + +Twist Path_Cyclic_Closed::Acc(double s,double sd,double sdd) const { + return geom->Acc( fmod(s,geom->PathLength()),sd,sdd ); +} + + +Path_Cyclic_Closed::~Path_Cyclic_Closed() { + if (aggregate) + delete geom; +} + +Path* Path_Cyclic_Closed::Clone() { + return new Path_Cyclic_Closed(geom->Clone(),times, aggregate); +} + +void Path_Cyclic_Closed::Write(std::ostream& os) { + os << "CYCLIC_CLOSED[ "; + os << " ";geom->Write(os);os << std::endl; + os << " " << times << std::endl; + os << "]" << std::endl; +} + +} + diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_cyclic_closed.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_cyclic_closed.hpp new file mode 100644 index 0000000000000000000000000000000000000000..73f98bdbc443daed53761dec32704ac2558b474d --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_cyclic_closed.hpp @@ -0,0 +1,90 @@ +/*************************************************************************** + tag: Erwin Aertbelien Mon Jan 10 16:38:38 CET 2005 path_cyclic_closed.h + + path_cyclic_closed.h - description + ------------------- + begin : Mon January 10 2005 + copyright : (C) 2005 Erwin Aertbelien + email : erwin.aertbelien@mech.kuleuven.ac.be + + *************************************************************************** + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; if not, write to the Free Software * + * Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307 USA * + * * + ***************************************************************************/ + + + /***************************************************************************** + * \author + * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven + * + * \version + * ORO_Geometry V2 + * + * \par History + * - $log$ + * + * \par Release + * $Id: path_cyclic_closed.h,v 1.1.1.1.2.3 2003/07/24 13:26:15 psoetens Exp $ + * $Name: $ + ****************************************************************************/ + +#ifndef KDL_MOTION_PATH_CYCLIC_CLOSED_H +#define KDL_MOTION_PATH_CYCLIC_CLOSED_H + +#include "frames.hpp" +#include "frames_io.hpp" +#include "path.hpp" +#include + + +namespace KDL { + + /** + * A Path representing a closed circular movement, + * which is traversed a number of times. + * @ingroup Motion + */ + class Path_Cyclic_Closed : public Path + { + int times; + Path* geom; + bool aggregate; + public: + Path_Cyclic_Closed(Path* _geom,int _times, bool _aggregate=true); + virtual double LengthToS(double length); + virtual double PathLength(); + virtual Frame Pos(double s) const; + virtual Twist Vel(double s,double sd) const; + virtual Twist Acc(double s,double sd,double sdd) const; + + virtual void Write(std::ostream& os); + static Path* Read(std::istream& is); + virtual Path* Clone(); + /** + * gets an identifier indicating the type of this Path object + */ + virtual IdentifierType getIdentifier() const { + return ID_CYCLIC_CLOSED; + } + virtual ~Path_Cyclic_Closed(); + }; + + + +} + + +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_line.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_line.cpp new file mode 100644 index 0000000000000000000000000000000000000000..789f5428db876665546c26145f4d5a63d180007a --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_line.cpp @@ -0,0 +1,184 @@ +/*************************************************************************** + tag: Erwin Aertbelien Mon May 10 19:10:36 CEST 2004 path_line.cxx + + path_line.cxx - description + ------------------- + begin : Mon May 10 2004 + copyright : (C) 2004 Erwin Aertbelien + email : erwin.aertbelien@mech.kuleuven.ac.be + + *************************************************************************** + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; if not, write to the Free Software * + * Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307 USA * + * * + ***************************************************************************/ +/***************************************************************************** + * \author + * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven + * + * \version + * ORO_Geometry V0.2 + * + * \par History + * - $log$ + * + * \par Release + * $Id: path_line.cpp,v 1.1.1.1.2.3 2003/07/24 13:26:15 psoetens Exp $ + * $Name: $ + ****************************************************************************/ + + +#include "path_line.hpp" + +namespace KDL { + +Path_Line::Path_Line(const Frame& startpos, + const Frame& endpos, + RotationalInterpolation* _orient, + double _eqradius, + bool _aggregate ): + orient(_orient), + V_base_start(startpos.p), + V_base_end(endpos.p), + eqradius(_eqradius), + aggregate(_aggregate) + { + V_start_end = V_base_end - V_base_start; + double dist = V_start_end.Normalize(); + orient->SetStartEnd(startpos.M,endpos.M); + double alpha = orient->Angle(); + + // See what has the slowest eq. motion, and adapt + // the other to this slower motion + // use eqradius to transform between rot and transl. + + // Only modify if non zero (prevent division by zero) + if ( alpha != 0 && alpha*eqradius > dist) { + // rotational_interpolation is the limitation + pathlength = alpha*eqradius; + scalerot = 1/eqradius; + scalelin = dist/pathlength; + } else if ( dist != 0 ) { + // translation is the limitation + pathlength = dist; + scalerot = alpha/pathlength; + scalelin = 1; + } else { + // both were zero + pathlength = 0; + scalerot = 1; + scalelin = 1; + } + } + +Path_Line::Path_Line(const Frame& startpos, + const Twist& starttwist, + RotationalInterpolation* _orient, + double _eqradius, + bool _aggregate ): + orient(_orient), + V_base_start(startpos.p), + V_base_end(startpos.p + starttwist.vel), + eqradius(_eqradius), + aggregate(_aggregate) + { + // startframe and starttwist are expressed in Wo. + // after 1 time unit, startframe has translated over starttwist.vel + // and rotated over starttwist.rot.Norm() (both vectors can be zero) + // Thus the frame on the path after 1 time unit is defined by + // startframe.Integrate(starttwist, 1); + V_start_end = V_base_end - V_base_start; + double dist = V_start_end.Normalize(); // distance traveled during 1 time unit + orient->SetStartEnd(startpos.M, (startpos*Frame( Rotation::Rot(starttwist.rot, starttwist.rot.Norm() ), starttwist.vel )).M); + double alpha = orient->Angle(); // rotation during 1 time unit + + // See what has the slowest eq. motion, and adapt + // the other to this slower motion + // use eqradius to transform between rot and transl. + // Only modify if non zero (prevent division by zero) + if ( alpha != 0 && alpha*eqradius > dist) { + // rotational_interpolation is the limitation + pathlength = alpha*eqradius; + scalerot = 1/eqradius; + scalelin = dist/pathlength; + } else if ( dist != 0 ) { + // translation is the limitation + pathlength = dist; + scalerot = alpha/pathlength; + scalelin = 1; + } else { + // both were zero + pathlength = 0; + scalerot = 1; + scalelin = 1; + } + } + +double Path_Line::LengthToS(double length) { + return length/scalelin; +} +double Path_Line::PathLength(){ + return pathlength; +} +Frame Path_Line::Pos(double s) const { + return Frame(orient->Pos(s*scalerot),V_base_start + V_start_end*s*scalelin ); +} + +Twist Path_Line::Vel(double s,double sd) const { + return Twist( V_start_end*sd*scalelin, orient->Vel(s*scalerot,sd*scalerot) ); +} + +Twist Path_Line::Acc(double s,double sd,double sdd) const { + return Twist( V_start_end*sdd*scalelin, orient->Acc(s*scalerot,sd*scalerot,sdd*scalerot) ); +} + + +Path_Line::~Path_Line() { + if (aggregate) + delete orient; +} + +Path* Path_Line::Clone() { + if (aggregate ) + return new Path_Line( + Frame(orient->Pos(0),V_base_start), + Frame(orient->Pos(pathlength*scalerot),V_base_end), + orient->Clone(), + eqradius, + true + ); + // else : + return new Path_Line( + Frame(orient->Pos(0),V_base_start), + Frame(orient->Pos(pathlength*scalerot),V_base_end), + orient, + eqradius, + false + ); + +} + +void Path_Line::Write(std::ostream& os) { + os << "LINE[ "; + os << " " << Frame(orient->Pos(0),V_base_start) << std::endl; + os << " " << Frame(orient->Pos(pathlength*scalerot),V_base_end) << std::endl; + os << " ";orient->Write(os); + os << " " << eqradius; + os << "]" << std::endl; +} + + +} + diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_line.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_line.hpp new file mode 100644 index 0000000000000000000000000000000000000000..7f27ba906291a690465d734d0e06752b95361646 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_line.hpp @@ -0,0 +1,138 @@ +/*************************************************************************** + tag: Erwin Aertbelien Mon Jan 10 16:38:39 CET 2005 path_line.h + + path_line.h - description + ------------------- + begin : Mon January 10 2005 + copyright : (C) 2005 Erwin Aertbelien + email : erwin.aertbelien@mech.kuleuven.ac.be + + *************************************************************************** + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; if not, write to the Free Software * + * Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307 USA * + * * + ***************************************************************************/ + + +/***************************************************************************** +* ALTERNATIVE FOR trajectory_line.h/cpp + * \author + * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven + * + * \version + * ORO_Geometry V0.2 + * + * \par History + * - $log$ + * + * \par Release + * $Id: path_line.h,v 1.1.1.1.2.3 2003/07/24 13:26:15 psoetens Exp $ + * $Name: $ + ****************************************************************************/ + +#ifndef KDL_MOTION_PATH_LINE_H +#define KDL_MOTION_PATH_LINE_H + +#include "path.hpp" +#include "rotational_interpolation.hpp" + + +namespace KDL { + + + /** + * A path representing a line from A to B. + * @ingroup Motion + */ +class Path_Line : public Path + { + // Orientatie gedeelte + RotationalInterpolation* orient; + + // Lineair gedeelte + Vector V_base_start; + Vector V_base_end; + Vector V_start_end; + + double eqradius; // equivalent radius + + // verdeling baanlengte over pos/rot + double pathlength; + double scalelin; + double scalerot; + + bool aggregate; + public: + /** + * Constructs a Line Path + * F_base_start and F_base_end give the begin and end frame wrt the base + * orient gives the method of rotation interpolation + * eqradius : equivalent radius : + * serves to compare rotations and translations. + * the "amount of motion"(pos,vel,acc) of the rotation is taken + * to be the amount motion of a point at distance eqradius from the + * rotation axis. + * + * Eqradius is introduced because it is unavoidable that you have to compare rotations and translations : + * e.g. : You can have motions that only contain rotation, and motions that only contain translations. + * The motion planning goes as follows : + * - translation is planned with the given parameters + * - rotation is planned planned with the parameters calculated with eqradius. + * - The longest of the previous two remains unchanged, + * the shortest in duration is scaled to take as long as the longest. + * This guarantees that the geometric path in 6D space remains independent of the motion profile parameters. + * + * RotationalInterpolation_SingleAxis() has the advantage that it is independent + * of the frame in which you express your path. + * Other implementations for RotationalInterpolations COULD be + * (not implemented) (yet) : + * 1) quaternion interpolation : but this is more difficult for the human to interpret + * 2) 3-axis interpolation : express the orientation of the frame in e.g. + * euler zyx angles alfa,beta, gamma and interpolate these parameters. + * But this is dependent of the frame you choose as a reference and + * their can occur representation singularities. + */ + Path_Line(const Frame& F_base_start, + const Frame& F_base_end, + RotationalInterpolation* orient, + double eqradius, + bool _aggregate=true); + Path_Line(const Frame& F_base_start, + const Twist& twist_in_base, + RotationalInterpolation* orient, + double eqradius, + bool _aggregate=true); + double LengthToS(double length); + virtual double PathLength(); + virtual Frame Pos(double s) const; + virtual Twist Vel(double s,double sd) const ; + virtual Twist Acc(double s,double sd,double sdd) const; + virtual void Write(std::ostream& os); + virtual Path* Clone(); + + /** + * gets an identifier indicating the type of this Path object + */ + virtual IdentifierType getIdentifier() const { + return ID_LINE; + } + virtual ~Path_Line(); + }; + + +} + + +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_point.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_point.cpp new file mode 100644 index 0000000000000000000000000000000000000000..854843dc60ed0c39f80d71bbb17be92fa48d8287 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_point.cpp @@ -0,0 +1,83 @@ +/*************************************************************************** + tag: Erwin Aertbelien Mon May 10 19:10:36 CEST 2004 path_point.cxx + + path_point.cxx - description + ------------------- + begin : Mon May 10 2004 + copyright : (C) 2004 Erwin Aertbelien + email : erwin.aertbelien@mech.kuleuven.ac.be + + *************************************************************************** + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; if not, write to the Free Software * + * Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307 USA * + * * + ***************************************************************************/ +/***************************************************************************** + * \author + * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven + * + * \version + * ORO_Geometry V0.2 + * + * \par History + * - $log$ + * + * \par Release + * $Id: path_point.cpp,v 1.1.2.3 2003/07/24 13:40:49 psoetens Exp $ + * $Name: $ + ****************************************************************************/ + + +#include "path_point.hpp" + +namespace KDL { + +Path_Point::Path_Point(const Frame& startpos) + :F_base_start(startpos) + { + } + +double Path_Point::LengthToS(double length) { + return length; +} +double Path_Point::PathLength(){ + return 0; +} +Frame Path_Point::Pos(double s) const { + return F_base_start; +} + +Twist Path_Point::Vel(double s,double sd) const { + return Twist::Zero(); +} + +Twist Path_Point::Acc(double s,double sd,double sdd) const { + return Twist::Zero(); +} + +Path_Point::~Path_Point() { +} + +Path* Path_Point::Clone() { + return new Path_Point( F_base_start ); +} + +void Path_Point::Write(std::ostream& os) { + os << "POINT[ "<< F_base_start << "]" << std::endl; +} + + +} + diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_point.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_point.hpp new file mode 100644 index 0000000000000000000000000000000000000000..8045cb405429e201a03d252c5ccccd1169b117d7 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_point.hpp @@ -0,0 +1,89 @@ +/*************************************************************************** + tag: Erwin Aertbelien Mon Jan 10 16:38:39 CET 2005 path_point.h + + path_point.h - description + ------------------- + begin : Mon January 10 2005 + copyright : (C) 2005 Erwin Aertbelien + email : erwin.aertbelien@mech.kuleuven.ac.be + + *************************************************************************** + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; if not, write to the Free Software * + * Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307 USA * + * * + ***************************************************************************/ + + +/***************************************************************************** +* ALTERNATIVE FOR trajectory_stationary.h/cpp + * \author + * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven + * + * \version + + * ORO_Geometry V0.2 + * + * \par History + * - $log$ + * + * \par Release + * $Id: path_point.h,v 1.1.2.3 2003/07/24 13:40:49 psoetens Exp $ + * $Name: $ + ****************************************************************************/ + +#ifndef KDL_MOTION_PATH_POINT_H +#define KDL_MOTION_PATH_POINT_H + +#include "path.hpp" +#include "rotational_interpolation.hpp" + + +namespace KDL { + + + +/** + * A Path consisting only of a point in space. + * @ingroup Motion + */ +class Path_Point : public Path + { + Frame F_base_start; + public: + /** + * Constructs a Point Path + */ + Path_Point(const Frame& F_base_start); + double LengthToS(double length); + virtual double PathLength(); + virtual Frame Pos(double s) const; + virtual Twist Vel(double s,double sd) const ; + virtual Twist Acc(double s,double sd,double sdd) const; + virtual void Write(std::ostream& os); + virtual Path* Clone(); + + /** + * gets an identifier indicating the type of this Path object + */ + virtual IdentifierType getIdentifier() const { + return ID_POINT; + } + virtual ~Path_Point(); + }; + +} + + +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_roundedcomposite.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_roundedcomposite.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2916505c034c1abf8bbdeb223e89935ba46321e9 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_roundedcomposite.cpp @@ -0,0 +1,202 @@ +/*************************************************************************** + tag: Erwin Aertbelien Mon May 10 19:10:36 CEST 2004 path_roundedcomposite.cxx + + path_roundedcomposite.cxx - description + ------------------- + begin : Mon May 10 2004 + copyright : (C) 2004 Erwin Aertbelien + email : erwin.aertbelien@mech.kuleuven.ac.be + + *************************************************************************** + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; if not, write to the Free Software * + * Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307 USA * + * * + ***************************************************************************/ +/***************************************************************************** + * \author + * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven + * + * \version + * ORO_Geometry V0.2 + * + * \par History + * - $log$ + * + * \par Release + * $Id: path_roundedcomposite.cpp,v 1.1.1.1.2.5 2003/07/24 13:26:15 psoetens Exp $ + * $Name: $ + ****************************************************************************/ + + +#include "path_roundedcomposite.hpp" +#include "path_line.hpp" +#include "path_circle.hpp" +#include "utilities/error.h" +#include "utilities/scoped_ptr.hpp" +#include + + +namespace KDL { + +// private constructor, to keep the type when cloning a Path_RoundedComposite, such that getIdentifier keeps on returning +// the correct value: +Path_RoundedComposite::Path_RoundedComposite(Path_Composite* _comp, + double _radius, double _eqradius, RotationalInterpolation* _orient, + bool _aggregate,int _nrofpoints): + comp(_comp), radius(_radius), eqradius(_eqradius), orient(_orient), nrofpoints(_nrofpoints), aggregate(_aggregate) { +} + +Path_RoundedComposite::Path_RoundedComposite(double _radius,double _eqradius,RotationalInterpolation* _orient, bool _aggregate) : + comp( new Path_Composite()), radius(_radius),eqradius(_eqradius), orient(_orient), aggregate(_aggregate) +{ + nrofpoints = 0; + if (eqradius<=0) { + throw Error_MotionPlanning_Not_Feasible(1); + } +} + +void Path_RoundedComposite::Add(const Frame& F_base_point) { + double eps = 1E-7; + if (nrofpoints == 0) { + F_base_start = F_base_point; + } else if (nrofpoints == 1) { + F_base_via = F_base_point; + } else { + // calculate rounded segment : line + circle, + // determine the angle between the line segments : + Vector ab = F_base_via.p - F_base_start.p; + Vector bc = F_base_point.p - F_base_via.p; + double abdist = ab.Norm(); + double bcdist = bc.Norm(); + if (abdist < eps) { + throw Error_MotionPlanning_Not_Feasible(2); + } + if (bcdist < eps) { + throw Error_MotionPlanning_Not_Feasible(3); + } + // Clamp to avoid rounding errors (acos is defined between [-1 ; 1]) + double alpha = acos(std::max(-1., std::min(dot(ab, bc) / abdist / bcdist, 1.))); + if ((PI - alpha) < eps) { + throw Error_MotionPlanning_Not_Feasible(4); + } + if (alpha < eps) { + // no rounding is done in the case of parallel line segments + comp->Add( + new Path_Line(F_base_start, F_base_via, orient->Clone(), + eqradius)); + F_base_start = F_base_via; + F_base_via = F_base_point; + } else { + double d = radius / tan((PI - alpha) / 2); // tan. is guaranteed not to return zero. + if (d >= abdist) + throw Error_MotionPlanning_Not_Feasible(5); + + if (d >= bcdist) + throw Error_MotionPlanning_Not_Feasible(6); + + scoped_ptr < Path + > line1( + new Path_Line(F_base_start, F_base_via, + orient->Clone(), eqradius)); + scoped_ptr < Path + > line2( + new Path_Line(F_base_via, F_base_point, + orient->Clone(), eqradius)); + Frame F_base_circlestart = line1->Pos(line1->LengthToS(abdist - d)); + Frame F_base_circleend = line2->Pos(line2->LengthToS(d)); + // end of circle segment, beginning of next line + Vector V_base_t = ab * (ab * bc); + V_base_t.Normalize(); + comp->Add( + new Path_Line(F_base_start, F_base_circlestart, + orient->Clone(), eqradius)); + comp->Add( + new Path_Circle(F_base_circlestart, + F_base_circlestart.p - V_base_t * radius, + F_base_circleend.p, F_base_circleend.M, alpha, + orient->Clone(), eqradius)); + // shift for next line + F_base_start = F_base_circleend; // end of the circle segment + F_base_via = F_base_point; + } + } + + nrofpoints++; +} + +void Path_RoundedComposite::Finish() { + if (nrofpoints >= 1) { + comp->Add( + new Path_Line(F_base_start, F_base_via, orient->Clone(), + eqradius)); + } +} + +double Path_RoundedComposite::LengthToS(double length) { + return comp->LengthToS(length); +} + + +double Path_RoundedComposite::PathLength() { + return comp->PathLength(); +} + +Frame Path_RoundedComposite::Pos(double s) const { + return comp->Pos(s); +} + +Twist Path_RoundedComposite::Vel(double s, double sd) const { + return comp->Vel(s, sd); +} + +Twist Path_RoundedComposite::Acc(double s, double sd, double sdd) const { + return comp->Acc(s, sd, sdd); +} + +void Path_RoundedComposite::Write(std::ostream& os) { + comp->Write(os); +} + +int Path_RoundedComposite::GetNrOfSegments() { + return comp->GetNrOfSegments(); +} + +Path* Path_RoundedComposite::GetSegment(int i) { + return comp->GetSegment(i); +} + +double Path_RoundedComposite::GetLengthToEndOfSegment(int i) { + return comp->GetLengthToEndOfSegment(i); +} + +void Path_RoundedComposite::GetCurrentSegmentLocation(double s, + int& segment_number, double& inner_s) { + comp->GetCurrentSegmentLocation(s,segment_number,inner_s); +} + + + +Path_RoundedComposite::~Path_RoundedComposite() { + if (aggregate) + delete orient; + delete comp; +} + + +Path* Path_RoundedComposite::Clone() { + return new Path_RoundedComposite(static_cast(comp->Clone()),radius,eqradius,orient->Clone(), true, nrofpoints); +} + +} diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_roundedcomposite.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_roundedcomposite.hpp new file mode 100644 index 0000000000000000000000000000000000000000..a7451359dc4a6df22e7ed4d69df1b1adbb0f555b --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/path_roundedcomposite.hpp @@ -0,0 +1,203 @@ +/*************************************************************************** + tag: Erwin Aertbelien Mon Jan 10 16:38:38 CET 2005 path_roundedcomposite.h + + path_roundedcomposite.h - description + ------------------- + begin : Mon January 10 2005 + copyright : (C) 2005 Erwin Aertbelien + email : erwin.aertbelien@mech.kuleuven.ac.be + + *************************************************************************** + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; if not, write to the Free Software * + * Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307 USA * + * * + ***************************************************************************/ + + + /***************************************************************************** + * \author + * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven + * + * \version + * ORO_Geometry V0.2 + * + * \par History + * - $log$ + * + * \par Release + * $Id: path_roundedcomposite.h,v 1.1.1.1.2.3 2003/07/24 13:26:15 psoetens Exp $ + * $Name: $ + ****************************************************************************/ + +#ifndef KDL_MOTION_ROUNDEDCOMPOSITE_H +#define KDL_MOTION_ROUNDEDCOMPOSITE_H + +#include "path.hpp" +#include "path_composite.hpp" +#include "rotational_interpolation.hpp" + +namespace KDL { + +/** + * The specification of a path, composed of way-points with rounded corners. + * + * @ingroup Motion + */ +class Path_RoundedComposite : public Path + { + /** a Path_Composite is aggregated to hold the rounded trajectory + * with circles and lines + */ + Path_Composite* comp; + + + double radius; + double eqradius; + RotationalInterpolation* orient; + // cached from underlying path objects for generating the rounding : + Frame F_base_start; + Frame F_base_via; + //Frame F_base_end; + int nrofpoints; + + bool aggregate; + + Path_RoundedComposite(Path_Composite* comp,double radius,double eqradius,RotationalInterpolation* orient, bool aggregate, int nrofpoints); + + public: + + /** + * @param radius : radius of the rounding circles + * @param eqradius : equivalent radius to compare rotations/velocities + * @param orient : method of rotational_interpolation interpolation + * @param aggregate : if true, this object will own the _orient pointer, i.e. it will delete the _orient pointer + * when the destructor of this object is called. + */ + Path_RoundedComposite(double radius,double eqradius,RotationalInterpolation* orient, bool aggregate=true); + + /** + * Adds a point to this rounded composite, between two adjacent points + * a Path_Line will be created, between two lines there will be + * rounding with the given radius with a Path_Circle + * + * The Error_MotionPlanning_Not_Feasible has a type (obtained by GetType) of: + * - 3101 if the eq. radius <= 0 + * - 3102 if the first segment in a rounding has zero length. + * - 3103 if the second segment in a rounding has zero length. + * - 3104 if the angle between the first and the second segment is close to PI. + * (meaning that the segments are on top of each other) + * - 3105 if the distance needed for the rounding is larger then the first segment. + * - 3106 if the distance needed for the rounding is larger then the second segment. + * + * @param F_base_point the pose of a new via point. + * @warning Can throw Error_MotionPlanning_Not_Feasible object + * @TODO handle the case of error type 3105 and 3106 by skipping segments, such that the class could be applied + * with points that are very close to each other. + */ + void Add(const Frame& F_base_point); + + /** + * to be called after the last line is added to finish up + * the work + */ + void Finish(); + + + virtual double LengthToS(double length); + + /** + * Returns the total path length of the trajectory + * (has dimension LENGTH) + * This is not always a physical length , ie when dealing with rotations + * that are dominant. + */ + virtual double PathLength(); + + + /** + * Returns the Frame at the current path length s + */ + virtual Frame Pos(double s) const; + + /** + * Returns the velocity twist at path length s theta and with + * derivative of s == sd + */ + virtual Twist Vel(double s,double sd) const; + + /** + * Returns the acceleration twist at path length s and with + * derivative of s == sd, and 2nd derivative of s == sdd + */ + virtual Twist Acc(double s,double sd,double sdd) const; + + /** + * virtual constructor, constructing by copying. + * In this case it returns the Clone() of the aggregated Path_Composite + * because this is all one ever will need. + */ + virtual Path* Clone(); + + /** + * Writes one of the derived objects to the stream + */ + virtual void Write(std::ostream& os); + + /** + * returns the number of underlying segments. + */ + virtual int GetNrOfSegments(); + + /** + * returns a pointer to the underlying Path of the given segment number i. + * \param i segment number + * \return pointer to the underlying Path + * \warning The pointer is still owned by this class and is lifetime depends on the lifetime + * of this class. + */ + virtual Path* GetSegment(int i); + + /** + * gets the length to the end of the given segment. + * \param i segment number + * \return length to the end of the segment, i.e. the value for s corresponding to the end of + * this segment. + */ + virtual double GetLengthToEndOfSegment(int i); + + /** + * \param s [INPUT] path length variable for the composite. + * \param segment_number [OUTPUT] segments that corresponds to the path length variable s. + * \param inner_s [OUTPUT] path length to use within the segment. + */ + virtual void GetCurrentSegmentLocation(double s, int &segment_number, double& inner_s); + + /** + * gets an identifier indicating the type of this Path object + */ + virtual IdentifierType getIdentifier() const { + return ID_ROUNDED_COMPOSITE; + } + + + virtual ~Path_RoundedComposite(); + }; + + + +} + + +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/rigidbodyinertia.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/rigidbodyinertia.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3bfd938d03742066e9905a8059161ce64d988430 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/rigidbodyinertia.cpp @@ -0,0 +1,98 @@ +// Copyright (C) 2007 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#include "rigidbodyinertia.hpp" + +#include + +namespace KDL{ + + const static bool mhi=true; + + RigidBodyInertia::RigidBodyInertia(double m_,const Vector& h_,const RotationalInertia& I_,bool mhi): + m(m_),h(h_),I(I_) + { + } + + RigidBodyInertia::RigidBodyInertia(double m_, const Vector& c_, const RotationalInertia& Ic): + m(m_),h(m*c_){ + //I=Ic-c x c x + Eigen::Vector3d c_eig=Eigen::Map(c_.data); + Eigen::Map(I.data)=Eigen::Map(Ic.data)-m_*(c_eig*c_eig.transpose()-c_eig.dot(c_eig)*Eigen::Matrix3d::Identity()); + } + + RigidBodyInertia operator*(double a,const RigidBodyInertia& I){ + return RigidBodyInertia(a*I.m,a*I.h,a*I.I,mhi); + } + + RigidBodyInertia operator+(const RigidBodyInertia& Ia, const RigidBodyInertia& Ib){ + return RigidBodyInertia(Ia.m+Ib.m,Ia.h+Ib.h,Ia.I+Ib.I,mhi); + } + + Wrench operator*(const RigidBodyInertia& I,const Twist& t){ + return Wrench(I.m*t.vel-I.h*t.rot,I.I*t.rot+I.h*t.vel); + } + + RigidBodyInertia operator*(const Frame& T,const RigidBodyInertia& I){ + Frame X=T.Inverse(); + //mb=ma + //hb=R*(h-m*r) + //Ib = R(Ia+r x h x + (h-m*r) x r x)R' + Vector hmr = (I.h-I.m*X.p); + Eigen::Vector3d r_eig = Eigen::Map(X.p.data); + Eigen::Vector3d h_eig = Eigen::Map(I.h.data); + Eigen::Vector3d hmr_eig = Eigen::Map(hmr.data); + Eigen::Matrix3d rcrosshcross = h_eig *r_eig.transpose()-r_eig.dot(h_eig)*Eigen::Matrix3d::Identity(); + Eigen::Matrix3d hmrcrossrcross = r_eig*hmr_eig.transpose()-hmr_eig.dot(r_eig)*Eigen::Matrix3d::Identity(); + Eigen::Matrix3d R = Eigen::Map(X.M.data); + RotationalInertia Ib; + Eigen::Map(Ib.data) = R*((Eigen::Map(I.I.data)+rcrosshcross+hmrcrossrcross)*R.transpose()); + + return RigidBodyInertia(I.m,T.M*hmr,Ib,mhi); + } + + RigidBodyInertia operator*(const Rotation& M,const RigidBodyInertia& I){ + //mb=ma + //hb=R*h + //Ib = R(Ia)R' with r=0 + Eigen::Matrix3d R = Eigen::Map(M.data); + RotationalInertia Ib; + Eigen::Map(Ib.data) = R.transpose()*(Eigen::Map(I.I.data)*R); + + return RigidBodyInertia(I.m,M*I.h,Ib,mhi); + } + + RigidBodyInertia RigidBodyInertia::RefPoint(const Vector& p){ + //mb=ma + //hb=(h-m*r) + //Ib = (Ia+r x h x + (h-m*r) x r x) + Vector hmr = (this->h-this->m*p); + Eigen::Vector3d r_eig = Eigen::Map(p.data); + Eigen::Vector3d h_eig = Eigen::Map(this->h.data); + Eigen::Vector3d hmr_eig = Eigen::Map(hmr.data); + Eigen::Matrix3d rcrosshcross = h_eig * r_eig.transpose()-r_eig.dot(h_eig)*Eigen::Matrix3d::Identity(); + Eigen::Matrix3d hmrcrossrcross = r_eig*hmr_eig.transpose()-hmr_eig.dot(r_eig)*Eigen::Matrix3d::Identity(); + RotationalInertia Ib; + Eigen::Map(Ib.data) = Eigen::Map(this->I.data)+rcrosshcross+hmrcrossrcross; + + return RigidBodyInertia(this->m,hmr,Ib,mhi); + } +}//namespace diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/rigidbodyinertia.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/rigidbodyinertia.hpp new file mode 100644 index 0000000000000000000000000000000000000000..42fc5d0fd21793cd69026fa7c121f1b450d4250c --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/rigidbodyinertia.hpp @@ -0,0 +1,140 @@ +// Copyright (C) 2009 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#ifndef KDL_RIGIDBODYINERTIA_HPP +#define KDL_RIGIDBODYINERTIA_HPP + +#include "frames.hpp" + +#include "rotationalinertia.hpp" + +namespace KDL { + + /** + * \brief 6D Inertia of a rigid body + * + * The inertia is defined in a certain reference point and a certain reference base. + * The reference point does not have to coincide with the origin of the reference frame. + */ + class RigidBodyInertia{ + public: + + /** + * This constructor creates a cartesian space inertia matrix, + * the arguments are the mass, the vector from the reference point to cog and the rotational inertia in the cog. + */ + explicit RigidBodyInertia(double m=0, const Vector& oc=Vector::Zero(), const RotationalInertia& Ic=RotationalInertia::Zero()); + + /** + * Creates an inertia with zero mass, and zero RotationalInertia + */ + static inline RigidBodyInertia Zero(){ + return RigidBodyInertia(0.0,Vector::Zero(),RotationalInertia::Zero()); + }; + + + ~RigidBodyInertia(){}; + + friend RigidBodyInertia operator*(double a,const RigidBodyInertia& I); + friend RigidBodyInertia operator+(const RigidBodyInertia& Ia,const RigidBodyInertia& Ib); + friend Wrench operator*(const RigidBodyInertia& I,const Twist& t); + friend RigidBodyInertia operator*(const Frame& T,const RigidBodyInertia& I); + friend RigidBodyInertia operator*(const Rotation& R,const RigidBodyInertia& I); + + /** + * Reference point change with v the vector from the old to + * the new point expressed in the current reference frame + */ + RigidBodyInertia RefPoint(const Vector& p); + + /** + * Get the mass of the rigid body + */ + double getMass() const{ + return m; + }; + + /** + * Get the spatial momentum of the rigid body + */ + const Vector& getSpatialMomentum() const + { + return h; + } + + /** + * Get the center of gravity of the rigid body + */ + Vector getCOG() const{ + if(m==0) return Vector::Zero(); + else return h/m; + }; + + /** + * Get the rotational inertia expressed in the reference frame (not the cog) + */ + RotationalInertia getRotationalInertia() const{ + return I; + }; + + private: + RigidBodyInertia(double m,const Vector& h,const RotationalInertia& I,bool mhi); + double m; + Vector h; + RotationalInertia I; + + friend class ArticulatedBodyInertia; + + }; + + /** + * Scalar product: I_new = double * I_old + */ + RigidBodyInertia operator*(double a,const RigidBodyInertia& I); + /** + * addition I: I_new = I_old1 + I_old2, make sure that I_old1 + * and I_old2 are expressed in the same reference frame/point, + * otherwise the result is worth nothing + */ + RigidBodyInertia operator+(const RigidBodyInertia& Ia,const RigidBodyInertia& Ib); + + /** + * calculate spatial momentum: h = I*v + * make sure that the twist v and the inertia are expressed in the same reference frame/point + */ + Wrench operator*(const RigidBodyInertia& I,const Twist& t); + + /** + * Coordinate system transform Ia = T_a_b*Ib with T_a_b the frame from a to b. + */ + RigidBodyInertia operator*(const Frame& T,const RigidBodyInertia& I); + /** + * Reference frame orientation change Ia = R_a_b*Ib with R_a_b + * the rotation of b expressed in a + */ + RigidBodyInertia operator*(const Rotation& R,const RigidBodyInertia& I); + + + +}//namespace + + +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/rotational_interpolation.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/rotational_interpolation.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74999ae9293fecee3197e7381ee2a93788b01230 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/rotational_interpolation.cpp @@ -0,0 +1,82 @@ +/*************************************************************************** + tag: Erwin Aertbelien Mon May 10 19:10:36 CEST 2004 rotational_interpolation.cxx + + rotational_interpolation.cxx - description + ------------------- + begin : Mon May 10 2004 + copyright : (C) 2004 Erwin Aertbelien + email : erwin.aertbelien@mech.kuleuven.ac.be + + *************************************************************************** + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; if not, write to the Free Software * + * Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307 USA * + * * + ***************************************************************************/ +/***************************************************************************** + * \author + * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven + * + * \version + * ORO_Geometry V0.2 + * + * \par History + * - $log$ + * + * \par Release + * $Id: rotational_interpolation.cpp,v 1.1.1.1.2.3 2003/02/24 13:13:06 psoetens Exp $ + * $Name: $ + ****************************************************************************/ + +#include "utilities/error.h" +#include "utilities/error_stack.h" +#include "rotational_interpolation.hpp" +#include "rotational_interpolation_sa.hpp" +#include +#include + +namespace KDL { + +RotationalInterpolation* RotationalInterpolation::Read(std::istream& is) { + IOTrace("RotationalInterpolation::Read"); + char storage[64]; + EatWord(is,"[",storage,sizeof(storage)); + Eat(is,'['); + if (strcmp(storage,"SINGLEAXIS")==0) { + IOTrace("SINGLEAXIS"); + EatEnd(is,']'); + IOTracePop(); + IOTracePop(); + return new RotationalInterpolation_SingleAxis(); + } else if (strcmp(storage,"THREEAXIS")==0) { + IOTrace("THREEAXIS"); + throw Error_Not_Implemented(); + EatEnd(is,']'); + IOTracePop(); + IOTracePop(); + return NULL; + } else if (strcmp(storage,"TWOAXIS")==0) { + IOTrace("TWOAXIS"); + throw Error_Not_Implemented(); + EatEnd(is,']'); + IOTracePop(); + IOTracePop(); + return NULL; + } else { + throw Error_MotionIO_Unexpected_Traj(); + } + return NULL; // just to avoid the warning; +} + +} diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/rotational_interpolation.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/rotational_interpolation.hpp new file mode 100644 index 0000000000000000000000000000000000000000..ced4914bb671c23dd92e02f0aed5b65ee25cb8e3 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/rotational_interpolation.hpp @@ -0,0 +1,118 @@ +/*************************************************************************** + tag: Erwin Aertbelien Mon Jan 10 16:38:38 CET 2005 rotational_interpolation.h + + rotational_interpolation.h - description + ------------------- + begin : Mon January 10 2005 + copyright : (C) 2005 Erwin Aertbelien + email : erwin.aertbelien@mech.kuleuven.ac.be + + *************************************************************************** + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; if not, write to the Free Software * + * Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307 USA * + * * + ***************************************************************************/ + + + /***************************************************************************** + * \author + * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven + * + * \version + * ORO_Geometry V0.2 + * + * \par History + * - $log$ + * + * \par Release + * $Id: rotational_interpolation.h,v 1.1.1.1.2.2 2003/02/24 13:13:06 psoetens Exp $ + * $Name: $ + ****************************************************************************/ + +#ifndef KDL_ROTATIONALINTERPOLATION_H +#define KDL_ROTATIONALINTERPOLATION_H + +#include "frames.hpp" +#include "frames_io.hpp" + +namespace KDL { + +/** + * RotationalInterpolation specifies the rotational part of a geometric trajectory + * - The different derived objects specify different methods for interpolating + * rotations. + * - SetStartEnd should be called before + * using the other methods + * - The start and end position do NOT belong to the persistent state ! The owner of this + * object is responsible for setting these each time + * @ingroup Motion + */ +class RotationalInterpolation + { + public: + /** + * Set the start and end rotational_interpolation + */ + virtual void SetStartEnd(Rotation start,Rotation end) = 0; + + /** + * - Returns the angle value to move from start to end. + * This should have units radians, + * - With Single Axis interp corresponds to the angle rotation + * - With Three Axis interp corresponds to the slowest of the three + * rotations. + */ + virtual double Angle() = 0; + + /** + * Returns the rotation matrix at angle theta + */ + virtual Rotation Pos(double theta) const = 0; + + /** + * Returns the rotational velocity at angle theta and with + * derivative of theta == thetad + */ + virtual Vector Vel(double theta,double thetad) const = 0; + + /** + * Returns the rotational acceleration at angle theta and with + * derivative of theta == thetad, and 2nd derivative of theta == thdd + */ + virtual Vector Acc(double theta,double thetad,double thetadd) const = 0; + + /** + * Writes one of the derived objects to the stream + */ + virtual void Write(std::ostream& os) const = 0; + + /** + * Reads one of the derived objects from the stream and returns a pointer + * (factory method) + */ + static RotationalInterpolation* Read(std::istream& is); + + /** + * virtual constructor, construction by copying .. + */ + virtual RotationalInterpolation* Clone() const = 0; + + virtual ~RotationalInterpolation() {} + }; + +} + + +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/rotational_interpolation_sa.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/rotational_interpolation_sa.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6b5a56b5c10d0752084a782ff4332a08cdee0adb --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/rotational_interpolation_sa.cpp @@ -0,0 +1,88 @@ +/*************************************************************************** + tag: Erwin Aertbelien Mon May 10 19:10:36 CEST 2004 rotational_interpolation_sa.cxx + + rotational_interpolation_sa.cxx - description + ------------------- + begin : Mon May 10 2004 + copyright : (C) 2004 Erwin Aertbelien + email : erwin.aertbelien@mech.kuleuven.ac.be + + *************************************************************************** + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; if not, write to the Free Software * + * Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307 USA * + * * + ***************************************************************************/ +/***************************************************************************** + * \author + * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven + * + * \version + * ORO_Geometry V0.2 + * + * \par History + * - $log$ + * + * \par Release + * $Id: rotational_interpolation_singleaxis.cpp,v 1.1.1.1.2.2 2003/02/24 13:13:06 psoetens Exp $ + * $Name: $ + ****************************************************************************/ + + +#include "rotational_interpolation_sa.hpp" +#include "trajectory.hpp" + +namespace KDL { + + +RotationalInterpolation_SingleAxis::RotationalInterpolation_SingleAxis() + {} + +void RotationalInterpolation_SingleAxis::SetStartEnd(Rotation start,Rotation end) { + R_base_start = start; + R_base_end = end; + Rotation R_start_end = R_base_start.Inverse()*R_base_end; + angle = R_start_end.GetRotAngle(rot_start_end); +} + +Rotation RotationalInterpolation_SingleAxis::Pos(double theta) const { + return R_base_start* Rotation::Rot2(rot_start_end,theta); +} + +Vector RotationalInterpolation_SingleAxis::Vel(double theta,double thetad) const { + return R_base_start * ( rot_start_end*thetad ); +} + +Vector RotationalInterpolation_SingleAxis::Acc(double theta,double thetad,double thetadd) const { + return R_base_start * ( rot_start_end* thetadd); +} + +double RotationalInterpolation_SingleAxis::Angle() { + return angle; +} + +void RotationalInterpolation_SingleAxis::Write(std::ostream& os) const { + os << "SingleAxis[] " << std::endl; +} + +RotationalInterpolation_SingleAxis::~RotationalInterpolation_SingleAxis() { +} + + +RotationalInterpolation* RotationalInterpolation_SingleAxis::Clone() const { + return new RotationalInterpolation_SingleAxis(); +} + +} + diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/rotational_interpolation_sa.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/rotational_interpolation_sa.hpp new file mode 100644 index 0000000000000000000000000000000000000000..1845e9a142c7b0db8c804044fd1c3b9f5669ecce --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/rotational_interpolation_sa.hpp @@ -0,0 +1,84 @@ +/*************************************************************************** + tag: Erwin Aertbelien Mon Jan 10 16:38:39 CET 2005 rotational_interpolation_sa.h + + rotational_interpolation_sa.h - description + ------------------- + begin : Mon January 10 2005 + copyright : (C) 2005 Erwin Aertbelien + email : erwin.aertbelien@mech.kuleuven.ac.be + + *************************************************************************** + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; if not, write to the Free Software * + * Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307 USA * + * * + ***************************************************************************/ + + + /***************************************************************************** + * \author + * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven + * + * \version + * ORO_Geometry V0.2 + * + * \par History + * - $log$ + * + * \par Release + * $Id: rotational_interpolation_singleaxis.h,v 1.1.1.1.2.3 2003/07/24 13:26:15 psoetens Exp $ + * $Name: $ + ****************************************************************************/ + +#ifndef KDL_ROTATIONALINTERPOLATION_SINGLEAXIS_H +#define KDL_ROTATIONALINTERPOLATION_SINGLEAXIS_H + +#include "frames.hpp" +#include "frames_io.hpp" +#include "rotational_interpolation.hpp" + + +namespace KDL { + + + /** + * An interpolation algorithm which rotates a frame over the existing + * single rotation axis + * formed by start and end rotation. If more than one rotational axis + * exist, an arbitrary one will be chosen, therefore it is not recommended + * to try to interpolate a 180 degrees rotation. + * @ingroup Motion + */ +class RotationalInterpolation_SingleAxis: public RotationalInterpolation + { + Rotation R_base_start; + Rotation R_base_end; + Vector rot_start_end; + double angle; + public: + RotationalInterpolation_SingleAxis(); + virtual void SetStartEnd(Rotation start,Rotation end); + virtual double Angle(); + virtual Rotation Pos(double th) const; + virtual Vector Vel(double th,double thd) const; + virtual Vector Acc(double th,double thd,double thdd) const; + virtual void Write(std::ostream& os) const; + virtual RotationalInterpolation* Clone() const; + virtual ~RotationalInterpolation_SingleAxis(); + }; + +} + + +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/rotationalinertia.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/rotationalinertia.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f52c0ecc67da77ee13460706d23b4591bdadfa33 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/rotationalinertia.cpp @@ -0,0 +1,62 @@ +// Copyright (C) 2009 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#include "rotationalinertia.hpp" +#include + + +namespace KDL +{ + RotationalInertia::RotationalInertia(double Ixx,double Iyy,double Izz,double Ixy,double Ixz,double Iyz) + { + data[0]=Ixx; + data[1]=data[3]=Ixy; + data[2]=data[6]=Ixz; + data[4]=Iyy; + data[5]=data[7]=Iyz; + data[8]=Izz; + + } + + RotationalInertia::~RotationalInertia() + { + } + + Vector RotationalInertia::operator*(const Vector& omega) const { + // Complexity : 9M+6A + Vector result; + Eigen::Map(result.data) = Eigen::Map(this->data) * Eigen::Map(omega.data); + return result; + } + + RotationalInertia operator*(double a, const RotationalInertia& I){ + RotationalInertia result; + Eigen::Map(result.data) = a * Eigen::Map(I.data); + return result; + } + + RotationalInertia operator+(const RotationalInertia& Ia, const RotationalInertia& Ib){ + RotationalInertia result; + Eigen::Map(result.data) = Eigen::Map(Ia.data) + Eigen::Map(Ib.data); + return result; + } +} + diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/rotationalinertia.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/rotationalinertia.hpp new file mode 100644 index 0000000000000000000000000000000000000000..c3c21c48dd39dd7e945161eddd6a0be7fd895dd8 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/rotationalinertia.hpp @@ -0,0 +1,74 @@ +// Copyright (C) 2009 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#ifndef KDL_ROTATIONALINERTIA_HPP +#define KDL_ROTATIONALINERTIA_HPP + +#include "frames.hpp" + +//------- class for only the Rotational Inertia -------- + +namespace KDL +{ + //Forward declaration + class RigidBodyInertia; + + class RotationalInertia{ + public: + + explicit RotationalInertia(double Ixx=0,double Iyy=0,double Izz=0,double Ixy=0,double Ixz=0,double Iyz=0); + + static inline RotationalInertia Zero(){ + return RotationalInertia(0,0,0,0,0,0); + }; + + friend RotationalInertia operator*(double a, const RotationalInertia& I); + friend RotationalInertia operator+(const RotationalInertia& Ia, const RotationalInertia& Ib); + + /** + * This function calculates the angular momentum resulting from a rotational velocity omega + */ + KDL::Vector operator*(const KDL::Vector& omega) const; + + ~RotationalInertia(); + + friend class RigidBodyInertia; + ///Scalar product + friend RigidBodyInertia operator*(double a,const RigidBodyInertia& I); + ///addition + friend RigidBodyInertia operator+(const RigidBodyInertia& Ia,const RigidBodyInertia& Ib); + ///calculate spatial momentum + friend Wrench operator*(const RigidBodyInertia& I,const Twist& t); + ///coordinate system transform Ia = T_a_b*Ib with T_a_b the frame from a to b + friend RigidBodyInertia operator*(const Frame& T,const RigidBodyInertia& I); + ///base frame orientation change Ia = R_a_b*Ib with R_a_b the rotation for frame from a to b + friend RigidBodyInertia operator*(const Rotation& R,const RigidBodyInertia& I); + + double data[9]; + }; + + RotationalInertia operator*(double a, const RotationalInertia& I); + RotationalInertia operator+(const RotationalInertia& Ia, const RotationalInertia& Ib); + +} + +#endif + diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/segment.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/segment.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7c7573304b39cfe3ee9222608a213ce050f51ff3 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/segment.cpp @@ -0,0 +1,73 @@ +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#include "segment.hpp" + +namespace KDL { + + Segment::Segment(const std::string& _name, const Joint& _joint, const Frame& _f_tip, const RigidBodyInertia& _I): + name(_name), + joint(_joint),I(_I), + f_tip(_joint.pose(0).Inverse() * _f_tip) + { + } + + Segment::Segment(const Joint& _joint, const Frame& _f_tip, const RigidBodyInertia& _I): + name("NoName"), + joint(_joint),I(_I), + f_tip(_joint.pose(0).Inverse() * _f_tip) + { + } + + Segment::Segment(const Segment& in): + name(in.name),joint(in.joint),I(in.I), + f_tip(in.f_tip) + { + } + + Segment& Segment::operator=(const Segment& arg) + { + name=arg.name; + joint=arg.joint; + I=arg.I; + f_tip=arg.f_tip; + return *this; + } + + Segment::~Segment() + { + } + + Frame Segment::pose(const double& q)const + { + return joint.pose(q)*f_tip; + } + + Twist Segment::twist(const double& q, const double& qdot)const + { + return joint.twist(qdot).RefPoint(joint.pose(q).M * f_tip.p); + } + + void Segment::setFrameToTip(const Frame& f_tip_new) + { + f_tip = joint.pose(0).Inverse() * f_tip_new; + } + +}//end of namespace KDL + diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/segment.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/segment.hpp new file mode 100644 index 0000000000000000000000000000000000000000..8000763612f3e66ca9c7fd4ab01fb9ee44b2278c --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/segment.hpp @@ -0,0 +1,177 @@ +// Copyright (C) 2007 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + +#ifndef KDL_SEGMENT_HPP +#define KDL_SEGMENT_HPP + +#include "frames.hpp" +#include "rigidbodyinertia.hpp" +#include "joint.hpp" +#include + +namespace KDL { + + /** + * \brief This class encapsulates a simple segment, that is a "rigid + * body" (i.e., a frame and a rigid body inertia) with a joint and with + * "handles", root and tip to connect to other segments. + * + * A simple segment is described by the following properties : + * - Joint + * - Rigid Body Inertia: of the rigid body part of the Segment + * - Offset from the end of the joint to the tip of the segment: + * the joint is located at the root of the segment. + * + * @ingroup KinematicFamily + */ + class Segment { + friend class Chain; + private: + std::string name; + Joint joint; + RigidBodyInertia I; + Frame f_tip; + + public: + /** + * Constructor of the segment + * + * @param name name of the segment + * @param joint joint of the segment, default: + * Joint(Joint::Fixed) + * @param f_tip frame from the end of the joint to the tip of + * the segment, default: Frame::Identity() + * @param M rigid body inertia of the segment, default: Inertia::Zero() + */ + explicit Segment(const std::string& name, const Joint& joint=Joint(Joint::Fixed), const Frame& f_tip=Frame::Identity(),const RigidBodyInertia& I = RigidBodyInertia::Zero()); + /** + * Constructor of the segment + * + * @param joint joint of the segment, default: + * Joint(Joint::Fixed) + * @param f_tip frame from the end of the joint to the tip of + * the segment, default: Frame::Identity() + * @param M rigid body inertia of the segment, default: Inertia::Zero() + */ + explicit Segment(const Joint& joint=Joint(Joint::Fixed), const Frame& f_tip=Frame::Identity(),const RigidBodyInertia& I = RigidBodyInertia::Zero()); + Segment(const Segment& in); + Segment& operator=(const Segment& arg); + + virtual ~Segment(); + + /** + * Request the pose of the segment, given the joint position q. + * + * @param q 1D position of the joint + * + * @return pose from the root to the tip of the segment + */ + Frame pose(const double& q)const; + /** + * Request the 6D-velocity of the tip of the segment, given + * the joint position q and the joint velocity qdot. + * + * @param q 1D position of the joint + * @param qdot 1D velocity of the joint + * + * @return 6D-velocity of the tip of the segment, expressed + *in the base-frame of the segment(root) and with the tip of + *the segment as reference point. + */ + Twist twist(const double& q,const double& qdot)const; + + /** + * Request the name of the segment + * + * + * @return const reference to the name of the segment + */ + const std::string& getName()const + { + return name; + } + /** + * Request the joint of the segment + * + * + * @return const reference to the joint of the segment + */ + const Joint& getJoint()const + { + return joint; + } + /** + * Request the inertia of the segment + * + * + * @return const reference to the inertia of the segment + */ + const RigidBodyInertia& getInertia()const + { + return I; + } + /** + * Request the inertia of the segment + * + * + * @return const reference to the inertia of the segment + */ + void setInertia(const RigidBodyInertia& Iin) + { + this->I=Iin; + } + + /** + * Request the pose from the joint end to the tip of the + *segment. + * + * @return the original parent end - segment end pose. + */ + Frame getFrameToTip()const + { + + return joint.pose(0)*f_tip; + } + + /** + * Set the pose from the joint end to the tip of the + * segment. + * + * @param f_tip_new pose from the joint end to the tip of the segment + */ + void setFrameToTip(const Frame& f_tip_new); + + /** + * Request the pose from the end of the joint to the tip of the segment + * at joint position 0. + * + * @return const reference to the pose from the end of the joint to the tip of the segment + * at joint position 0 + */ + const Frame& getFrameToTipZero() const + { + return f_tip; + } + }; +}//end of namespace KDL + +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/solveri.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/solveri.hpp new file mode 100644 index 0000000000000000000000000000000000000000..2f2457bdf71b251861d6fce373a57ff67f52d244 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/solveri.hpp @@ -0,0 +1,154 @@ +// Copyright (C) 2013 Stephen Roderick + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#ifndef __SOLVERI_HPP +#define __SOLVERI_HPP + +namespace KDL { + +/** + * Solver interface supporting storage and description of the latest error. + * + * Error codes: Zero (0) indicates no error, positive error codes indicate more + * of a warning (e.g. a degraded solution, but motion can continue), and + * negative error codes indicate failure (e.g. a singularity, and motion + * can not continue). + * + * Error codes between -99 and +99 (inclusive) are reserved for system-wide + * error codes. Derived classes should use values > +100, and < -100. + * + * Example use + * + * \code + * class MySolver : public SolverI + * { + * public: + * static const int E_CHILDFAILD = xxx; + * + * MySolver(SomeOtherSolver& other); + * virtual ~MySolver(); + * int CartToJnt(...); + * virtual const char* strError(const int error) const; + * protected: + * SomeOtherSolver& child; + * }; + * + * ... + * + * int MySolver::CartToJnt(...) + * { + * error = child->SomeCall(); + * if (E_NOERROR != error) { + * error = E_CHILDFAILED; + * } else { + * ... + * } + * return error; + * } + * + * const char* MySolver::strError(const int error) const + * { + * if (E_CHILDFAILED == error) return "Child solver failed"; + * else return SolverI::strError(error); + * } + * + * void someFunc() + * { + * SomeOtherSolver child = new SomeOtherSolver(...); + * MySolver parent = new MySolver(child); + * ... + * int rc = parent->CartToJnt(...); + * if (E_NOERROR != rc) { + * if (MySolver::E_CHILDFAILED == rc) { + * rc = child->getError(); + * // cope with child failure 'rc' + * } + * } + * ... + * } + * \endcode + */ +class SolverI +{ +public: + enum { + /// Converged but degraded solution (e.g. WDLS with psuedo-inverse singular) + E_DEGRADED = +1, + //! No error + E_NOERROR = 0, + //! Failed to converge + E_NO_CONVERGE = -1, + //! Undefined value (e.g. computed a NAN, or tan(90 degrees) ) + E_UNDEFINED = -2, + //! Chain size changed + E_NOT_UP_TO_DATE = -3, + //! Input size does not match internal state + E_SIZE_MISMATCH = -4, + //! Maximum number of iterations exceeded + E_MAX_ITERATIONS_EXCEEDED = -5, + //! Requested index out of range + E_OUT_OF_RANGE = -6, + //! Not yet implemented + E_NOT_IMPLEMENTED = -7, + //! Internal svd calculation failed + E_SVD_FAILED = -8 + }; + + /// Initialize latest error to E_NOERROR + SolverI() : + error(E_NOERROR) + {} + + virtual ~SolverI() + {} + + /// Return the latest error + virtual int getError() const { return error; } + + /** Return a description of the latest error + \return if \a error is known then a description of \a error, otherwise + "UNKNOWN ERROR" + */ + virtual const char* strError(const int error) const + { + if (E_NOERROR == error) return "No error"; + else if (E_NO_CONVERGE == error) return "Failed to converge"; + else if (E_UNDEFINED == error) return "Undefined value"; + else if (E_DEGRADED == error) return "Converged but degraded solution"; + else if (E_NOT_UP_TO_DATE == error) return "Internal data structures not up to date with Chain"; + else if (E_SIZE_MISMATCH == error) return "The size of the input does not match the internal state"; + else if (E_MAX_ITERATIONS_EXCEEDED == error) return "The maximum number of iterations is exceeded"; + else if (E_OUT_OF_RANGE == error) return "The requested index is out of range"; + else if (E_NOT_IMPLEMENTED == error) return "The requested function is not yet implemented"; + else if (E_SVD_FAILED == error) return "SVD failed"; + else return "UNKNOWN ERROR"; + } + + /** + * Update the internal data structures. This is required if the number + * of segments or number of joints of a chain/tree have changed. + * This provides a single point of contact for solver memory allocations. + */ + virtual void updateInternalDataStructures() = 0; + +protected: + /// Latest error, initialized to E_NOERROR in constructor + int error; +}; + +} // namespaces + +#endif diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/stiffness.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/stiffness.hpp new file mode 100644 index 0000000000000000000000000000000000000000..907d86d3419fa8e40544362fcb5566dab915aff1 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/stiffness.hpp @@ -0,0 +1,120 @@ +// Copyright (C) 2007 Ruben Smits + +// Version: 1.0 +// Author: Ruben Smits +// Maintainer: Ruben Smits +// URL: http://www.orocos.org/kdl + +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. + +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +#ifndef KDL_STIFFNESS_H +#define KDL_STIFFNESS_H +#include "frames.hpp" + + +namespace KDL { +/** + * Preliminary class to implement Stiffness, only diagonal stiffness is implemented + * no transformations provided... + * + * Implements a diagonal stiffness matrix. + * first 3 elements are stiffness for translations + * last 3 elements are stiffness for rotations. + */ +class Stiffness { + double data[6]; +public: + Stiffness() { + data[0]=0; + data[1]=0; + data[2]=0; + data[3]=0; + data[4]=0; + data[5]=0; + } + Stiffness(double* d) { + data[0]=d[0]; + data[1]=d[1]; + data[2]=d[2]; + data[3]=d[3]; + data[4]=d[4]; + data[5]=d[5]; + } + Stiffness(double x,double y,double z,double rx,double ry,double rz) { + data[0]=x; + data[1]=y; + data[2]=z; + data[3]=rx; + data[4]=ry; + data[5]=rz; + } + double& operator[](int i) { + return data[i]; + } + double operator[](int i) const { + return data[i]; + } + Twist Inverse(const Wrench& w) const{ + Twist t; + t[0]=w[0]/data[0]; + t[1]=w[1]/data[1]; + t[2]=w[2]/data[2]; + t[3]=w[3]/data[3]; + t[4]=w[4]/data[4]; + t[5]=w[5]/data[5]; + return t; + } +}; + +inline Wrench operator * (const Stiffness& s, const Twist& t) { + Wrench w; + w[0]=s[0]*t[0]; + w[1]=s[1]*t[1]; + w[2]=s[2]*t[2]; + w[3]=s[3]*t[3]; + w[4]=s[4]*t[4]; + w[5]=s[5]*t[5]; + return w; +} + +inline Stiffness operator+(const Stiffness& s1, const Stiffness& s2) { + Stiffness s; + s[0]=s1[0]+s2[0]; + s[1]=s1[1]+s2[1]; + s[2]=s1[2]+s2[2]; + s[3]=s1[3]+s2[3]; + s[4]=s1[4]+s2[4]; + s[5]=s1[5]+s2[5]; + return s; +} +inline void posrandom(Stiffness& F) { + posrandom(F[0]); + posrandom(F[1]); + posrandom(F[2]); + posrandom(F[3]); + posrandom(F[4]); + posrandom(F[5]); +} + +inline void random(Stiffness& F) { + posrandom(F); +} + + +} +#endif + + + diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/trajectory.cpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/trajectory.cpp new file mode 100644 index 0000000000000000000000000000000000000000..914af5f635e819be30875e582d9f74287d5fabff --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/trajectory.cpp @@ -0,0 +1,75 @@ +/*************************************************************************** + tag: Erwin Aertbelien Mon May 10 19:10:36 CEST 2004 trajectory.cxx + + trajectory.cxx - description + ------------------- + begin : Mon May 10 2004 + copyright : (C) 2004 Erwin Aertbelien + email : erwin.aertbelien@mech.kuleuven.ac.be + + *************************************************************************** + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; if not, write to the Free Software * + * Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307 USA * + * * + ***************************************************************************/ +/***************************************************************************** + * \author + * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven + * + * \version + * ORO_Geometry V0.2 + * + * \par History + * - $log$ + * + * \par Release + * $Id: trajectory.cpp,v 1.1.1.1.2.4 2003/07/18 14:58:36 psoetens Exp $ + * $Name: $ + ****************************************************************************/ + + +#include "utilities/error.h" +#include "utilities/error_stack.h" +#include "utilities/scoped_ptr.hpp" +#include "trajectory.hpp" +#include "path.hpp" +#include "trajectory_segment.hpp" + +#include +#include + + +namespace KDL { + +Trajectory* Trajectory::Read(std::istream& is) { + IOTrace("Trajectory::Read"); + char storage[64]; + EatWord(is,"[",storage,sizeof(storage)); + Eat(is,'['); + if (strcmp(storage,"SEGMENT")==0) { + IOTrace("SEGMENT"); + scoped_ptr geom( Path::Read(is) ); + scoped_ptr motprof( VelocityProfile::Read(is) ); + EatEnd(is,']'); + IOTracePop(); + IOTracePop(); + return new Trajectory_Segment(geom.release(),motprof.release()); + } else { + throw Error_MotionIO_Unexpected_Traj(); + } + return NULL; // just to avoid the warning; +} + +} diff --git a/third_party/orocos_kinematics_dynamics/orocos_kdl/src/trajectory.hpp b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/trajectory.hpp new file mode 100644 index 0000000000000000000000000000000000000000..b851cda3ba224b5945e391839f6fdc0c7614f5f7 --- /dev/null +++ b/third_party/orocos_kinematics_dynamics/orocos_kdl/src/trajectory.hpp @@ -0,0 +1,105 @@ +/*************************************************************************** + tag: Erwin Aertbelien Mon Jan 10 16:38:39 CET 2005 trajectory.h + + trajectory.h - description + ------------------- + begin : Mon January 10 2005 + copyright : (C) 2005 Erwin Aertbelien + email : erwin.aertbelien@mech.kuleuven.ac.be + + *************************************************************************** + * This library is free software; you can redistribute it and/or * + * modify it under the terms of the GNU Lesser General Public * + * License as published by the Free Software Foundation; either * + * version 2.1 of the License, or (at your option) any later version. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this library; if not, write to the Free Software * + * Foundation, Inc., 59 Temple Place, * + * Suite 330, Boston, MA 02111-1307 USA * + * * + ***************************************************************************/ + + +/***************************************************************************** + * \author + * Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven + * + * \version + * ORO_Geometry V0.2 + * + * \par History + * - $log$ + * + * \par Release + * $Id: trajectory.h,v 1.1.1.1.2.5 2003/07/23 16:44:25 psoetens Exp $ + * $Name: $ + * \todo + * Peter's remark : should separate I/O from other routines in the + * motion/chain directories + * The problem is that the I/O uses virtual inheritance to write + * the trajectories/geometries/velocityprofiles/... + * Have no good solution for this, perhaps + * * #ifdef's + * * declaring dummy ostream/istream and change implementation file .cpp + * * declaring some sort of VISITOR object (containing an ostream) , + * the classes contain code to pass this object around along its children + * a subroutine can then be called with overloading. + * PROBLEM : if you declare a friend you have to fully declare it ==> exposing I/O with ostream/istream decl + * CONSEQUENCE : everything has to be declared public. + ****************************************************************************/ + +#ifndef TRAJECTORY_H +#define TRAJECTORY_H + +#include "frames.hpp" +#include "frames_io.hpp" +#include "path.hpp" +#include "velocityprofile.hpp" + + + +namespace KDL { + + + + + /** + * An abstract class that implements + * a trajectory contains a cartesian space trajectory and an underlying + * velocity profile. + * @ingroup Motion + */ + class Trajectory + { + public: + virtual double Duration() const = 0; + // The duration of the trajectory + + virtual Frame Pos(double time) const = 0; + // Position of the trajectory at