31 #include <VirtualRobot/MathTools.h>
35 #include "../PlatformContext.h"
39 using namespace PlatformGroup;
48 CalcPathGeneratedBase<
CalcPath>(stateData)
54 const ::memoryx::GraphNodeBaseList& nodes,
float currentX,
float currentY,
57 Eigen::Vector2f robotLocation(currentX, currentY);
60 for (
auto& basenode : nodes)
64 auto size = node->getOutdegree();
65 for (
int i = 0; i < size; ++i)
67 memoryx::GraphNodePtr nextNode = memoryx::GraphNodePtr::dynamicCast(node->getAdjacentNode(i)->getEntity());
69 Eigen::Vector2f node1 = FramedPosePtr::dynamicCast(node->getPose())->toEigen().block<2, 1>(0, 3);
70 Eigen::Vector2f node2 = FramedPosePtr::dynamicCast(nextNode->getPose())->toEigen().block<2, 1>(0, 3);
72 if ((node1 - node2).norm() > 1e-9)
74 Eigen::Vector2f result = VirtualRobot::MathTools::nearestPointOnSegment<Eigen::Vector2f>(node1, node2, robotLocation);
76 if ((closest.x() == 0 && closest.y() == 0) || (result - robotLocation).norm() < (closest - robotLocation).norm())
86 return (closest.x() != 0 || closest.y() != 0);
101 std::vector<EdgeProjection> projections;
103 for (
auto& basenode : nodes)
107 auto size = node->getOutdegree();
108 for (
int i = 0; i < size; ++i)
110 memoryx::GraphNodePtr nextNode = memoryx::GraphNodePtr::dynamicCast(node->getAdjacentNode(i)->getEntity());
112 Eigen::Vector2f node1 = FramedPosePtr::dynamicCast(node->getPose())->toEigen().block<2, 1>(0, 3);
113 Eigen::Vector2f node2 = FramedPosePtr::dynamicCast(nextNode->getPose())->toEigen().block<2, 1>(0, 3);
115 if ((node1 - node2).norm() > 1e-9)
117 Eigen::Vector2f result = VirtualRobot::MathTools::nearestPointOnSegment<Eigen::Vector2f>(node1, node2, robotPos);
140 std::vector<armarx::Vector3Ptr> targetPositions;
143 const std::string sceneName = in.getSceneName();
144 if (!graphSegment->hasScene(sceneName))
146 ARMARX_WARNING <<
"Target scene '" << sceneName <<
"' does not exist in menory";
147 out.settargetPositions(targetPositions);
153 const std::string landmark = in.gettargetLandmark();
154 if (!graphSegment->hasNodeWithName(sceneName, landmark))
156 ARMARX_WARNING <<
"Target landmark '" << landmark <<
"' doesn't exist in graph (scene) '" << sceneName <<
"'";
157 auto nodes = graphSegment->getNodesByScene(sceneName);
158 for (
auto& node : nodes)
162 out.settargetPositions(targetPositions);
163 sendEvent<EvNoPathFound>();
167 memoryx::GraphNodeBasePtr goalNode;
169 memoryx::GraphNodeBasePtr landmarkNode = graphSegment->getNodeFromSceneByName(sceneName, landmark);
170 ARMARX_CHECK_NOT_NULL(landmarkNode) <<
"Could not find node with name '" << landmark <<
"' in scene '" << sceneName <<
"'";
173 ARMARX_CHECK_NOT_NULL(goalNode) <<
"No nearest node found for landmark '" << landmark <<
"' in scene '" << sceneName <<
"'";
175 ARMARX_INFO <<
"Resolved target landmark to '" << goalNode->getName() <<
"'";
183 const float platformPositionX = poseRef->getDataField(
"positionX")->getFloat();
184 const float platformPositionY = poseRef->getDataField(
"positionY")->getFloat();
185 const float platformYaw = poseRef->getDataField(
"rotation")->getFloat();
187 Eigen::Vector3f currentPos(platformPositionX, platformPositionY, 0);
188 const Eigen::Vector2f currentPos2d = currentPos.head<2>();
192 .
orientation(VirtualRobot::MathTools::rpy2eigen3f(0, 0, platformYaw)));
195 std::vector<EdgeProjection> projections =
getNearestPositionOnEdges(graphSegment->getNodesByScene(sceneName), currentPos2d);
196 if (projections.empty())
198 ARMARX_ERROR <<
"Could not find nearest point on graph";
203 std::sort(projections.begin(), projections.end(), [¤tPos2d](
const auto & p1,
const auto & p2)
205 const float c1 = (p1.position - currentPos2d).norm();
206 const float c2 = (p2.position - currentPos2d).norm();
212 std::optional<EdgeProjection> bestProjection;
213 ::memoryx::GraphNodeBaseList bestPath;
218 const Eigen::Vector3f nodePosition =
fromIce(projection.targetNode->getPose()->position);
219 const float distToNode = (nodePosition - currentPos).
norm();
220 ARMARX_INFO <<
"Current node: '" << projection.targetNode->getName() <<
"' | " <<
VAROUT(distToNode);
223 ::memoryx::GraphNodeBaseList path = graphSegment->aStar(projection.targetNode->getId(), goalNode->getId());
234 if (bestProjection.has_value() and (bestProjection->position - projection.position).norm() > 1e-4)
239 if (pathCost < bestPathCosts)
241 bestProjection = projection;
243 bestPathCosts = pathCost;
247 if (not bestProjection.has_value())
250 out.settargetPositions(targetPositions);
255 const Eigen::Vector2f nearestPoint = bestProjection->position;
256 ARMARX_INFO <<
"Nearest Point on graph: " << nearestPoint.transpose();
258 Eigen::Vector3f closestPointPos;
259 closestPointPos << nearestPoint, 0;
262 layerClosestPoint.
add(
viz::Cylinder(
"To Closest Point").fromTo(currentPos, closestPointPos)
268 bool fixedOrentation =
true;
271 bool useWaypointOrientations = in.getUseWaypointOrientations();
272 if (!useWaypointOrientations && length > in.getdriveMode_lookAhead_thresholdMM())
274 ARMARX_INFO <<
"long path (" << length <<
" mm) -> rotating platform towards moving direction";
275 fixedOrentation =
false;
279 PosePtr pose = PosePtr::dynamicCast(bestPath.back()->getPose());
280 VirtualRobot::MathTools::eigen4f2rpy(pose->toEigen(), rpy);
281 const float goalYaw = rpy[2];
286 for (
auto& node : bestPath)
290 v->x = currentPose->position->x;
291 v->y = currentPose->position->y;
293 if (useWaypointOrientations)
295 VirtualRobot::MathTools::eigen4f2rpy(currentPose->toEigen(), rpy);
299 else if (fixedOrentation)
305 if (count <= 0 || count >=
int(bestPath.size() - 1))
308 VirtualRobot::MathTools::eigen4f2rpy(currentPose->toEigen(), rpy);
309 ARMARX_DEBUG <<
"start or last pose of path, using orientation of waypoint: " << rpy[2];
315 double x = currentPose->position->x - lastPose->position->x;
316 double y = currentPose->position->y - lastPose->position->y;
321 ARMARX_DEBUG <<
"points close together, using last orientation: " << lastAlpha;
327 Eigen::Vector2d
a(x, y);
329 Eigen::Vector2d b(0, 1);
330 v->z = - (std::atan2(b.y(), b.x()) - std::atan2(
a.y(),
a.x()));
339 ARMARX_DEBUG <<
"waypoint angle between " <<
a.transpose() <<
" and " << b.transpose() <<
": " <<
v->z;
344 lastPose = currentPose;
348 targetPositions.push_back(
v);
349 ARMARX_INFO <<
"waypoint #" << targetPositions.size() <<
": " << node->getName() <<
" Coordinates:\n " <<
v->output();
353 Vector3Ptr v(
new Vector3(nearestPoint.x(), nearestPoint.y(), Vector3Ptr::dynamicCast(targetPositions[0])->toEigen().z()));
354 targetPositions.insert(targetPositions.begin(),
v);
356 out.settargetPositions(targetPositions);
363 context->
debugDrawer->removeSphereDebugLayerVisu(
"closestPoint");
369 ::armarx::FramedPoseBasePtr lastPose;
371 for (
auto& node : path)
375 lastPose = node->getPose();
379 ::armarx::FramedPoseBasePtr currentPose = node->getPose();
380 float x = currentPose->position->x - lastPose->position->x;
381 float y = currentPose->position->y - lastPose->position->y;
383 lastPose = currentPose;