2 #include <boost/algorithm/clamp.hpp>
4 #include <SimoxUtility/math/convert/rpy_to_mat3f.h>
5 #include <SimoxUtility/math/convert/mat4f_to_pos.h>
6 #include <SimoxUtility/math/convert/mat4f_to_quat.h>
7 #include <SimoxUtility/math/compare/is_equal.h>
8 #include <SimoxUtility/json.h>
10 #include <VirtualRobot/MathTools.h>
28 ik.reset(
new VirtualRobot::DifferentialIK(
29 rns, rns->getRobot()->getRootNode(),
30 VirtualRobot::JacobiProvider::eSVDDamped));
33 numOfJoints = rns->getSize();
41 ARMARX_INFO <<
"preactivate control law KeypointsImpedanceController";
46 s.qpos = rns->getJointValuesEigen();
47 s.qvel.setZero(numOfJoints);
49 s.currentPose = currentPose;
50 s.currentTwist.setZero();
52 s.desiredPose = currentPose;
53 s.desiredVel.setZero();
54 s.desiredAcc.setZero();
56 s.jacobi.setZero(6, numOfJoints);
57 s.jtpinv.setZero(6, numOfJoints);
62 for (
int i = 0; i <
c.numPoints; i++)
64 s.currentKeypointPosition.segment(3*i, 3) = currentPose.block(0, 3, 3, 1);
66 s.currentKeypointPosition =
s.currentKeypointPosition +
c.fixedTranslation;
69 s.currentKeypointVelocity.setZero(
c.numPoints*3);
71 s.pointTrackingForce.setZero();
72 s.filteredKeypointPosition =
s.currentKeypointPosition;
73 s.previousKeypointPosition =
s.currentKeypointPosition;
75 s.desiredKeypointPosition =
s.currentKeypointPosition;
76 s.desiredKeypointVelocity.setZero(
c.numPoints*3);
77 s.desiredDensityForce.setZero(
c.numPoints*3);
86 s.desiredPose = currentPose;
87 s.desiredTwist.setZero();
89 s.forceImpedance.setZero();
91 s.nullspaceTorque.setZero(numOfJoints);
92 s.desiredJointTorques.setZero(numOfJoints);
104 sRt.desiredPose = sNoneRt.currentPose;
105 sRt.desiredTwist.setZero();
107 sRt.forceImpedance.setZero();
109 sRt.nullspaceTorque.setZero();
110 sRt.desiredJointTorques.setZero();
137 s.currentPose =
tcp->getPoseInRootFrame();
138 Eigen::MatrixXf jacobi = ik->getJacobianMatrix(
tcp, VirtualRobot::IKSolver::CartesianSelection::All);
139 jacobi.block(0, 0, 3, numOfJoints) = 0.001 * jacobi.block(0, 0, 3, numOfJoints);
144 s.jtpinv = ik->computePseudoInverseJacobianMatrix(
s.jacobi.transpose(), lambda);
148 Eigen::VectorXf qvelRaw(numOfJoints);
149 for (
size_t i = 0; i < robotStatus.
jointVelocity.size(); ++i)
157 s.currentTwist = jacobi *
s.qvel;
165 s.currentKeypointPosition.segment(3*i, 3) =
s.currentPose.block(0, 0, 3, 3) * cfg.
fixedTranslation.segment(3*i, 3) +
s.currentPose.block<3, 1>(0, 3);
174 Eigen::VectorXf currentKeypointVelocity;
175 currentKeypointVelocity.setZero(cfg.
numPoints * 3);
178 Eigen::Vector3f angular_vel =
s.currentTwist.tail(3);
180 currentKeypointVelocity.segment(3*i, 3) = angular_vel.cross(dist) +
s.currentTwist.head(3);
190 s.previousKeypointPosition =
s.filteredKeypointPosition;
193 auto difference =
s.desiredKeypointPosition -
s.filteredKeypointPosition;
195 Eigen::VectorXf trackingForce = difference.cwiseProduct(cfg.
keypointKp) -
s.currentKeypointVelocity.cwiseProduct(cfg.
keypointKd);
196 if (trackingForce.size() != 3 * cfg.
numPoints)
198 trackingForce.setZero(3 * cfg.
numPoints);
201 Eigen::Vector3f meanKeypointPosition = Eigen::Vector3f::Zero();
204 meanKeypointPosition = meanKeypointPosition +
s.filteredKeypointPosition.segment(3*i, 3);
206 meanKeypointPosition = meanKeypointPosition / cfg.
numPoints;
208 s.pointTrackingForce.setZero();
212 Eigen::Vector3f dist =
s.filteredKeypointPosition.segment(3*i, 3) - meanKeypointPosition;
214 Eigen::Vector3f force = trackingForce.segment(3*i, 3) +
s.desiredDensityForce.segment(3*i, 3);
215 if (force.norm() > 1000)
220 s.pointTrackingForce.head(3) += force;
221 s.pointTrackingForce.tail(3) += dist.cross(force);
228 Eigen::VectorXf deltaPose = 0.5 *
static_cast<float>(robotStatus.
deltaT) * (vel +
s.desiredVel);
232 Eigen::Matrix3f deltaPoseMat = VirtualRobot::MathTools::rpy2eigen3f(deltaPose(3), deltaPose(4), deltaPose(5));
233 s.desiredPose.block<3, 1>(0, 3) += deltaPose.head(3);
234 s.desiredPose.block<3, 3>(0, 0) = deltaPoseMat *
s.desiredPose.block<3, 3>(0, 0);
257 if ((sRt.desiredPose.block<3, 1>(0, 3) -
s.desiredPose.block<3, 1>(0, 3)).norm() > 100.0f)
263 sRt.desiredPose =
s.desiredPose;
273 sRt.desiredPose.block<3, 3>(0, 0) *
s.currentPose.block<3, 3>(0, 0).transpose();
275 poseErrorImp.head(3) = 0.001 * (sRt.desiredPose.block<3, 1>(0, 3) -
s.currentPose.block<3, 1>(0, 3));
276 poseErrorImp.tail(3) = VirtualRobot::MathTools::eigen3f2rpy(diffMat);
278 c.kpImpedance.cwiseProduct(poseErrorImp) -
279 c.kdImpedance.cwiseProduct(
s.currentTwist);
286 Eigen::VectorXf nullspaceTorque =
287 c.kpNullspace.cwiseProduct(
c.desiredNullspaceJointAngles.value() -
s.qpos) -
288 c.kdNullspace.cwiseProduct(
s.qvel);
297 sRt.desiredJointTorques =
298 s.jacobi.transpose() * sRt.forceImpedance +
299 (I -
s.jacobi.transpose() *
s.jtpinv) * nullspaceTorque;
302 for (
size_t i = 0; i < (unsigned)sRt.desiredJointTorques.rows(); ++i)
304 sRt.desiredJointTorques(i) =
std::clamp(sRt.desiredJointTorques(i), -
c.torqueLimit,
c.torqueLimit);
305 targets.
targets.at(i) = sRt.desiredJointTorques(i);