27 #include <VirtualRobot/IK/ConstrainedOptimizationIK.h>
28 #include <VirtualRobot/IK/constraints/OrientationConstraint.h>
29 #include <VirtualRobot/IK/constraints/PoseConstraint.h>
30 #include <VirtualRobot/IK/constraints/PositionConstraint.h>
31 #include <VirtualRobot/Robot.h>
32 #include <VirtualRobot/RobotNodeSet.h>
33 #include <VirtualRobot/XML/RobotIO.h>
34 #include <VirtualRobot/math/Helpers.h>
55 defineOptionalProperty<std::string>(
56 "DebugObserverName",
"DebugObserver",
"Name of the topic the DebugObserver listens on");
57 defineOptionalProperty<std::string>(
58 "ArVizTopicName",
"ArVizTopic",
"Layer updates are sent over this topic.");
66 return "NaturalIKTest";
86 debugObserver = getTopicFromProperty<DebugObserverInterfacePrx>(
"DebugObserverName");
121 .addTextLabel(
"L.scale")
124 .addTextLabel(
"L.minElbZ")
128 .addChild(
new RemoteGui::HSpacer)
130 .addTextLabel(
"L.rX")
133 .addTextLabel(
"L.rY")
136 .addTextLabel(
"L.rZ")
151 .addTextLabel(
"R.scale")
154 .addTextLabel(
"R.minElbZ")
158 .addChild(
new RemoteGui::HSpacer)
160 .addTextLabel(
"R.rX")
163 .addTextLabel(
"R.rY")
166 .addTextLabel(
"R.rZ")
175 .addTextLabel(
"elbowKp")
178 .addTextLabel(
"jlaKp")
191 float x, y, z, rx, ry, rz;
193 x = prx.
getValue<
float>(
"R.X").get();
194 y = prx.
getValue<
float>(
"R.Y").get();
195 z = prx.
getValue<
float>(
"R.Z").get();
200 rx = prx.
getValue<
float>(
"R.rX").get();
201 ry = prx.
getValue<
float>(
"R.rY").get();
202 rz = prx.
getValue<
float>(
"R.rZ").get();
205 x = prx.
getValue<
float>(
"L.X").get();
206 y = prx.
getValue<
float>(
"L.Y").get();
207 z = prx.
getValue<
float>(
"L.Z").get();
212 rx = prx.
getValue<
float>(
"L.rX").get();
213 ry = prx.
getValue<
float>(
"L.rY").get();
214 rz = prx.
getValue<
float>(
"L.rZ").get();
228 std::stringstream ss;
229 ss <<
"err_R: " << p.
err_R(0) <<
" " << p.
err_R(1) <<
" " << p.
err_R(2) <<
"\n";
230 ss <<
"err_L: " << p.
err_L(0) <<
" " << p.
err_L(1) <<
" " << p.
err_L(2) <<
"";
237 runTestTask->start();
258 Eigen::Vector3f elb = Eigen::Vector3f ::Zero();
263 return elb /
elbow.size();
271 const VirtualRobot::RobotNodeSetPtr& nodeSet,
274 const Eigen::Vector3f&
target,
276 VirtualRobot::IKSolver::All,
277 float tolerance = 3.0f) :
278 PositionConstraint(robot, nodeSet, node,
target, cartesianSelection, tolerance),
281 optimizationFunctions.clear();
283 addOptimizationFunction(0,
true);
291 int size = nodeSet->getSize();
294 Eigen::MatrixXf J = ik->getJacobianMatrix(
tcp);
297 Eigen::FullPivLU<Eigen::MatrixXf> lu_decomp(J);
298 Eigen::MatrixXf nullspace = lu_decomp.kernel();
300 Eigen::VectorXf grad = PositionConstraint::optimizationGradient(
id);
302 Eigen::VectorXf mapped = Eigen::VectorXf::Zero(grad.size());
303 for (
int i = 0; i < nullspace.cols(); i++)
305 float squaredNorm = nullspace.col(i).squaredNorm();
307 if (squaredNorm > 1.0e-32f)
309 mapped += nullspace.col(i) * nullspace.col(i).dot(grad) /
310 nullspace.col(i).squaredNorm();
327 std::string robotFile =
328 finder.
getDataDir() +
"/armar6_rt/robotmodel/Armar6-SH/Armar6-SH.xml";
332 VirtualRobot::RobotNodeSetPtr rns_R = robot->getRobotNodeSet(
"RightArm");
333 VirtualRobot::RobotNodeSetPtr rns_L = robot->getRobotNodeSet(
"LeftArm");
335 VirtualRobot::RobotNodePtr sho1_R = rns_R->getNode(1);
336 VirtualRobot::RobotNodePtr sho1_L = rns_L->getNode(1);
338 VirtualRobot::RobotNodePtr elb_R = rns_R->getNode(4);
339 VirtualRobot::RobotNodePtr wri1_R = rns_R->getNode(6);
340 VirtualRobot::RobotNodePtr tcp_R = rns_R->getTCP();
343 float upper_arm_length =
344 (sho1_R->getPositionInRootFrame() - elb_R->getPositionInRootFrame()).
norm();
346 float lower_arm_length =
347 (elb_R->getPositionInRootFrame() - rns_R->getTCP()->getPositionInRootFrame()).
norm();
350 Eigen::Vector3f shoulder_R = sho1_R->getPositionInRootFrame();
351 Eigen::Vector3f shoulder_L = sho1_L->getPositionInRootFrame();
353 Eigen::Vector3f offset = (shoulder_R + shoulder_L) / 2;
368 arm_R.
tcp = rns_R->getTCP();
370 std::vector<NaturalIK::Parameters> ikParamList;
371 std::vector<IKStats> ikStats = {
IKStats(
"NaturalIK"),
389 ikParamList.emplace_back(p);
396 ikParamList.emplace_back(p);
400 pc.resetRnsValues =
false;
403 std::random_device rd;
404 std::mt19937 gen(rd());
405 std::uniform_real_distribution<> dis(0.0, 1.0);
407 std::vector<Eigen::VectorXf> initialConfigurations;
408 for (VirtualRobot::RobotNodePtr rn : rns_R->getAllRobotNodes())
410 if (rn->isLimitless())
412 rn->setJointValue(0);
416 rn->setJointValue((rn->getJointLimitHi() + rn->getJointLimitLo()) / 2);
420 rns_R->getNode(1)->setJointValue(0);
421 rns_R->getNode(3)->setJointValue(0);
422 rns_R->getNode(5)->setJointValue(0);
423 initialConfigurations.emplace_back(rns_R->getJointValuesEigen());
424 rns_R->getNode(1)->setJointValue(0);
425 rns_R->getNode(3)->setJointValue(0);
426 rns_R->getNode(5)->setJointValue(
M_PI);
427 initialConfigurations.emplace_back(rns_R->getJointValuesEigen());
428 rns_R->getNode(1)->setJointValue(0);
429 rns_R->getNode(3)->setJointValue(
M_PI);
430 rns_R->getNode(5)->setJointValue(0);
431 initialConfigurations.emplace_back(rns_R->getJointValuesEigen());
432 rns_R->getNode(1)->setJointValue(0);
433 rns_R->getNode(3)->setJointValue(
M_PI);
434 rns_R->getNode(5)->setJointValue(
M_PI);
435 initialConfigurations.emplace_back(rns_R->getJointValuesEigen());
438 std::vector<Eigen::Matrix4f> targets;
439 while (targets.size() < 100)
441 for (
size_t i = 0; i < rns_R->getSize(); i++)
443 if (rns_R->getNode(i)->isLimitless())
445 rns_R->getNode(i)->setJointValue(dis(gen) * 2 *
M_PI);
449 float lo = rns_R->getNode(i)->getJointLimitLo();
450 float hi = rns_R->getNode(i)->getJointLimitHi();
451 rns_R->getNode(i)->setJointValue(dis(gen) * (
hi -
lo) +
lo);
454 Eigen::Vector3f tcpPos = tcp_R->getPositionInRootFrame();
457 targets.emplace_back(tcp_R->getPoseInRootFrame());
463 for (
size_t i = 0; i < ikParamList.size(); i++)
466 for (
size_t n = 0; n < targets.size(); n++)
468 for (
const Eigen::VectorXf& initjv : initialConfigurations)
470 rns_R->setJointValues(initjv);
472 ik_R.
calculateIK(targets.at(n), arm_R, ikParamList.at(i));
475 ikStats.at(i).solved = ikStats.at(i).solved + 1;
476 ikStats.at(i).elbow.emplace_back(elb_R->getPositionInRootFrame());
485 std::vector<VirtualRobot::RobotNodePtr> elbJoints_R;
486 for (
int i = 0; i < 4; i++)
488 elbJoints_R.push_back(rns_R->getNode(i));
490 VirtualRobot::RobotNodeSetPtr rnsElb_R = VirtualRobot::RobotNodeSet::createRobotNodeSet(
491 robot,
"ELB_R", elbJoints_R, rns_R->getKinematicRoot(), elb_R);
492 for (
size_t n = 0; n < targets.size(); n++)
494 rns_R->setJointValues(initialConfigurations.at(0));
495 VirtualRobot::ConstrainedOptimizationIK optIK(robot, rns_R);
502 math::Helpers::GetPosition(targets.at(n)),
503 VirtualRobot::IKSolver::CartesianSelection::All));
504 posConstraint->setOptimizationFunctionFactor(1);
510 math::Helpers::GetOrientation(targets.at(n)),
511 VirtualRobot::IKSolver::CartesianSelection::All,
513 oriConstraint->setOptimizationFunctionFactor(1000);
516 Eigen::Vector3f elbowPos =
525 VirtualRobot::IKSolver::CartesianSelection::All));
526 elbConstraint->setOptimizationFunctionFactor(0.01);
532 optIK.addConstraint(posConstraint);
533 optIK.addConstraint(oriConstraint);
537 bool reached = optIK.solve();
540 ikStats.at(2).solved = ikStats.at(2).solved + 1;
541 ikStats.at(2).elbow.emplace_back(elb_R->getPositionInRootFrame());
551 for (
size_t n = 0; n < targets.size(); n++)
553 rns_R->setJointValues(initialConfigurations.at(0));
554 VirtualRobot::DifferentialIK diffIK(rns_R);
555 diffIK.setGoal(targets.at(n));
556 bool reached = diffIK.solveIK();
559 ikStats.at(3).solved = ikStats.at(3).solved + 1;
560 ikStats.at(3).elbow.emplace_back(elb_R->getPositionInRootFrame());
566 for (
size_t n = 0; n < targets.size(); n++)
568 for (
const Eigen::VectorXf& initjv : initialConfigurations)
570 rns_R->setJointValues(initjv);
573 rns_R, tcp_R, targets.at(n), VirtualRobot::IKSolver::CartesianSelection::All));
577 nsjt->set(2, 0.2, 1);
581 nsjla->setWeight(2, 0);
586 ikStats.at(4).solved = ikStats.at(4).solved + 1;
587 ikStats.at(4).elbow.emplace_back(elb_R->getPositionInRootFrame());
594 for (
size_t i = 0; i < ikStats.size(); i++)
596 ARMARX_IMPORTANT << ikStats.at(i).name <<
" solved: " << ikStats.at(i).solved
597 <<
", T: " << ikStats.at(i).duration;
633 .position(Eigen::Vector3f::Zero())
634 .file(
"armar6_rt",
"armar6_rt/robotmodel/Armar6-SH/Armar6-SH.xml");
636 layer_robot.
add(vizrobot);
639 std::string robotFile =
640 finder.
getDataDir() +
"/armar6_rt/robotmodel/Armar6-SH/Armar6-SH.xml";
644 VirtualRobot::RobotNodeSetPtr rns_R = robot->getRobotNodeSet(
"RightArm");
645 VirtualRobot::RobotNodeSetPtr rns_L = robot->getRobotNodeSet(
"LeftArm");
647 VirtualRobot::RobotNodePtr sho1_R = rns_R->getNode(1);
648 VirtualRobot::RobotNodePtr sho1_L = rns_L->getNode(1);
650 VirtualRobot::RobotNodePtr elb_R = rns_R->getNode(4);
651 VirtualRobot::RobotNodePtr wri1_R = rns_R->getNode(6);
653 VirtualRobot::RobotNodePtr elb_L = rns_L->getNode(4);
662 float upper_arm_length =
663 (sho1_R->getPositionInRootFrame() - elb_R->getPositionInRootFrame()).
norm();
665 float lower_arm_length =
666 (elb_R->getPositionInRootFrame() - rns_R->getTCP()->getPositionInRootFrame()).
norm();
674 Eigen::Vector3f shoulder_R = sho1_R->getPositionInRootFrame();
675 Eigen::Vector3f shoulder_L = sho1_L->getPositionInRootFrame();
677 Eigen::Vector3f offset = (shoulder_R + shoulder_L) / 2;
691 arm_R.
tcp = rns_R->getTCP();
696 arm_L.
tcp = rns_L->getTCP();
704 Eigen::Vector3f vtgt =
target;
708 vtgt = vtgt + 1.0 * err;
715 fwd = ik.forwardKinematics(ik.CalculateSoechtingAngles(vtgt));
734 arrUpperArm.
fromTo(ik.getShoulderPos(), fwd.
elbow);
741 layer.
add(arrUpperArm);
742 layer.
add(arrLowerArm);
746 while (!vizTask->isStopped())
750 c.waitForCycleDuration();
770 0.036805182695388794,
771 0.22703713178634644);
777 referenceOri_R.toRotationMatrix() * aa_R.toRotationMatrix();
783 referenceOri_L.toRotationMatrix() * aa_L.toRotationMatrix();
812 elb = Eigen::AngleAxisf(-SE, Eigen::Vector3f::UnitX()) * elb;
813 float SR = std::atan2(elb(0), -elb(2));
823 VirtualRobot::RobotNodeSetPtr rnsP = robot->getRobotNodeSet(
"PlatformPlanningBothArms");
830 p.
p_R.
setOri ? VirtualRobot::IKSolver::CartesianSelection::All
833 p.
p_L.
setOri ? VirtualRobot::IKSolver::CartesianSelection::All
850 nsjt->set(
"ArmR2_Sho1", +sa_R.
SE, 1);
851 nsjt->set(
"ArmR3_Sho2", +sa_R.
SR, 2);
852 nsjt->set(
"ArmR4_Sho3", -
M_PI / 4, 0.5f);
854 nsjt->set(
"ArmL2_Sho1", -sa_L.
SE, 1);
855 nsjt->set(
"ArmL3_Sho2", +sa_L.
SR, 2);
856 nsjt->set(
"ArmL4_Sho3", +
M_PI / 4, 0.5f);
858 nsjt->set(
"ArmR1_Cla1", 0, 0.5f);
859 nsjt->set(
"ArmL1_Cla1", 0, 0.5f);
880 nsjla->setWeight(0, 0);
881 nsjla->setWeight(1, 0);
882 nsjla->setWeight(2, 0);
883 nsjla->setWeight(
"ArmR4_Sho3", 0);
884 nsjla->setWeight(
"ArmL4_Sho3", 0);
892 layer_iksteps.
clear();
929 rnsP->getNode(0)->setJointValue(0);
930 rnsP->getNode(1)->setJointValue(0);
931 rnsP->getNode(2)->setJointValue(0);
943 layer_iksteps.
clear();
944 std::stringstream ss;
948 ss <<
s.pdTcp.norm() <<
"; " <<
s.odTcp.norm() <<
"; " <<
s.pdElb.norm()
964 vizrobot.
joints(rnsP->getJointValueMap());
967 arviz.
commit({layer_R, layer_L, layer_robot, layer_iksteps});
971 c.waitForCycleDuration();
981 ori.w() = oriOld.z();
982 ori.x() = oriOld.y() * -1;
983 ori.y() = oriOld.x() * -1;
984 ori.z() = oriOld.w();
987 pose.block<3, 3>(0, 0) =
988 (ori * Eigen::AngleAxisf(
M_PI, Eigen::Vector3f::UnitZ())).toRotationMatrix();
989 pose(0, 3) = -oldPose(0, 3);
990 pose(1, 3) = oldPose(1, 3);
991 pose(2, 3) = oldPose(2, 3);
1001 ori.w() = oriOld.z();
1002 ori.x() = oriOld.y() * -1;
1003 ori.y() = oriOld.x() * -1;
1004 ori.z() = oriOld.w();
1006 return (ori * Eigen::AngleAxisf(
M_PI, Eigen::Vector3f::UnitZ()));