3 #include <boost/archive/text_iarchive.hpp>
4 #include <boost/archive/text_oarchive.hpp>
6 #include <VirtualRobot/MathTools.h>
7 #include <VirtualRobot/Nodes/RobotNode.h>
8 #include <VirtualRobot/RobotNodeSet.h>
9 #include <VirtualRobot/IK/DifferentialIK.h>
15 NJointControllerRegistration<NJointTSDMPController>
19 const armarx::NJointControllerConfigPtr& config,
23 cfg = NJointTaskSpaceDMPControllerConfigPtr::dynamicCast(config);
25 VirtualRobot::RobotNodeSetPtr rns =
rtGetRobot()->getRobotNodeSet(cfg->nodeSetName);
26 jointNames = rns->getNodeNames();
28 for (
size_t i = 0; i < rns->getSize(); ++i)
30 std::string jointName = rns->getNode(i)->getName();
33 targets.push_back(ct->
asA<ControlTarget1DoFActuatorVelocity>());
35 const SensorValue1DoFActuatorTorque* torqueSensor =
36 sv->
asA<SensorValue1DoFActuatorTorque>();
37 const SensorValue1DoFActuatorVelocity* velocitySensor =
38 sv->
asA<SensorValue1DoFActuatorVelocity>();
39 const SensorValue1DoFGravityTorque* gravityTorqueSensor =
40 sv->
asA<SensorValue1DoFGravityTorque>();
41 const SensorValue1DoFActuatorPosition* positionSensor =
42 sv->
asA<SensorValue1DoFActuatorPosition>();
47 if (!gravityTorqueSensor)
49 ARMARX_WARNING <<
"No Gravity Torque sensor available for " << jointName;
52 torqueSensors.push_back(torqueSensor);
53 gravityTorqueSensors.push_back(gravityTorqueSensor);
54 velocitySensors.push_back(velocitySensor);
55 positionSensors.push_back(positionSensor);
58 tcp = (cfg->tcpName.empty()) ? rns->getTCP() :
rtGetRobot()->getRobotNode(cfg->tcpName);
59 refFrame = (cfg->frameName.empty()) ? rns->getRobot()->getRootNode()
65 nodeSetName = cfg->nodeSetName;
66 torquePIDs.resize(tcpController->rns->getSize(), pidController());
68 ik.reset(
new VirtualRobot::DifferentialIK(
69 rns, refFrame, VirtualRobot::JacobiProvider::eSVDDamped));
76 taskSpaceDMPConfig.
DMPMode = cfg->dmpMode;
77 taskSpaceDMPConfig.
DMPStyle = cfg->dmpStyle;
94 RTToControllerData initSensorData;
95 initSensorData.deltaT = 0;
96 initSensorData.currentTime = 0;
97 initSensorData.currentPose.setZero();
98 initSensorData.currentTwist.setZero();
101 targetVels.setZero(6);
104 initData.
targetPose = refFrame->toLocalCoordinateSystem(tcp->getGlobalPose());
106 initData.
torqueKp.resize(tcpController->rns->getSize(), 0);
107 initData.
torqueKd.resize(tcpController->rns->getSize(), 0);
111 debugName = cfg->debugName;
113 KpF = cfg->Kp_LinearVel;
114 KoF = cfg->Kp_AngularVel;
115 DpF = cfg->Kd_LinearVel;
116 DoF = cfg->Kd_AngularVel;
118 filtered_qvel.setZero(targets.size());
119 vel_filter_factor = cfg->vel_filter;
121 filtered_position.setZero(3);
122 pos_filter_factor = cfg->pos_filter;
128 jointLowLimits.setZero(targets.size());
129 jointHighLimits.setZero(targets.size());
130 for (
size_t i = 0; i < rns->getSize(); i++)
132 VirtualRobot::RobotNodePtr rn = rns->getAllRobotNodes().at(i);
134 jointLowLimits(i) = rn->getJointLimitLo();
135 jointHighLimits(i) = rn->getJointLimitHi();
140 RTToUserData initInterfaceData;
148 return "NJointTSDMPController";
166 Eigen::VectorXf currentTwist = rt2CtrlData.
getReadBuffer().currentTwist;
169 dmpCtrl->flow(deltaT, currentPose, currentTwist);
171 if (dmpCtrl->canVal < 1e-8)
175 targetVels = dmpCtrl->getTargetVelocity();
176 targetPose = dmpCtrl->getTargetPoseMat();
177 std::vector<double> targetState = dmpCtrl->getTargetPose();
179 debugOutputData.
getWriteBuffer().latestTargetVelocities[
"x_vel"] = targetVels(0);
180 debugOutputData.
getWriteBuffer().latestTargetVelocities[
"y_vel"] = targetVels(1);
181 debugOutputData.
getWriteBuffer().latestTargetVelocities[
"z_vel"] = targetVels(2);
182 debugOutputData.
getWriteBuffer().latestTargetVelocities[
"roll_vel"] = targetVels(3);
183 debugOutputData.
getWriteBuffer().latestTargetVelocities[
"pitch_vel"] = targetVels(4);
184 debugOutputData.
getWriteBuffer().latestTargetVelocities[
"yaw_vel"] = targetVels(5);
185 debugOutputData.
getWriteBuffer().dmpTargets[
"dmp_x"] = targetState[0];
186 debugOutputData.
getWriteBuffer().dmpTargets[
"dmp_y"] = targetState[1];
187 debugOutputData.
getWriteBuffer().dmpTargets[
"dmp_z"] = targetState[2];
188 debugOutputData.
getWriteBuffer().dmpTargets[
"dmp_qw"] = targetState[3];
189 debugOutputData.
getWriteBuffer().dmpTargets[
"dmp_qx"] = targetState[4];
190 debugOutputData.
getWriteBuffer().dmpTargets[
"dmp_qy"] = targetState[5];
191 debugOutputData.
getWriteBuffer().dmpTargets[
"dmp_qz"] = targetState[6];
192 debugOutputData.
getWriteBuffer().currentPose[
"currentPose_x"] = currentPose(0, 3);
193 debugOutputData.
getWriteBuffer().currentPose[
"currentPose_y"] = currentPose(1, 3);
194 debugOutputData.
getWriteBuffer().currentPose[
"currentPose_z"] = currentPose(2, 3);
197 VirtualRobot::MathTools::eigen4f2quat(currentPose);
198 debugOutputData.
getWriteBuffer().currentPose[
"currentPose_qw"] = currentQ.w;
199 debugOutputData.
getWriteBuffer().currentPose[
"currentPose_qx"] = currentQ.x;
200 debugOutputData.
getWriteBuffer().currentPose[
"currentPose_qy"] = currentQ.y;
201 debugOutputData.
getWriteBuffer().currentPose[
"currentPose_qz"] = currentQ.z;
202 debugOutputData.
getWriteBuffer().currentCanVal = dmpCtrl->debugData.canVal;
203 debugOutputData.
getWriteBuffer().mpcFactor = dmpCtrl->debugData.mpcFactor;
204 debugOutputData.
getWriteBuffer().error = dmpCtrl->debugData.poseError;
205 debugOutputData.
getWriteBuffer().posError = dmpCtrl->debugData.posiError;
206 debugOutputData.
getWriteBuffer().oriError = dmpCtrl->debugData.oriError;
218 const Eigen::VectorXf& nullspaceVel,
221 Eigen::MatrixXf jacobi = ik->getJacobianMatrix(tcp, mode);
223 Eigen::FullPivLU<Eigen::MatrixXf> lu_decomp(jacobi);
225 Eigen::MatrixXf nullspace = lu_decomp.kernel();
226 Eigen::VectorXf nsv = Eigen::VectorXf::Zero(nullspace.rows());
227 for (
int i = 0; i < nullspace.cols(); i++)
229 float squaredNorm = nullspace.col(i).squaredNorm();
231 if (squaredNorm > 1.0e-32f)
233 nsv += nullspace.col(i) * nullspace.col(i).dot(nullspaceVel) /
234 nullspace.col(i).squaredNorm();
238 Eigen::MatrixXf inv =
239 ik->computePseudoInverseJacobianMatrix(jacobi, ik->getJacobiRegularization(mode));
241 Eigen::VectorXf jointVel = inv * cartesianVel;
250 Eigen::Matrix4f currentPose = refFrame->toLocalCoordinateSystem(tcp->getGlobalPose());
256 filtered_position = currentPose.block<3, 1>(0, 3);
259 for (
size_t i = 0; i < targets.size(); ++i)
261 targets.at(i)->velocity = 0;
267 filtered_position = (1 - pos_filter_factor) * filtered_position +
268 pos_filter_factor * currentPose.block<3, 1>(0, 3);
271 double deltaT = timeSinceLastIteration.toSecondsDouble();
273 Eigen::MatrixXf jacobi =
274 ik->getJacobianMatrix(tcp, VirtualRobot::IKSolver::CartesianSelection::All);
276 Eigen::VectorXf qvel(velocitySensors.size());
277 for (
size_t i = 0; i < velocitySensors.size(); ++i)
279 qvel(i) = velocitySensors[i]->velocity;
282 filtered_qvel = (1 - vel_filter_factor) * filtered_qvel + vel_filter_factor * qvel;
283 Eigen::VectorXf tcptwist = jacobi * filtered_qvel;
297 Eigen::VectorXf jointTargetVelocities = Eigen::VectorXf::Zero(targets.size());
304 targetPose.block<3, 3>(0, 0) * currentPose.block<3, 3>(0, 0).inverse();
305 Eigen::Vector3f errorRPY = VirtualRobot::MathTools::eigen3f2rpy(diffMat);
308 rtTargetVel.block<3, 1>(0, 0) =
309 KpF * (targetPose.block<3, 1>(0, 3) - currentPose.block<3, 1>(0, 3)) +
310 DpF * (-tcptwist.block<3, 1>(0, 0));
312 rtTargetVel.block<3, 1>(3, 0) =
313 KoF * errorRPY + DoF * (targetVel.block<3, 1>(3, 0) - tcptwist.block<3, 1>(3, 0));
317 float normLinearVelocity = rtTargetVel.block<3, 1>(0, 0).
norm();
318 if (normLinearVelocity > cfg->maxLinearVel)
320 rtTargetVel.block<3, 1>(0, 0) =
321 cfg->maxLinearVel * rtTargetVel.block<3, 1>(0, 0) / normLinearVelocity;
324 float normAngularVelocity = rtTargetVel.block<3, 1>(3, 0).
norm();
325 if (normAngularVelocity > cfg->maxAngularVel)
327 rtTargetVel.block<3, 1>(3, 0) =
328 cfg->maxAngularVel * rtTargetVel.block<3, 1>(3, 0) / normAngularVelocity;
339 Eigen::VectorXf jnv = Eigen::VectorXf::Zero(tcpController->rns->getSize());
341 if (jointLimitAvoidanceKp > 0)
343 jnv += jointLimitAvoidanceKp * tcpController->calculateJointLimitAvoidance();
345 for (
size_t i = 0; i < tcpController->rns->getSize(); i++)
351 jointTargetVelocities =
352 calcIK(rtTargetVel, jnv, VirtualRobot::IKSolver::CartesianSelection::All);
358 for (
size_t i = 0; i < targets.size(); ++i)
360 targets.at(i)->velocity = jointTargetVelocities(i);
362 if (!targets.at(i)->isValid() || fabs(targets.at(i)->velocity) > cfg->maxJointVelocity)
364 targets.at(i)->velocity = 0.0f;
367 rtDebugData.
getWriteBuffer().targetJointVels = jointTargetVelocities;
377 dmpCtrl->learnDMPFromFiles(fileNames);
384 dmpCtrl->setSpeed(times);
389 const Ice::DoubleSeq& viapoint,
393 dmpCtrl->setViaPose(u, viapoint);
400 for (
size_t i = 0; i < tcpController->rns->getSize(); i++)
403 torqueKp.at(tcpController->rns->getNode(i)->getName());
410 const StringFloatDictionary& nullspaceJointVelocities,
414 for (
size_t i = 0; i < tcpController->rns->getSize(); i++)
417 nullspaceJointVelocities.at(tcpController->rns->getNode(i)->getName());
439 dmpCtrl->removeAllViaPoints();
446 dmpCtrl->setGoalPoseVec(goals);
452 dmpCtrl->pauseController();
458 dmpCtrl->resumeController();
481 return dmpCtrl->saveDMPToString();
487 dmpCtrl->loadDMPFromString(dmpString);
488 return dmpCtrl->dmpPtr->defaultGoal;
505 return VirtualRobot::IKSolver::CartesianSelection::All;
529 dmpCtrl->prepareExecution(dmpCtrl->eigen4f2vec(pose), goals);
554 dmpCtrl->config.motionTimeDuration = timeDuration;
555 dmpCtrl->prepareExecution(dmpCtrl->eigen4f2vec(pose), goals);
576 std::string datafieldName = debugName;
581 datafieldName = pair.first +
"_" + debugName;
582 datafields[datafieldName] =
new Variant(pair.second);
586 for (
auto& pair : dmpTargets)
588 datafieldName = pair.first +
"_" + debugName;
589 datafields[datafieldName] =
new Variant(pair.second);
593 for (
auto& pair : currentPose)
595 datafieldName = pair.first +
"_" + debugName;
596 datafields[datafieldName] =
new Variant(pair.second);
599 datafieldName =
"canVal_" + debugName;
600 datafields[datafieldName] =
602 datafieldName =
"mpcFactor_" + debugName;
604 datafieldName =
"error_" + debugName;
606 datafieldName =
"posError_" + debugName;
608 datafieldName =
"oriError_" + debugName;
610 datafieldName =
"deltaT_" + debugName;
615 for (
int i = 0; i < targetJoints.size(); ++i)
617 datafieldName = jointNames[i] +
"_velocity";
618 datafields[datafieldName] =
new Variant(targetJoints[i]);
621 datafieldName =
"DMPController_" + debugName;
622 debugObs->setDebugChannel(datafieldName, datafields);
630 runTask(
"NJointTSDMPController",
635 while (
getState() == eManagedIceObjectStarted)
641 c.waitForCycleDuration();
655 dmpCtrl->setWeights(weights);
661 DMP::DVec2d res = dmpCtrl->getWeights();
663 for (
size_t i = 0; i < res.size(); ++i)
665 std::vector<double> cvec;
666 for (
size_t j = 0; j < res[i].size(); ++j)
668 cvec.push_back(res[i][j]);
670 resvec.push_back(cvec);