CollisionAvoidanceMixedImpedanceVelocityController.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 #include <RobotAPI/components/units/RobotUnit/RobotUnit.h> // FIXME avoid this
26 
28 
30 {
31  NJointControllerRegistration<NJointTSCollisionAvoidanceMixedImpedanceVelocityMPController>
33  "NJointTSCollisionAvoidanceMixedImpedanceVelocityMPController");
34 
37  const RobotUnitPtr& robotUnit,
38  const NJointControllerConfigPtr& config,
39  const VirtualRobot::RobotPtr& robot) :
42  {
43  ARMARX_IMPORTANT << getClassName() + " construction done";
44  }
45 
46  std::string
48  const Ice::Current&) const
49  {
50  return "NJointTSCollisionAvoidanceMixedImpedanceVelocityMPController";
51  }
52 
53  void
56  const Ice::Current& iceCurrent)
57  {
59  {
60  for (auto& pair : limb)
61  {
62  pair.second->rtFirstRun.store(true);
63  }
64  }
65  }
66 
67  void
69  {
70  std::lock_guard<std::recursive_mutex> lock(mtx_mps);
71  bool rtSafe = additionalTaskUpdateStatus();
72 
73  // when ft guard is enabled and rt is not safe due to ft trigger, then we stop the corresponding mp
74  std::map<std::string, bool> flagMPToStop;
75  for (auto& pair : limb)
76  {
77  flagMPToStop[pair.first] = not limb.at(pair.first)->controller.ftsensor.ftSafe.load();
78  pair.second->nonRTDeltaT =
79  pair.second->rtStatusInNonRT.accumulateTime - pair.second->nonRTAccumulateTime;
80  pair.second->nonRTAccumulateTime = pair.second->rtStatusInNonRT.accumulateTime;
81  }
82  if (hands)
83  {
84  for (auto& pair : hands->hands)
85  {
86  pair.second->nonRTDeltaT =
87  pair.second->rtsInNonRT.accumulateTime - pair.second->nonRTAccumulateTime;
88  pair.second->nonRTAccumulateTime = pair.second->rtsInNonRT.accumulateTime;
89  }
90  }
91 
92  std::map<std::string, bool> mpRunning;
93  for (auto& _mp : mps)
94  {
95  const auto mpNodeSet = _mp.second.mp->getNodeSetName();
96  mpRunning[_mp.second.mp->getMPName()] = _mp.second.mp->isRunning();
97 
98  auto search = flagMPToStop.find(mpNodeSet);
99  if (search != flagMPToStop.end() and flagMPToStop.at(mpNodeSet) and
100  _mp.second.mp->isRunning() and
101  (limb.at(mpNodeSet)->controller.ftsensor.isForceGuardEnabled() or
102  limb.at(mpNodeSet)->controller.ftsensor.isTorqueGuardEnabled()))
103  {
104  _mp.second.mp->stop();
105  auto& force = limb.at(mpNodeSet)->rtStatusInNonRT.currentForceTorque;
106  ARMARX_INFO << "Due to force/torque safe guard, MP " << _mp.second.mp->getMPName()
107  << " is stopped at " << _mp.second.mp->getCanonicalValue()
108  << " with current ft: " << force;
109  }
110 
111  if (_mp.second.mp->isFinished())
112  {
113  continue;
114  }
115  if (_mp.second.mp->getRole() == "taskspace")
116  {
117  auto& arm = limb.at(_mp.second.mp->getNodeSetName());
118  if (_mp.second.mp->isFirstRun())
119  {
120  ARMARX_INFO << "checking TSMP initial status ...";
121  /// Here we check if the TSMP start pose is too far away from the current pose, if so, we would
122  /// prefer to reset the mp start from the current pose.
123  const std::vector<double> mpStart = _mp.second.mp->getStartVec();
124  const auto mpStartPose = common::dVecToMat4(mpStart);
125  if (_mp.second.mp->getStartFromPrevTarget() or
127  arm->rtStatusInNonRT.currentPose,
128  mpStartPose,
129  "current pose",
130  "TSMP initial start pose",
131  arm->nonRtConfig.safeDistanceMMToGoal,
132  arm->nonRtConfig.safeRotAngleDegreeToGoal,
133  _mp.second.mp->getMPName() + "mp_set_target"))
134  {
135  if (_mp.second.mp->getStartFromPrevTarget())
136  {
137  ARMARX_INFO << "User requested to start the current TS MP from the "
138  "previous target pose";
139  }
140  else
141  {
143  << "deviation from current pose too large, reset MP start pose";
144  }
145  _mp.second.mp->validateInitialState(
146  common::mat4ToDVec(arm->rtStatusInNonRT.desiredPose));
147  }
148  ARMARX_INFO << "done";
149  }
150  mp::TSMPInputPtr in = std::dynamic_pointer_cast<mp::TSMPInput>(_mp.second.input);
151  in->pose = arm->rtStatusInNonRT.currentPose;
152  in->vel = arm->rtStatusInNonRT.currentTwist;
153  // in->deltaT = arm->rtStatusInNonRT.deltaT;
154  in->deltaT = arm->nonRTDeltaT;
155  }
156  else if (_mp.second.mp->getRole() == "nullspace")
157  {
158  auto& arm = limb.at(_mp.second.mp->getNodeSetName());
159  mp::JSMPInputPtr in = std::dynamic_pointer_cast<mp::JSMPInput>(_mp.second.input);
160  ARMARX_CHECK_EQUAL(arm->rtStatusInNonRT.numJoints,
161  arm->rtStatusInNonRT.jointPos.size());
162  in->angleRadian = arm->rtStatusInNonRT.jointPos;
163  in->angularVel = arm->rtStatusInNonRT.qvelFiltered;
164  in->deltaT = arm->nonRTDeltaT;
165  }
166  else if (_mp.second.mp->getRole() == "hand")
167  {
168  auto& hand = hands->hands.at(_mp.second.mp->getNodeSetName());
169  mp::JSMPInputPtr in = std::dynamic_pointer_cast<mp::JSMPInput>(_mp.second.input);
170  in->angleRadian = hand->rtsInNonRT.jointPosition.value();
171  in->deltaT = hand->nonRTDeltaT;
172  }
173  }
174  if (not rtSafe)
175  {
177 
178  // return;
179  // Make sure that you do not call additionalTaskSetTarget before return;
180  // since we overwrite the arm->nonRtConfig in additionalTaskUpdateStatus() with user config, which does not
181  // align with the up-to-date mp status. userConfig is only updated to align with MP status everytime a MP
182  // finishes execution. Only then, user can use this controller as if no mp is there.
183  }
184  runMPs(rtSafe);
185 
186  /// set mp target to nonRT config data structure
187  for (auto& _mp : mps)
188  {
189  if (not mpRunning.at(_mp.second.mp->getMPName()))
190  {
191  continue;
192  }
193  for (auto& ftGuard : mpConfig.ftGuard)
194  {
195  if (ftGuard.mpName == _mp.second.mp->getMPName())
196  {
197  bool const forceGuard =
198  (ftGuard.force.has_value() and
199  ftGuard.force.value() >= _mp.second.mp->getCanonicalValue());
200  bool const torqueGuard =
201  (ftGuard.torque.has_value() and
202  ftGuard.torque.value() >= _mp.second.mp->getCanonicalValue());
203 
204  auto& arm = limb.at(_mp.second.mp->getNodeSetName());
205  bool const resetForce =
206  not arm->controller.ftsensor.isForceGuardEnabled() and forceGuard;
207  bool const resetTorque =
208  not arm->controller.ftsensor.isTorqueGuardEnabled() and torqueGuard;
209  if (resetForce or resetTorque)
210  {
211  ARMARX_INFO << "Triggering force torque safety guard for "
212  << arm->kinematicChainName << " at can value "
213  << _mp.second.mp->getCanonicalValue();
214  }
215 
216  arm->nonRtConfig.ftConfig.enableSafeGuardForce = forceGuard;
217  arm->nonRtConfig.ftConfig.enableSafeGuardTorque = torqueGuard;
218  arm->controller.ftsensor.enableSafeGuard(resetForce, resetTorque);
219  }
220  }
221 
222  if (_mp.second.mp->getRole() == "taskspace")
223  {
224  auto& arm = limb.at(_mp.second.mp->getNodeSetName());
225  mp::TSMPOutputPtr out =
226  std::dynamic_pointer_cast<mp::TSMPOutput>(_mp.second.output);
227 
228  /// Note that here we only report warning but still set target to RT, we rely on MP phase stop and
229  /// RT safety mechanism to stop or pause the action if needed
231  arm->rtStatusInNonRT.currentPose,
232  out->pose,
233  "current pose",
234  "TSMP pose",
235  arm->nonRtConfig.safeDistanceMMToGoal,
236  arm->nonRtConfig.safeRotAngleDegreeToGoal,
237  _mp.second.mp->getMPName() + "mp_set_target");
238  arm->nonRtConfig.desiredPose = out->pose;
239  arm->nonRtConfig.desiredTwist = out->vel;
240  }
241  else if (_mp.second.mp->getRole() == "nullspace")
242  {
243  auto& arm = limb.at(_mp.second.mp->getNodeSetName());
244  mp::JSMPOutputPtr out =
245  std::dynamic_pointer_cast<mp::JSMPOutput>(_mp.second.output);
246  ARMARX_CHECK_EQUAL(arm->rtStatusInNonRT.numJoints, out->angleRadian.size());
247  arm->nonRtConfig.desiredNullspaceJointAngles.value() = out->angleRadian;
248  }
249  else if (_mp.second.mp->getRole() == "hand")
250  {
251  mp::JSMPOutputPtr out =
252  std::dynamic_pointer_cast<mp::JSMPOutput>(_mp.second.output);
253  auto& hand = hands->hands.at(_mp.second.mp->getNodeSetName());
254  mp::JSMPInputPtr in = std::dynamic_pointer_cast<mp::JSMPInput>(_mp.second.input);
255  ARMARX_CHECK_EQUAL(hand->targetNonRT.jointPosition.value().size(),
256  out->angleRadian.size());
257  hand->targetNonRT.jointPosition.value() = out->angleRadian;
258  }
259  if (mpRunning.at(_mp.second.mp->getMPName()) and _mp.second.mp->isFinished())
260  {
261  ARMARX_INFO << "reset buffer for nonRtConfig of " << _mp.second.mp->getMPName();
262  for (auto& pair : limb)
263  {
264  auto& arm = pair.second;
265  arm->bufferConfigUserToNonRt.reinitAllBuffers(arm->nonRtConfig);
266  }
267  for (auto& pair : limb)
268  {
269  userConfig.limbs.at(pair.first) = pair.second->nonRtConfig;
270  }
271  if (hands)
272  {
273  hands->reinitBuffer(userConfig.hands);
274  }
275  }
276  }
278  }
279 } // namespace armarx::control::njoint_mp_controller::task_space
armarx::control::njoint_controller::task_space::NJointTaskspaceMixedImpedanceVelocityController::userConfig
ConfigDict userConfig
Definition: MixedImpedanceVelocityController.h:174
armarx::control::njoint_mp_controller::task_space::NJointTSCollisionAvoidanceMixedImpedanceVelocityMPController::updateMPConfig
void updateMPConfig(const ::armarx::aron::data::dto::DictPtr &dto, const Ice::Current &iceCurrent=Ice::emptyCurrent) override
Definition: CollisionAvoidanceMixedImpedanceVelocityController.cpp:54
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:190
armarx::control::njoint_controller::task_space::NJointTaskspaceMixedImpedanceVelocityController::limb
std::map< std::string, ArmPtr > limb
Definition: MixedImpedanceVelocityController.h:171
armarx::control::common::mp::JSMPInputPtr
std::shared_ptr< JSMPInput > JSMPInputPtr
Definition: JSMP.h:47
RobotUnit.h
armarx::control::njoint_controller::task_space::NJointTaskspaceMixedImpedanceVelocityController::handleRTNotSafeInNonRT
void handleRTNotSafeInNonRT()
Definition: MixedImpedanceVelocityController.cpp:255
armarx::control::njoint_controller::task_space::NJointTaskspaceMixedImpedanceVelocityController::controllableNodeSets
std::map< std::string, VirtualRobot::RobotNodeSetPtr > controllableNodeSets
Definition: MixedImpedanceVelocityController.h:176
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:646
armarx::control::njoint_controller::task_space::NJointTaskspaceCollisionAvoidanceMixedImpedanceVelocityController
Brief description of class NJointTaskspaceCollisionAvoidanceMixedImpedanceVelocityController.
Definition: CollisionAvoidanceMixedImpedanceVelocityController.h:55
armarx::control::common::mp::MPPool::runMPs
void runMPs(const bool rtSafe)
Definition: MPPool.cpp:87
armarx::control::common::mp::JSMPOutputPtr
std::shared_ptr< JSMPOutput > JSMPOutputPtr
Definition: JSMP.h:48
armarx::control::njoint_mp_controller::task_space::NJointTSCollisionAvoidanceMixedImpedanceVelocityMPController::NJointTSCollisionAvoidanceMixedImpedanceVelocityMPController
NJointTSCollisionAvoidanceMixedImpedanceVelocityMPController(const RobotUnitPtr &robotUnit, const NJointControllerConfigPtr &config, const VirtualRobot::RobotPtr &)
Definition: CollisionAvoidanceMixedImpedanceVelocityController.cpp:36
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:503
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:50
armarx::control::common::mp::MPPool::mtx_mps
std::recursive_mutex mtx_mps
Definition: MPPool.h:128
CollisionAvoidanceMixedImpedanceVelocityController.h
armarx::control::common::dVecToMat4
Eigen::Matrix4f dVecToMat4(const mplib::core::DVec &dvec)
create Eigen:Matrix4f from 7D double vector.
Definition: utils.cpp:230
armarx::control::njoint_controller::task_space::NJointTaskspaceMixedImpedanceVelocityController::additionalTaskUpdateStatus
bool additionalTaskUpdateStatus()
Definition: MixedImpedanceVelocityController.cpp:223
utils.h
armarx::control::njoint_mp_controller::task_space
This file is part of ArmarX.
Definition: AdmittanceController.cpp:29
armarx::control::njoint_mp_controller::task_space::NJointTSCollisionAvoidanceMixedImpedanceVelocityMPController::getClassName
std::string getClassName(const Ice::Current &=Ice::emptyCurrent) const override
Definition: CollisionAvoidanceMixedImpedanceVelocityController.cpp:47
armarx::control::njoint_mp_controller::task_space::NJointTSCollisionAvoidanceMixedImpedanceVelocityMPController::additionalTask
void additionalTask() override
Definition: CollisionAvoidanceMixedImpedanceVelocityController.cpp:68
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:181
IceUtil::Handle< class RobotUnit >
armarx::control::njoint_controller::task_space::NJointTaskspaceMixedImpedanceVelocityController::hands
core::HandControlPtr hands
Definition: MixedImpedanceVelocityController.h:175
armarx::control::common::mat4ToDVec
mplib::core::DVec mat4ToDVec(const Eigen::Matrix4f &mat)
convert Eigen:Matrix4f to 7D double vector.
Definition: utils.cpp:252
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:242
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:173
armarx::control::njoint_mp_controller::task_space::registrationControllerNJointTSCollisionAvoidanceMixedImpedanceVelocityMPController
NJointControllerRegistration< NJointTSCollisionAvoidanceMixedImpedanceVelocityMPController > registrationControllerNJointTSCollisionAvoidanceMixedImpedanceVelocityMPController("NJointTSCollisionAvoidanceMixedImpedanceVelocityMPController")
NJointControllerRegistry.h
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:19
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