WSDLSSolver.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /** \file itasc/WSDLSSolver.cpp
  2. * \ingroup itasc
  3. */
  4. /*
  5. * WDLSSolver.hpp.cpp
  6. *
  7. * Created on: Jan 8, 2009
  8. * Author: rubensmits
  9. */
  10. #include "WSDLSSolver.hpp"
  11. #include "kdl/utilities/svd_eigen_HH.hpp"
  12. #include <cstdio>
  13. namespace iTaSC {
  14. WSDLSSolver::WSDLSSolver() :
  15. m_ns(0), m_nc(0), m_nq(0)
  16. {
  17. // default maximum speed: 50 rad/s
  18. m_qmax = 50.0;
  19. }
  20. WSDLSSolver::~WSDLSSolver() {
  21. }
  22. bool WSDLSSolver::init(unsigned int _nq, unsigned int _nc, const std::vector<bool>& gc)
  23. {
  24. if (_nc == 0 || _nq == 0 || gc.size() != _nc)
  25. return false;
  26. m_nc = _nc;
  27. m_nq = _nq;
  28. m_ns = std::min(m_nc,m_nq);
  29. m_AWq = e_zero_matrix(m_nc,m_nq);
  30. m_WyAWq = e_zero_matrix(m_nc,m_nq);
  31. m_WyAWqt = e_zero_matrix(m_nq,m_nc);
  32. m_S = e_zero_vector(std::max(m_nc,m_nq));
  33. m_Wy_ydot = e_zero_vector(m_nc);
  34. m_ytask = gc;
  35. if (m_nq > m_nc) {
  36. m_transpose = true;
  37. m_temp = e_zero_vector(m_nc);
  38. m_U = e_zero_matrix(m_nc,m_nc);
  39. m_V = e_zero_matrix(m_nq,m_nc);
  40. m_WqV = e_zero_matrix(m_nq,m_nc);
  41. } else {
  42. m_transpose = false;
  43. m_temp = e_zero_vector(m_nq);
  44. m_U = e_zero_matrix(m_nc,m_nq);
  45. m_V = e_zero_matrix(m_nq,m_nq);
  46. m_WqV = e_zero_matrix(m_nq,m_nq);
  47. }
  48. return true;
  49. }
  50. bool WSDLSSolver::solve(const e_matrix& A, const e_vector& Wy, const e_vector& ydot, const e_matrix& Wq, e_vector& qdot, e_scalar& nlcoef)
  51. {
  52. unsigned int i, j, l;
  53. e_scalar N, M;
  54. // Create the Weighted jacobian
  55. m_AWq.noalias() = A*Wq;
  56. for (i=0; i<m_nc; i++)
  57. m_WyAWq.row(i) = Wy(i)*m_AWq.row(i);
  58. // Compute the SVD of the weighted jacobian
  59. int ret;
  60. if (m_transpose) {
  61. m_WyAWqt = m_WyAWq.transpose();
  62. ret = KDL::svd_eigen_HH(m_WyAWqt,m_V,m_S,m_U,m_temp);
  63. } else {
  64. ret = KDL::svd_eigen_HH(m_WyAWq,m_U,m_S,m_V,m_temp);
  65. }
  66. if(ret<0)
  67. return false;
  68. m_Wy_ydot = Wy.array() * ydot.array();
  69. m_WqV.noalias() = Wq*m_V;
  70. qdot.setZero();
  71. e_scalar maxDeltaS = e_scalar(0.0);
  72. e_scalar prevS = e_scalar(0.0);
  73. e_scalar maxS = e_scalar(1.0);
  74. for(i=0;i<m_ns;++i) {
  75. e_scalar norm, mag, alpha, _qmax, Sinv, vmax, damp;
  76. e_scalar S = m_S(i);
  77. bool prev;
  78. if (S < KDL::epsilon)
  79. break;
  80. Sinv = e_scalar(1.)/S;
  81. if (i > 0) {
  82. if ((prevS-S) > maxDeltaS) {
  83. maxDeltaS = (prevS-S);
  84. maxS = prevS;
  85. }
  86. }
  87. N = M = e_scalar(0.);
  88. for (l=0, prev=m_ytask[0], norm=e_scalar(0.); l<m_nc; l++) {
  89. if (prev == m_ytask[l]) {
  90. norm += m_U(l,i)*m_U(l,i);
  91. } else {
  92. N += std::sqrt(norm);
  93. norm = m_U(l,i)*m_U(l,i);
  94. }
  95. prev = m_ytask[l];
  96. }
  97. N += std::sqrt(norm);
  98. for (j=0; j<m_nq; j++) {
  99. for (l=0, prev=m_ytask[0], norm=e_scalar(0.), mag=e_scalar(0.); l<m_nc; l++) {
  100. if (prev == m_ytask[l]) {
  101. norm += m_WyAWq(l,j)*m_WyAWq(l,j);
  102. } else {
  103. mag += std::sqrt(norm);
  104. norm = m_WyAWq(l,j)*m_WyAWq(l,j);
  105. }
  106. prev = m_ytask[l];
  107. }
  108. mag += std::sqrt(norm);
  109. M += fabs(m_V(j,i))*mag;
  110. }
  111. M *= Sinv;
  112. alpha = m_U.col(i).dot(m_Wy_ydot);
  113. _qmax = (N < M) ? m_qmax*N/M : m_qmax;
  114. vmax = m_WqV.col(i).array().abs().maxCoeff();
  115. norm = fabs(Sinv*alpha*vmax);
  116. if (norm > _qmax) {
  117. damp = Sinv*alpha*_qmax/norm;
  118. } else {
  119. damp = Sinv*alpha;
  120. }
  121. qdot += m_WqV.col(i)*damp;
  122. prevS = S;
  123. }
  124. if (maxDeltaS == e_scalar(0.0))
  125. nlcoef = e_scalar(KDL::epsilon);
  126. else
  127. nlcoef = (maxS-maxDeltaS)/maxS;
  128. return true;
  129. }
  130. }