24 #include <VirtualRobot/MathTools.h>
25 #include <VirtualRobot/RobotNodeSet.h>
34 using namespace MotionControlGroup;
41 GetClosestPoint(
const Eigen::Vector3f& p1,
const Eigen::Vector3f& p2,
const Eigen::Vector3f& p)
43 Eigen::Vector3f dir = p2 - p1;
44 return p1 - (p1 - p).
dot(dir) * dir / dir.squaredNorm();
48 GetT(
const Eigen::Vector3f& p1,
const Eigen::Vector3f& p2,
const Eigen::Vector3f& p)
50 Eigen::Vector3f dir = p2 - p1;
51 return (p - p1).dot(dir) / dir.squaredNorm();
55 Get(
const Eigen::Vector3f& p1,
const Eigen::Vector3f& p2,
float t)
57 Eigen::Vector3f dir = p2 - p1;
75 bool wasAlreadyRequested =
c->getTCPControlUnit()->isRequested();
76 if (!wasAlreadyRequested)
78 c->getTCPControlUnit()->request();
81 if (in.isTCPNameSet())
83 tcpName = in.getTCPName();
87 tcpName =
c->getRobot()->getRobotNodeSet(in.getKinematicChainName())->getTCP()->getName();
89 std::string kinematicChainName = in.getKinematicChainName();
91 new FramedPose(
c->getRobot()->getRobotNode(tcpName)->getPoseInRootFrame(),
92 c->getRobot()->getRootNode()->getName(),
93 c->getRobot()->getName());
95 auto robot =
c->getLocalRobot();
99 Eigen::Vector3f startPositionInRootFrame = startPose->toEigen().block<3, 1>(0, 3);
100 Eigen::Vector3f targetPositionInRootFrame =
101 targetPose->toRootEigen(
c->getRobot()->clone()).block<3, 1>(0, 3);
103 auto sharedRobot =
c->getRobotStateComponent()->getSynchronizedRobot();
104 auto tcp = robot->getRobotNode(tcpName);
105 float tcpVelocity = in.getTCPVelocity();
111 Eigen::Quaternionf targetOri(in.getTargetPose()->toRootEigen(robot).block<3, 3>(0, 0));
112 float tOffset = in.getTOffset();
113 float ignoreOrientation = in.getIgnoreOrientationBeforeT();
115 auto toleratedAngleError = in.getToleratedOrientationDifference();
117 float pOri = in.getKpOrientation();
119 while (!isRunningTaskStopped())
124 startPositionInRootFrame, targetPositionInRootFrame, tcp->getPositionInRootFrame());
125 float tPos = t + tOffset;
131 Eigen::Vector3f orientationDelta;
133 currentTargetOri.toRotationMatrix(), robot->getRootNode()->getName(), robot->getName());
134 TCPDeltaToCurrentTarget->changeFrame(robot, tcpName);
136 orientationDelta4x4.block<3, 3>(0, 0) = TCPDeltaToCurrentTarget->toEigen();
137 VirtualRobot::MathTools::eigen4f2rpy(orientationDelta4x4, orientationDelta);
138 orientationVel =
new FramedDirection(orientationDelta * pOri, tcpName, robot->getName());
139 orientationVel->changeFrame(robot, robot->getRootNode()->getName());
140 Eigen::Vector3f axis;
141 float angleError = 0;
142 orientationDelta4x4.block<3, 3>(0, 0) = TCPDeltaToCurrentTarget->toEigen();
143 VirtualRobot::MathTools::eigen4f2axisangle(orientationDelta4x4, axis, angleError);
146 Eigen::Vector3f currentTargetPosition =
147 Get(startPositionInRootFrame, targetPositionInRootFrame, tPos);
148 float positionError = (currentTargetPosition - tcp->getPositionInRootFrame()).
norm();
149 c->getDebugDrawerTopicProxy()->setPoseVisu(
150 "MoveTCPToTarget",
"CurrentPose",
new Pose(tcp->getGlobalPose()));
151 c->getDebugDrawerTopicProxy()->setPoseVisu(
152 "MoveTCPToTarget",
"TargetPose", globalTargetPose);
153 c->getDebugDrawerTopicProxy()->setSphereVisu(
156 FramedPosition(currentTargetPosition, robot->getRootNode()->getName(), robot->getName())
158 DrawColor{1.0, 0, 0, 1},
161 Eigen::Vector3f positionVelEigen =
162 (currentTargetPosition - tcp->getPositionInRootFrame()).normalized() * tcpVelocity;
164 positionVelEigen, robot->getRootNode()->getName(), robot->getName());
177 if (angleError < toleratedAngleError)
179 orientationVel =
nullptr;
181 c->getTCPControlUnit()->setTCPVelocity(
182 kinematicChainName, tcpName, positionVel, orientationVel);
185 c->getDebugDrawerTopicProxy()->clearLayer(
"MoveTCPToTarget");
188 c->getTCPControlUnit()->setTCPVelocity(kinematicChainName, tcpName, zeroVelocity, zeroVelocity);
190 if (!wasAlreadyRequested)
192 c->getTCPControlUnit()->release();