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>
39 const Eigen::VectorXf& twist)
48 const Eigen::VectorXf& twist)
65 Eigen::Vector3f currentPosition;
70 getError(currentPose, currentPosition, currentOrientation, posiError, oriError);
86 double phaseStop = phaseL / (1 + exp(-phaseK * (poseError - phaseDist)));
87 double mpcFactor = 1 - (phaseStop / phaseL);
96 isDisturbance =
false;
100 canVal -= tau * deltaT * 1;
103 DMP::Vec<DMP::DMPState> temporalState =
dmpPtr->calculateDirectlyVelocity(
104 currentState,
canVal / timeDuration, deltaT / timeDuration, targetPoseVec);
107 for (
size_t i = 0; i < 3; ++i)
109 currentState[i].vel = tau * temporalState[i].vel *
config.
DMPAmplitude / timeDuration;
110 currentState[i].pos += deltaT * currentState[i].vel;
118 Eigen::Vector3f linearVel;
119 linearVel << twist(0), twist(1), twist(2);
120 for (
size_t i = 0; i < 3; i++)
122 vel0 = currentState[i].vel;
125 targetVel(i) = mpcFactor * vel0 + (1 - mpcFactor) * vel1;
130 for (
size_t i = 0; i < 3; i++)
132 targetVel(i) = currentState[i].vel;
139 dmpQuaternionVel.w() = temporalState[3].vel;
140 dmpQuaternionVel.x() = temporalState[4].vel;
141 dmpQuaternionVel.y() = temporalState[5].vel;
142 dmpQuaternionVel.z() = temporalState[6].vel;
145 dmpQuaternionPosi.w() = currentState[3].pos;
146 dmpQuaternionPosi.x() = currentState[4].pos;
147 dmpQuaternionPosi.y() = currentState[5].pos;
148 dmpQuaternionPosi.z() = currentState[6].pos;
152 angularVel0.w() *= 2;
153 angularVel0.x() *= 2;
154 angularVel0.y() *= 2;
155 angularVel0.z() *= 2;
158 double angularChange = angularVel0.angularDistance(oldDMPAngularVelocity);
159 if (angularVel0.w() * oldDMPAngularVelocity.w() < 0 &&
160 angularVel0.x() * oldDMPAngularVelocity.x() < 0 &&
161 angularVel0.y() * oldDMPAngularVelocity.y() < 0 &&
162 angularVel0.z() * oldDMPAngularVelocity.z() < 0 && angularChange < 1e-2)
164 angularVel0.w() = -angularVel0.w();
165 angularVel0.x() = -angularVel0.x();
166 angularVel0.y() = -angularVel0.y();
167 angularVel0.z() = -angularVel0.z();
169 oldDMPAngularVelocity = angularVel0;
183 for (
size_t i = 3; i < 7; ++i)
185 currentState[i].vel = tau * temporalState[i].vel *
config.
oriAmplitude / timeDuration;
186 currentState[i].pos += currentState[i].vel * deltaT;
191 Eigen::Vector3f currentAngularVel;
192 currentAngularVel << twist(3), twist(4), twist(5);
195 targetQuaternionf.w() = targetPoseVec[3];
196 targetQuaternionf.x() = targetPoseVec[4];
197 targetQuaternionf.y() = targetPoseVec[5];
198 targetQuaternionf.z() = targetPoseVec[6];
203 Eigen::Vector3f diffRPY = VirtualRobot::MathTools::eigen3f2rpy(diffMat);
208 mpcFactor * angularVel0.x() / timeDuration + (1 - mpcFactor) * angularVel1(0);
210 mpcFactor * angularVel0.y() / timeDuration + (1 - mpcFactor) * angularVel1(1);
212 mpcFactor * angularVel0.z() / timeDuration + (1 - mpcFactor) * angularVel1(2);
216 targetVel(3) = angularVel0.x();
217 targetVel(4) = angularVel0.y();
218 targetVel(5) = angularVel0.z();
232 const std::vector<double>& ratios)
234 if (ratios.size() != fileNames.size())
236 ARMARX_ERROR <<
"ratios should have the same size with files";
241 DMP::Vec<DMP::SampledTrajectoryV2> trajs;
244 for (
size_t i = 0; i < fileNames.size(); ++i)
246 DMP::SampledTrajectoryV2 traj;
249 traj.readFromCSVFile(absPath);
251 trajs.push_back(traj);
253 ratioSum += ratios.at(i);
258 ARMARX_ERROR <<
"ratios are invalid. The sum is equal to 0";
263 ratiosVec.resize(ratios.size());
264 for (
size_t i = 0; i < ratios.size(); ++i)
266 ratiosVec.at(i) = ratios.at(i) / ratioSum;
269 dmpPtr->learnFromTrajectories(trajs);
270 dmpPtr->styleParas =
dmpPtr->getStyleParasWithRatio(ratiosVec);
276 std::vector<double> ratios;
277 for (
size_t i = 0; i < fileNames.size(); ++i)
281 ratios.push_back(1.0);
285 ratios.push_back(0.0);
294 const DMP::Vec<DMP::SampledTrajectoryV2>& trajs,
295 const std::vector<double>& ratios)
297 dmpPtr->learnFromTrajectories(trajs);
298 dmpPtr->styleParas =
dmpPtr->getStyleParasWithRatio(ratios);
304 dmpPtr->styleParas =
dmpPtr->getStyleParasWithRatio(ratios);
311 DMP::SampledTrajectoryV2 dmpTraj;
313 DMP::DVec timestamps(traj->getTimestamps());
314 for (
size_t i = 0; i < traj->dim(); ++i)
316 DMP::DVec dimData(traj->getDimensionData(i, 0));
317 dmpTraj.addDimension(timestamps, dimData);
320 DMP::Vec<DMP::SampledTrajectoryV2> trajs;
323 trajs.push_back(dmpTraj);
325 ratiosVec.push_back(1.0);
326 dmpPtr->learnFromTrajectories(trajs);
327 dmpPtr->styleParas =
dmpPtr->getStyleParasWithRatio(ratiosVec);
333 std::stringstream ss;
335 boost::archive::text_iarchive ia{ss};
336 DMP::UMITSMP* dmpPointer;
345 std::stringstream ss;
346 boost::archive::text_oarchive oa{ss};
356 DMP::UMITSMP* dmpPointer;
357 std::ifstream ifs(absPath);
358 boost::archive::xml_iarchive ai(ifs);
359 ai >> boost::serialization::make_nvp(
"dmp", dmpPointer);
369 std::ofstream ofs(absPath);
370 boost::archive::xml_oarchive ar(ofs);
371 DMP::UMITSMP* dmpPointer =
dmpPtr.get();
372 ar << boost::serialization::make_nvp(
"dmp", dmpPointer);
385 const std::vector<double>& viaPoseWithQuaternion)
387 if (canVal <= dmpPtr->getUMin())
389 goalPoseVec = viaPoseWithQuaternion;
397 dmpPtr->removeViaPoints();
404 std::vector<double> currentPoseVec =
eigen4f2vec(currentPose);
405 std::vector<double> goalPoseVec =
eigen4f2vec(goalPose);
412 const std::vector<double>& goalPoseVec)
418 for (
size_t i = 0; i < currentPoseVec.size(); ++i)
420 currentState[i].pos = currentPoseVec.at(i);
421 currentState[i].vel = 0;
422 targetPoseVec.at(i) = currentPoseVec.at(i);
425 dmpPtr->prepareExecution(goalPoseVec, currentState, 1, 1);
426 this->goalPoseVec = goalPoseVec;
427 isDisturbance =
false;
429 oldDMPAngularVelocity.setIdentity();
437 ARMARX_WARNING <<
"TaskSpaceDMPController: cannot set non-positive speed times";
448 ARMARX_WARNING <<
"TaskSpaceDMPController: cannot set non-positive amplitude";
456 std::vector<double> viaPoseVec;
457 viaPoseVec.resize(7);
459 for (
size_t i = 0; i < 3; ++i)
461 viaPoseVec.at(i) = pose(i, 3);
466 viaPoseVec.at(3) = quat.w;
467 viaPoseVec.at(4) = quat.x;
468 viaPoseVec.at(5) = quat.y;
469 viaPoseVec.at(6) = quat.z;
476 Eigen::Vector3f& currentPosition,
481 currentPosition.setZero();
482 currentPosition << currentPose(0, 3), currentPose(1, 3), currentPose(2, 3);
485 VirtualRobot::MathTools::eigen4f2quat(currentPose);
486 currentOrientation.w() = quat.w;
487 currentOrientation.x() = quat.x;
488 currentOrientation.y() = quat.y;
489 currentOrientation.z() = quat.z;
492 for (
size_t i = 0; i < 3; ++i)
494 posiError += pow(currentPosition(i) - targetPoseVec[i], 2);
496 posiError =
sqrt(posiError);
499 targetQuaternion.w() = targetPoseVec[3];
500 targetQuaternion.x() = targetPoseVec[4];
501 targetQuaternion.y() = targetPoseVec[5];
502 targetQuaternion.z() = targetPoseVec[6];
504 oriError = targetQuaternion.angularDistance(currentOrientation);
507 oriError = 2 *
M_PI - oriError;