Jacobian.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. *
  3. * Inverse Kinematics software, with several solvers including
  4. * Selectively Damped Least Squares Method
  5. * Damped Least Squares Method
  6. * Pure Pseudoinverse Method
  7. * Jacobian Transpose Method
  8. *
  9. *
  10. * Author: Samuel R. Buss, [email protected].
  11. * Web page: http://www.math.ucsd.edu/~sbuss/ResearchWeb/ikmethods/index.html
  12. *
  13. *
  14. This software is provided 'as-is', without any express or implied warranty.
  15. In no event will the authors be held liable for any damages arising from the use of this software.
  16. Permission is granted to anyone to use this software for any purpose,
  17. including commercial applications, and to alter it and redistribute it freely,
  18. subject to the following restrictions:
  19. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  20. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  21. 3. This notice may not be removed or altered from any source distribution.
  22. *
  23. *
  24. */
  25. #include "Node.h"
  26. #include "Tree.h"
  27. #include "MathMisc.h"
  28. #include "LinearR3.h"
  29. #include "VectorRn.h"
  30. #include "MatrixRmn.h"
  31. #ifndef _CLASS_JACOBIAN
  32. #define _CLASS_JACOBIAN
  33. #ifdef _DYNAMIC
  34. const double BASEMAXDIST = 0.02;
  35. #else
  36. const double MAXDIST = 0.08; // optimal value for double Y shape : 0.08
  37. #endif
  38. const double DELTA = 0.4;
  39. const long double LAMBDA = 2.0; // only for DLS. optimal : 0.24
  40. const double NEARZERO = 0.0000000001;
  41. enum UpdateMode {
  42. JACOB_Undefined = 0,
  43. JACOB_JacobianTranspose = 1,
  44. JACOB_PseudoInverse = 2,
  45. JACOB_DLS = 3,
  46. JACOB_SDLS = 4 };
  47. class Jacobian {
  48. public:
  49. Jacobian(Tree*);
  50. Jacobian(bool useAngularJacobian, int nDof);
  51. void ComputeJacobian(VectorR3* targets);
  52. const MatrixRmn& ActiveJacobian() const { return *Jactive; }
  53. void SetJendActive() { Jactive = &Jend; } // The default setting is Jend.
  54. void SetJtargetActive() { Jactive = &Jtarget; }
  55. void SetJendTrans(MatrixRmn& J);
  56. void SetDeltaS(VectorRn& S);
  57. void CalcDeltaThetas(); // Use this only if the Current Mode has been set.
  58. void ZeroDeltaThetas();
  59. void CalcDeltaThetasTranspose();
  60. void CalcDeltaThetasPseudoinverse();
  61. void CalcDeltaThetasDLS();
  62. void CalcDeltaThetasDLSwithSVD();
  63. void CalcDeltaThetasSDLS();
  64. void CalcDeltaThetasDLSwithNullspace( const VectorRn& desiredV);
  65. void UpdateThetas();
  66. void UpdateThetaDot();
  67. double UpdateErrorArray(VectorR3* targets); // Returns sum of errors
  68. const VectorRn& GetErrorArray() const { return errorArray; }
  69. void UpdatedSClampValue(VectorR3* targets);
  70. void SetCurrentMode( UpdateMode mode ) { CurrentUpdateMode = mode; }
  71. UpdateMode GetCurrentMode() const { return CurrentUpdateMode; }
  72. void SetDampingDLS( double lambda ) { DampingLambda = lambda; DampingLambdaSq = Square(lambda); }
  73. void Reset();
  74. static void CompareErrors( const Jacobian& j1, const Jacobian& j2, double* weightedDist1, double* weightedDist2 );
  75. static void CountErrors( const Jacobian& j1, const Jacobian& j2, int* numBetter1, int* numBetter2, int* numTies );
  76. int GetNumRows() {return nRow;}
  77. int GetNumCols() {return nCol;}
  78. public:
  79. Tree* m_tree; // tree associated with this Jacobian matrix
  80. int m_nEffector; // Number of end effectors
  81. int nJoint; // Number of joints
  82. int nRow; // Total number of rows the real J (= 3*number of end effectors for now)
  83. int nCol; // Total number of columns in the real J (= number of joints for now)
  84. MatrixRmn Jend; // Jacobian matrix based on end effector positions
  85. MatrixRmn Jtarget; // Jacobian matrix based on target positions
  86. MatrixRmn Jnorms; // Norms of 3-vectors in active Jacobian (SDLS only)
  87. MatrixRmn U; // J = U * Diag(w) * V^T (Singular Value Decomposition)
  88. VectorRn w;
  89. MatrixRmn V;
  90. UpdateMode CurrentUpdateMode;
  91. VectorRn dS; // delta s
  92. VectorRn dT1; // delta t -- these are delta S values clamped to smaller magnitude
  93. VectorRn dSclamp; // Value to clamp magnitude of dT at.
  94. VectorRn dTheta; // delta theta
  95. VectorRn dPreTheta; // delta theta for single eigenvalue (SDLS only)
  96. VectorRn errorArray; // Distance of end effectors from target after updating
  97. // Parameters for pseudoinverses
  98. static const double PseudoInverseThresholdFactor; // Threshold for treating eigenvalue as zero (fraction of largest eigenvalue)
  99. // Parameters for damped least squares
  100. static const double DefaultDampingLambda;
  101. double DampingLambda;
  102. double DampingLambdaSq;
  103. //double DampingLambdaSDLS;
  104. // Cap on max. value of changes in angles in single update step
  105. static const double MaxAngleJtranspose;
  106. static const double MaxAnglePseudoinverse;
  107. static const double MaxAngleDLS;
  108. static const double MaxAngleSDLS;
  109. MatrixRmn* Jactive;
  110. void CalcdTClampedFromdS();
  111. static const double BaseMaxTargetDist;
  112. };
  113. #endif