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();
99 rt2CtrlData.reinitAllBuffers(initSensorData);
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;
141 initInterfaceData.currentTcpPose = Eigen::Matrix4f::Identity();
142 rt2UserData.reinitAllBuffers(initInterfaceData);
159 if (!rt2CtrlData.updateReadBuffer() || !dmpCtrl)
164 double deltaT = rt2CtrlData.getReadBuffer().deltaT;
165 Eigen::Matrix4f currentPose = rt2CtrlData.getReadBuffer().currentPose;
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);
196 VirtualRobot::MathTools::Quaternion currentQ =
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;
207 debugOutputData.getWriteBuffer().deltaT = deltaT;
209 debugOutputData.commitWrite();
218 const Eigen::VectorXf& nullspaceVel,
219 VirtualRobot::IKSolver::CartesianSelection mode)
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;
248 const IceUtil::Time& timeSinceLastIteration)
250 Eigen::Matrix4f currentPose = refFrame->toLocalCoordinateSystem(tcp->getGlobalPose());
251 rt2UserData.getWriteBuffer().currentTcpPose = currentPose;
252 rt2UserData.commitWrite();
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;
285 rt2CtrlData.getWriteBuffer().currentPose = currentPose;
286 rt2CtrlData.getWriteBuffer().currentTwist = tcptwist;
287 rt2CtrlData.getWriteBuffer().deltaT = deltaT;
288 rt2CtrlData.getWriteBuffer().currentTime += deltaT;
289 rt2CtrlData.commitWrite();
291 rt2UserData.getWriteBuffer().currentTcpPose = currentPose;
292 rt2UserData.commitWrite();
297 Eigen::VectorXf jointTargetVelocities = Eigen::VectorXf::Zero(targets.size());
303 Eigen::Matrix3f diffMat =
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;
368 rtDebugData.commitWrite();
576 std::string datafieldName = debugName;
578 auto values = debugOutputData.getUpToDateReadBuffer().latestTargetVelocities;
579 for (
auto& pair : values)
581 datafieldName = pair.first +
"_" + debugName;
582 datafields[datafieldName] =
new Variant(pair.second);
585 auto dmpTargets = debugOutputData.getUpToDateReadBuffer().dmpTargets;
586 for (
auto& pair : dmpTargets)
588 datafieldName = pair.first +
"_" + debugName;
589 datafields[datafieldName] =
new Variant(pair.second);
592 auto currentPose = debugOutputData.getUpToDateReadBuffer().currentPose;
593 for (
auto& pair : currentPose)
595 datafieldName = pair.first +
"_" + debugName;
596 datafields[datafieldName] =
new Variant(pair.second);
599 datafieldName =
"canVal_" + debugName;
600 datafields[datafieldName] =
601 new Variant(debugOutputData.getUpToDateReadBuffer().currentCanVal);
602 datafieldName =
"mpcFactor_" + debugName;
603 datafields[datafieldName] =
new Variant(debugOutputData.getUpToDateReadBuffer().mpcFactor);
604 datafieldName =
"error_" + debugName;
605 datafields[datafieldName] =
new Variant(debugOutputData.getUpToDateReadBuffer().error);
606 datafieldName =
"posError_" + debugName;
607 datafields[datafieldName] =
new Variant(debugOutputData.getUpToDateReadBuffer().posError);
608 datafieldName =
"oriError_" + debugName;
609 datafields[datafieldName] =
new Variant(debugOutputData.getUpToDateReadBuffer().oriError);
610 datafieldName =
"deltaT_" + debugName;
611 datafields[datafieldName] =
new Variant(debugOutputData.getUpToDateReadBuffer().deltaT);
614 Eigen::VectorXf targetJoints = rtDebugData.getUpToDateReadBuffer().targetJointVels;
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);