| 1 | #cxx . . $ `<file>/orocos_kdl/src/chainiksolverpos_lma.hpp`/ifndef KDL_CHAINIKSOLVERPOS_GN_HPP |
| 2 | #define KDL_CHAINIKSOLVERPOS_GN_HPPcxx . . $ `orocos_kdl/src/chainiksolverpos_lma.hpp:2:9`! |
| 3 | /** |
| 4 | \file chainiksolverpos_lma.hpp |
| 5 | \brief computing inverse position kinematics using Levenberg-Marquardt. |
| 6 | */ |
| 7 | |
| 8 | /************************************************************************** |
| 9 | begin : May 2012 |
| 10 | copyright : (C) 2012 Erwin Aertbelien |
| 11 | email : [email protected] |
| 12 | |
| 13 | History (only major changes)( AUTHOR-Description ) : |
| 14 | |
| 15 | *************************************************************************** |
| 16 | * This library is free software; you can redistribute it and/or * |
| 17 | * modify it under the terms of the GNU Lesser General Public * |
| 18 | * License as published by the Free Software Foundation; either * |
| 19 | * version 2.1 of the License, or (at your option) any later version. * |
| 20 | * * |
| 21 | * This library is distributed in the hope that it will be useful, * |
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 24 | * Lesser General Public License for more details. * |
| 25 | * * |
| 26 | * You should have received a copy of the GNU Lesser General Public * |
| 27 | * License along with this library; if not, write to the Free Software * |
| 28 | * Foundation, Inc., 59 Temple Place, * |
| 29 | * Suite 330, Boston, MA 02111-1307 USA * |
| 30 | * * |
| 31 | ***************************************************************************/ |
| 32 | |
| 33 | |
| 34 | #include "chainiksolver.hpp"cxx . . $ `<file>/orocos_kdl/src/chainiksolver.hpp`/ |
| 35 | #include "chain.hpp"cxx . . $ `<file>/orocos_kdl/src/chain.hpp`/ |
| 36 | #include <Eigen/Dense> |
| 37 | |
| 38 | namespace KDLcxx . . $ KDL/ |
| 39 | { |
| 40 | |
| 41 | /** |
| 42 | * \brief Solver for the inverse position kinematics that uses Levenberg-Marquardt. |
| 43 | * |
| 44 | * The robustness and speed of this solver is improved in several ways: |
| 45 | * - by using a Levenberg-Marquardt method that automatically adapts the damping when |
| 46 | * computing the inverse damped least squares inverse velocity kinematics. |
| 47 | * - by using an internal implementation of forward position kinematics and the |
| 48 | * Jacobian kinematics. This implementation is more numerically robust, |
| 49 | * is able to cache previous computations, and implements an \f$ \mathcal{O}(N) \f$ |
| 50 | * algorithm for the computation of the Jacobian (with \f$N\f$, the number of joints, and for |
| 51 | * a fixed size task space). |
| 52 | * - by providing a way to specify the weights in task space, you can weigh rotations wrt translations. |
| 53 | * This is important e.g. to specify that rotations do not matter for the problem at hand, or to |
| 54 | * specify how important you judge rotations w.r.t. translations, typically in S.I.-units, ([m],[rad]), |
| 55 | * the rotations are over-specified, this can be avoided using the weight matrix. <B>Weights also |
| 56 | * make the solver more robust </B>. |
| 57 | * - only the constructors call <B>memory allocation</B>. |
| 58 | * |
| 59 | * De general principles behind the optimisation is inspired on: |
| 60 | * Jorge Nocedal, Stephen J. Wright, Numerical Optimization,Springer-Verlag New York, 1999. |
| 61 | |
| 62 | * \ingroup KinematicFamily |
| 63 | */ |
| 64 | class ChainIkSolverPos_LMAcxx . . $ KDL/ChainIkSolverPos_LMA# : public KDLcxx . . $ KDL/::ChainIkSolverPoscxx . . $ KDL/ChainIkSolverPos# |
| 65 | { |
| 66 | private: |
| 67 | typedef double ScalarTypecxx . . $ KDL/ChainIkSolverPos_LMA#ScalarType#; |
| 68 | typedef Eigencxx . . $ Eigen/::Matrixcxx . . $ Eigen/Matrix#<ScalarTypecxx . . $ KDL/ChainIkSolverPos_LMA#ScalarType#,Eigencxx . . $ Eigen/::Dynamiccxx . . $ Eigen/Dynamic.,Eigencxx . . $ Eigen/::Dynamiccxx . . $ Eigen/Dynamic.> MatrixXqcxx . . $ KDL/ChainIkSolverPos_LMA#MatrixXq#; |
| 69 | typedef Eigencxx . . $ Eigen/::Matrixcxx . . $ Eigen/Matrix#<ScalarTypecxx . . $ KDL/ChainIkSolverPos_LMA#ScalarType#,Eigencxx . . $ Eigen/::Dynamiccxx . . $ Eigen/Dynamic.,1> VectorXqcxx . . $ KDL/ChainIkSolverPos_LMA#VectorXq#; |
| 70 | public: |
| 71 | |
| 72 | static const int E_GRADIENT_JOINTS_TOO_SMALLcxx . . $ KDL/ChainIkSolverPos_LMA#E_GRADIENT_JOINTS_TOO_SMALL. = -100; |
| 73 | static const int E_INCREMENT_JOINTS_TOO_SMALLcxx . . $ KDL/ChainIkSolverPos_LMA#E_INCREMENT_JOINTS_TOO_SMALL. = -101; |
| 74 | |
| 75 | /** |
| 76 | * \brief constructs an ChainIkSolverPos_LMA solver. |
| 77 | * |
| 78 | * The default parameters are chosen to be applicable to industrial-size robots |
| 79 | * (e.g. 0.5 to 3 meters range in task space), with an accuracy that is more then |
| 80 | * sufficient for typical industrial applications. |
| 81 | * |
| 82 | * Weights are applied in task space, i.e. the kinematic solver minimizes: |
| 83 | * \f$ E = \Delta \mathbf{x}^T \mathbf{L} \mathbf{L}^T \Delta \mathbf{x} \f$, with \f$\mathbf{L}\f$ a diagonal matrix. |
| 84 | * |
| 85 | * \param _chain specifies the kinematic chain. |
| 86 | * \param _l specifies the "square root" of the weight (diagonal) matrix in task space. This diagonal matrix is specified as a vector. |
| 87 | * \param _eps specifies the desired accuracy in task space; <B>after</B> weighing with |
| 88 | * the weight matrix, it is applied on \f$E\f$. |
| 89 | * \param _maxiter specifies the maximum number of iterations. |
| 90 | * \param _eps_joints specifies that the algorithm has to stop when the computed joint angle increments are |
| 91 | * smaller then _eps_joints. This is to avoid unnecessary computations up to _maxiter when the joint angle |
| 92 | * increments are so small that they effectively (in floating point) do not change the joint angles any more. The default |
| 93 | * is a few digits above numerical accuracy. |
| 94 | */ |
| 95 | ChainIkSolverPos_LMAcxx . . $ KDL/ChainIkSolverPos_LMA#ChainIkSolverPos_LMA(10323cf10742c179).( |
| 96 | const KDLcxx . . $ KDL/::Chaincxx . . $ KDL/Chain#& _chain, |
| 97 | const Eigencxx . . $ Eigen/::Matrixcxx . . $ Eigen/Matrix#<double,6,1>& _l, |
| 98 | double _eps=1E-5, |
| 99 | int _maxiter=500, |
| 100 | double _eps_joints=1E-15 |
| 101 | ); |
| 102 | |
| 103 | /** |
| 104 | * \brief identical the full constructor for ChainIkSolverPos_LMA, but provides for a default weight matrix. |
| 105 | * |
| 106 | * \f$\mathbf{L} = \mathrm{diag}\left( \begin{bmatrix} 1 & 1 & 1 & 0.01 & 0.01 & 0.01 \end{bmatrix} \right) \f$. |
| 107 | */ |
| 108 | ChainIkSolverPos_LMAcxx . . $ KDL/ChainIkSolverPos_LMA#ChainIkSolverPos_LMA(deca3c62f68dd354).( |
| 109 | const KDLcxx . . $ KDL/::Chaincxx . . $ KDL/Chain#& _chain, |
| 110 | double _eps=1E-5, |
| 111 | int _maxiter=500, |
| 112 | double _eps_joints=1E-15 |
| 113 | ); |
| 114 | |
| 115 | /** |
| 116 | * \brief computes the inverse position kinematics. |
| 117 | * |
| 118 | * \param q_init initial joint position. |
| 119 | * \param T_base_goal goal position expressed with respect to the robot base. |
| 120 | * \param q_out joint position that achieves the specified goal position (if successful). |
| 121 | * \return E_NOERROR if successful, |
| 122 | * E_GRADIENT_JOINTS_TOO_SMALL the gradient of \f$ E \f$ towards the joints is to small, |
| 123 | * E_INCREMENT_JOINTS_TOO_SMALL if joint position increments are to small, |
| 124 | * E_MAX_ITER_EXCEEDED if number of iterations is exceeded. |
| 125 | */ |
| 126 | virtual int CartToJntcxx . . $ KDL/ChainIkSolverPos_LMA#CartToJnt(bf7678dc1356c18d).(const KDLcxx . . $ KDL/::JntArraycxx . . $ KDL/JntArray#& q_init, const KDLcxx . . $ KDL/::Framecxx . . $ KDL/Frame#& T_base_goal, KDLcxx . . $ KDL/::JntArraycxx . . $ KDL/JntArray#& q_out); |
| 127 | |
| 128 | /** |
| 129 | * \brief destructor. |
| 130 | */ |
| 131 | virtual ~cxx . . $ KDL/ChainIkSolverPos_LMA#`~ChainIkSolverPos_LMA`(ced63f7c635d850d).ChainIkSolverPos_LMAcxx . . $ KDL/ChainIkSolverPos_LMA#(); |
| 132 | |
| 133 | /** |
| 134 | * \brief for internal use only. |
| 135 | * |
| 136 | * Only exposed for test and diagnostic purposes. |
| 137 | */ |
| 138 | void compute_fwdposcxx . . $ KDL/ChainIkSolverPos_LMA#compute_fwdpos(a6d9049bb3d94e99).(const VectorXqcxx . . $ KDL/ChainIkSolverPos_LMA#VectorXq#& q); |
| 139 | |
| 140 | /** |
| 141 | * \brief for internal use only. |
| 142 | * Only exposed for test and diagnostic purposes. |
| 143 | * compute_fwdpos(q) should always have been called before. |
| 144 | */ |
| 145 | void compute_jacobiancxx . . $ KDL/ChainIkSolverPos_LMA#compute_jacobian(a6d9049bb3d94e99).(const VectorXqcxx . . $ KDL/ChainIkSolverPos_LMA#VectorXq#& q); |
| 146 | |
| 147 | /** |
| 148 | * \brief for internal use only. |
| 149 | * Only exposed for test and diagnostic purposes. |
| 150 | */ |
| 151 | void display_jaccxx . . $ KDL/ChainIkSolverPos_LMA#display_jac(1132327219278043).(const KDLcxx . . $ KDL/::JntArraycxx . . $ KDL/JntArray#& jval); |
| 152 | |
| 153 | /// @copydoc KDL::SolverI::updateInternalDataStructures |
| 154 | void updateInternalDataStructurescxx . . $ KDL/ChainIkSolverPos_LMA#updateInternalDataStructures(49f6e7a06ebc5aa8).(); |
| 155 | |
| 156 | /// @copydoc KDL::SolverI::strError() |
| 157 | virtual const char* strErrorcxx . . $ KDL/ChainIkSolverPos_LMA#strError(afbf07f2bc1a9437).(const int error) const; |
| 158 | |
| 159 | private: |
| 160 | const KDLcxx . . $ KDL/::Chaincxx . . $ KDL/Chain#& chaincxx . . $ KDL/ChainIkSolverPos_LMA#chain.; |
| 161 | unsigned int njcxx . . $ KDL/ChainIkSolverPos_LMA#nj.; |
| 162 | unsigned int nscxx . . $ KDL/ChainIkSolverPos_LMA#ns.; |
| 163 | |
| 164 | public: |
| 165 | |
| 166 | |
| 167 | /** |
| 168 | * \brief contains the last number of iterations for an execution of CartToJnt. |
| 169 | */ |
| 170 | int lastNrOfItercxx . . $ KDL/ChainIkSolverPos_LMA#lastNrOfIter.; |
| 171 | |
| 172 | /** |
| 173 | * \brief contains the last value for \f$ E \f$ after an execution of CartToJnt. |
| 174 | */ |
| 175 | double lastDifferencecxx . . $ KDL/ChainIkSolverPos_LMA#lastDifference.; |
| 176 | |
| 177 | /** |
| 178 | * \brief contains the last value for the (unweighted) translational difference after an execution of CartToJnt. |
| 179 | */ |
| 180 | double lastTransDiffcxx . . $ KDL/ChainIkSolverPos_LMA#lastTransDiff.; |
| 181 | |
| 182 | /** |
| 183 | * \brief contains the last value for the (unweighted) rotational difference after an execution of CartToJnt. |
| 184 | */ |
| 185 | double lastRotDiffcxx . . $ KDL/ChainIkSolverPos_LMA#lastRotDiff.; |
| 186 | |
| 187 | /** |
| 188 | * \brief contains the last values for the singular values of the weighted Jacobian after an execution of CartToJnt. |
| 189 | */ |
| 190 | VectorXqcxx . . $ KDL/ChainIkSolverPos_LMA#VectorXq# lastSVcxx . . $ KDL/ChainIkSolverPos_LMA#lastSV.; |
| 191 | |
| 192 | /** |
| 193 | * \brief for internal use only. |
| 194 | * |
| 195 | * contains the last value for the Jacobian after an execution of compute_jacobian. |
| 196 | */ |
| 197 | MatrixXqcxx . . $ KDL/ChainIkSolverPos_LMA#MatrixXq# jaccxx . . $ KDL/ChainIkSolverPos_LMA#jac.; |
| 198 | |
| 199 | /** |
| 200 | * \brief for internal use only. |
| 201 | * |
| 202 | * contains the gradient of the error criterion after an execution of CartToJnt. |
| 203 | */ |
| 204 | VectorXqcxx . . $ KDL/ChainIkSolverPos_LMA#VectorXq# gradcxx . . $ KDL/ChainIkSolverPos_LMA#grad.; |
| 205 | /** |
| 206 | * \brief for internal use only. |
| 207 | * |
| 208 | * 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. |
| 209 | */ |
| 210 | KDLcxx . . $ KDL/::Framecxx . . $ KDL/Frame# T_base_headcxx . . $ KDL/ChainIkSolverPos_LMA#T_base_head.; |
| 211 | |
| 212 | /** |
| 213 | * \brief display information on each iteration step to the console. |
| 214 | */ |
| 215 | bool display_informationcxx . . $ KDL/ChainIkSolverPos_LMA#display_information.; |
| 216 | private: |
| 217 | // additional specification of the inverse position kinematics problem: |
| 218 | unsigned int maxitercxx . . $ KDL/ChainIkSolverPos_LMA#maxiter.; |
| 219 | double epscxx . . $ KDL/ChainIkSolverPos_LMA#eps.; |
| 220 | double eps_jointscxx . . $ KDL/ChainIkSolverPos_LMA#eps_joints.; |
| 221 | Eigencxx . . $ Eigen/::Matrixcxx . . $ Eigen/Matrix#<ScalarTypecxx . . $ KDL/ChainIkSolverPos_LMA#ScalarType#,6,1> Lcxx . . $ KDL/ChainIkSolverPos_LMA#L.; |
| 222 | |
| 223 | |
| 224 | |
| 225 | // state of compute_fwdpos and compute_jacobian: |
| 226 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<KDLcxx . . $ KDL/::Framecxx . . $ KDL/Frame#> T_base_jointrootcxx . . $ KDL/ChainIkSolverPos_LMA#T_base_jointroot.; |
| 227 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<KDLcxx . . $ KDL/::Framecxx . . $ KDL/Frame#> T_base_jointtipcxx . . $ KDL/ChainIkSolverPos_LMA#T_base_jointtip.; |
| 228 | // need 2 vectors because of the somewhat strange definition of segment.hpp |
| 229 | // you could also recompute jointtip out of jointroot, |
| 230 | // but then you'll need more expensive cos/sin functions. |
| 231 | |
| 232 | |
| 233 | // the following are state of CartToJnt that is pre-allocated: |
| 234 | |
| 235 | VectorXqcxx . . $ KDL/ChainIkSolverPos_LMA#VectorXq# qcxx . . $ KDL/ChainIkSolverPos_LMA#q.; |
| 236 | MatrixXqcxx . . $ KDL/ChainIkSolverPos_LMA#MatrixXq# Acxx . . $ KDL/ChainIkSolverPos_LMA#A.; |
| 237 | VectorXqcxx . . $ KDL/ChainIkSolverPos_LMA#VectorXq# tmpcxx . . $ KDL/ChainIkSolverPos_LMA#tmp.; |
| 238 | Eigencxx . . $ Eigen/::LDLTcxx . . $ Eigen/LDLT#<MatrixXqcxx . . $ KDL/ChainIkSolverPos_LMA#MatrixXq#> ldltcxx . . $ KDL/ChainIkSolverPos_LMA#ldlt.; |
| 239 | Eigencxx . . $ Eigen/::JacobiSVDcxx . . $ Eigen/JacobiSVD#<MatrixXqcxx . . $ KDL/ChainIkSolverPos_LMA#MatrixXq#> svdcxx . . $ KDL/ChainIkSolverPos_LMA#svd.; |
| 240 | VectorXqcxx . . $ KDL/ChainIkSolverPos_LMA#VectorXq# diffqcxx . . $ KDL/ChainIkSolverPos_LMA#diffq.; |
| 241 | VectorXqcxx . . $ KDL/ChainIkSolverPos_LMA#VectorXq# q_newcxx . . $ KDL/ChainIkSolverPos_LMA#q_new.; |
| 242 | VectorXqcxx . . $ KDL/ChainIkSolverPos_LMA#VectorXq# original_Aiicxx . . $ KDL/ChainIkSolverPos_LMA#original_Aii.; |
| 243 | }; |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | } // namespace KDL |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | #endif |