MixedImpedanceVelocityController.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 2024
18  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
19  * GNU General Public License
20  */
21 
23 
25 
27 {
28  NJointControllerRegistration<NJointTSMixedImpedanceVelocityMPController>
30  "NJointTSMixedImpedanceVelocityMPController");
31 
33  const RobotUnitPtr& robotUnit,
34  const NJointControllerConfigPtr& config,
35  const VirtualRobot::RobotPtr& robot) :
37  {
38  ARMARX_IMPORTANT << getClassName() + " construction done";
39  }
40 
41  std::string
43  {
44  return "NJointTSMixedImpedanceVelocityMPController";
45  }
46 
47  void
50  const Ice::Current& iceCurrent)
51  {
53  {
54  for (auto& pair : limb)
55  {
56  pair.second->rtFirstRun.store(true);
57  }
58  }
59  }
60 
61  void
63  {
64  std::lock_guard<std::recursive_mutex> lock(mtx_mps);
65  bool rtSafe = additionalTaskUpdateStatus();
66 
67  std::map<std::string, bool> mpRunning;
68  for (auto& _mp : mps)
69  {
70  mpRunning[_mp.second.mp->getMPName()] = _mp.second.mp->isRunning();
71  if (_mp.second.mp->isFinished())
72  {
73  continue;
74  }
75  if (_mp.second.mp->getRole() == "taskspace")
76  {
77  auto& arm = limb.at(_mp.second.mp->getNodeSetName());
78  if (_mp.second.mp->isFirstRun())
79  {
80  ARMARX_INFO << "checking TSMP initial status ...";
81  /// Here we check if the TSMP start pose is too far away from the current pose, if so, we would
82  /// prefer to reset the mp start from the current pose.
83  const std::vector<double> mpStart = _mp.second.mp->getStartVec();
84  const auto mpStartPose = common::dVecToMat4(mpStart);
86  arm->rtStatusInNonRT.currentPose,
87  mpStartPose,
88  "current pose",
89  "TSMP initial start pose",
90  arm->nonRtConfig.safeDistanceMMToGoal,
91  arm->nonRtConfig.safeRotAngleDegreeToGoal,
92  _mp.second.mp->getMPName() + "mp_set_target"))
93  {
94  ARMARX_INFO << "deviation from current pose too large, reset MP start pose";
95  _mp.second.mp->validateInitialState(
96  common::mat4ToDVec(arm->rtStatusInNonRT.currentPose));
97  }
98  ARMARX_INFO << "done";
99  }
100  mp::TSMPInputPtr in = std::dynamic_pointer_cast<mp::TSMPInput>(_mp.second.input);
101  in->pose = arm->rtStatusInNonRT.currentPose;
102  in->vel = arm->rtStatusInNonRT.currentTwist;
103  in->deltaT = arm->rtStatusInNonRT.deltaT;
104  }
105  else if (_mp.second.mp->getRole() == "nullspace")
106  {
107  auto& arm = limb.at(_mp.second.mp->getNodeSetName());
108  mp::JSMPInputPtr in = std::dynamic_pointer_cast<mp::JSMPInput>(_mp.second.input);
109  ARMARX_CHECK_EQUAL(arm->rtStatusInNonRT.numJoints,
110  arm->rtStatusInNonRT.jointPos.size());
111  in->angleRadian = arm->rtStatusInNonRT.jointPos;
112  in->angularVel = arm->rtStatusInNonRT.qvelFiltered;
113  in->deltaT = arm->rtStatusInNonRT.deltaT;
114  }
115  else if (_mp.second.mp->getRole() == "hand")
116  {
117  auto& hand = hands->hands.at(_mp.second.mp->getNodeSetName());
118  mp::JSMPInputPtr in = std::dynamic_pointer_cast<mp::JSMPInput>(_mp.second.input);
119  in->angleRadian =
120  hand->bufferRTToNonRT.getUpToDateReadBuffer().jointPosition.value();
121  in->deltaT = hand->bufferRTToNonRT.getReadBuffer().deltaT;
122  }
123  }
124  if (not rtSafe)
125  {
127 
128  // return;
129  // Make sure that you do not call additionalTaskSetTarget before return;
130  // since we overwrite the arm->nonRtConfig in additionalTaskUpdateStatus() with user config, which does not
131  // align with the up-to-date mp status. userConfig is only updated to align with MP status everytime a MP
132  // finishes execution. Only then, user can use this controller as if no mp is there.
133  }
134  runMPs(rtSafe);
135 
136  /// set mp target to nonRT config data structure
137  for (auto& _mp : mps)
138  {
139  if (not mpRunning.at(_mp.second.mp->getMPName()))
140  {
141  continue;
142  }
143  for (auto& ftGuard : mpConfig.ftGuard)
144  {
145  if (ftGuard.mpName == _mp.second.mp->getMPName())
146  {
147  bool const forceGuard =
148  (ftGuard.force.has_value() and
149  ftGuard.force.value() >= _mp.second.mp->getCanonicalValue());
150  bool const torqueGuard =
151  (ftGuard.torque.has_value() and
152  ftGuard.torque.value() >= _mp.second.mp->getCanonicalValue());
153 
154  auto& arm = limb.at(_mp.second.mp->getNodeSetName());
155  bool const resetForce =
156  not arm->controller.ftsensor.isForceGuardEnabled() and forceGuard;
157  bool const resetTorque =
158  not arm->controller.ftsensor.isTorqueGuardEnabled() and torqueGuard;
159  if (resetForce or resetTorque)
160  {
161  ARMARX_INFO << "Triggering force torque safety guard for "
162  << arm->kinematicChainName << " at can value "
163  << _mp.second.mp->getCanonicalValue();
164  }
165 
166  arm->nonRtConfig.ftConfig.enableSafeGuardForce = forceGuard;
167  arm->nonRtConfig.ftConfig.enableSafeGuardTorque = torqueGuard;
168  arm->controller.ftsensor.enableSafeGuard(resetForce, resetTorque);
169  }
170  }
171 
172  if (_mp.second.mp->getRole() == "taskspace")
173  {
174  auto& arm = limb.at(_mp.second.mp->getNodeSetName());
175  mp::TSMPOutputPtr out =
176  std::dynamic_pointer_cast<mp::TSMPOutput>(_mp.second.output);
177 
178  /// Note that here we only report warning but still set target to RT, we rely on MP phase stop and
179  /// RT safety mechanism to stop or pause the action if needed
181  arm->rtStatusInNonRT.currentPose,
182  out->pose,
183  "current pose",
184  "TSMP pose",
185  arm->nonRtConfig.safeDistanceMMToGoal,
186  arm->nonRtConfig.safeRotAngleDegreeToGoal,
187  _mp.second.mp->getMPName() + "mp_set_target");
188  arm->nonRtConfig.desiredPose = out->pose;
189  arm->nonRtConfig.desiredTwist = out->vel;
190  }
191  else if (_mp.second.mp->getRole() == "nullspace")
192  {
193  auto& arm = limb.at(_mp.second.mp->getNodeSetName());
194  mp::JSMPOutputPtr out =
195  std::dynamic_pointer_cast<mp::JSMPOutput>(_mp.second.output);
196  ARMARX_CHECK_EQUAL(arm->rtStatusInNonRT.numJoints, out->angleRadian.size());
197  arm->nonRtConfig.desiredNullspaceJointAngles.value() = out->angleRadian;
198  }
199  else if (_mp.second.mp->getRole() == "hand")
200  {
201  mp::JSMPOutputPtr out =
202  std::dynamic_pointer_cast<mp::JSMPOutput>(_mp.second.output);
203  auto& hand = hands->hands.at(_mp.second.mp->getNodeSetName());
204  mp::JSMPInputPtr in = std::dynamic_pointer_cast<mp::JSMPInput>(_mp.second.input);
205  ARMARX_CHECK_EQUAL(hand->targetNonRT.jointPosition.value().size(),
206  out->angleRadian.size());
207  hand->targetNonRT.jointPosition.value() = out->angleRadian;
208  }
209  if (mpRunning.at(_mp.second.mp->getMPName()) and _mp.second.mp->isFinished())
210  {
211  ARMARX_INFO << "reset buffer for nonRtConfig of " << _mp.second.mp->getMPName();
212  for (auto& pair : limb)
213  {
214  auto& arm = pair.second;
215  arm->bufferConfigUserToNonRt.reinitAllBuffers(arm->nonRtConfig);
216  }
217  for (auto& pair : limb)
218  {
219  userConfig.limbs.at(pair.first) = pair.second->nonRtConfig;
220  }
221  if (hands)
222  {
223  hands->reinitBuffer(userConfig.hands);
224  }
225  }
226  }
228  }
229 } // namespace armarx::control::njoint_mp_controller::task_space
armarx::control::njoint_controller::task_space::NJointTaskspaceMixedImpedanceVelocityController::userConfig
ConfigDict userConfig
Definition: MixedImpedanceVelocityController.h:171
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::control::njoint_controller::task_space::NJointTaskspaceMixedImpedanceVelocityController::limb
std::map< std::string, ArmPtr > limb
Definition: MixedImpedanceVelocityController.h:168
armarx::control::njoint_mp_controller::task_space::registrationControllerNJointTSMixedImpedanceVelocityMPController
NJointControllerRegistration< NJointTSMixedImpedanceVelocityMPController > registrationControllerNJointTSMixedImpedanceVelocityMPController("NJointTSMixedImpedanceVelocityMPController")
armarx::control::common::mp::JSMPInputPtr
std::shared_ptr< JSMPInput > JSMPInputPtr
Definition: JSMP.h:47
armarx::control::njoint_controller::task_space::NJointTaskspaceMixedImpedanceVelocityController::handleRTNotSafeInNonRT
void handleRTNotSafeInNonRT()
Definition: MixedImpedanceVelocityController.cpp:247
armarx::control::njoint_controller::task_space::NJointTaskspaceMixedImpedanceVelocityController::controllableNodeSets
std::map< std::string, VirtualRobot::RobotNodeSetPtr > controllableNodeSets
Definition: MixedImpedanceVelocityController.h:173
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::njoint_mp_controller::task_space::NJointTSMixedImpedanceVelocityMPController::additionalTask
void additionalTask() override
Definition: MixedImpedanceVelocityController.cpp:62
armarx::control::common::mp::MPPool::runMPs
void runMPs(const bool rtSafe)
Definition: MPPool.cpp:83
armarx::control::njoint_mp_controller::task_space::NJointTSMixedImpedanceVelocityMPController::getClassName
std::string getClassName(const Ice::Current &=Ice::emptyCurrent) const override
Definition: MixedImpedanceVelocityController.cpp:42
armarx::control::common::mp::JSMPOutputPtr
std::shared_ptr< JSMPOutput > JSMPOutputPtr
Definition: JSMP.h:48
MixedImpedanceVelocityController.h
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
armarx::control::njoint_controller::task_space::NJointTaskspaceMixedImpedanceVelocityController
Brief description of class NJointTaskspaceMixedImpedanceVelocityController.
Definition: MixedImpedanceVelocityController.h:51
armarx::control::common::mp::MPPool::mtx_mps
std::recursive_mutex mtx_mps
Definition: MPPool.h:128
armarx::control::common::dVecToMat4
Eigen::Matrix4f dVecToMat4(const mplib::core::DVec &dvec)
create Eigen:Matrix4f from 7D double vector.
Definition: utils.cpp:218
armarx::control::njoint_controller::task_space::NJointTaskspaceMixedImpedanceVelocityController::additionalTaskUpdateStatus
bool additionalTaskUpdateStatus()
Definition: MixedImpedanceVelocityController.cpp:215
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_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::control::njoint_mp_controller::task_space::NJointTSMixedImpedanceVelocityMPController::NJointTSMixedImpedanceVelocityMPController
NJointTSMixedImpedanceVelocityMPController(const RobotUnitPtr &robotUnit, const NJointControllerConfigPtr &config, const VirtualRobot::RobotPtr &)
Definition: MixedImpedanceVelocityController.cpp:32
armarx::control::njoint_controller::task_space::NJointTaskspaceMixedImpedanceVelocityController::hands
core::HandControlPtr hands
Definition: MixedImpedanceVelocityController.h:172
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::njoint_controller::task_space::NJointTaskspaceMixedImpedanceVelocityController::additionalTaskSetTarget
void additionalTaskSetTarget()
Definition: MixedImpedanceVelocityController.cpp:234
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::NJointTaskspaceMixedImpedanceVelocityController::robotUnit
RobotUnitPtr robotUnit
Definition: MixedImpedanceVelocityController.h:170
armarx::control::njoint_mp_controller::task_space::NJointTSMixedImpedanceVelocityMPController::updateMPConfig
void updateMPConfig(const ::armarx::aron::data::dto::DictPtr &dto, const Ice::Current &iceCurrent=Ice::emptyCurrent) override
Definition: MixedImpedanceVelocityController.cpp:48
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