25 #include <boost/archive/text_iarchive.hpp>
26 #include <boost/archive/text_oarchive.hpp>
27 #include <boost/archive/xml_iarchive.hpp>
28 #include <boost/archive/xml_oarchive.hpp>
30 #include <Eigen/Geometry>
32 #include <VirtualRobot/MathTools.h>
33 #include <VirtualRobot/RobotNodeSet.h>
41 #include <dmp/representation/dmp/umitsmp.h>
49 const Eigen::VectorXf& twist)
58 const Eigen::VectorXf& twist)
75 Eigen::Vector3f currentPosition;
80 getError(currentPose, currentPosition, currentOrientation, posiError, oriError);
96 double phaseStop = phaseL / (1 + exp(-phaseK * (poseError - phaseDist)));
97 double mpcFactor = 1 - (phaseStop / phaseL);
101 isDisturbance =
true;
106 isDisturbance =
false;
110 canVal -= tau * deltaT * 1;
113 DMP::Vec<DMP::DMPState> temporalState =
dmpPtr->calculateDirectlyVelocity(
114 currentState,
canVal / timeDuration, deltaT / timeDuration, targetPoseVec);
117 for (
size_t i = 0; i < 3; ++i)
119 currentState[i].vel = tau * temporalState[i].vel *
config.
DMPAmplitude / timeDuration;
120 currentState[i].pos += deltaT * currentState[i].vel;
128 Eigen::Vector3f linearVel;
129 linearVel << twist(0), twist(1), twist(2);
130 for (
size_t i = 0; i < 3; i++)
132 vel0 = currentState[i].vel;
135 targetVel(i) = mpcFactor * vel0 + (1 - mpcFactor) * vel1;
140 for (
size_t i = 0; i < 3; i++)
142 targetVel(i) = currentState[i].vel;
149 dmpQuaternionVel.w() = temporalState[3].vel;
150 dmpQuaternionVel.x() = temporalState[4].vel;
151 dmpQuaternionVel.y() = temporalState[5].vel;
152 dmpQuaternionVel.z() = temporalState[6].vel;
155 dmpQuaternionPosi.w() = currentState[3].pos;
156 dmpQuaternionPosi.x() = currentState[4].pos;
157 dmpQuaternionPosi.y() = currentState[5].pos;
158 dmpQuaternionPosi.z() = currentState[6].pos;
162 angularVel0.w() *= 2;
163 angularVel0.x() *= 2;
164 angularVel0.y() *= 2;
165 angularVel0.z() *= 2;
168 double angularChange = angularVel0.angularDistance(oldDMPAngularVelocity);
169 if (angularVel0.w() * oldDMPAngularVelocity.w() < 0 &&
170 angularVel0.x() * oldDMPAngularVelocity.x() < 0 &&
171 angularVel0.y() * oldDMPAngularVelocity.y() < 0 &&
172 angularVel0.z() * oldDMPAngularVelocity.z() < 0 && angularChange < 1e-2)
174 angularVel0.w() = -angularVel0.w();
175 angularVel0.x() = -angularVel0.x();
176 angularVel0.y() = -angularVel0.y();
177 angularVel0.z() = -angularVel0.z();
179 oldDMPAngularVelocity = angularVel0;
193 for (
size_t i = 3; i < 7; ++i)
195 currentState[i].vel = tau * temporalState[i].vel *
config.
oriAmplitude / timeDuration;
196 currentState[i].pos += currentState[i].vel * deltaT;
201 Eigen::Vector3f currentAngularVel;
202 currentAngularVel << twist(3), twist(4), twist(5);
205 targetQuaternionf.w() = targetPoseVec[3];
206 targetQuaternionf.x() = targetPoseVec[4];
207 targetQuaternionf.y() = targetPoseVec[5];
208 targetQuaternionf.z() = targetPoseVec[6];
213 Eigen::Vector3f diffRPY = VirtualRobot::MathTools::eigen3f2rpy(diffMat);
218 mpcFactor * angularVel0.x() / timeDuration + (1 - mpcFactor) * angularVel1(0);
220 mpcFactor * angularVel0.y() / timeDuration + (1 - mpcFactor) * angularVel1(1);
222 mpcFactor * angularVel0.z() / timeDuration + (1 - mpcFactor) * angularVel1(2);
226 targetVel(3) = angularVel0.x();
227 targetVel(4) = angularVel0.y();
228 targetVel(5) = angularVel0.z();
242 const std::vector<double>& ratios)
244 if (ratios.size() != fileNames.size())
246 ARMARX_ERROR <<
"ratios should have the same size with files";
251 DMP::Vec<DMP::SampledTrajectoryV2> trajs;
254 for (
size_t i = 0; i < fileNames.size(); ++i)
256 DMP::SampledTrajectoryV2 traj;
259 traj.readFromCSVFile(absPath);
261 trajs.push_back(traj);
263 ratioSum += ratios.at(i);
268 ARMARX_ERROR <<
"ratios are invalid. The sum is equal to 0";
273 ratiosVec.resize(ratios.size());
274 for (
size_t i = 0; i < ratios.size(); ++i)
276 ratiosVec.at(i) = ratios.at(i) / ratioSum;
279 dmpPtr->learnFromTrajectories(trajs);
280 dmpPtr->styleParas =
dmpPtr->getStyleParasWithRatio(ratiosVec);
286 std::vector<double> ratios;
287 for (
size_t i = 0; i < fileNames.size(); ++i)
291 ratios.push_back(1.0);
295 ratios.push_back(0.0);
304 const DMP::Vec<DMP::SampledTrajectoryV2>& trajs,
305 const std::vector<double>& ratios)
307 dmpPtr->learnFromTrajectories(trajs);
308 dmpPtr->styleParas =
dmpPtr->getStyleParasWithRatio(ratios);
314 dmpPtr->styleParas =
dmpPtr->getStyleParasWithRatio(ratios);
321 DMP::SampledTrajectoryV2 dmpTraj;
323 DMP::DVec timestamps(traj->getTimestamps());
324 for (
size_t i = 0; i < traj->dim(); ++i)
326 DMP::DVec dimData(traj->getDimensionData(i, 0));
327 dmpTraj.addDimension(timestamps, dimData);
330 DMP::Vec<DMP::SampledTrajectoryV2> trajs;
333 trajs.push_back(dmpTraj);
335 ratiosVec.push_back(1.0);
336 dmpPtr->learnFromTrajectories(trajs);
337 dmpPtr->styleParas =
dmpPtr->getStyleParasWithRatio(ratiosVec);
343 std::stringstream ss;
345 boost::archive::text_iarchive ia{ss};
346 DMP::UMITSMP* dmpPointer;
354 std::stringstream ss;
355 boost::archive::text_oarchive oa{ss};
365 DMP::UMITSMP* dmpPointer;
366 std::ifstream ifs(absPath);
367 boost::archive::xml_iarchive ai(ifs);
368 ai >> boost::serialization::make_nvp(
"dmp", dmpPointer);
377 std::ofstream ofs(absPath);
378 boost::archive::xml_oarchive ar(ofs);
379 DMP::UMITSMP* dmpPointer =
dmpPtr.get();
380 ar << boost::serialization::make_nvp(
"dmp", dmpPointer);
393 const std::vector<double>& viaPoseWithQuaternion)
395 if (canVal <= dmpPtr->getUMin())
397 goalPoseVec = viaPoseWithQuaternion;
405 dmpPtr->removeViaPoints();
412 std::vector<double> currentPoseVec =
eigen4f2vec(currentPose);
413 std::vector<double> goalPoseVec =
eigen4f2vec(goalPose);
420 const std::vector<double>& goalPoseVec)
426 for (
size_t i = 0; i < currentPoseVec.size(); ++i)
428 currentState[i].pos = currentPoseVec.at(i);
429 currentState[i].vel = 0;
430 targetPoseVec.at(i) = currentPoseVec.at(i);
433 dmpPtr->prepareExecution(goalPoseVec, currentState, 1, 1);
434 this->goalPoseVec = goalPoseVec;
435 isDisturbance =
false;
437 oldDMPAngularVelocity.setIdentity();
445 ARMARX_WARNING <<
"TaskSpaceDMPController: cannot set non-positive speed times";
456 ARMARX_WARNING <<
"TaskSpaceDMPController: cannot set non-positive amplitude";
464 std::vector<double> viaPoseVec;
465 viaPoseVec.resize(7);
467 for (
size_t i = 0; i < 3; ++i)
469 viaPoseVec.at(i) = pose(i, 3);
474 viaPoseVec.at(3) = quat.w;
475 viaPoseVec.at(4) = quat.x;
476 viaPoseVec.at(5) = quat.y;
477 viaPoseVec.at(6) = quat.z;
484 Eigen::Vector3f& currentPosition,
489 currentPosition.setZero();
490 currentPosition << currentPose(0, 3), currentPose(1, 3), currentPose(2, 3);
493 VirtualRobot::MathTools::eigen4f2quat(currentPose);
494 currentOrientation.w() = quat.w;
495 currentOrientation.x() = quat.x;
496 currentOrientation.y() = quat.y;
497 currentOrientation.z() = quat.z;
500 for (
size_t i = 0; i < 3; ++i)
502 posiError += pow(currentPosition(i) - targetPoseVec[i], 2);
504 posiError =
sqrt(posiError);
507 targetQuaternion.w() = targetPoseVec[3];
508 targetQuaternion.x() = targetPoseVec[4];
509 targetQuaternion.y() = targetPoseVec[5];
510 targetQuaternion.z() = targetPoseVec[6];
512 oriError = targetQuaternion.angularDistance(currentOrientation);
515 oriError = 2 *
M_PI - oriError;
521 bool isPhaseStopControl)
539 targetPoseVec.resize(7);
542 currentState.resize(7);
546 this->paused =
false;
565 return targetPoseVec;
572 targetPoseVec.at(4), targetPoseVec.at(5), targetPoseVec.at(6), targetPoseVec.at(3));
573 res(0, 3) = targetPoseVec.at(0);
574 res(1, 3) = targetPoseVec.at(1);
575 res(2, 3) = targetPoseVec.at(2);
583 Eigen::Matrix4f res = VirtualRobot::MathTools::quat2eigen4f(currentState.at(4).pos,
584 currentState.at(5).pos,
585 currentState.at(6).pos,
586 currentState.at(3).pos);
587 res(0, 3) = currentState.at(0).pos;
588 res(1, 3) = currentState.at(1).pos;
589 res(2, 3) = currentState.at(2).pos;
621 this->paused =
false;
627 dmpPtr->setWeights(weights);
635 for (
size_t i = 0; i < 3; ++i)
637 dmpPtr->setWeights(i, weights[i]);
646 for (
size_t i = 0; i < 4; ++i)
648 dmpPtr->setWeights(3 + i, weights[i]);
655 return dmpPtr->getWeights();
663 for (
size_t i = 0; i < 3; ++i)
665 res.push_back(weights[i]);
675 for (
size_t i = 3; i < 7; ++i)
677 res.push_back(weights[i]);