ImpedanceController.cpp
Go to the documentation of this file.
1 /**
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @author Jianfeng Gao ( jianfeng dot gao at kit dot edu )
17  * @date 2023
18  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
19  * GNU General Public License
20  */
21 
22 #include "ImpedanceController.h"
23 
25 
27 {
28  NJointControllerRegistration<NJointTSImpedanceMPController>
29  registrationControllerNJointTSImpedanceMPController("NJointTSImpedanceMPController");
30 
32  const RobotUnitPtr& robotUnit,
33  const NJointControllerConfigPtr& config,
34  const VirtualRobot::RobotPtr& robot) :
36  {
37  ARMARX_IMPORTANT << getClassName() + " construction done";
38  }
39 
40  std::string
42  {
43  return "NJointTSImpedanceMPController";
44  }
45 
46  void
48  const Ice::Current& iceCurrent)
49  {
51  {
52  for (auto& pair : limb)
53  {
54  pair.second->rtFirstRun.store(true);
55  }
56  }
57  }
58 
59  void
61  {
62  std::lock_guard<std::recursive_mutex> lock(mtx_mps);
63  bool rtSafe = additionalTaskUpdateStatus();
64 
65  std::map<std::string, bool> mpRunning;
66  for (auto& _mp : mps)
67  {
68  mpRunning[_mp.second.mp->getMPName()] = _mp.second.mp->isRunning();
69  if (_mp.second.mp->isFinished())
70  {
71  continue;
72  }
73  if (_mp.second.mp->getRole() == "taskspace")
74  {
75  auto& arm = limb.at(_mp.second.mp->getNodeSetName());
76  if (_mp.second.mp->isFirstRun())
77  {
78  _mp.second.mp->validateInitialState(common::mat4ToDVec(arm->rtStatusInNonRT.currentPose));
79  }
80  mp::TSMPInputPtr in = std::dynamic_pointer_cast<mp::TSMPInput>(_mp.second.input);
81  in->pose = arm->rtStatusInNonRT.currentPose;
82  in->vel = arm->rtStatusInNonRT.currentTwist;
83  in->deltaT = arm->rtStatusInNonRT.deltaT;
84  }
85  else if (_mp.second.mp->getRole() == "nullspace")
86  {
87  auto& arm = limb.at(_mp.second.mp->getNodeSetName());
88  mp::JSMPInputPtr in = std::dynamic_pointer_cast<mp::JSMPInput>(_mp.second.input);
89  ARMARX_CHECK_EQUAL(arm->rtStatusInNonRT.numJoints,
90  arm->rtStatusInNonRT.jointPos.size());
91  in->angleRadian = arm->rtStatusInNonRT.jointPos;
92  in->angularVel = arm->rtStatusInNonRT.qvelFiltered;
93  in->deltaT = arm->rtStatusInNonRT.deltaT;
94  }
95  else if (_mp.second.mp->getRole() == "hand")
96  {
97  auto& hand = hands->hands.at(_mp.second.mp->getNodeSetName());
98  mp::JSMPInputPtr in = std::dynamic_pointer_cast<mp::JSMPInput>(_mp.second.input);
99  in->angleRadian =
100  hand->bufferRTToNonRT.getUpToDateReadBuffer().jointPosition.value();
101  in->deltaT = hand->bufferRTToNonRT.getReadBuffer().deltaT;
102  }
103  }
104  if (not rtSafe)
105  {
107  return;
108  // Make sure that you do not call additionalTaskSetTarget before return;
109  // since we overwrite the arm->nonRtConfig in additionalTaskUpdateStatus() with user config, which does not
110  // align with the up-to-date mp status. userConfig is only updated to align with MP status everytime a MP
111  // finishes execution. Only then, user can use this controller as if no mp is there.
112  }
113 
114  runMPs(rtSafe);
115 
116  /// set mp target to nonRT config data structure
117  for (auto& _mp : mps)
118  {
119  if (not mpRunning.at(_mp.second.mp->getMPName()))
120  {
121  continue;
122  }
123  for (auto& ftGuard : mpConfig.ftGuard)
124  {
125  if (ftGuard.mpName == _mp.second.mp->getMPName())
126  {
127  bool const forceGuard =
128  (ftGuard.force.has_value() and
129  ftGuard.force.value() >= _mp.second.mp->getCanonicalValue());
130  bool const torqueGuard =
131  (ftGuard.torque.has_value() and
132  ftGuard.torque.value() >= _mp.second.mp->getCanonicalValue());
133 
134  auto& arm = limb.at(_mp.second.mp->getNodeSetName());
135  bool const resetForce =
136  not arm->nonRtConfig.ftConfig.enableSafeGuardForce and forceGuard;
137  bool const resetTorque =
138  not arm->nonRtConfig.ftConfig.enableSafeGuardTorque and torqueGuard;
139  if (resetForce or resetTorque)
140  {
141  ARMARX_INFO << "Triggering force torque safety guard for "
142  << arm->kinematicChainName << " at can value "
143  << _mp.second.mp->getCanonicalValue();
144  }
145 
146  arm->nonRtConfig.ftConfig.enableSafeGuardForce = forceGuard;
147  arm->nonRtConfig.ftConfig.enableSafeGuardTorque = torqueGuard;
148  arm->controller.ftsensor.enableSafeGuard(resetForce, resetTorque);
149  }
150  }
151 
152  if (_mp.second.mp->getRole() == "taskspace")
153  {
154  auto& arm = limb.at(_mp.second.mp->getNodeSetName());
155  mp::TSMPOutputPtr out =
156  std::dynamic_pointer_cast<mp::TSMPOutput>(_mp.second.output);
157 
158  /// Note that here we only report warning but still set target to RT, we rely on MP phase stop and
159  /// RT safety mechanism to stop or pause the action if needed
160  common::detectAndReportPoseDeviationWarning(arm->rtStatusInNonRT.currentPose,
161  out->pose,
162  "current pose", "TSMP pose",
163  arm->nonRtConfig.safeDistanceMMToGoal,
164  arm->nonRtConfig.safeRotAngleDegreeToGoal,
165  _mp.second.mp->getMPName() + "mp_set_target");
166  arm->nonRtConfig.desiredPose = out->pose;
167  arm->nonRtConfig.desiredTwist = out->vel;
168  }
169  else if (_mp.second.mp->getRole() == "nullspace")
170  {
171  auto& arm = limb.at(_mp.second.mp->getNodeSetName());
172  mp::JSMPOutputPtr out =
173  std::dynamic_pointer_cast<mp::JSMPOutput>(_mp.second.output);
174  ARMARX_CHECK_EQUAL(arm->rtStatusInNonRT.numJoints, out->angleRadian.size());
175  arm->nonRtConfig.desiredNullspaceJointAngles.value() = out->angleRadian;
176  }
177  else if (_mp.second.mp->getRole() == "hand")
178  {
179  mp::JSMPOutputPtr out =
180  std::dynamic_pointer_cast<mp::JSMPOutput>(_mp.second.output);
181  auto& hand = hands->hands.at(_mp.second.mp->getNodeSetName());
182  mp::JSMPInputPtr in = std::dynamic_pointer_cast<mp::JSMPInput>(_mp.second.input);
183  ARMARX_CHECK_EQUAL(hand->targetNonRT.jointPosition.value().size(),
184  out->angleRadian.size());
185  hand->targetNonRT.jointPosition.value() = out->angleRadian;
186  }
187  if (mpRunning.at(_mp.second.mp->getMPName()) and _mp.second.mp->isFinished())
188  {
189  ARMARX_INFO << "reset buffer for nonRtConfig of " << _mp.second.mp->getMPName();
190  for (auto& pair : limb)
191  {
192  auto& arm = pair.second;
193  arm->bufferConfigUserToNonRt.reinitAllBuffers(arm->nonRtConfig);
194  }
195  for (auto& pair : limb)
196  {
197  userConfig.limbs.at(pair.first) = pair.second->nonRtConfig;
198  }
199  if (hands)
200  {
201  hands->reinitBuffer(userConfig.hands);
202  }
203  }
204  }
206  }
207 } // namespace armarx::control::njoint_mp_controller::task_space
armarx::control::njoint_mp_controller::task_space::NJointTSImpedanceMPController::NJointTSImpedanceMPController
NJointTSImpedanceMPController(const RobotUnitPtr &robotUnit, const NJointControllerConfigPtr &config, const VirtualRobot::RobotPtr &)
Definition: ImpedanceController.cpp:31
armarx::control::njoint_mp_controller::task_space::NJointTSImpedanceMPController::getClassName
std::string getClassName(const Ice::Current &=Ice::emptyCurrent) const override
Definition: ImpedanceController.cpp:41
armarx::control::njoint_controller::task_space::NJointTaskspaceImpedanceController
Brief description of class NJointTaskspaceImpedanceController.
Definition: ImpedanceController.h:51
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::control::njoint_controller::task_space::NJointTaskspaceImpedanceController::handleRTNotSafeInNonRT
void handleRTNotSafeInNonRT()
Definition: ImpedanceController.cpp:210
armarx::control::common::mp::JSMPInputPtr
std::shared_ptr< JSMPInput > JSMPInputPtr
Definition: JSMP.h:47
armarx::control::njoint_controller::task_space::NJointTaskspaceImpedanceController::hands
core::HandControlPtr hands
Definition: ImpedanceController.h:164
armarx::control::common::detectAndReportPoseDeviationWarning
bool detectAndReportPoseDeviationWarning(const Eigen::Matrix4f &pose1, const Eigen::Matrix4f &pose2, const std::string &namePose1, const std::string &namePose2, float positionThrMM, float angleThrDeg, const std::string &who)
Definition: utils.cpp:600
armarx::control::common::mp::MPPool::runMPs
void runMPs(const bool rtSafe)
Definition: MPPool.cpp:83
armarx::control::common::mp::JSMPOutputPtr
std::shared_ptr< JSMPOutput > JSMPOutputPtr
Definition: JSMP.h:48
armarx::control::njoint_mp_controller::task_space::registrationControllerNJointTSImpedanceMPController
NJointControllerRegistration< NJointTSImpedanceMPController > registrationControllerNJointTSImpedanceMPController("NJointTSImpedanceMPController")
armarx::control::common::mp::MPPool::resetMPs
bool resetMPs(const ::armarx::aron::data::dto::DictPtr &dto, const std::map< std::string, VirtualRobot::RobotNodeSetPtr > &rnsMap)
Definition: MPPool.cpp:500
armarx::control::common::mp::TSMPOutputPtr
std::shared_ptr< TSMPOutput > TSMPOutputPtr
Definition: TSMP.h:47
ImpedanceController.h
armarx::control::common::mp::MPPool::mtx_mps
std::recursive_mutex mtx_mps
Definition: MPPool.h:128
armarx::control::njoint_mp_controller::task_space::NJointTSImpedanceMPController::additionalTask
void additionalTask() override
Definition: ImpedanceController.cpp:60
utils.h
armarx::control::njoint_mp_controller::task_space
This file is part of ArmarX.
Definition: AdmittanceController.cpp:26
armarx::control::common::MPType::hand
@ hand
armarx::aron::data::DictPtr
std::shared_ptr< Dict > DictPtr
Definition: Dict.h:41
armarx::control::njoint_controller::task_space::NJointTaskspaceImpedanceController::controllableNodeSets
std::map< std::string, VirtualRobot::RobotNodeSetPtr > controllableNodeSets
Definition: ImpedanceController.h:165
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::control::njoint_controller::task_space::NJointTaskspaceImpedanceController::additionalTaskSetTarget
void additionalTaskSetTarget()
Definition: ImpedanceController.cpp:197
armarx::control::njoint_controller::task_space::NJointTaskspaceImpedanceController::limb
std::map< std::string, ArmPtr > limb
Definition: ImpedanceController.h:160
armarx::control::njoint_mp_controller::task_space::NJointTSImpedanceMPController::updateMPConfig
void updateMPConfig(const ::armarx::aron::data::dto::DictPtr &dto, const Ice::Current &iceCurrent=Ice::emptyCurrent) override
Definition: ImpedanceController.cpp:47
armarx::control::common::mp::MPPool
Definition: MPPool.h:47
armarx::control::common::mat4ToDVec
mplib::core::DVec mat4ToDVec(const Eigen::Matrix4f &mat)
convert Eigen:Matrix4f to 7D double vector.
Definition: utils.cpp:240
armarx::control::common::mp::MPPool::mps
std::map< std::string, MPInputOutput > mps
Definition: MPPool.h:127
armarx::control::common::mp::TSMPInputPtr
std::shared_ptr< TSMPInput > TSMPInputPtr
Definition: TSMP.h:46
ARMARX_CHECK_EQUAL
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
Definition: ExpressionException.h:130
armarx::control::njoint_controller::task_space::NJointTaskspaceImpedanceController::additionalTaskUpdateStatus
bool additionalTaskUpdateStatus()
Definition: ImpedanceController.cpp:178
armarx::control::njoint_controller::task_space::NJointTaskspaceImpedanceController::userConfig
ConfigDict userConfig
Definition: ImpedanceController.h:163
armarx::control::njoint_controller::task_space::NJointTaskspaceImpedanceController::robotUnit
RobotUnitPtr robotUnit
Definition: ImpedanceController.h:162
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18
armarx::control::common::mp::MPPool::mpConfig
MPListConfig mpConfig
this variable is only needed when constructing the MP instances, therefore you don't need to use trip...
Definition: MPPool.h:134