31 const std::string& configFileName)
36 ik.reset(
new VirtualRobot::DifferentialIK(
37 rns, rns->getRobot()->getRootNode(), VirtualRobot::JacobiProvider::eSVDDamped));
40 numOfJoints = rns->getSize();
41 s.qpos.resize(numOfJoints);
42 s.qvel.resize(numOfJoints);
43 s.desiredJointTorques.resize(numOfJoints);
45 s.currentForceTorque.setZero();
46 s.currentTwist.setZero();
47 s.forceImpedance.setZero();
48 s.virtualVel.setZero();
49 s.virtualAcc.setZero();
50 s.pointTrackingForce.setZero();
53 nlohmann::json userConfig;
54 nlohmann::json defaultConfig;
58 "njoint_controller/impedance_controller_config.json",
61 std::ifstream ifs{filename};
63 if (defaultConfig.empty())
68 if (configFileName.empty())
71 <<
"No control parameter specified by user, use default parameter in \n"
73 userConfig = defaultConfig;
77 std::ifstream ifs{configFileName};
80 if (userConfig.empty())
85 if (userConfig.find(
"control") != userConfig.end())
87 auto c = userConfig[
"control"];
88 auto dc = defaultConfig[
"control"];
94 s.kpNullspaceTorque =
getEigenVec(
c, dc,
"nullspace_stiffness", numOfJoints, 10.0f);
95 s.kdNullspaceTorque =
getEigenVec(
c, dc,
"nullspace_damping", numOfJoints, 2.0f);
96 s.desiredNullspaceJointAngles =
getEigenVec(
c, dc,
"desired_nullspace_joint_angles");
97 if (
s.desiredNullspaceJointAngles.size() != numOfJoints)
99 ARMARX_IMPORTANT <<
"default value is not consistent with requested kinematic "
100 "chain, reinit in preactivation";
101 enablePreactivateInit.store(
true);
109 s.currentKeypointPosition =
getEigenVec(
c, dc,
"current_keypoint_position");
110 s.desiredKeypointPosition =
s.currentKeypointPosition;
111 s.filteredKeypointPosition =
s.currentKeypointPosition;
112 s.previousKeypointPosition =
s.currentKeypointPosition;
117 s.fixedTranslation =
getEigenVec(
c, dc,
"fixed_translation");
118 s.desiredKeypointVelocity.setZero(3 *
s.numPoints);
119 s.currentKeypointVelocity.setZero(3 *
s.numPoints);
123 ARMARX_WARNING <<
"controller parameters not found, they should be placed in 'control' "
124 "tag in your json file";
127 I = Eigen::MatrixXf::Identity(numOfJoints, numOfJoints);
174 Eigen::Matrix4f currentPose = rns->getTCP()->getPoseInRootFrame();
175 s.qpos = rns->getJointValuesEigen();
176 s.qvel.setZero(numOfJoints);
177 s.nullspaceTorque.setZero(numOfJoints);
178 s.desiredJointTorques.setZero(numOfJoints);
182 for (
int i = 0; i <
s.numPoints; i++)
184 s.currentKeypointPosition.segment(3 * i, 3) = currentPose.block(0, 3, 3, 1);
186 s.currentKeypointPosition =
s.currentKeypointPosition +
s.fixedTranslation;
189 s.previousDesiredPose = currentPose;
190 s.desiredPose = currentPose;
191 s.desiredVel.setZero();
192 s.desiredAcc.setZero();
194 s.currentPose = currentPose;
195 s.currentTwist.setZero();
196 s.forceImpedance.setZero();
198 s.virtualPose = currentPose;
199 s.virtualVel.setZero();
200 s.virtualAcc.setZero();
202 s.pointTrackingForce.setZero();
203 s.filteredKeypointPosition =
s.currentKeypointPosition;
204 s.previousKeypointPosition =
s.currentKeypointPosition;
205 s.currentKeypointVelocity.setZero(
s.numPoints * 3);
206 s.desiredKeypointVelocity.setZero(
s.numPoints * 3);
208 if (enablePreactivateInit.load())
210 s.desiredNullspaceJointAngles = rns->getJointValuesEigen();
252 const IceUtil::Time& timeSinceLastIteration,
253 std::vector<const SensorValue1DoFActuatorTorque*> torqueSensors,
254 std::vector<const SensorValue1DoFActuatorVelocity*> velocitySensors,
255 std::vector<const SensorValue1DoFActuatorPosition*> positionSensors)
312 s.currentPose =
tcp->getPoseInRootFrame();
313 Eigen::MatrixXf jacobi =
314 ik->getJacobianMatrix(
tcp, VirtualRobot::IKSolver::CartesianSelection::All);
315 jacobi.block(0, 0, 3, numOfJoints) = 0.001 * jacobi.block(0, 0, 3, numOfJoints);
320 Eigen::VectorXf qvelRaw(numOfJoints);
321 for (
size_t i = 0; i < velocitySensors.size(); ++i)
323 s.qpos(i) = positionSensors[i]->position;
324 qvelRaw(i) = velocitySensors[i]->velocity;
328 s.currentTwist = jacobi *
s.qvel;
329 s.deltaT = timeSinceLastIteration.toSecondsDouble();
335 for (
int i = 0; i <
s.numPoints; i++)
337 s.currentKeypointPosition.segment(3 * i, 3) =
338 s.currentPose.block(0, 0, 3, 3) *
s.fixedTranslation.segment(3 * i, 3) +
339 s.currentPose.block<3, 1>(0, 3);
343 s.filteredKeypointPosition =
344 (1.0f -
s.keypointPositionFilter) *
s.filteredKeypointPosition +
345 s.keypointPositionFilter *
s.currentKeypointPosition;
349 Eigen::VectorXf currentKeypointVelocity;
350 currentKeypointVelocity.setZero(
s.numPoints * 3);
351 for (
int i = 0; i <
s.numPoints; i++)
353 Eigen::Vector3f angular_vel =
s.currentTwist.tail<3>();
354 Eigen::Vector3f dist =
s.fixedTranslation.segment(3 * i, 3);
355 currentKeypointVelocity.segment(3 * i, 3) =
356 angular_vel.cross(dist) +
s.currentTwist.head<3>();
358 s.currentKeypointVelocity =
359 (1.0f -
s.keypointVelocityFilter) *
s.currentKeypointVelocity +
360 s.keypointVelocityFilter * currentKeypointVelocity;
364 s.currentKeypointVelocity =
365 (1.0f -
s.keypointVelocityFilter) *
s.currentKeypointVelocity +
366 s.keypointVelocityFilter *
367 (
s.filteredKeypointPosition -
s.previousKeypointPosition) /
s.deltaT;
369 s.previousKeypointPosition =
s.filteredKeypointPosition;
372 auto difference =
s.desiredKeypointPosition -
s.filteredKeypointPosition;
374 Eigen::VectorXf trackingForce = difference.cwiseProduct(
s.keypointKp) -
375 s.currentKeypointVelocity.cwiseProduct(
s.keypointKd);
376 if (trackingForce.size() != 3 *
s.numPoints)
378 trackingForce.setZero(3 *
s.numPoints);
381 s.pointTrackingForce.setZero();
382 for (
int i = 0; i <
s.numPoints; i++)
384 Eigen::Vector3f dist =
385 s.filteredKeypointPosition.segment(3 * i, 3) -
s.currentPose.block<3, 1>(0, 3);
387 Eigen::Vector3f force = trackingForce.segment(3 * i, 3);
388 if (force.norm() > 200)
393 s.pointTrackingForce.head<3>() += force;
394 s.pointTrackingForce.tail<3>() += dist.cross(force);
398 s.kdAdmittance.cwiseProduct(
s.desiredVel);
401 Eigen::VectorXf deltaPose = 0.5 *
s.deltaT * (vel +
s.desiredVel);
405 Eigen::Matrix3f deltaPoseMat =
406 VirtualRobot::MathTools::rpy2eigen3f(deltaPose(3), deltaPose(4), deltaPose(5));
407 s.desiredPose.block<3, 1>(0, 3) += deltaPose.head<3>();
408 s.desiredPose.block<3, 3>(0, 0) = deltaPoseMat *
s.desiredPose.block<3, 3>(0, 0);
415 std::vector<ControlTarget1DoFActuatorTorque*>
targets)
421 s.desiredPose =
s.previousDesiredPose;
422 s.desiredVel.setZero();
423 s.kmAdmittance.setZero();
427 Eigen::Matrix3f objDiffMat =
428 s.virtualPose.block<3, 3>(0, 0) *
s.desiredPose.block<3, 3>(0, 0).transpose();
430 poseError.head<3>() =
s.virtualPose.block<3, 1>(0, 3) -
s.desiredPose.block<3, 1>(0, 3);
431 poseError.tail<3>() = VirtualRobot::MathTools::eigen3f2rpy(objDiffMat);
435 s.kpAdmittance.cwiseProduct(poseError) -
436 s.kdAdmittance.cwiseProduct(
s.virtualVel);
439 Eigen::VectorXf deltaPose = 0.5 *
s.deltaT * (vel +
s.virtualVel);
443 Eigen::Matrix3f deltaPoseMat =
444 VirtualRobot::MathTools::rpy2eigen3f(deltaPose(3), deltaPose(4), deltaPose(5));
445 s.virtualPose.block<3, 1>(0, 3) += deltaPose.head<3>();
446 s.virtualPose.block<3, 3>(0, 0) = deltaPoseMat *
s.virtualPose.block<3, 3>(0, 0);
453 Eigen::Matrix3f diffMat =
454 s.virtualPose.block<3, 3>(0, 0) *
s.currentPose.block<3, 3>(0, 0).transpose();
456 poseErrorImp.head<3>() =
457 0.001 * (
s.virtualPose.block<3, 1>(0, 3) -
s.currentPose.block<3, 1>(0, 3));
458 poseErrorImp.tail<3>() = VirtualRobot::MathTools::eigen3f2rpy(diffMat);
460 s.kpImpedance.cwiseProduct(poseErrorImp) -
s.kdImpedance.cwiseProduct(
s.currentTwist);
463 Eigen::VectorXf nullspaceTorque =
464 s.kpNullspaceTorque.cwiseProduct(
s.desiredNullspaceJointAngles -
s.qpos) -
465 s.kdNullspaceTorque.cwiseProduct(
s.qvel);
468 const Eigen::MatrixXf jtpinv =
469 ik->computePseudoInverseJacobianMatrix(
s.jacobi.transpose(), lambda);
470 s.desiredJointTorques =
s.jacobi.transpose() *
s.forceImpedance +
471 (I -
s.jacobi.transpose() * jtpinv) * nullspaceTorque;
474 for (
size_t i = 0; i <
targets.size(); ++i)
476 s.desiredJointTorques(i) =
477 boost::algorithm::clamp(
s.desiredJointTorques(i), -
s.torqueLimit,
s.torqueLimit);
478 targets.at(i)->torque =
s.desiredJointTorques(i);