30 const std::string& configFileName)
35 ik.reset(
new VirtualRobot::DifferentialIK(
36 rns, rns->getRobot()->getRootNode(), VirtualRobot::JacobiProvider::eSVDDamped));
39 numOfJoints = rns->getSize();
40 s.qpos.resize(numOfJoints);
41 s.qvel.resize(numOfJoints);
42 s.desiredJointTorques.resize(numOfJoints);
44 s.currentForceTorque.setZero();
45 s.currentTwist.setZero();
46 s.forceImpedance.setZero();
47 s.virtualVel.setZero();
48 s.virtualAcc.setZero();
49 s.pointTrackingForce.setZero();
52 nlohmann::json userConfig;
53 nlohmann::json defaultConfig;
57 "njoint_controller/impedance_controller_config.json",
60 std::ifstream ifs{filename};
62 if (defaultConfig.empty())
67 if (configFileName.empty())
70 <<
"No control parameter specified by user, use default parameter in \n"
72 userConfig = defaultConfig;
76 std::ifstream ifs{configFileName};
79 if (userConfig.empty())
84 if (userConfig.find(
"control") != userConfig.end())
86 auto c = userConfig[
"control"];
87 auto dc = defaultConfig[
"control"];
93 s.kpNullspaceTorque =
getEigenVec(
c, dc,
"nullspace_stiffness", numOfJoints, 10.0f);
94 s.kdNullspaceTorque =
getEigenVec(
c, dc,
"nullspace_damping", numOfJoints, 2.0f);
95 s.desiredNullspaceJointAngles =
getEigenVec(
c, dc,
"desired_nullspace_joint_angles");
96 if (
s.desiredNullspaceJointAngles.size() != numOfJoints)
98 ARMARX_IMPORTANT <<
"default value is not consistent with requested kinematic "
99 "chain, reinit in preactivation";
100 enablePreactivateInit.store(
true);
108 s.currentKeypointPosition =
getEigenVec(
c, dc,
"current_keypoint_position");
109 s.desiredKeypointPosition =
s.currentKeypointPosition;
110 s.filteredKeypointPosition =
s.currentKeypointPosition;
111 s.previousKeypointPosition =
s.currentKeypointPosition;
116 s.fixedTranslation =
getEigenVec(
c, dc,
"fixed_translation");
117 s.desiredKeypointVelocity.setZero(3 *
s.numPoints);
118 s.currentKeypointVelocity.setZero(3 *
s.numPoints);
122 ARMARX_WARNING <<
"controller parameters not found, they should be placed in 'control' "
123 "tag in your json file";
126 I = Eigen::MatrixXf::Identity(numOfJoints, numOfJoints);
173 Eigen::Matrix4f currentPose = rns->getTCP()->getPoseInRootFrame();
174 s.qpos = rns->getJointValuesEigen();
175 s.qvel.setZero(numOfJoints);
176 s.nullspaceTorque.setZero(numOfJoints);
177 s.desiredJointTorques.setZero(numOfJoints);
181 for (
int i = 0; i <
s.numPoints; i++)
183 s.currentKeypointPosition.segment(3 * i, 3) = currentPose.block(0, 3, 3, 1);
185 s.currentKeypointPosition =
s.currentKeypointPosition +
s.fixedTranslation;
188 s.previousDesiredPose = currentPose;
189 s.desiredPose = currentPose;
190 s.desiredVel.setZero();
191 s.desiredAcc.setZero();
193 s.currentPose = currentPose;
194 s.currentTwist.setZero();
195 s.forceImpedance.setZero();
197 s.virtualPose = currentPose;
198 s.virtualVel.setZero();
199 s.virtualAcc.setZero();
201 s.pointTrackingForce.setZero();
202 s.filteredKeypointPosition =
s.currentKeypointPosition;
203 s.previousKeypointPosition =
s.currentKeypointPosition;
204 s.currentKeypointVelocity.setZero(
s.numPoints * 3);
205 s.desiredKeypointVelocity.setZero(
s.numPoints * 3);
207 if (enablePreactivateInit.load())
209 s.desiredNullspaceJointAngles = rns->getJointValuesEigen();
251 const IceUtil::Time& timeSinceLastIteration,
252 std::vector<const SensorValue1DoFActuatorTorque*> torqueSensors,
253 std::vector<const SensorValue1DoFActuatorVelocity*> velocitySensors,
254 std::vector<const SensorValue1DoFActuatorPosition*> positionSensors)
311 s.currentPose =
tcp->getPoseInRootFrame();
312 Eigen::MatrixXf jacobi =
313 ik->getJacobianMatrix(
tcp, VirtualRobot::IKSolver::CartesianSelection::All);
314 jacobi.block(0, 0, 3, numOfJoints) = 0.001 * jacobi.block(0, 0, 3, numOfJoints);
319 Eigen::VectorXf qvelRaw(numOfJoints);
320 for (
size_t i = 0; i < velocitySensors.size(); ++i)
322 s.qpos(i) = positionSensors[i]->position;
323 qvelRaw(i) = velocitySensors[i]->velocity;
327 s.currentTwist = jacobi *
s.qvel;
328 s.deltaT = timeSinceLastIteration.toSecondsDouble();
334 for (
int i = 0; i <
s.numPoints; i++)
336 s.currentKeypointPosition.segment(3 * i, 3) =
337 s.currentPose.block(0, 0, 3, 3) *
s.fixedTranslation.segment(3 * i, 3) +
338 s.currentPose.block<3, 1>(0, 3);
342 s.filteredKeypointPosition =
343 (1.0f -
s.keypointPositionFilter) *
s.filteredKeypointPosition +
344 s.keypointPositionFilter *
s.currentKeypointPosition;
348 Eigen::VectorXf currentKeypointVelocity;
349 currentKeypointVelocity.setZero(
s.numPoints * 3);
350 for (
int i = 0; i <
s.numPoints; i++)
352 Eigen::Vector3f angular_vel =
s.currentTwist.tail<3>();
353 Eigen::Vector3f dist =
s.fixedTranslation.segment(3 * i, 3);
354 currentKeypointVelocity.segment(3 * i, 3) =
355 angular_vel.cross(dist) +
s.currentTwist.head<3>();
357 s.currentKeypointVelocity =
358 (1.0f -
s.keypointVelocityFilter) *
s.currentKeypointVelocity +
359 s.keypointVelocityFilter * currentKeypointVelocity;
363 s.currentKeypointVelocity =
364 (1.0f -
s.keypointVelocityFilter) *
s.currentKeypointVelocity +
365 s.keypointVelocityFilter *
366 (
s.filteredKeypointPosition -
s.previousKeypointPosition) /
s.deltaT;
368 s.previousKeypointPosition =
s.filteredKeypointPosition;
371 auto difference =
s.desiredKeypointPosition -
s.filteredKeypointPosition;
373 Eigen::VectorXf trackingForce = difference.cwiseProduct(
s.keypointKp) -
374 s.currentKeypointVelocity.cwiseProduct(
s.keypointKd);
375 if (trackingForce.size() != 3 *
s.numPoints)
377 trackingForce.setZero(3 *
s.numPoints);
380 s.pointTrackingForce.setZero();
381 for (
int i = 0; i <
s.numPoints; i++)
383 Eigen::Vector3f dist =
384 s.filteredKeypointPosition.segment(3 * i, 3) -
s.currentPose.block<3, 1>(0, 3);
386 Eigen::Vector3f force = trackingForce.segment(3 * i, 3);
387 if (force.norm() > 200)
392 s.pointTrackingForce.head<3>() += force;
393 s.pointTrackingForce.tail<3>() += dist.cross(force);
397 s.kdAdmittance.cwiseProduct(
s.desiredVel);
400 Eigen::VectorXf deltaPose = 0.5 *
s.deltaT * (vel +
s.desiredVel);
404 Eigen::Matrix3f deltaPoseMat =
405 VirtualRobot::MathTools::rpy2eigen3f(deltaPose(3), deltaPose(4), deltaPose(5));
406 s.desiredPose.block<3, 1>(0, 3) += deltaPose.head<3>();
407 s.desiredPose.block<3, 3>(0, 0) = deltaPoseMat *
s.desiredPose.block<3, 3>(0, 0);
414 std::vector<ControlTarget1DoFActuatorTorque*>
targets)
420 s.desiredPose =
s.previousDesiredPose;
421 s.desiredVel.setZero();
422 s.kmAdmittance.setZero();
426 Eigen::Matrix3f objDiffMat =
427 s.virtualPose.block<3, 3>(0, 0) *
s.desiredPose.block<3, 3>(0, 0).transpose();
429 poseError.head<3>() =
s.virtualPose.block<3, 1>(0, 3) -
s.desiredPose.block<3, 1>(0, 3);
430 poseError.tail<3>() = VirtualRobot::MathTools::eigen3f2rpy(objDiffMat);
434 s.kpAdmittance.cwiseProduct(poseError) -
435 s.kdAdmittance.cwiseProduct(
s.virtualVel);
438 Eigen::VectorXf deltaPose = 0.5 *
s.deltaT * (vel +
s.virtualVel);
442 Eigen::Matrix3f deltaPoseMat =
443 VirtualRobot::MathTools::rpy2eigen3f(deltaPose(3), deltaPose(4), deltaPose(5));
444 s.virtualPose.block<3, 1>(0, 3) += deltaPose.head<3>();
445 s.virtualPose.block<3, 3>(0, 0) = deltaPoseMat *
s.virtualPose.block<3, 3>(0, 0);
452 Eigen::Matrix3f diffMat =
453 s.virtualPose.block<3, 3>(0, 0) *
s.currentPose.block<3, 3>(0, 0).transpose();
455 poseErrorImp.head<3>() =
456 0.001 * (
s.virtualPose.block<3, 1>(0, 3) -
s.currentPose.block<3, 1>(0, 3));
457 poseErrorImp.tail<3>() = VirtualRobot::MathTools::eigen3f2rpy(diffMat);
459 s.kpImpedance.cwiseProduct(poseErrorImp) -
s.kdImpedance.cwiseProduct(
s.currentTwist);
462 Eigen::VectorXf nullspaceTorque =
463 s.kpNullspaceTorque.cwiseProduct(
s.desiredNullspaceJointAngles -
s.qpos) -
464 s.kdNullspaceTorque.cwiseProduct(
s.qvel);
467 const Eigen::MatrixXf jtpinv =
468 ik->computePseudoInverseJacobianMatrix(
s.jacobi.transpose(), lambda);
469 s.desiredJointTorques =
s.jacobi.transpose() *
s.forceImpedance +
470 (I -
s.jacobi.transpose() * jtpinv) * nullspaceTorque;
473 for (
size_t i = 0; i <
targets.size(); ++i)
475 s.desiredJointTorques(i) =
476 std::clamp(
s.desiredJointTorques(i), -
s.torqueLimit,
s.torqueLimit);
477 targets.at(i)->torque =
s.desiredJointTorques(i);