30 #include <VirtualRobot/Robot.h>
31 #include <VirtualRobot/XML/RobotIO.h>
32 #include <VirtualRobot/math/Helpers.h>
36 #include <VirtualRobot/IK/ConstrainedOptimizationIK.h>
38 #include <VirtualRobot/IK/constraints/OrientationConstraint.h>
39 #include <VirtualRobot/IK/constraints/PoseConstraint.h>
40 #include <VirtualRobot/IK/constraints/PositionConstraint.h>
54 defineOptionalProperty<std::string>(
"DebugObserverName",
"DebugObserver",
"Name of the topic the DebugObserver listens on");
55 defineOptionalProperty<std::string>(
"ArVizTopicName",
"ArVizTopic",
"Layer updates are sent over this topic.");
63 return "NaturalIKTest";
83 debugObserver = getTopicFromProperty<DebugObserverInterfacePrx>(
"DebugObserverName");
116 .addTextLabel(
"L.scale")
119 .addTextLabel(
"L.minElbZ")
123 .addChild(
new RemoteGui::HSpacer)
125 .addTextLabel(
"L.rX")
128 .addTextLabel(
"L.rY")
131 .addTextLabel(
"L.rZ")
145 .addTextLabel(
"R.scale")
148 .addTextLabel(
"R.minElbZ")
152 .addChild(
new RemoteGui::HSpacer)
154 .addTextLabel(
"R.rX")
157 .addTextLabel(
"R.rY")
160 .addTextLabel(
"R.rZ")
168 .addTextLabel(
"elbowKp")
171 .addTextLabel(
"jlaKp")
184 float x, y, z, rx, ry, rz;
186 x = prx.
getValue<
float>(
"R.X").get();
187 y = prx.
getValue<
float>(
"R.Y").get();
188 z = prx.
getValue<
float>(
"R.Z").get();
193 rx = prx.
getValue<
float>(
"R.rX").get();
194 ry = prx.
getValue<
float>(
"R.rY").get();
195 rz = prx.
getValue<
float>(
"R.rZ").get();
198 x = prx.
getValue<
float>(
"L.X").get();
199 y = prx.
getValue<
float>(
"L.Y").get();
200 z = prx.
getValue<
float>(
"L.Z").get();
205 rx = prx.
getValue<
float>(
"L.rX").get();
206 ry = prx.
getValue<
float>(
"L.rY").get();
207 rz = prx.
getValue<
float>(
"L.rZ").get();
221 std::stringstream ss;
222 ss <<
"err_R: " << p.
err_R(0) <<
" " << p.
err_R(1) <<
" " << p.
err_R(2) <<
"\n";
223 ss <<
"err_L: " << p.
err_L(0) <<
" " << p.
err_L(1) <<
" " << p.
err_L(2) <<
"";
230 runTestTask->start();
246 Eigen::Vector3f elb = Eigen::Vector3f ::Zero();
251 return elb /
elbow.size();
260 : PositionConstraint(robot, nodeSet, node,
target, cartesianSelection, tolerance),
tcp(
tcp)
262 optimizationFunctions.clear();
264 addOptimizationFunction(0,
true);
271 int size = nodeSet->getSize();
274 Eigen::MatrixXf J = ik->getJacobianMatrix(
tcp);
277 Eigen::FullPivLU<Eigen::MatrixXf> lu_decomp(J);
278 Eigen::MatrixXf nullspace = lu_decomp.kernel();
280 Eigen::VectorXf grad = PositionConstraint::optimizationGradient(
id);
282 Eigen::VectorXf mapped = Eigen::VectorXf::Zero(grad.size());
283 for (
int i = 0; i < nullspace.cols(); i++)
285 float squaredNorm = nullspace.col(i).squaredNorm();
287 if (squaredNorm > 1.0e-32f)
289 mapped += nullspace.col(i) * nullspace.col(i).dot(grad) / nullspace.col(i).squaredNorm();
304 std::string robotFile = finder.
getDataDir() +
"/armar6_rt/robotmodel/Armar6-SH/Armar6-SH.xml";
308 VirtualRobot::RobotNodeSetPtr rns_R = robot->getRobotNodeSet(
"RightArm");
309 VirtualRobot::RobotNodeSetPtr rns_L = robot->getRobotNodeSet(
"LeftArm");
311 VirtualRobot::RobotNodePtr sho1_R = rns_R->getNode(1);
312 VirtualRobot::RobotNodePtr sho1_L = rns_L->getNode(1);
314 VirtualRobot::RobotNodePtr elb_R = rns_R->getNode(4);
315 VirtualRobot::RobotNodePtr wri1_R = rns_R->getNode(6);
316 VirtualRobot::RobotNodePtr tcp_R = rns_R->getTCP();
319 float upper_arm_length = (sho1_R->getPositionInRootFrame() - elb_R->getPositionInRootFrame()).
norm();
321 float lower_arm_length = (elb_R->getPositionInRootFrame() - rns_R->getTCP()->getPositionInRootFrame()).
norm();
326 Eigen::Vector3f shoulder_R = sho1_R->getPositionInRootFrame();
327 Eigen::Vector3f shoulder_L = sho1_L->getPositionInRootFrame();
329 Eigen::Vector3f offset = (shoulder_R + shoulder_L) / 2;
344 arm_R.
tcp = rns_R->getTCP();
346 std::vector<NaturalIK::Parameters> ikParamList;
361 ikParamList.emplace_back(p);
368 ikParamList.emplace_back(p);
372 pc.resetRnsValues =
false;
376 std::random_device rd;
377 std::mt19937 gen(rd());
378 std::uniform_real_distribution<> dis(0.0, 1.0);
380 std::vector<Eigen::VectorXf> initialConfigurations;
381 for (VirtualRobot::RobotNodePtr rn : rns_R->getAllRobotNodes())
383 if (rn->isLimitless())
385 rn->setJointValue(0);
389 rn->setJointValue((rn->getJointLimitHi() + rn->getJointLimitLo()) / 2);
393 rns_R->getNode(1)->setJointValue(0);
394 rns_R->getNode(3)->setJointValue(0);
395 rns_R->getNode(5)->setJointValue(0);
396 initialConfigurations.emplace_back(rns_R->getJointValuesEigen());
397 rns_R->getNode(1)->setJointValue(0);
398 rns_R->getNode(3)->setJointValue(0);
399 rns_R->getNode(5)->setJointValue(
M_PI);
400 initialConfigurations.emplace_back(rns_R->getJointValuesEigen());
401 rns_R->getNode(1)->setJointValue(0);
402 rns_R->getNode(3)->setJointValue(
M_PI);
403 rns_R->getNode(5)->setJointValue(0);
404 initialConfigurations.emplace_back(rns_R->getJointValuesEigen());
405 rns_R->getNode(1)->setJointValue(0);
406 rns_R->getNode(3)->setJointValue(
M_PI);
407 rns_R->getNode(5)->setJointValue(
M_PI);
408 initialConfigurations.emplace_back(rns_R->getJointValuesEigen());
412 std::vector<Eigen::Matrix4f> targets;
413 while (targets.size() < 100)
415 for (
size_t i = 0; i < rns_R->getSize(); i++)
417 if (rns_R->getNode(i)->isLimitless())
419 rns_R->getNode(i)->setJointValue(dis(gen) * 2 *
M_PI);
423 float lo = rns_R->getNode(i)->getJointLimitLo();
424 float hi = rns_R->getNode(i)->getJointLimitHi();
425 rns_R->getNode(i)->setJointValue(dis(gen) * (
hi -
lo) +
lo);
428 Eigen::Vector3f tcpPos = tcp_R->getPositionInRootFrame();
431 targets.emplace_back(tcp_R->getPoseInRootFrame());
437 for (
size_t i = 0; i < ikParamList.size(); i++)
440 for (
size_t n = 0; n < targets.size(); n++)
442 for (
const Eigen::VectorXf& initjv : initialConfigurations)
444 rns_R->setJointValues(initjv);
448 ikStats.at(i).solved = ikStats.at(i).solved + 1;
449 ikStats.at(i).elbow.emplace_back(elb_R->getPositionInRootFrame());
458 std::vector<VirtualRobot::RobotNodePtr> elbJoints_R;
459 for (
int i = 0; i < 4; i++)
461 elbJoints_R.push_back(rns_R->getNode(i));
463 VirtualRobot::RobotNodeSetPtr rnsElb_R = VirtualRobot::RobotNodeSet::createRobotNodeSet(robot,
"ELB_R", elbJoints_R, rns_R->getKinematicRoot(), elb_R);
464 for (
size_t n = 0; n < targets.size(); n++)
466 rns_R->setJointValues(initialConfigurations.at(0));
467 VirtualRobot::ConstrainedOptimizationIK optIK(robot, rns_R);
471 math::Helpers::GetPosition(targets.at(n)), VirtualRobot::IKSolver::CartesianSelection::All));
472 posConstraint->setOptimizationFunctionFactor(1);
475 math::Helpers::GetOrientation(targets.at(n)), VirtualRobot::IKSolver::CartesianSelection::All, 2.f / 180 *
M_PI));
476 oriConstraint->setOptimizationFunctionFactor(1000);
482 elbowPos, VirtualRobot::IKSolver::CartesianSelection::All));
483 elbConstraint->setOptimizationFunctionFactor(0.01);
489 optIK.addConstraint(posConstraint);
490 optIK.addConstraint(oriConstraint);
494 bool reached = optIK.solve();
497 ikStats.at(2).solved = ikStats.at(2).solved + 1;
498 ikStats.at(2).elbow.emplace_back(elb_R->getPositionInRootFrame());
508 for (
size_t n = 0; n < targets.size(); n++)
510 rns_R->setJointValues(initialConfigurations.at(0));
511 VirtualRobot::DifferentialIK diffIK(rns_R);
512 diffIK.setGoal(targets.at(n));
513 bool reached = diffIK.solveIK();
516 ikStats.at(3).solved = ikStats.at(3).solved + 1;
517 ikStats.at(3).elbow.emplace_back(elb_R->getPositionInRootFrame());
523 for (
size_t n = 0; n < targets.size(); n++)
525 for (
const Eigen::VectorXf& initjv : initialConfigurations)
527 rns_R->setJointValues(initjv);
532 nsjt->set(2, 0.2, 1);
535 nsjla->setWeight(2, 0);
540 ikStats.at(4).solved = ikStats.at(4).solved + 1;
541 ikStats.at(4).elbow.emplace_back(elb_R->getPositionInRootFrame());
548 for (
size_t i = 0; i < ikStats.size(); i++)
550 ARMARX_IMPORTANT << ikStats.at(i).name <<
" solved: " << ikStats.at(i).solved <<
", T: " << ikStats.at(i).duration;
586 .position(Eigen::Vector3f::Zero())
587 .file(
"armar6_rt",
"armar6_rt/robotmodel/Armar6-SH/Armar6-SH.xml");
589 layer_robot.
add(vizrobot);
592 std::string robotFile = finder.
getDataDir() +
"/armar6_rt/robotmodel/Armar6-SH/Armar6-SH.xml";
596 VirtualRobot::RobotNodeSetPtr rns_R = robot->getRobotNodeSet(
"RightArm");
597 VirtualRobot::RobotNodeSetPtr rns_L = robot->getRobotNodeSet(
"LeftArm");
599 VirtualRobot::RobotNodePtr sho1_R = rns_R->getNode(1);
600 VirtualRobot::RobotNodePtr sho1_L = rns_L->getNode(1);
602 VirtualRobot::RobotNodePtr elb_R = rns_R->getNode(4);
603 VirtualRobot::RobotNodePtr wri1_R = rns_R->getNode(6);
605 VirtualRobot::RobotNodePtr elb_L = rns_L->getNode(4);
614 float upper_arm_length = (sho1_R->getPositionInRootFrame() - elb_R->getPositionInRootFrame()).
norm();
616 float lower_arm_length = (elb_R->getPositionInRootFrame() - rns_R->getTCP()->getPositionInRootFrame()).
norm();
625 Eigen::Vector3f shoulder_R = sho1_R->getPositionInRootFrame();
626 Eigen::Vector3f shoulder_L = sho1_L->getPositionInRootFrame();
628 Eigen::Vector3f offset = (shoulder_R + shoulder_L) / 2;
643 arm_R.
tcp = rns_R->getTCP();
648 arm_L.
tcp = rns_L->getTCP();
655 Eigen::Vector3f vtgt =
target;
659 vtgt = vtgt + 1.0 * err;
664 fwd = ik.forwardKinematics(ik.CalculateSoechtingAngles(vtgt));
681 arrUpperArm.
fromTo(ik.getShoulderPos(), fwd.
elbow);
688 layer.
add(arrUpperArm);
689 layer.
add(arrLowerArm);
695 while (!vizTask->isStopped())
699 c.waitForCycleDuration();
719 0.036805182695388794,
720 0.22703713178634644);
724 Eigen::Matrix3f targetOri_R = referenceOri_R.toRotationMatrix() * aa_R.toRotationMatrix();
728 Eigen::Matrix3f targetOri_L = referenceOri_L.toRotationMatrix() * aa_L.toRotationMatrix();
756 elb = Eigen::AngleAxisf(-SE, Eigen::Vector3f::UnitX()) * elb;
757 float SR = std::atan2(elb(0), -elb(2));
767 VirtualRobot::RobotNodeSetPtr rnsP = robot->getRobotNodeSet(
"PlatformPlanningBothArms");
789 nsjt->set(
"ArmR2_Sho1", +sa_R.
SE, 1);
790 nsjt->set(
"ArmR3_Sho2", +sa_R.
SR, 2);
791 nsjt->set(
"ArmR4_Sho3", -
M_PI / 4, 0.5f);
793 nsjt->set(
"ArmL2_Sho1", -sa_L.
SE, 1);
794 nsjt->set(
"ArmL3_Sho2", +sa_L.
SR, 2);
795 nsjt->set(
"ArmL4_Sho3", +
M_PI / 4, 0.5f);
797 nsjt->set(
"ArmR1_Cla1", 0, 0.5f);
798 nsjt->set(
"ArmL1_Cla1", 0, 0.5f);
816 nsjla->setWeight(0, 0);
817 nsjla->setWeight(1, 0);
818 nsjla->setWeight(2, 0);
819 nsjla->setWeight(
"ArmR4_Sho3", 0);
820 nsjla->setWeight(
"ArmL4_Sho3", 0);
828 layer_iksteps.
clear();
854 rnsP->getNode(0)->setJointValue(0);
855 rnsP->getNode(1)->setJointValue(0);
856 rnsP->getNode(2)->setJointValue(0);
867 layer_iksteps.
clear();
868 std::stringstream ss;
872 ss <<
s.pdTcp.norm() <<
"; " <<
s.odTcp.norm() <<
"; " <<
s.pdElb.norm() <<
"\n";
882 vizrobot.
joints(rnsP->getJointValueMap());
885 arviz.
commit({layer_R, layer_L, layer_robot, layer_iksteps});
889 c.waitForCycleDuration();
899 ori.w() = oriOld.z();
900 ori.x() = oriOld.y() * -1;
901 ori.y() = oriOld.x() * -1;
902 ori.z() = oriOld.w();
905 pose.block<3, 3>(0, 0) = (ori * Eigen::AngleAxisf(
M_PI, Eigen::Vector3f::UnitZ())).toRotationMatrix();
906 pose(0, 3) = -oldPose(0, 3);
907 pose(1, 3) = oldPose(1, 3);
908 pose(2, 3) = oldPose(2, 3);
916 ori.w() = oriOld.z();
917 ori.x() = oriOld.y() * -1;
918 ori.y() = oriOld.x() * -1;
919 ori.z() = oriOld.w();
921 return (ori * Eigen::AngleAxisf(
M_PI, Eigen::Vector3f::UnitZ()));