26 #include "../MotionControlGroupStatechartContext.h"
33 #include <VirtualRobot/IK/ConstrainedOptimizationIK.h>
34 #include <VirtualRobot/IK/GenericIKSolver.h>
35 #include <VirtualRobot/IK/constraints/PositionConstraint.h>
36 #include <VirtualRobot/IK/constraints/OrientationConstraint.h>
37 #include <VirtualRobot/LinkedCoordinate.h>
38 #include <VirtualRobot/XML/RobotIO.h>
41 using namespace MotionControlGroup;
57 if (in.getTimeout() > 0)
59 setTimeoutEvent(in.getTimeout(), createEventTimeout());
67 std::string kinChainName = in.getKinematicChainName();
68 float positionTolerance = in.getTcpPositionTolerance();
69 float orientationTolerance = in.getTcpOrientationTolerance();
100 if (!robot->hasRobotNodeSet(kinChainName))
106 auto nodeSet = localrobot->getRobotNodeSet(kinChainName);
110 if (isInputParameterSet(
"TcpTargetPosition") && isInputParameterSet(
"TcpTargetOrientation"))
112 ikType = VirtualRobot::IKSolver::All;
116 ARMARX_INFO <<
"Target position (Frame = " << targetPosition->getFrame() <<
"): " << targetPosition->toEigen();
117 ARMARX_INFO <<
"Target orientation (Frame = " << targetOrientation->getFrame() <<
"): " << targetOrientation->toEigen();
120 mat.block<3, 3>(0, 0) = targetOrientation->toEigen();
121 VirtualRobot::MathTools::eigen4f2rpy(mat, rpy);
124 targetPosition->changeToGlobal(robot);
125 targetOrientation->changeToGlobal(robot);
129 else if (isInputParameterSet(
"TcpTargetPosition"))
135 targetPosition->changeToGlobal(robot);
137 ARMARX_INFO <<
"Target position (Frame = " << targetPosition->getFrame() <<
": " << targetPosition->toEigen();
141 else if (isInputParameterSet(
"TcpTargetOrientation"))
147 targetOrientation->changeToGlobal(robot);
149 ARMARX_INFO <<
"Target orientation (Frame = " << targetOrientation->getFrame() <<
": " << targetOrientation->toEigen();
161 globalTargetPose.block<3, 1>(0, 3), ikType, positionTolerance));
162 positionConstraint->setOptimizationFunctionFactor(1);
165 globalTargetPose.block<3, 3>(0, 0), ikType, orientationTolerance));
166 orientationConstraint->setOptimizationFunctionFactor(1000);
174 VirtualRobot::ConstrainedOptimizationIK ikSolver(localrobot, nodeSet);
176 ikSolver.addConstraint(positionConstraint);
177 ikSolver.addConstraint(orientationConstraint);
178 ikSolver.initialize();
180 bool ikSuccess = ikSolver.solve();
181 ARMARX_INFO <<
"Solving IK took: " << (IceUtil::Time::now() - start).toMilliSecondsDouble() <<
" ms " <<
VAROUT(ikSuccess);
183 float remainingErrorPos = (globalTargetPose.block<3, 1>(0, 3) - nodeSet->getTCP()->getGlobalPose().block<3, 1>(0, 3)).norm();
184 Eigen::Matrix4f deltaMat = globalTargetPose * nodeSet->getTCP()->getGlobalPose().inverse();
185 Eigen::AngleAxisf aa(deltaMat.block<3, 3>(0, 0));
186 float remaingErrorOri = fabs(aa.angle());
193 ARMARX_INFO <<
"Found IK solution: remaining position error " << remainingErrorPos <<
" mm, orientation error: " << remaingErrorOri <<
" rad (" << (remaingErrorOri * 180 /
M_PI) <<
" deg)";
197 ARMARX_WARNING <<
"IK Solver failed: remaining position error " << remainingErrorPos <<
" mm, orientation error: " << remaingErrorOri <<
" rad (" << (remaingErrorOri * 180 /
M_PI) <<
" deg)";
198 ARMARX_DEBUG <<
"Reached Pose: " << nodeSet->getTCP()->getGlobalPose();
200 robot->setGlobalPose(robot->getGlobalPose());
202 if (in.getAlwaysExecuteBestFoundIkSolution())
204 ARMARX_WARNING <<
"Using failed IK anyway due to AlwaysExecuteBestFoundIkSolution == true";
208 if (!ikSuccess && !in.getAlwaysExecuteBestFoundIkSolution())
215 std::map<std::string, float> jointTargetValues;
218 VirtualRobot::RobotNodeSetPtr kinematicChain = localrobot->getRobotNodeSet(kinChainName);
220 for (
unsigned int i = 0; i < kinematicChain->getSize(); i++)
222 VirtualRobot::RobotNodePtr node = kinematicChain->getNode(i);
224 jointTargetValues[node->getName()] = node->getJointValue();
228 out.setJointTargetValues(jointTargetValues);
230 out.setJointTargetTolerance(0.1f);