123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- #include <iostream>
- #include <fstream>
- #include <iomanip>
- #include <string>
- #include <queue>
- #include "logger/RusLog.h"
- #include "Mutex/RusMutex.h"
- #include "AdmittanceControl.h"
- using namespace std;
- using namespace AdmittanceAlgorithm;
- static std::mutex m_mutex;
- std::queue<WayPoint> _curWayPtList;
- std::queue<FTData> _curForceList;
- std::queue<WayPoint> _refWayPtList;
- AdmittanceControl::AdmittanceControl()
- {}
- AdmittanceControl::~AdmittanceControl()
- {}
- void AdmittanceControl::SetAdmittanceControl(AdmittanceSpace admittanceSpace, AdmittanceType admittanceType)
- {
- Init();
- switch (admittanceType)
- {
- case AdmittanceType::FreeDrag:
- switch (admittanceSpace)
- {
- case AdmittanceSpace::Joint:
- _pAdmittance = &_pJointFreeDrag;
- break;
- case AdmittanceSpace::Cartesian:
- _pAdmittance = &_pCartFreeDrag;
- break;
- default:
- break;
- }
- break;
- case AdmittanceType::ForceScan:
- switch (admittanceSpace)
- {
- case AdmittanceSpace::Joint:
- _pAdmittance = &_pJointForceScan;
- break;
- case AdmittanceSpace::Cartesian:
- _pAdmittance = &_pCartForceScan;
- break;
- default:
- break;
- }
- break;
- default:
- break;
- }
- _pAdmittance->Init();
- SetLogFileDirectory("F:/RUSLog");
- SetLogFileName("Admittance");
- SetLogLevel(RUS_LOG_INFO);
- EnableLogFile(true);
- }
- void AdmittanceControl::UpdateCurrentWayPoint(WayPoint wayPoint)
- {
- try
- {
- if (wayPoint.jointPos.jointAngle != nullptr || wayPoint.cartPos.position != nullptr)
- {
- CRusLockGuard lock(m_mutex);
- _curWayPtList.push(wayPoint);
- }
- }
- catch (...)
- {
- LogError("Failed in UpdateCurrentWayPoint!\n")
- }
- }
- /**
- * \brief 获取六维力
- * \param force 六维力
- * \param lastTime 返回回调上次已运行的时间ms
- */
- void AdmittanceControl::UpdateCurrentForce(FTData force)
- {
- try
- {
- if (force.CartFT.GenForce != nullptr || force.JointT.Torque != nullptr)
- {
- CRusLockGuard lock(m_mutex);
- _curForceList.push(force);
- }
- }
- catch (...)
- {
- LogError("Failed in UpdateCurrentForce!\n")
- }
- }
- void AdmittanceControl::UpdateReferWayPoint(WayPoint wayPoint)
- {
- try
- {
- if (wayPoint.jointPos.jointAngle != nullptr || wayPoint.cartPos.position != nullptr)
- {
- CRusLockGuard lock(m_mutex);
- _refWayPtList.push(wayPoint);
- }
- }
- catch (...)
- {
- LogError("Failed in UpdateReferWayPoint!\n")
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="wayPoint"路点</param>
- void AdmittanceControl::GetTargetWayPoint(WayPoint& wayPoint)
- {
- try
- {
- //
- WayPoint curWayPt;
- while (true)
- {
- {
- CRusLockGuard lock(m_mutex);
- if (!_curWayPtList.empty())
- {
- curWayPt = _curWayPtList.front();
- _curWayPtList.pop();
- break;
- }
- }
- }
- FTData curForce;
- while (true)
- {
- {
- CRusLockGuard lock(m_mutex);
- if (!_curForceList.empty())
- {
- curForce = _curForceList.front();
- _curForceList.pop();
- break;
- }
- }
- }
- WayPoint refWayPt;
- while (true)
- {
- {
- CRusLockGuard lock(m_mutex);
- if (!_refWayPtList.empty())
- {
- refWayPt = _refWayPtList.front();
- _refWayPtList.pop();
- break;
- }
- }
- }
- _pAdmittance->LoadData(curWayPt, curForce, refWayPt);
- _pAdmittance->GetActualTarget(wayPoint);
- }
- catch (...)
- {
- LogError("Failed in GetTargetWayPoint!\n")
- }
- }
- void AdmittanceControl::StopAdmittanceControl()
- {
- try
- {
- Init();
- _pAdmittance->RefreshAdmittancePara();
- }
- catch (const std::exception&)
- {
- LogError("Failed in StopAdmittanceControl!\n")
- }
- }
- void AdmittanceControl::Init()
- {
- while (!_curForceList.empty()) { _curForceList.pop(); }
- while (!_curWayPtList.empty()) { _curWayPtList.pop(); }
- while (!_refWayPtList.empty()) { _refWayPtList.pop(); }
- }
|