29 #include "MotionPlanning/CSpace/CSpaceSampled.h"
30 #include "MotionPlanning/Planner/BiRrt.h"
31 #include "MotionPlanning/Planner/Rrt.h"
32 #include "MotionPlanning/PostProcessing/ShortcutProcessor.h"
33 #include "VirtualRobot/CollisionDetection/CDManager.h"
34 #include "VirtualRobot/Workspace/Reachability.h"
48 this->robotProxy = robot->clone();
49 this->currentRns =
"";
52 this->IKCalibrationMap = std::map<std::string, IKCalibration>();
53 this->IKCalibrationMap.insert(std::pair<std::string, IKCalibration>(
54 "Armar3", IKCalibration::createIKCalibration(1000, 0.005, 500, 0.3, 100, 1.0)));
55 this->IKCalibrationMap.insert(std::pair<std::string, IKCalibration>(
56 "Armar4", IKCalibration::createIKCalibration(10000, 0.005, 100, 0.6, 100, 1.0)));
57 this->IKCalibrationMap.insert(std::pair<std::string, IKCalibration>(
58 "Armar6", IKCalibration::createIKCalibration(100, 0.5, 500, 1.1, 500, 1.1)));
59 this->IKCalibrationMap.insert(std::pair<std::string, IKCalibration>(
60 "Custom", IKCalibration::createIKCalibration(100, 100, 100, 100, 100, 100)));
83 KinematicSolver::IKCalibration
84 KinematicSolver::IKCalibration::createIKCalibration(
int smallIterations,
87 double mediumStepSize,
91 IKCalibration calibration;
92 calibration.smallIterations = smallIterations;
93 calibration.smallStepSize = smallStepSize;
94 calibration.mediumIterations = mediumIterations;
95 calibration.mediumStepSize = mediumStepSize;
96 calibration.largeIterations = largeIterations;
97 calibration.largeStepSize = largeStepSize;
113 return KinematicSolver::singleton;
130 PoseBasePtr globalPose,
134 IKCalibration calibration = calibrate();
137 RobotNodeSetPtr kcPrx = robotProxy->getRobotNodeSet(kc->getName());
138 std::vector<double> jointValues;
139 int failureCounter = 0;
144 std::vector<float> jv;
145 float rn = 1.0f / (
float)RAND_MAX;
146 for (
unsigned int i = 0; i < kcPrx->getSize(); i++)
148 RobotNodePtr ro = kcPrx->getNode(i);
149 float r = (
float)rand() * rn;
150 float v = ro->getJointLimitLo() + (ro->getJointLimitHi() - ro->getJointLimitLo()) * r;
153 robotProxy->setJointValues(kcPrx, jv);
157 GenericIKSolverPtr ikSolver = this->IKSetup(
158 calibration.largeStepSize, calibration.largeIterations, 10.0, 0.50, kcPrx);
159 Pose targetPose =
Pose(globalPose->position, globalPose->orientation);
160 std::vector<float> output = ikSolver->solveNoRNSUpdate(targetPose.
toEigen(), selection);
161 jointValues = std::vector<double>(output.begin(), output.end());
163 }
while (jointValues.size() == 0 &&
164 failureCounter <= iterations);
170 PoseBasePtr framedPose,
174 PoseBasePtr globalPose = PoseBasePtr(
175 new Pose(kc->getKinematicRoot()->toGlobalCoordinateSystem(localPose->toEigen())));
176 return this->solveIK(kc, globalPose, selection, 50);
181 std::vector<float> startingPoint,
182 PoseBasePtr globalDestination,
185 IKCalibration calibration = calibrate();
187 RobotNodeSetPtr kcPrx = robotProxy->getRobotNodeSet(kc->getName());
188 kcPrx->setJointValues(startingPoint);
189 GenericIKSolverPtr ikSolver =
190 this->IKSetup(calibration.mediumStepSize, calibration.mediumIterations, 10.0, 1.0, kcPrx);
191 Pose targetPose =
Pose(globalDestination->position, globalDestination->orientation);
192 std::vector<float> output = ikSolver->solveNoRNSUpdate(targetPose.
toEigen(), selection);
193 if (output.size() == 0)
195 std::vector<double> random =
196 this->solveIK(kc, PoseBasePtr(
new Pose(targetPose.
toEigen())), selection, 5);
197 output = std::vector<float>(random.begin(), random.end());
204 std::vector<float> startingPoint,
205 PoseBasePtr framedDestination,
210 PosePtr(
new Pose(framedDestination->position, framedDestination->orientation));
212 PoseBasePtr globalPose = PoseBasePtr(
213 new Pose(kc->getKinematicRoot()->toGlobalCoordinateSystem(localPose->toEigen())));
214 return this->solveRecursiveIK(kc, startingPoint, globalPose, selection);
219 std::vector<double> startingPoint,
220 PoseBasePtr globalDestination,
223 IKCalibration calibration = calibrate();
225 RobotNodeSetPtr kcPrx = robotProxy->getRobotNodeSet(kc->getName());
226 kcPrx->setJointValues(std::vector<float>(startingPoint.begin(), startingPoint.end()));
227 GenericIKSolverPtr ikSolver =
228 this->IKSetup(calibration.smallStepSize, calibration.smallIterations, 10, 0.5, kcPrx);
229 Pose targetPose =
Pose(globalDestination->position, globalDestination->orientation);
231 std::vector<float> output = ikSolver->solveNoRNSUpdate(targetPose.
toEigen(), selection);
232 std::vector<double> outputd = std::vector<double>(output.begin(), output.end());
238 std::vector<double> startingPoint,
239 PoseBasePtr framedDestination,
244 PosePtr(
new Pose(framedDestination->position, framedDestination->orientation));
245 PoseBasePtr globalPose = PoseBasePtr(
246 new Pose(kc->getKinematicRoot()->toGlobalCoordinateSystem(localPose->toEigen())));
247 return this->solveRecursiveIK(kc, startingPoint, globalPose, selection);
250 std::vector<std::vector<double>>
252 std::vector<double> startingPoint,
253 std::vector<PoseBasePtr> globalDestinations,
254 std::vector<IKSolver::CartesianSelection> selections)
257 globalDestinations[globalDestinations.size() - 1],
258 selections[selections.size() - 1]))
260 throw LocalException(
"Desired Pose is not Reachable");
263 RobotNodeSetPtr kcPrx = robotProxy->getRobotNodeSet(kc->getName());
265 std::vector<std::vector<double>> bestResult;
266 int minSingularities = -1;
268 while ((minSingularities == -1 || minSingularities != 0) && tries < 2)
272 int failureCounter = 0;
273 int singularities = 0;
274 std::vector<std::vector<double>> result = std::vector<std::vector<double>>();
275 std::vector<float> currentStart =
276 std::vector<float>(startingPoint.begin(), startingPoint.end());
278 for (PoseBasePtr currentDest : globalDestinations)
280 std::vector<double> temp =
281 std::vector<double>(currentStart.begin(), currentStart.end());
285 globalDestinations.at(i - 1)->orientation));
287 PosePtr(
new Pose(currentDest->position, currentDest->orientation));
288 if (priorPose->toEigen() == currentPose->toEigen() && temp.size() != 0)
296 kc, temp, currentDest, selections.at(i));
297 if (currentResult.size() == 0)
300 if (failureCounter >= 1)
304 Eigen::VectorXf start = Eigen::VectorXf(temp.size());
305 Eigen::VectorXf goal = Eigen::VectorXf(temp.size());
306 std::vector<double> end = this->solveIK(kc, currentDest, selections[i], 100);
321 for (
double d : temp)
326 std::vector<double> nowStart = std::vector<double>();
328 nowStart = std::vector<double>(currentStart.begin(), currentStart.end());
329 int bestNode = getFurthestNode(kcPrx, start, goal);
330 RobotNodePtr node = kcPrx->getAllRobotNodes()[bestNode];
332 kcPrx->setJointValues(currentStart);
334 PoseBasePtr startPose = PoseBasePtr(
new Pose(node->getGlobalPose()));
335 kcPrx->setJointValues(goal);
336 PoseBasePtr endPose = PoseBasePtr(
new Pose(node->getGlobalPose()));
337 std::vector<PoseBasePtr> cp = {startPose, endPose};
338 IKCalibration calibration = calibrate();
341 for (
int i = 0; i < 25; i++)
343 PoseBasePtr poseBase = ip->getPoseAt(i * 0.04);
345 PosePtr(
new Pose(poseBase->position, poseBase->orientation));
346 GenericIKSolverPtr ikSolver = this->IKSetup(
347 calibration.smallStepSize, calibration.smallIterations, 5, 0.5, kcPrx);
348 DifferentialIKPtr dIK = ikSolver->getDifferentialIK();
351 PosePtr(
new Pose(currentDest->position, currentDest->orientation));
353 dIK->setGoal(posePose->toEigen(), node, IKSolver::CartesianSelection::X);
354 dIK->setGoal(pose->toEigen(), kcPrx->getTCP(), sel);
355 kcPrx->setJointValues(std::vector<float>(nowStart.begin(), nowStart.end()));
357 if (dIK->solveIK(0.4))
360 std::vector<float> ca = kcPrx->getJointValues();
361 nowStart = std::vector<double>(ca.begin(), ca.end());
362 result.push_back(std::vector<double>(ca.begin(), ca.end()));
367 posePose->toEigen(), node, IKSolver::CartesianSelection::Y);
368 kcPrx->setJointValues(
369 std::vector<float>(nowStart.begin(), nowStart.end()));
370 if (dIK->solveIK(0.4))
373 std::vector<float> ca = kcPrx->getJointValues();
374 nowStart = std::vector<double>(ca.begin(), ca.end());
375 result.push_back(std::vector<double>(ca.begin(), ca.end()));
379 if (dIK->solveIK(0.4))
382 posePose->toEigen(), node, IKSolver::CartesianSelection::Z);
383 kcPrx->setJointValues(
384 std::vector<float>(nowStart.begin(), nowStart.end()));
386 std::vector<float> ca = kcPrx->getJointValues();
387 nowStart = std::vector<double>(ca.begin(), ca.end());
388 result.push_back(std::vector<double>(ca.begin(), ca.end()));
395 if (singularities == 1)
417 currentStart = std::vector<float>(nowStart.begin(), nowStart.end());
423 result.push_back(currentResult);
424 currentStart = std::vector<float>(currentResult.begin(), currentResult.end());
428 if (minSingularities <= 0 || singularities <= minSingularities)
430 minSingularities = singularities;
437 std::vector<std::vector<double>>
440 std::vector<double> startingPoint,
441 std::vector<PoseBasePtr> globalDestinations,
442 std::vector<IKSolver::CartesianSelection> selections)
446 int failureCounter = 0;
447 RobotNodeSetPtr kcPrx = robotProxy->getRobotNodeSet(kc->getName());
448 std::vector<std::vector<double>> result = std::vector<std::vector<double>>();
449 std::vector<float> currentStart =
450 std::vector<float>(startingPoint.begin(), startingPoint.end());
454 for (PoseBasePtr currentDest : globalDestinations)
456 std::vector<double> temp = std::vector<double>(currentStart.begin(), currentStart.end());
460 globalDestinations.at(i - 1)->orientation));
462 PosePtr(
new Pose(currentDest->position, currentDest->orientation));
463 if (priorPose->toEigen() == currentPose->toEigen() && temp.size() != 0)
465 result.push_back(temp);
471 kc, temp, currentDest, selections.at(i));
473 if (currentResult.size() == 0)
475 result.push_back(temp);
477 currentStart = std::vector<float>(temp.begin(), temp.end());
478 if (failureCounter > (
double)globalDestinations.size() / 20.0)
480 throw LocalException(
481 "Too, many faults at current calculation: " +
std::to_string(failureCounter) +
482 " Poses not reachable - only " +
483 std::to_string((
double)globalDestinations.size() / 5.0) +
" allowed!");
489 result.push_back(currentResult);
490 currentStart = std::vector<float>(currentResult.begin(), currentResult.end());
496 std::vector<std::vector<double>>
498 std::vector<double> startingPoint,
499 std::vector<PoseBasePtr> globalDestinations,
503 std::vector<IKSolver::CartesianSelection> selections =
504 std::vector<IKSolver::CartesianSelection>();
505 for (
unsigned int i = 0; i < globalDestinations.size(); i++)
507 selections.push_back(selection);
509 return this->solveRecursiveIK(kc, startingPoint, globalDestinations, selections);
512 std::vector<std::vector<double>>
514 std::vector<double> startingPoint,
515 std::vector<PoseBasePtr> framedDestinations,
516 std::vector<IKSolver::CartesianSelection> selections)
519 std::vector<PoseBasePtr> globalDestinations = std::vector<PoseBasePtr>();
520 for (PoseBasePtr framedDestination : framedDestinations)
523 PosePtr(
new Pose(framedDestination->position, framedDestination->orientation));
525 PoseBasePtr globalPose = PoseBasePtr(
526 new Pose(kc->getKinematicRoot()->toGlobalCoordinateSystem(localPose->toEigen())));
527 globalDestinations.push_back(globalPose);
529 return this->solveRecursiveIK(kc, startingPoint, globalDestinations, selections);
539 RobotNodeSetPtr kcPrx = this->setupProxy(kc,
jointAngles);
540 return PoseBasePtr(
new Pose(kcPrx->getTCP()->getGlobalPose()));
547 RobotNodeSetPtr kcPrx = this->setupProxy(kc,
jointAngles);
548 return PoseBasePtr(
new Pose(
549 kcPrx->getKinematicRoot()->toLocalCoordinateSystem(kcPrx->getTCP()->getGlobalPose())));
572 return this->solveIK(rns, pose, cs, 50).size() != 0;
578 if (currentRns != rns->getName())
580 currentRns = rns->getName();
581 this->syncRobotPrx();
589 for (RobotNodePtr node : robot->getRobotNodes())
591 this->robotProxy->getRobotNode(node->getName())->setJointValue(node->getJointValue());
599 KinematicSolver::IKSetup(
double jacStepSize,
602 double orientationError,
605 GenericIKSolverPtr ikSolver =
606 GenericIKSolverPtr(
new GenericIKSolver(kc, JacobiProvider::eSVDDamped));
607 ikSolver->setupJacobian(jacStepSize, IKIterations);
608 ikSolver->setMaximumError(vectorError, orientationError);
613 KinematicSolver::setupProxy(RobotNodeSetPtr kc, std::vector<double>
jointAngles)
615 RobotNodeSetPtr kcPrx = robotProxy->getRobotNodeSet(kc->getName());
617 kcPrx->setJointValues(jointValues);
621 KinematicSolver::IKCalibration
622 KinematicSolver::calibrate()
625 if (robot->getName() !=
"Armar3" && robot->getName() !=
"Armar4" &&
626 robot->getName() !=
"Armar6")
628 return this->IKCalibrationMap.at(
"Armar3");
632 return this->IKCalibrationMap.at(robot->getName());
637 KinematicSolver::getMaxDistance(Eigen::Vector3f destination,
644 PoseBasePtr pose = doForwardKinematicRelative(rns, ja);
645 double distance =
sqrt(pow(pose->position->x - destination[0], 2) +
646 pow(pose->position->y - destination[1], 2) +
647 pow(pose->position->z - destination[2], 2));
657 KinematicSolver::getFurthestNode(RobotNodeSetPtr rns,
658 Eigen::VectorXf startConfig,
659 Eigen::VectorXf endConfig)
662 int best = rns->getSize() - 1;
663 rns->setJointValues(startConfig);
664 std::vector<Eigen::Vector3f> startPoses = std::vector<Eigen::Vector3f>();
665 std::vector<Eigen::Vector3f> endPoses = std::vector<Eigen::Vector3f>();
666 for (RobotNodePtr node : rns->getAllRobotNodes())
668 Eigen::Vector3f currentPose = Eigen::Vector3f();
669 currentPose[0] = node->getGlobalPose()(0, 3);
670 currentPose[1] = node->getGlobalPose()(1, 3);
671 currentPose[2] = node->getGlobalPose()(2, 3);
672 startPoses.push_back(currentPose);
674 rns->setJointValues(endConfig);
675 for (RobotNodePtr node : rns->getAllRobotNodes())
677 Eigen::Vector3f currentPose = Eigen::Vector3f();
678 currentPose[0] = node->getGlobalPose()(0, 3);
679 currentPose[1] = node->getGlobalPose()(1, 3);
680 currentPose[2] = node->getGlobalPose()(2, 3);
681 endPoses.push_back(currentPose);
683 for (
unsigned int i = 0; i < startPoses.size(); i++)