CartForceScan.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #include "CartForceScan.h"
  2. void CartForceScan::Init()
  3. {
  4. // 导纳控制参数
  5. M.diagonal() << 0.01, 0.01, 0.01, 0.001, 0.001, 0.001;
  6. B.diagonal() << 10.0, 10.0, 10.0, 0.8, 0.8, 0.8;
  7. K.diagonal() << 50.0, 50.0, 5.0, 50.0, 50.0, 50.0;
  8. Md = M.toDenseMatrix();
  9. Bd = B.toDenseMatrix();
  10. Kd = K.toDenseMatrix();
  11. deltaFkminus1.setZero();
  12. deltaFkminus2.setZero();
  13. Vkminus1.setZero();
  14. Vkminus2.setZero();
  15. tool2Basekminus1 = Eigen::Matrix4d::Identity();
  16. first = true;
  17. }
  18. void CartForceScan::LoadData(WayPoint curWayPt, FTData curForce, WayPoint refWayPt)
  19. {
  20. //扫查模式下需要当前的笛卡尔力,当前的笛卡尔位姿和参考目标笛卡尔位姿
  21. _curCart = curWayPt.cartPos;
  22. _curForce = curForce.CartFT;
  23. _refCart = refWayPt.cartPos;
  24. }
  25. void CartForceScan::GetActualTarget(WayPoint& wayPoint)
  26. {
  27. try
  28. {
  29. CartScanFunction(wayPoint);
  30. }
  31. catch (const std::exception&)
  32. {
  33. }
  34. }
  35. void CartForceScan::RefreshAdmittancePara()
  36. {
  37. deltaFkminus1.setZero();
  38. deltaFkminus2.setZero();
  39. Vkminus1.setZero();
  40. Vkminus2.setZero();
  41. tool2Basekminus1 = Eigen::Matrix4d::Identity();
  42. first = true;
  43. M.setZero();
  44. B.setZero();
  45. K.setZero();
  46. Md.setZero();
  47. Bd.setZero();
  48. Kd.setZero();
  49. }
  50. /**
  51. * \brief 笛卡尔恒力扫查回调函数
  52. * \param robotState 机器人实时状态
  53. */
  54. void CartForceScan::CartScanFunction(WayPoint& wayPoint)
  55. {
  56. // 获取当前机器人位置
  57. Eigen::Matrix4d tool2BaseNow = Eigen::Map<Eigen::Matrix4d>(_curCart.position).transpose();
  58. if (first)
  59. {
  60. tool2Basekminus1 = tool2BaseNow;
  61. first = false;
  62. }
  63. // 传输六维力信息
  64. Sophus::Vector6d toolFk = Eigen::Map<Sophus::Vector6d>(_curForce.GenForce);
  65. // 获取机器人目标参考位置
  66. Eigen::Matrix4d tool2BaseRefer = Eigen::Map<Eigen::Matrix4d>(_refCart.position).transpose();
  67. //The transformation of force/torques in different coordinate systems
  68. Sophus::SE3d tool2BaseT(tool2BaseNow);
  69. Sophus::Matrix6d AdjTool2Base = tool2BaseT.Adj();
  70. Sophus::Vector6d baseFk = AdjTool2Base * toolFk;
  71. // 机器人基座坐标系中的期望力
  72. Sophus::Vector6d baseFd;
  73. baseFd << baseFk(0), baseFk(1), 10, baseFk(3), baseFk(4), baseFk(5);
  74. // deltaF
  75. Sophus::Vector6d deltaFk = baseFk - baseFd;
  76. // deltaF -> deltaT
  77. Eigen::Matrix4d deltaT = Eigen::Matrix4d::Identity();
  78. #if false //方法一
  79. Sophus::Matrix6d P = Md + Bd * Ts + Kd * Ts * Ts;
  80. Sophus::Matrix6d alpha0 = Ts * Ts * P.inverse();
  81. Sophus::Matrix6d alpha1 = (2 * Md + Bd * Ts) * P.inverse();
  82. Sophus::Matrix6d alpha2 = -Md * P.inverse();
  83. Sophus::Vector6d Vk = alpha0 * deltaFk + alpha1 * Vkminus1 + alpha2 * Vkminus2;
  84. deltaT = Sophus::SE3d::exp(Vk - Vkminus1).matrix();
  85. #else //方法二
  86. Sophus::Matrix6d Q = 4 * Md + 2 * Bd * Ts + Kd * Ts * Ts;
  87. Sophus::Matrix6d beta0 = Ts * Ts * Q.inverse();
  88. Sophus::Matrix6d beta1 = 2 * Ts * Ts * Q.inverse();
  89. Sophus::Matrix6d beta2 = -(2 * Kd * Ts * Ts - 8 * Md) * Q.inverse();
  90. Sophus::Matrix6d beta3 = -(4 * Md - 2 * Bd * Ts + Kd * Ts * Ts) * Q.inverse();
  91. Sophus::Vector6d Vk = beta0 * deltaFk + beta1 * deltaFkminus1 + beta0 * deltaFkminus2 + beta2 * Vkminus1 + beta3 * Vkminus2;
  92. deltaT = Sophus::SE3d::exp(Vk - Vkminus1).matrix();
  93. #endif //理论上方法二更好,实际上由于RCI内部可能存在的调节机制两种方法效果差不多
  94. deltaFkminus2 = deltaFkminus1;
  95. deltaFkminus1 = deltaFk;
  96. Vkminus2 = Vkminus1;
  97. //将真实位移加入算法过程
  98. if (false)
  99. {
  100. Eigen::Matrix4d deltaTkminus1 = tool2BaseNow * tool2Basekminus1.inverse();
  101. Sophus::SE3d tmp(deltaTkminus1);
  102. Vkminus1 = tmp.log() + Vkminus2;
  103. }
  104. else
  105. {
  106. Vkminus1 = Vk;
  107. }
  108. tool2Basekminus1 = tool2BaseNow;
  109. // X = XRefer + XDelta
  110. Eigen::Matrix4d tool2BaseTarget = deltaT * tool2BaseRefer;
  111. //Eigen::Matrix4d tool2BaseTarget = tool2BaseRefer;
  112. Eigen::Map<Eigen::Matrix<double, 4, 4, Eigen::RowMajor>>(wayPoint.cartPos.position) = tool2BaseTarget;
  113. #if true
  114. //欧拉角是多少
  115. Eigen::Vector3d eulerNow = tool2BaseNow.block<3, 3>(0, 0).eulerAngles(2, 1, 0);
  116. Eigen::Vector3d eulerDelta = deltaT.block<3, 3>(0, 0).eulerAngles(2, 1, 0);
  117. Eigen::Vector3d eulerTarget = tool2BaseTarget.block<3, 3>(0, 0).eulerAngles(2, 1, 0);
  118. LogInfo((
  119. std::to_string(toolFk(0)) + " " +
  120. std::to_string(toolFk(1)) + " " +
  121. std::to_string(toolFk(2)) + " " +
  122. //std::to_string(toolFk(3)) + " " +
  123. //std::to_string(toolFk(4)) + " " +
  124. //std::to_string(toolFk(5)) + " " +
  125. std::to_string(baseFk(0)) + " " +
  126. std::to_string(baseFk(1)) + " " +
  127. std::to_string(baseFk(2)) + " " +
  128. //std::to_string(baseFk(3)) + " " +
  129. //std::to_string(baseFk(4)) + " " +
  130. //std::to_string(baseFk(5)) + " " +
  131. std::to_string(tool2BaseNow(0, 3)) + " " +
  132. std::to_string(tool2BaseNow(1, 3)) + " " +
  133. std::to_string(tool2BaseNow(2, 3)) + " " +
  134. std::to_string(tool2BaseRefer(0, 3)) + " " +
  135. std::to_string(tool2BaseRefer(1, 3)) + " " +
  136. std::to_string(tool2BaseRefer(2, 3)) + " " +
  137. //std::to_string(eulerNow(0)) + " " +
  138. //std::to_string(eulerNow(1)) + " " +
  139. //std::to_string(eulerNow(2)) + " " +
  140. std::to_string(tool2BaseTarget(0, 3)) + " " +
  141. std::to_string(tool2BaseTarget(1, 3)) + " " +
  142. std::to_string(tool2BaseTarget(2, 3)) + " " +
  143. //std::to_string(eulerTarget(0)) + " " +
  144. //std::to_string(eulerTarget(1)) + " " +
  145. //std::to_string(eulerTarget(2)) + " " +
  146. std::to_string(deltaT(0, 3)) + " " +
  147. std::to_string(deltaT(1, 3)) + " " +
  148. std::to_string(deltaT(2, 3)) + " " +
  149. //std::to_string(eulerDelta(0)) + " " +
  150. //std::to_string(eulerDelta(1)) + " " +
  151. //std::to_string(eulerDelta(2)) + " " +
  152. "\n").c_str());
  153. #endif
  154. }