57 const Eigen::Matrix4f& currentPose,
58 const Eigen::VectorXf& twist)
75 Eigen::Vector3f currentPosition;
80 getError(currentPose, currentPosition, currentOrientation, posiError, oriError);
82 double poseError = posiError +
config.phaseStopParas.mm2radi * oriError;
87 phaseDist =
config.phaseStopParas.backDist;
91 phaseDist =
config.phaseStopParas.goDist;
93 double phaseL =
config.phaseStopParas.maxValue;
94 double phaseK =
config.phaseStopParas.slop;
96 double phaseStop = phaseL / (1 + exp(-phaseK * (poseError - phaseDist)));
97 double mpcFactor = 1 - (phaseStop / phaseL);
101 isDisturbance =
true;
106 isDisturbance =
false;
109 double timeDuration =
config.motionTimeDuration;
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;
133 vel1 =
config.phaseStopParas.Kpos * (targetPoseVec[i] - currentPosition(i)) -
134 config.phaseStopParas.Dpos * linearVel(i);
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;
183 angularVel0.x() = tau * angularVel0.x() *
config.oriAmplitude / timeDuration;
184 angularVel0.y() = tau * angularVel0.y() *
config.oriAmplitude / timeDuration;
185 angularVel0.z() = tau * angularVel0.z() *
config.oriAmplitude / timeDuration;
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];
210 Eigen::Matrix3f desiredMat(targetQuaternionf);
211 Eigen::Matrix3f currentMat = currentPose.block<3, 3>(0, 0);
212 Eigen::Matrix3f diffMat = desiredMat * currentMat.inverse();
213 Eigen::Vector3f diffRPY = VirtualRobot::MathTools::eigen3f2rpy(diffMat);
214 Eigen::Vector3f angularVel1 =
config.phaseStopParas.Kori * diffRPY -
215 config.phaseStopParas.Dori * currentAngularVel;
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);
260 traj = DMP::SampledTrajectoryV2::normalizeTimestamps(traj, 0, 1);
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);