3 #include <SimoxUtility/json.h>
4 #include <SimoxUtility/math/compare/is_equal.h>
5 #include <SimoxUtility/math/convert/mat4f_to_pos.h>
6 #include <SimoxUtility/math/convert/mat4f_to_quat.h>
7 #include <SimoxUtility/math/convert/rpy_to_mat3f.h>
8 #include <VirtualRobot/IK/DifferentialIK.h>
9 #include <VirtualRobot/IK/JacobiProvider.h>
10 #include <VirtualRobot/MathTools.h>
11 #include <VirtualRobot/Robot.h>
12 #include <VirtualRobot/RobotNodeSet.h>
31 ik.reset(
new VirtualRobot::DifferentialIK(
32 rns, rns->getRobot()->getRootNode(), VirtualRobot::JacobiProvider::eSVDDamped));
35 numOfJoints = rns->getSize();
43 ARMARX_INFO <<
"preactivate control law KeypointsImpedanceController";
48 s.qpos = rns->getJointValuesEigen();
49 s.qvel.setZero(numOfJoints);
51 s.currentPose = currentPose;
52 s.currentTwist.setZero();
54 s.desiredPose = currentPose;
55 s.desiredVel.setZero();
56 s.desiredAcc.setZero();
58 s.jacobi.setZero(6, numOfJoints);
59 s.jtpinv.setZero(6, numOfJoints);
64 for (
int i = 0; i <
c.numPoints; i++)
66 s.currentKeypointPosition.segment(3 * i, 3) = currentPose.block(0, 3, 3, 1);
68 s.currentKeypointPosition =
s.currentKeypointPosition +
c.fixedTranslation;
71 s.currentKeypointVelocity.setZero(
c.numPoints * 3);
73 s.pointTrackingForce.setZero();
74 s.filteredKeypointPosition =
s.currentKeypointPosition;
75 s.previousKeypointPosition =
s.currentKeypointPosition;
77 s.desiredKeypointPosition =
s.currentKeypointPosition;
78 s.desiredKeypointVelocity.setZero(
c.numPoints * 3);
79 s.desiredDensityForce.setZero(
c.numPoints * 3);
88 s.desiredPose = currentPose;
89 s.desiredTwist.setZero();
91 s.forceImpedance.setZero();
93 s.nullspaceTorque.setZero(numOfJoints);
94 s.desiredJointTorques.setZero(numOfJoints);
106 sRt.desiredPose = sNoneRt.currentPose;
107 sRt.desiredTwist.setZero();
109 sRt.forceImpedance.setZero();
111 sRt.nullspaceTorque.setZero();
112 sRt.desiredJointTorques.setZero();
137 s.currentPose =
tcp->getPoseInRootFrame();
138 Eigen::MatrixXf jacobi =
139 ik->getJacobianMatrix(
tcp, VirtualRobot::IKSolver::CartesianSelection::All);
140 jacobi.block(0, 0, 3, numOfJoints) = 0.001 * jacobi.block(0, 0, 3, numOfJoints);
145 s.jtpinv = ik->computePseudoInverseJacobianMatrix(
s.jacobi.transpose(), lambda);
149 Eigen::VectorXf qvelRaw(numOfJoints);
150 for (
size_t i = 0; i < robotStatus.
jointVelocity.size(); ++i)
158 s.currentTwist = jacobi *
s.qvel;
166 s.currentKeypointPosition.segment(3 * i, 3) =
168 s.currentPose.block<3, 1>(0, 3);
172 s.filteredKeypointPosition =
179 Eigen::VectorXf currentKeypointVelocity;
180 currentKeypointVelocity.setZero(cfg.
numPoints * 3);
183 Eigen::Vector3f angular_vel =
s.currentTwist.tail<3>();
185 currentKeypointVelocity.segment(3 * i, 3) =
186 angular_vel.cross(dist) +
s.currentTwist.head<3>();
188 s.currentKeypointVelocity =
194 s.currentKeypointVelocity =
197 (
s.filteredKeypointPosition -
s.previousKeypointPosition) /
198 static_cast<float>(robotStatus.
deltaT);
200 s.previousKeypointPosition =
s.filteredKeypointPosition;
203 auto difference =
s.desiredKeypointPosition -
s.filteredKeypointPosition;
205 Eigen::VectorXf trackingForce = difference.cwiseProduct(cfg.
keypointKp) -
206 s.currentKeypointVelocity.cwiseProduct(cfg.
keypointKd);
207 if (trackingForce.size() != 3 * cfg.
numPoints)
209 trackingForce.setZero(3 * cfg.
numPoints);
212 Eigen::Vector3f meanKeypointPosition = Eigen::Vector3f::Zero();
215 meanKeypointPosition =
216 meanKeypointPosition +
s.filteredKeypointPosition.segment(3 * i, 3);
218 meanKeypointPosition = meanKeypointPosition / cfg.
numPoints;
220 s.pointTrackingForce.setZero();
224 Eigen::Vector3f dist =
225 s.filteredKeypointPosition.segment(3 * i, 3) - meanKeypointPosition;
227 Eigen::Vector3f force =
228 trackingForce.segment(3 * i, 3) +
s.desiredDensityForce.segment(3 * i, 3);
229 if (force.norm() > 1000)
234 s.pointTrackingForce.head<3>() += force;
235 s.pointTrackingForce.tail<3>() += dist.cross(force);
241 s.desiredVel + 0.5 *
static_cast<float>(robotStatus.
deltaT) * (acc +
s.desiredAcc);
242 Eigen::VectorXf deltaPose =
243 0.5 *
static_cast<float>(robotStatus.
deltaT) * (vel +
s.desiredVel);
248 VirtualRobot::MathTools::rpy2eigen3f(deltaPose(3), deltaPose(4), deltaPose(5));
249 s.desiredPose.block<3, 1>(0, 3) += deltaPose.head<3>();
250 s.desiredPose.block<3, 3>(0, 0) = deltaPoseMat *
s.desiredPose.block<3, 3>(0, 0);
271 if ((sRt.desiredPose.block<3, 1>(0, 3) -
s.desiredPose.block<3, 1>(0, 3)).norm() >
280 sRt.desiredPose =
s.desiredPose;
290 sRt.desiredPose.block<3, 3>(0, 0) *
s.currentPose.block<3, 3>(0, 0).transpose();
292 poseErrorImp.head<3>() =
293 0.001 * (sRt.desiredPose.block<3, 1>(0, 3) -
s.currentPose.block<3, 1>(0, 3));
294 poseErrorImp.tail<3>() = VirtualRobot::MathTools::eigen3f2rpy(diffMat);
296 c.kpImpedance.cwiseProduct(poseErrorImp) -
c.kdImpedance.cwiseProduct(
s.currentTwist);
303 Eigen::VectorXf nullspaceTorque =
304 c.kpNullspace.cwiseProduct(
c.desiredNullspaceJointAngles.value() -
s.qpos) -
305 c.kdNullspace.cwiseProduct(
s.qvel);
314 sRt.desiredJointTorques =
s.jacobi.transpose() * sRt.forceImpedance +
315 (I -
s.jacobi.transpose() *
s.jtpinv) * nullspaceTorque;
318 for (
size_t i = 0; i < (unsigned)sRt.desiredJointTorques.rows(); ++i)
320 sRt.desiredJointTorques(i) =
321 std::clamp(sRt.desiredJointTorques(i), -
c.torqueLimit,
c.torqueLimit);
322 targets.
targets.at(i) = sRt.desiredJointTorques(i);