24 #include <VirtualRobot/MathTools.h>
36 const NJointControllerConfigPtr& config,
40 mpConfig = MPListConfig();
52 const Ice::Current& iceCurrent)
57 auto configData = MPListConfig::FromAron(dto);
59 while (additionalTaskRunning.load())
63 for (
const auto& _mp : configData.mpList)
65 if (
limb.find(_mp.nodeSetName) ==
limb.end())
67 ARMARX_WARNING <<
"You have to make sure the RobotNodeSet " << _mp.nodeSetName
68 <<
" in your MP config "
69 <<
"corresponds to one of the RobotNodeSet in the controllers";
78 ARMARX_INFO <<
"-- successfully updated MP config.";
83 "If you want to force reset, please call \n\n"
84 " ctrl->stop('all') \n\n"
85 "before you continue";
97 const Ice::Current& iceCurrent)
99 ARMARX_IMPORTANT <<
"---------------------------------- Updating BiKAC Config "
100 "-------------------------------------------";
103 auto configData = ::armarx::fromAronDict<arondto::BiKACConfig, BiKACConfig>(dto);
104 isConstraintReady.store(
false);
105 for (
const auto& _c : configData.cList)
107 ARMARX_INFO <<
"reinit mp in/output data, and checking " << _c.idx
108 <<
"-th constraint with type " << _c.type;
109 if (
limb.find(_c.rnsKpt) ==
limb.end())
111 ARMARX_WARNING <<
"You have to make sure the RobotNodeSet " << _c.rnsFrame
112 <<
" in your " << _c.idx
113 <<
"-th constraint config for a keypoint "
114 <<
"corresponds to one of the RobotNodeSet in the controllers";
118 auto& _mp =
mps.at(_c.mpName);
119 if (_c.type ==
"pose")
123 if (_mp.mp->getClassName() !=
"TSMP")
128 std::dynamic_pointer_cast<mp::TSMPInput>(_mp.input)->pose =
129 _c.currentKptPoseLocal;
130 std::dynamic_pointer_cast<mp::TSMPOutput>(_mp.output)->pose =
131 _c.currentKptPoseLocal;
138 std::dynamic_pointer_cast<mp::JSMPInput>(_mp.input)->angleRadian =
139 _c.currentKptPoseLocal.block<3, 1>(0, 3);
140 std::dynamic_pointer_cast<mp::JSMPInput>(_mp.input)->angularVel =
141 Eigen::Vector3f::Zero();
142 std::dynamic_pointer_cast<mp::JSMPOutput>(_mp.output)->angleRadian =
143 _c.currentKptPoseLocal.block<3, 1>(0, 3);
144 std::dynamic_pointer_cast<mp::JSMPOutput>(_mp.output)->angularVel =
145 Eigen::Vector3f::Zero();
148 if (_c.rnsFrame !=
"static" and
limb.find(_c.rnsFrame) ==
limb.end())
150 ARMARX_WARNING <<
"You have to make sure the RobotNodeSet " << _c.rnsFrame
151 <<
" in your " << _c.idx <<
"-th constraint config for a frame "
152 <<
"corresponds to one of the RobotNodeSet in the controllers";
157 for (
const auto&
c : configData.cList)
159 ARMARX_IMPORTANT <<
"----------------------- c -----------------------------";
166 ARMARX_IMPORTANT <<
"-------------------------------------------------------";
171 isConstraintReady.store(
true);
176 ARMARX_WARNING <<
"MP is not finished yet, cannot reset constraints \n"
177 "If you want to force reset, please call \n\n"
178 " ctrl->stop('all') \n\n"
179 "before you continue";
181 ARMARX_IMPORTANT <<
"---------------------------------- BiKAC Config Updated "
182 "-------------------------------------------";
188 return ::armarx::toAronDict<arondto::BiKACConfig, BiKACConfig>(
biKacConfig);
197 auto& _mp =
mps.at(
c.mpName);
198 auto& slaveArm =
limb.at(
c.rnsKpt);
199 slaveArm->rtStatusInNonRT = slaveArm->bufferRtToNonRt.getUpToDateReadBuffer();
200 const auto currentSlaveTCPPose = slaveArm->controller.tcp->getPoseInRootFrame();
201 c.currentKptPoseRoot = currentSlaveTCPPose *
c.poseDiffKptLocal;
202 deltaT = slaveArm->rtStatusInNonRT.deltaT;
204 if (
c.type ==
"pose")
207 const auto masterFrameInv =
c.currentFrameRoot.inverse();
208 c.currentKptPoseLocal = masterFrameInv *
c.currentKptPoseRoot;
209 c.currentKptVelocityRoot =
210 (1 -
c.keypointVelocityFilter) *
c.currentKptVelocityRoot +
211 c.keypointVelocityFilter * slaveArm->rtStatusInNonRT.currentTwist.head<3>();
214 c.currentKptVelocityLocal.head<3>() =
215 c.currentFrameRoot.block<3, 3>(0, 0).transpose() *
216 c.currentKptVelocityRoot.head<3>();
217 c.currentKptVelocityLocal.tail<3>() =
218 c.currentFrameRoot.block<3, 3>(0, 0).transpose() *
219 c.currentKptVelocityRoot.tail<3>();
222 in->pose =
c.currentKptPoseLocal;
223 in->vel =
c.currentKptVelocityLocal;
226 _mp.mp->run(_mp.input, _mp.output);
229 c.targetKptPoseLocal = out->pose;
230 c.targetKptPoseRoot =
c.currentFrameRoot * out->pose;
233 vel.head<3>() =
c.currentFrameRoot.block<3, 3>(0, 0) * out->vel.head<3>();
234 vel.tail<3>() =
c.currentFrameRoot.block<3, 3>(0, 0) * out->vel.tail<3>();
236 slaveArm->nonRtData.c.desiredPose =
237 c.targetKptPoseRoot *
c.poseDiffKptLocal.inverse();
238 slaveArm->nonRtData.c.desiredTwist = vel;
245 if (
c.rnsFrame !=
"static")
247 auto& masterArm =
limb.at(
c.rnsFrame);
248 const auto currentMasterTCPPose =
249 masterArm->controller.tcp->getPoseInRootFrame();
250 c.currentFrameRoot = currentMasterTCPPose *
c.poseDiffFrameLocal;
252 const auto masterFrameInv =
c.currentFrameRoot.inverse();
253 c.currentKptPoseLocal = masterFrameInv *
c.currentKptPoseRoot;
255 Eigen::Vector3f angular_vel = slaveArm->rtStatusInNonRT.currentTwist.tail<3>();
256 Eigen::Vector3f vecRoot =
257 currentSlaveTCPPose.block<3, 3>(0, 0) *
c.poseDiffKptLocal.block<3, 1>(0, 3);
258 c.currentKptVelocityRoot.head<3>() =
259 (1 -
c.keypointVelocityFilter) *
c.currentKptVelocityRoot.head<3>() +
260 c.keypointVelocityFilter * (angular_vel.cross(vecRoot) +
261 slaveArm->rtStatusInNonRT.currentTwist.head<3>());
263 c.currentKptVelocityLocal.head<3>() =
264 c.currentFrameRoot.block<3, 3>(0, 0).transpose() *
265 c.currentKptVelocityRoot.head<3>();
271 in->angleRadian =
c.currentKptPoseLocal.block<3, 1>(0, 3);
272 in->angularVel =
c.currentKptVelocityLocal.head<3>();
275 _mp.mp->run(_mp.input, _mp.output);
278 c.targetKptPoseLocal.block<3, 1>(0, 3) = out->angleRadian;
282 Eigen::Vector3f modified_target =
c.targetKptPoseLocal.block<3, 1>(0, 3);
283 if (
c.p2pIdx >= 0 and
c.type !=
"p2p")
286 Eigen::Vector3f p2p_local =
287 (masterFrameInv * p2p.currentKptPoseRoot).block<3, 1>(0, 3);
288 const auto x_ =
c.targetKptPoseLocal.block<3, 1>(0, 3);
289 float radius = (
c.currentKptPoseLocal.block<3, 1>(0, 3) - p2p_local).norm();
293 Eigen::Vector3f line_vec =
c.pcaComponentsLocal.row(0);
295 auto vec_mean_to_p2p = p2p_local -
c.pcaMeanLocal;
296 float proj_p2p = vec_mean_to_p2p.dot(line_vec);
297 auto proj_point_p2p =
c.pcaMeanLocal + line_vec * proj_p2p;
299 Eigen::Vector3f line_norm_to_p2p =
300 (p2p_local - proj_point_p2p).normalized();
301 float proj_x_ = (x_ - p2p_local).
dot(line_norm_to_p2p);
302 Eigen::Vector3f proj_point = p2p_local + proj_x_ * line_norm_to_p2p;
304 Eigen::Vector3f radial_vec = (x_ - proj_point).normalized();
306 proj_point = p2p_local + proj_x_ * line_norm_to_p2p;
308 sqrt(radius * radius - proj_x_ * proj_x_) * radial_vec + proj_point;
310 else if (
c.type ==
"p2P")
312 Eigen::Vector3f plane_norm_vec =
c.pcaComponentsLocal.row(2);
313 float proj_x_ = (x_ - p2p_local).
dot(plane_norm_vec);
314 Eigen::Vector3f proj_point = p2p_local + proj_x_ * plane_norm_vec;
316 Eigen::Vector3f radial_vec = (x_ - proj_point).normalized();
318 proj_point = p2p_local + proj_x_ * plane_norm_vec;
320 sqrt(radius * radius - proj_x_ * proj_x_) * radial_vec + proj_point;
323 c.targetKptPoseLocal.block<3, 1>(0, 3) = modified_target;
324 c.targetKptPoseRoot =
c.currentFrameRoot *
c.targetKptPoseLocal;
326 Eigen::Vector3f kpt_error =
327 modified_target -
c.currentKptPoseLocal.block<3, 1>(0, 3);
329 c.trackingForceLocal =
330 (1 -
c.keypointForceFilter) *
c.trackingForceLocal +
331 c.keypointForceFilter *
332 (
c.kptKp.cwiseProduct(kpt_error) -
333 c.kptKd.cwiseProduct(
c.currentKptVelocityLocal.head<3>()));
334 c.trackingForceRoot =
c.currentFrameRoot.block<3, 3>(0, 0) *
c.trackingForceLocal;
341 auto& force_torque = pair.second.forceTorque;
343 force_torque_current.setZero();
344 Eigen::Vector3f meanKeypointPositionRoot = Eigen::Vector3f::Zero();
345 for (
auto i : pair.second.constraintIdxList)
347 meanKeypointPositionRoot =
348 meanKeypointPositionRoot +
351 meanKeypointPositionRoot =
352 meanKeypointPositionRoot / pair.second.constraintIdxList.size();
354 for (
auto i : pair.second.constraintIdxList)
356 Eigen::Vector3f devi =
358 meanKeypointPositionRoot;
361 if (force.norm() > 100.0)
366 force_torque_current.head<3>() += force;
367 force_torque_current.tail<3>() += devi.cross(force);
369 force_torque = 0.1 * force_torque + 0.9 * force_torque_current;
376 0.5 *
static_cast<float>(deltaT) * (acc + pair.second.desiredAcc);
377 Eigen::VectorXf deltaPose =
378 0.5 *
static_cast<float>(deltaT) * (vel + pair.second.desiredVel);
379 pair.second.desiredAcc = acc;
380 pair.second.desiredVel = vel;
383 VirtualRobot::MathTools::rpy2eigen3f(deltaPose(3), deltaPose(4), deltaPose(5));
384 pair.second.desiredPose.block<3, 1>(0, 3) += deltaPose.head<3>();
385 pair.second.desiredPose.block<3, 3>(0, 0) =
386 deltaPoseMat * pair.second.desiredPose.block<3, 3>(0, 0);
389 auto& slaveArm =
limb.at(pair.first);
390 slaveArm->nonRtData.c.desiredPose = pair.second.desiredPose;
391 slaveArm->nonRtData.c.desiredTwist = pair.second.desiredVel;
401 for (
auto& pair :
limb)
403 rtSafe = rtSafe and pair.second->bufferRtToNonRt.getUpToDateReadBuffer().rtSafe;
407 if (isConstraintReady.load() and
isMPReady.load())
411 for (
auto& pair :
limb)
420 arm->bufferConfigNonRtToRt.getWriteBuffer() = arm->nonRtConfig;
421 arm->bufferConfigNonRtToRt.commitWrite();
457 for (
auto& pair :
limb)
469 datafields,
"c_kpt_posi_root_" + idx,
c.currentKptPoseRoot.block<3, 1>(0, 3));
471 datafields,
"c_kpt_posi_local_" + idx,
c.currentKptPoseLocal.block<3, 1>(0, 3));
473 datafields,
"t_kpt_posi_root_" + idx,
c.targetKptPoseRoot.block<3, 1>(0, 3));
475 datafields,
"t_kpt_posi_local_" + idx,
c.targetKptPoseLocal.block<3, 1>(0, 3));
493 debugObs->setDebugChannel(
"BiKAC", datafields);