11 "DeprecatedNJointTaskSpaceImpedanceDMPController");
14 const armarx::NJointControllerConfigPtr& config,
19 cfg = DeprecatedNJointTaskSpaceImpedanceDMPControllerConfigPtr::dynamicCast(config);
22 rns =
rtGetRobot()->getRobotNodeSet(cfg->nodeSetName);
25 for (
size_t i = 0; i < rns->getSize(); ++i)
27 std::string jointName = rns->getNode(i)->getName();
28 jointNames.push_back(jointName);
31 targets.push_back(ct->
asA<ControlTarget1DoFActuatorTorque>());
32 const SensorValue1DoFActuatorVelocity* velocitySensor = sv->
asA<SensorValue1DoFActuatorVelocity>();
33 const SensorValue1DoFActuatorPosition* positionSensor = sv->
asA<SensorValue1DoFActuatorPosition>();
44 velocitySensors.push_back(velocitySensor);
45 positionSensors.push_back(positionSensor);
47 const SensorValueBase* svlf = robotUnit->getSensorDevice(cfg->forceSensorName)->getSensorValue();
51 forceOffset.setZero();
52 filteredForce.setZero();
53 filteredForceInRoot.setZero();
57 ik.reset(
new VirtualRobot::DifferentialIK(rns,
rtGetRobot()->getRootNode(), VirtualRobot::JacobiProvider::eSVDDamped));
58 ik->setDampedSvdLambda(0.0001);
61 numOfJoints = targets.size();
66 taskSpaceDMPConfig.
DMPMode = cfg->dmpMode;
67 taskSpaceDMPConfig.
DMPStyle = cfg->dmpType;
82 useNullSpaceJointDMP = cfg->useNullSpaceJointDMP;
83 nullSpaceJointDMPPtr.reset(
new DMP::UMIDMP(100));
85 isNullSpaceJointDMPLearned =
false;
87 Eigen::VectorXf nullspaceValues(targets.size());
91 for (
size_t i = 0; i < targets.size(); ++i)
93 nullspaceValues(i) = cfg->defaultNullSpaceJointValues.at(i);
98 Eigen::Vector3f kpos(cfg->Kpos[0], cfg->Kpos[1], cfg->Kpos[2]);
99 Eigen::Vector3f dpos(cfg->Dpos[0], cfg->Dpos[1], cfg->Dpos[2]);
100 Eigen::Vector3f kori(cfg->Kori[0], cfg->Kori[1], cfg->Kori[2]);
101 Eigen::Vector3f dori(cfg->Dori[0], cfg->Dori[1], cfg->Dori[2]);
102 Eigen::VectorXf knull(targets.size());
103 Eigen::VectorXf dnull(targets.size());
108 for (
size_t i = 0; i < targets.size(); ++i)
110 knull(i) = cfg->Knull.at(i);
111 dnull(i) = cfg->Dnull.at(i);
114 CtrlParams initParams = {kpos, dpos, kori, dori, knull, dnull};
117 torqueLimit = cfg->torqueLimit;
118 timeDuration = cfg->timeDuration;
120 DeprecatedNJointTaskSpaceImpedanceDMPControllerInterfaceData initInterfaceData;
124 DeprecatedNJointTaskSpaceImpedanceDMPControllerSensorData initControllerSensorData;
126 initControllerSensorData.currentTime = 0;
127 initControllerSensorData.deltaT = 0;
128 initControllerSensorData.currentTwist.setZero();
132 useForceStop =
false;
139 return "DeprecatedNJointTaskSpaceImpedanceDMPController";
148 initData.
targetPose = tcp->getPoseInRootFrame();
171 double deltaT = 0.001;
173 Eigen::VectorXf currentTwist = controllerSensorData.
getReadBuffer().currentTwist;
200 if (dmpCtrl->canVal < 1e-8)
210 dmpCtrl->flow(deltaT, currentPose, currentTwist);
212 Eigen::VectorXf desiredNullSpaceJointValues(jointNames.size());
213 if (useNullSpaceJointDMP && isNullSpaceJointDMPLearned)
216 currentJointState = nullSpaceJointDMPPtr->calculateDirectlyVelocity(currentJointState,
217 dmpCtrl->canVal / timeDuration,
218 deltaT / timeDuration,
221 if (targetJointState.size() == jointNames.size())
223 for (
size_t i = 0; i < targetJointState.size(); ++i)
225 desiredNullSpaceJointValues(i) = targetJointState[i];
256 double deltaT = timeSinceLastIteration.toSecondsDouble();
258 Eigen::VectorXf targetVel;
259 Eigen::VectorXf desiredNullSpaceJointValues;
264 targetPose = currentPose;
265 stopPose = currentPose;
266 targetVel.setZero(6);
273 targetPose = stopPose;
274 targetVel.setZero(6);
276 forceOffset = (1 - cfg->forceFilter) * forceOffset + cfg->forceFilter * forceSensor->
force;
277 timeForCalibration = timeForCalibration + timeSinceLastIteration.toSecondsDouble();
284 stopPose = targetPose;
289 filteredForce = (1 - cfg->forceFilter) * filteredForce + cfg->forceFilter * (forceSensor->
force - forceOffset);
291 for (
size_t i = 0; i < 3; ++i)
293 if (fabs(filteredForce(i)) > cfg->forceDeadZone)
295 filteredForce(i) -= (filteredForce(i) / fabs(filteredForce(i))) * cfg->forceDeadZone;
299 filteredForce(i) = 0;
303 filteredForceInRoot = forceFrameInRoot.block<3, 3>(0, 0) * filteredForce;
305 for (
size_t i = 0; i < 3; ++i)
309 stopPose = currentPose;
310 targetVel.setZero(6);
323 Eigen::MatrixXf jacobi = ik->getJacobianMatrix(tcp, VirtualRobot::IKSolver::CartesianSelection::All);
325 Eigen::VectorXf qpos(positionSensors.size());
326 Eigen::VectorXf qvel(velocitySensors.size());
327 for (
size_t i = 0; i < positionSensors.size(); ++i)
329 qpos(i) = positionSensors[i]->position;
330 qvel(i) = velocitySensors[i]->velocity;
333 Eigen::VectorXf currentTwist = jacobi * qvel;
336 controllerSensorData.
getWriteBuffer().currentTwist = currentTwist;
344 jacobi.block(0, 0, 3, numOfJoints) = 0.001 * jacobi.block(0, 0, 3, numOfJoints);
355 Eigen::Vector3f targetTCPLinearVelocity;
356 targetTCPLinearVelocity << 0.001 * targetVel(0), 0.001 * targetVel(1), 0.001 * targetVel(2);
357 Eigen::Vector3f currentTCPLinearVelocity;
358 currentTCPLinearVelocity << 0.001 * currentTwist(0), 0.001 * currentTwist(1), 0.001 * currentTwist(2);
359 Eigen::Vector3f currentTCPPosition = currentPose.block<3, 1>(0, 3);
360 Eigen::Vector3f desiredPosition = targetPose.block<3, 1>(0, 3);
361 Eigen::Vector3f tcpDesiredForce = 0.001 * kpos.cwiseProduct(desiredPosition - currentTCPPosition) +
362 dpos.cwiseProduct(targetTCPLinearVelocity - currentTCPLinearVelocity);
364 Eigen::Vector3f currentTCPAngularVelocity;
365 currentTCPAngularVelocity << currentTwist(3), currentTwist(4), currentTwist(5);
367 Eigen::Matrix3f diffMat = targetPose.block<3, 3>(0, 0) * currentRotMat.inverse();
368 Eigen::Vector3f rpy = VirtualRobot::MathTools::eigen3f2rpy(diffMat);
369 Eigen::Vector3f tcpDesiredTorque = kori.cwiseProduct(rpy) - dori.cwiseProduct(currentTCPAngularVelocity);
370 jointControlWrench << tcpDesiredForce, tcpDesiredTorque;
375 Eigen::VectorXf nullspaceTorque =
376 knull.cwiseProduct(desiredNullSpaceJointValues - qpos) - dnull.cwiseProduct(qvel);
377 Eigen::MatrixXf jtpinv = ik->computePseudoInverseJacobianMatrix(jacobi.transpose(), 2.0);
378 Eigen::VectorXf jointDesiredTorques =
379 jacobi.transpose() * jointControlWrench + (I - jacobi.transpose() * jtpinv) * nullspaceTorque;
385 for (
size_t i = 0; i < targets.size(); ++i)
387 float desiredTorque = jointDesiredTorques(i);
389 if (isnan(desiredTorque))
394 desiredTorque = (desiredTorque > torqueLimit) ? torqueLimit : desiredTorque;
395 desiredTorque = (desiredTorque < -torqueLimit) ? -torqueLimit : desiredTorque;
397 debugOutputData.
getWriteBuffer().desired_torques[jointNames[i]] = jointDesiredTorques(i);
398 debugOutputData.
getWriteBuffer().desired_nullspaceJoint[jointNames[i]] = desiredNullSpaceJointValues(i);
400 targets.at(i)->torque = desiredTorque;
401 if (!targets.at(i)->isValid())
404 << targets.at(i)->torque;
405 targets.at(i)->torque = 0.0f;
410 debugOutputData.
getWriteBuffer().forceDesired_x = jointControlWrench(0);
411 debugOutputData.
getWriteBuffer().forceDesired_y = jointControlWrench(1);
412 debugOutputData.
getWriteBuffer().forceDesired_z = jointControlWrench(2);
413 debugOutputData.
getWriteBuffer().forceDesired_rx = jointControlWrench(3);
414 debugOutputData.
getWriteBuffer().forceDesired_ry = jointControlWrench(4);
415 debugOutputData.
getWriteBuffer().forceDesired_rz = jointControlWrench(5);
430 debugOutputData.
getWriteBuffer().currentPose_x = currentPose(0, 3);
431 debugOutputData.
getWriteBuffer().currentPose_y = currentPose(1, 3);
432 debugOutputData.
getWriteBuffer().currentPose_z = currentPose(2, 3);
460 debugOutputData.
getWriteBuffer().filteredForceInRoot = filteredForceInRoot;
469 dmpCtrl->learnDMPFromFiles(fileNames);
478 dmpCtrl->setViaPose(u, viapoint);
484 dmpCtrl->setGoalPoseVec(goals);
489 const Ice::FloatSeq& currentJVS,
492 DMP::Vec<DMP::SampledTrajectoryV2> trajs;
494 DMP::SampledTrajectoryV2 traj;
495 traj.readFromCSVFile(fileName);
497 if (traj.dim() != jointNames.size())
499 isNullSpaceJointDMPLearned =
false;
504 goal.resize(traj.dim());
505 currentJointState.resize(traj.dim());
507 for (
size_t i = 0; i < goal.size(); ++i)
509 goal.at(i) = traj.rbegin()->getPosition(i);
510 currentJointState.at(i).pos = currentJVS.at(i);
511 currentJointState.at(i).vel = 0;
514 trajs.push_back(traj);
515 nullSpaceJointDMPPtr->learnFromTrajectories(trajs);
518 nullSpaceJointDMPPtr->prepareExecution(goal, currentJointState, 1.0, 1.0);
520 isNullSpaceJointDMPLearned =
true;
543 return dmpCtrl->saveDMPToString();
548 dmpCtrl->loadDMPFromString(dmpString);
549 return dmpCtrl->dmpPtr->defaultGoal;
554 return dmpCtrl->canVal;
564 useNullSpaceJointDMP = enable;
571 dmpCtrl->canVal = timeDuration;
572 dmpCtrl->config.motionTimeDuration = timeDuration;
580 timeForCalibration = 0;
583 while (firstRun || timeForCalibration < cfg->waitTimeForCalibration)
600 dmpCtrl->prepareExecution(dmpCtrl->eigen4f2vec(pose), goals);
603 if (isNullSpaceJointDMPLearned && useNullSpaceJointDMP)
621 datafields[
"torqueDesired_" + pair.first] =
new Variant(pair.second);
625 for (
auto& pair : values_null)
627 datafields[
"nullspaceDesired_" + pair.first] =
new Variant(pair.second);
681 std::string channelName = cfg->nodeSetName +
"_TaskSpaceImpedanceControl";
682 debugObs->setDebugChannel(channelName, datafields);
689 runTask(
"DeprecatedNJointTaskSpaceImpedanceDMPController", [&]
693 while (
getState() == eManagedIceObjectStarted)
699 c.waitForCycleDuration();
711 dmpCtrl->setWeights(weights);
716 DMP::DVec2d res = dmpCtrl->getWeights();
718 for (
size_t i = 0; i < res.size(); ++i)
720 std::vector<double> cvec;
721 for (
size_t j = 0; j < res[i].size(); ++j)
723 cvec.push_back(res[i][j]);
725 resvec.push_back(cvec);
735 dmpCtrl->removeAllViaPoints();