32 using namespace MotionControlGroup;
37 Eigen::Vector3f
GetClosestPoint(
const Eigen::Vector3f& p1,
const Eigen::Vector3f& p2,
const Eigen::Vector3f& p)
39 Eigen::Vector3f dir = p2 - p1;
40 return p1 - (p1 - p).
dot(dir) * dir / dir.squaredNorm();
43 float GetT(
const Eigen::Vector3f& p1,
const Eigen::Vector3f& p2,
const Eigen::Vector3f& p)
45 Eigen::Vector3f dir = p2 - p1;
46 return (p - p1).dot(dir) / dir.squaredNorm();
50 Eigen::Vector3f
Get(
const Eigen::Vector3f& p1,
const Eigen::Vector3f& p2,
float t)
52 Eigen::Vector3f dir = p2 - p1;
69 bool wasAlreadyRequested =
c->getTCPControlUnit()->isRequested();
70 if (!wasAlreadyRequested)
72 c->getTCPControlUnit()->request();
75 if (in.isTCPNameSet())
77 tcpName = in.getTCPName();
81 tcpName =
c->getRobot()->getRobotNodeSet(in.getKinematicChainName())->getTCP()->getName();
83 std::string kinematicChainName = in.getKinematicChainName();
84 FramedPosePtr startPose =
new FramedPose(
c->getRobot()->getRobotNode(tcpName)->getPoseInRootFrame(),
c->getRobot()->getRootNode()->getName(),
c->getRobot()->getName());
86 auto robot =
c->getLocalRobot();
90 Eigen::Vector3f startPositionInRootFrame = startPose->toEigen().block<3, 1>(0, 3);
91 Eigen::Vector3f targetPositionInRootFrame = targetPose->toRootEigen(
c->getRobot()->clone()).block<3, 1>(0, 3);
93 auto sharedRobot =
c->getRobotStateComponent()->getSynchronizedRobot();
94 auto tcp = robot->getRobotNode(tcpName);
95 float tcpVelocity = in.getTCPVelocity();
101 Eigen::Quaternionf targetOri(in.getTargetPose()->toRootEigen(robot).block<3, 3>(0, 0));
102 float tOffset = in.getTOffset();
103 float ignoreOrientation = in.getIgnoreOrientationBeforeT();
105 auto toleratedAngleError = in.getToleratedOrientationDifference();
107 float pOri = in.getKpOrientation();
109 while (!isRunningTaskStopped())
113 float t =
GetT(startPositionInRootFrame, targetPositionInRootFrame, tcp->getPositionInRootFrame());
114 float tPos = t + tOffset;
120 Eigen::Vector3f orientationDelta;
122 TCPDeltaToCurrentTarget->changeFrame(robot, tcpName);
124 orientationDelta4x4.block<3, 3>(0, 0) = TCPDeltaToCurrentTarget->toEigen();
125 VirtualRobot::MathTools::eigen4f2rpy(orientationDelta4x4, orientationDelta);
126 orientationVel =
new FramedDirection(orientationDelta * pOri, tcpName, robot->getName());
127 orientationVel->changeFrame(robot, robot->getRootNode()->getName());
128 Eigen::Vector3f axis;
129 float angleError = 0;
130 orientationDelta4x4.block<3, 3>(0, 0) = TCPDeltaToCurrentTarget->toEigen();
131 VirtualRobot::MathTools::eigen4f2axisangle(orientationDelta4x4, axis, angleError);
134 Eigen::Vector3f currentTargetPosition =
Get(startPositionInRootFrame, targetPositionInRootFrame, tPos);
135 float positionError = (currentTargetPosition - tcp->getPositionInRootFrame()).
norm();
136 c->getDebugDrawerTopicProxy()->setPoseVisu(
"MoveTCPToTarget",
"CurrentPose",
new Pose(tcp->getGlobalPose()));
137 c->getDebugDrawerTopicProxy()->setPoseVisu(
"MoveTCPToTarget",
"TargetPose", globalTargetPose);
138 c->getDebugDrawerTopicProxy()->setSphereVisu(
"MoveTCPToTarget",
"CurrentTargetPose",
FramedPosition(currentTargetPosition, robot->getRootNode()->getName(), robot->getName()).toGlobal(robot), DrawColor {1.0, 0, 0, 1}, 10);
140 Eigen::Vector3f positionVelEigen = (currentTargetPosition - tcp->getPositionInRootFrame()).normalized() * tcpVelocity;
141 positionVel =
new FramedDirection(positionVelEigen, robot->getRootNode()->getName(), robot->getName());
150 if (angleError < toleratedAngleError)
152 orientationVel =
nullptr;
154 c->getTCPControlUnit()->setTCPVelocity(kinematicChainName, tcpName, positionVel, orientationVel);
157 c->getDebugDrawerTopicProxy()->clearLayer(
"MoveTCPToTarget");
160 c->getTCPControlUnit()->setTCPVelocity(kinematicChainName, tcpName, zeroVelocity, zeroVelocity);
162 if (!wasAlreadyRequested)
164 c->getTCPControlUnit()->release();