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