1
0

AdmittanceControl.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string>
  5. #include <queue>
  6. #include "logger/RusLog.h"
  7. #include "Mutex/RusMutex.h"
  8. #include "AdmittanceControl.h"
  9. using namespace std;
  10. using namespace AdmittanceAlgorithm;
  11. static std::mutex m_mutex;
  12. std::queue<WayPoint> _curWayPtList;
  13. std::queue<FTData> _curForceList;
  14. std::queue<WayPoint> _refWayPtList;
  15. AdmittanceControl::AdmittanceControl()
  16. {}
  17. AdmittanceControl::~AdmittanceControl()
  18. {}
  19. void AdmittanceControl::SetAdmittanceControl(AdmittanceSpace admittanceSpace, AdmittanceType admittanceType)
  20. {
  21. Init();
  22. switch (admittanceType)
  23. {
  24. case AdmittanceType::FreeDrag:
  25. switch (admittanceSpace)
  26. {
  27. case AdmittanceSpace::Joint:
  28. _pAdmittance = &_pJointFreeDrag;
  29. break;
  30. case AdmittanceSpace::Cartesian:
  31. _pAdmittance = &_pCartFreeDrag;
  32. break;
  33. default:
  34. break;
  35. }
  36. break;
  37. case AdmittanceType::ForceScan:
  38. switch (admittanceSpace)
  39. {
  40. case AdmittanceSpace::Joint:
  41. _pAdmittance = &_pJointForceScan;
  42. break;
  43. case AdmittanceSpace::Cartesian:
  44. _pAdmittance = &_pCartForceScan;
  45. break;
  46. default:
  47. break;
  48. }
  49. break;
  50. default:
  51. break;
  52. }
  53. _pAdmittance->Init();
  54. SetLogFileDirectory("F:/RUSLog");
  55. SetLogFileName("Admittance");
  56. SetLogLevel(RUS_LOG_INFO);
  57. EnableLogFile(true);
  58. }
  59. void AdmittanceControl::UpdateCurrentWayPoint(WayPoint wayPoint)
  60. {
  61. try
  62. {
  63. if (wayPoint.jointPos.jointAngle != nullptr || wayPoint.cartPos.position != nullptr)
  64. {
  65. CRusLockGuard lock(m_mutex);
  66. _curWayPtList.push(wayPoint);
  67. }
  68. }
  69. catch (...)
  70. {
  71. LogError("Failed in UpdateCurrentWayPoint!\n")
  72. }
  73. }
  74. /**
  75. * \brief 获取六维力
  76. * \param force 六维力
  77. * \param lastTime 返回回调上次已运行的时间ms
  78. */
  79. void AdmittanceControl::UpdateCurrentForce(FTData force)
  80. {
  81. try
  82. {
  83. if (force.CartFT.GenForce != nullptr || force.JointT.Torque != nullptr)
  84. {
  85. CRusLockGuard lock(m_mutex);
  86. _curForceList.push(force);
  87. }
  88. }
  89. catch (...)
  90. {
  91. LogError("Failed in UpdateCurrentForce!\n")
  92. }
  93. }
  94. void AdmittanceControl::UpdateReferWayPoint(WayPoint wayPoint)
  95. {
  96. try
  97. {
  98. if (wayPoint.jointPos.jointAngle != nullptr || wayPoint.cartPos.position != nullptr)
  99. {
  100. CRusLockGuard lock(m_mutex);
  101. _refWayPtList.push(wayPoint);
  102. }
  103. }
  104. catch (...)
  105. {
  106. LogError("Failed in UpdateReferWayPoint!\n")
  107. }
  108. }
  109. /// <summary>
  110. ///
  111. /// </summary>
  112. /// <param name="wayPoint"路点</param>
  113. void AdmittanceControl::GetTargetWayPoint(WayPoint& wayPoint)
  114. {
  115. try
  116. {
  117. //
  118. WayPoint curWayPt;
  119. while (true)
  120. {
  121. {
  122. CRusLockGuard lock(m_mutex);
  123. if (!_curWayPtList.empty())
  124. {
  125. curWayPt = _curWayPtList.front();
  126. _curWayPtList.pop();
  127. break;
  128. }
  129. }
  130. }
  131. FTData curForce;
  132. while (true)
  133. {
  134. {
  135. CRusLockGuard lock(m_mutex);
  136. if (!_curForceList.empty())
  137. {
  138. curForce = _curForceList.front();
  139. _curForceList.pop();
  140. break;
  141. }
  142. }
  143. }
  144. WayPoint refWayPt;
  145. while (true)
  146. {
  147. {
  148. CRusLockGuard lock(m_mutex);
  149. if (!_refWayPtList.empty())
  150. {
  151. refWayPt = _refWayPtList.front();
  152. _refWayPtList.pop();
  153. break;
  154. }
  155. }
  156. }
  157. _pAdmittance->LoadData(curWayPt, curForce, refWayPt);
  158. _pAdmittance->GetActualTarget(wayPoint);
  159. }
  160. catch (...)
  161. {
  162. LogError("Failed in GetTargetWayPoint!\n")
  163. }
  164. }
  165. void AdmittanceControl::StopAdmittanceControl()
  166. {
  167. try
  168. {
  169. Init();
  170. _pAdmittance->RefreshAdmittancePara();
  171. }
  172. catch (const std::exception&)
  173. {
  174. LogError("Failed in StopAdmittanceControl!\n")
  175. }
  176. }
  177. void AdmittanceControl::Init()
  178. {
  179. while (!_curForceList.empty()) { _curForceList.pop(); }
  180. while (!_curWayPtList.empty()) { _curWayPtList.pop(); }
  181. while (!_refWayPtList.empty()) { _refWayPtList.pop(); }
  182. }