34 DesignerTrajectoryManager::ManipulationInterval::addFirstUserWaypoint(
UserWaypointPtr uw)
36 userWaypointsList.push_back(uw);
42 userWaypointsList.push_front(uw);
43 transitionsList.push_front(t);
50 userWaypointsList.push_back(uw);
51 transitionsList.push_back(t);
56 DesignerTrajectoryManager::ManipulationInterval::addBreakpointIndex(
int index)
58 breakpointIndicesSet.insert(
index);
62 DesignerTrajectoryManager::ManipulationInterval::finishSearch()
65 userWaypoints.reserve(userWaypointsList.size());
67 userWaypointsList.begin(), userWaypointsList.end(), std::back_inserter(userWaypoints));
70 transitions.reserve(transitionsList.size());
71 std::copy(transitionsList.begin(), transitionsList.end(), std::back_inserter(transitions));
74 breakpointIndices.reserve(breakpointIndicesSet.size());
76 breakpointIndicesSet.end(),
77 std::back_inserter(breakpointIndices));
81 DesignerTrajectoryManager::ManipulationInterval::getUserWaypointByRealIndex(
unsigned int i)
const
83 if (i - lowerIntervalLimit < userWaypoints.size())
85 return userWaypoints[i - lowerIntervalLimit];
90 throw OutOfRangeException();
95 DesignerTrajectoryManager::ManipulationInterval::getUserWaypointByZeroBasedIndex(
98 if (i < userWaypoints.size())
100 return userWaypoints[i];
105 throw OutOfRangeException();
110 DesignerTrajectoryManager::ManipulationInterval::getTransitionByRealIndex(
unsigned int i)
const
112 if (i - lowerIntervalLimit < transitions.size())
114 return transitions[i - lowerIntervalLimit];
119 throw OutOfRangeException();
124 DesignerTrajectoryManager::ManipulationInterval::getTransitionByZeroBasedIndex(
unsigned int i)
const
126 if (i < transitions.size())
128 return transitions[i];
133 throw OutOfRangeException();
137 std::vector<std::vector<double>>
138 DesignerTrajectoryManager::ManipulationInterval::getUserWaypointsIKSolutions(
139 std::vector<std::vector<double>>& ikSolutions,
140 unsigned int intervalStart,
141 unsigned int intervalEnd)
143 std::vector<std::vector<double>> result;
144 for (
unsigned int& newIndex : newIndexOfUserWaypoint)
146 if (newIndex < intervalStart)
150 if (newIndex > intervalEnd)
155 result.push_back(ikSolutions[newIndex]);
161 DesignerTrajectoryManager::ManipulationInterval::applyJointAnglesOfUserWaypoints(
162 std::vector<std::vector<double>> ikSolution)
164 std::vector<std::vector<double>>
jointAngles = getUserWaypointsIKSolutions(
165 ikSolution, newIndexOfUserWaypoint[0], newIndexOfUserWaypoint[userWaypoints.size() - 1]);
179 DesignerTrajectoryManager::saveState()
182 if (mementos.currentMemento < mementos.mementoList.size() - 1)
184 mementos.mementoList.resize(mementos.currentMemento + 1);
187 if (designerTrajectory)
189 mementos.mementoList.push_back(
194 mementos.mementoList.push_back(
nullptr);
199 if (mementos.mementoList.size() > MEMENTO_MAX)
201 mementos.mementoList.pop_front();
205 mementos.currentMemento++;
209 DesignerTrajectoryManager::ManipulationInterval
210 DesignerTrajectoryManager::calculateManipulationInterval(
unsigned int manipulationIndex)
213 ManipulationInterval mi;
216 mi.lowerIntervalLimit = manipulationIndex;
217 mi.upperIntervalLimit = manipulationIndex;
220 mi.addFirstUserWaypoint(
dt->getUserWaypoint(manipulationIndex));
223 if (
dt->getUserWaypoint(manipulationIndex)->getIsTimeOptimalBreakpoint())
225 mi.addBreakpointIndex(manipulationIndex);
229 for (
unsigned int i = manipulationIndex + 1; i <
dt->getNrOfUserWaypoints(); i++)
233 mi.pushBack(tmpUwp, tmpTrans);
235 if (tmpUwp->getIsTimeOptimalBreakpoint())
237 mi.addBreakpointIndex(i);
250 for (
unsigned int i = manipulationIndex - 1; i !=
static_cast<unsigned>(-1); i--)
254 mi.pushFront(tmpUwp, tmpTrans);
256 if (tmpUwp->getIsTimeOptimalBreakpoint())
258 mi.addBreakpointIndex(i);
271 mi.addBreakpointIndex(mi.lowerIntervalLimit);
272 mi.addBreakpointIndex(mi.upperIntervalLimit);
278 std::vector<AbstractInterpolationPtr>
279 DesignerTrajectoryManager::getInterpolationObjects(ManipulationInterval& mi)
281 std::vector<PoseBasePtr> poses;
284 poses.push_back(uwp->getPose());
287 std::vector<InterpolationType> interpolationTypes;
290 interpolationTypes.push_back(tp->getInterpolationType());
297 DesignerTrajectoryManager::calculateInterpolatedPoints(
298 std::vector<AbstractInterpolationPtr> interpolationObjects,
300 ManipulationInterval& mi)
309 for (
unsigned int i = 0; i < mi.transitions.size(); i++)
311 std::vector<double> timestamps;
315 TransitionPtr tmpTransition = mi.getTransitionByZeroBasedIndex(i);
319 unsigned int totalFrameCount = tmpTransition->getTimeOptimalDuration() * fps;
321 if (totalFrameCount == 0)
323 totalFrameCount = DEFAULT_FRAME_COUNT;
326 if (totalFrameCount > 1)
329 double increment = 1.0 / (totalFrameCount - 1);
333 for (
unsigned int k = 0; k < totalFrameCount; k++)
335 timestamps.push_back(k * increment);
340 timestamps.push_back(0.0);
341 timestamps.push_back(1.0);
345 std::vector<PoseBasePtr> tmpInterpolatedPositions;
346 for (
double timestamp : timestamps)
349 tmpInterpolatedPositions.push_back(interpolationObjects[i]->getPoseAt(timestamp));
352 mi.interpolatedTransitions.push_back(tmpInterpolatedPositions);
353 tmpInterpolatedPositions.clear();
362 DesignerTrajectoryManager::checkTransitionReachability(ManipulationInterval& mi,
363 unsigned int transitionIndex)
365 for (PoseBasePtr pose : mi.interpolatedTransitions[transitionIndex])
380 std::vector<std::vector<double>>
381 DesignerTrajectoryManager::calculateIKSolutions(ManipulationInterval& mi)
384 std::vector<std::vector<double>> ikSolutions;
385 ikSolutions.push_back(mi.userWaypoints[0]->getJointAngles());
388 mi.newIndexOfUserWaypoint.push_back(0);
391 std::vector<InsertTransition> mpInserts;
393 for (
unsigned int transitionIndex = 0; transitionIndex < mi.interpolatedTransitions.size();
396 if (!checkTransitionReachability(mi, transitionIndex))
399 InsertTransition mpInsert;
400 mpInsert.transitionIndex = transitionIndex;
401 mpInsert.insertionIndex = ikSolutions.size() - 1;
402 mpInserts.push_back(mpInsert);
405 std::vector<double> start = {ikSolutions.back()};
407 std::vector<PoseBasePtr> destinations = {
408 mi.interpolatedTransitions[transitionIndex].back()};
410 std::vector<VirtualRobot::IKSolver::CartesianSelection> selections = {
411 mi.userWaypoints[transitionIndex + 1]->getIKSelection()};
413 std::vector<std::vector<double>> tmpIkSolutions =
414 kinSolver->solveRecursiveIKRelative(rns, start, destinations, selections);
417 ikSolutions.insert(ikSolutions.end(), tmpIkSolutions.begin(), tmpIkSolutions.end());
422 mi.newIndexOfUserWaypoint.push_back(-1);
430 std::vector<double> start = {ikSolutions.back()};
433 std::vector<PoseBasePtr> destinations(mi.interpolatedTransitions[transitionIndex].begin() +
435 mi.interpolatedTransitions[transitionIndex].end());
438 std::vector<VirtualRobot::IKSolver::CartesianSelection> selections;
441 for (
unsigned int i = 1; i < destinations.size(); i++)
443 selections.push_back(mi.userWaypoints[transitionIndex + 1]->getIKSelection());
447 selections.push_back(mi.userWaypoints[transitionIndex + 1]->getIKSelection());
450 std::vector<std::vector<double>> tmpIkSolutions =
451 kinSolver->solveRecursiveIKRelative(rns, start, destinations, selections);
458 ikSolutions.insert(ikSolutions.end(), tmpIkSolutions.begin(), tmpIkSolutions.end());
460 mi.newIndexOfUserWaypoint.push_back(ikSolutions.size() - 1);
465 std::vector<TimedTrajectory>
466 DesignerTrajectoryManager::calculateTimeOptimalTrajectories(
467 std::vector<std::vector<double>> ikSolutions,
468 ManipulationInterval& mi)
470 std::vector<TimedTrajectory> result;
472 for (
unsigned int i = 0; i < mi.breakpointIndices.size() - 1; i++)
477 int intervalStart = mi.breakpointIndices[i];
478 int intervalEnd = mi.breakpointIndices[i + 1];
479 std::vector<std::vector<double>> ikSolutionsInterval(
480 ikSolutions.begin() + mi.newIndexOfUserWaypoint[intervalStart],
481 ikSolutions.begin() + 1 + mi.newIndexOfUserWaypoint[intervalEnd]);
489 std::vector<std::vector<double>> userWaypointsIKSolution =
490 mi.getUserWaypointsIKSolutions(ikSolutions,
491 mi.newIndexOfUserWaypoint[intervalStart],
492 mi.newIndexOfUserWaypoint[intervalEnd]);
495 .calculateTimeOptimalTrajectory(
496 ikSolutionsInterval, userWaypointsIKSolution, rns, MAX_DEVIATION));
505 if (mementos.currentMemento != 0)
507 if (*(std::next(mementos.mementoList.begin(), mementos.currentMemento - 1)))
510 *(*(std::next(mementos.mementoList.begin(), mementos.currentMemento - 1)))));
511 isInitialized =
true;
515 isInitialized =
false;
517 mementos.currentMemento--;
529 if (mementos.currentMemento < mementos.mementoList.size() - 1)
531 if (*std::next(mementos.mementoList.begin(), mementos.currentMemento + 1))
534 *(*(std::next(mementos.mementoList.begin(), mementos.currentMemento + 1)))));
538 isInitialized =
false;
541 mementos.currentMemento++;
553 return isInitialized;
557 DesignerTrajectoryManager::theUniversalMethod(
unsigned int index)
559 if (designerTrajectory->getNrOfUserWaypoints() > 1)
562 ManipulationInterval mi = calculateManipulationInterval(
index);
565 std::vector<PoseBasePtr> poses;
566 std::vector<PoseBasePtr> posesBackup;
569 std::vector<VirtualRobot::IKSolver::CartesianSelection> selections;
570 std::vector<VirtualRobot::IKSolver::CartesianSelection> selectionsBackup;
574 poses.push_back(uwp->getPose());
575 posesBackup.push_back(PoseBasePtr(
576 new Pose(
Pose(uwp->getPose()->position, uwp->getPose()->orientation).toEigen())));
577 selections.push_back(uwp->getIKSelection());
578 selectionsBackup.push_back(uwp->getIKSelection());
586 std::vector<InterpolationType> interpolations;
589 interpolations.push_back(trans->getInterpolationType());
594 uwp->setPose(poses[count]);
595 uwp->setIKSelection(selections[count]);
599 calculateInterpolatedPoints(
607 std::vector<std::vector<double>> ikSolutions = calculateIKSolutions(mi);
612 uwp->setIKSelection(selectionsBackup[count]);
613 uwp->setPose(posesBackup[count]);
618 mi.applyJointAnglesOfUserWaypoints(ikSolutions);
621 std::vector<TimedTrajectory> timedTrajectories =
622 calculateTimeOptimalTrajectories(ikSolutions, mi);
627 if (mi.lowerIntervalLimit != 0)
630 for (
unsigned int i = mi.lowerIntervalLimit - 1; i > 0; i--)
632 if (designerTrajectory->getUserWaypoint(i)->getIsTimeOptimalBreakpoint())
639 if (mi.upperIntervalLimit != designerTrajectory->getNrOfUserWaypoints() - 1)
642 for (
unsigned int i = mi.upperIntervalLimit + 1;
643 i < designerTrajectory->getNrOfUserWaypoints() - 1;
650 std::vector<TrajectoryPtr> InterBreakPointTrajectories =
651 designerTrajectory->getInterBreakpointTrajectories();
652 std::vector<TrajectoryPtr> newInterBreakPointTrajectories;
653 newInterBreakPointTrajectories.insert(newInterBreakPointTrajectories.end(),
654 InterBreakPointTrajectories.begin(),
655 InterBreakPointTrajectories.begin() + countBefore);
661 newInterBreakPointTrajectories.push_back(traj);
664 newInterBreakPointTrajectories.insert(newInterBreakPointTrajectories.end(),
665 InterBreakPointTrajectories.end() - countAfter,
666 InterBreakPointTrajectories.end());
667 designerTrajectory->setInterBreakpointTrajectories(newInterBreakPointTrajectories);
672 TrajectoryPtr traj = designerTrajectory->getTimeOptimalTrajectory();
673 std::vector<double> timestamps = traj->getTimestamps();
674 unsigned int trajCount = 0;
677 while (trajCount < timestamps.size() &&
678 designerTrajectory->getUserWaypoint(mi.breakpointIndices[0])
679 ->getTimeOptimalTimestamp() > timestamps[trajCount])
684 for (
unsigned int t = 0; t < timedTrajectories.size(); t++)
687 std::vector<double> timedUserWaypoints = timedTrajectories[t].getUserPoints();
689 if (timedUserWaypoints.size() == 0)
691 throw InvalidArgumentException();
694 unsigned int count = 0;
698 for (
unsigned int i = mi.breakpointIndices[t]; i < mi.breakpointIndices[t + 1]; i++)
701 trans->setTimeOptimalDuration(timedUserWaypoints[count + 1] -
702 timedUserWaypoints[count]);
708 for (
unsigned int i = mi.breakpointIndices[t]; i < mi.breakpointIndices[t + 1]; i++)
712 unsigned int trajCountEnd = trajCount + 1;
713 std::vector<double> newTimestamps = {0.0};
714 std::vector<std::vector<double>> newTrajData;
716 for (
unsigned int dim = 0; dim < traj->dim(); dim++)
718 newTrajData.push_back({traj->getState(timestamps[trajCount], dim)});
722 while (trajCountEnd < timestamps.size() &&
723 timestamps[trajCountEnd] < end->getTimeOptimalTimestamp())
725 newTimestamps.push_back(timestamps[trajCountEnd] - timestamps[trajCount]);
726 for (
unsigned int dim = 0; dim < traj->dim(); dim++)
729 newTrajData[dim].push_back(traj->getState(timestamps[trajCountEnd], dim));
733 trajCount = trajCountEnd;
735 new Trajectory(newTrajData, newTimestamps, traj->getDimensionNames()));
736 trans->setTrajectory(newTraj);
742 for (
unsigned int i = mi.breakpointIndices.back();
743 i <= designerTrajectory->getNrOfUserWaypoints() - 2;
748 double time = designerTrajectory->getTransition(i)->getTimeOptimalDuration();
749 designerTrajectory->getTransition(i)->setTimeOptimalDuration(time);
754 for (
unsigned int i = 0; i < designerTrajectory->getNrOfUserWaypoints() - 2; i++)
756 double time = designerTrajectory->getTransition(i)->getStart()->getUserTimestamp() +
757 designerTrajectory->getTransition(i)->getUserDuration();
758 designerTrajectory->getTransition(i)->getEnd()->setUserTimestamp(time);
765 DesignerTrajectoryManager::getNewIkSolutionOfFirstPoint(PoseBasePtr oldStart,
766 PoseBasePtr newStart,
767 std::vector<double> jointAnglesOldStart)
770 std::vector<AbstractInterpolationPtr> ip =
772 std::vector<PoseBasePtr> poses;
773 std::vector<VirtualRobot::IKSolver::CartesianSelection> selections;
777 for (
double i = 1; i < DEFAULT_FRAME_COUNT - 1; i = i + 1.0)
779 poses.push_back(current->getPoseAt(i / DEFAULT_FRAME_COUNT));
783 poses.push_back(newStart);
784 selections.push_back(VirtualRobot::IKSolver::CartesianSelection::All);
785 std::vector<std::vector<double>> ikSolutions =
786 kinSolver->solveRecursiveIKRelative(rns, jointAnglesOldStart, poses, selections);
788 return ikSolutions.back();
797 kinSolver(
KinematicSolver::getInstance(environment->getScene(), environment->getRobot())),
798 environment(environment)
800 rns = environment->getRobot()->getRobotNodeSet(rnsName);
810 PoseBasePtr pb = kinSolver->doForwardKinematic(rns,
jointAngles);
811 Pose pose =
Pose(pb->position, pb->orientation);
812 pb = PoseBasePtr(
new Pose(rns->getKinematicRoot()->toLocalCoordinateSystem(pose.
toEigen())));
822 if (mementos.mementoList.size() != 0)
828 mementos.mementoList.push_back(
832 isInitialized =
true;
844 designerTrajectory->addLastUserWaypoint(newPoint);
845 theUniversalMethod(designerTrajectory->getNrOfUserWaypoints() - 1);
849 throw NotInitializedException(
"Manager is not intialized",
"Manager");
864 designerTrajectory->insertUserWaypoint(newPoint,
index);
865 theUniversalMethod(
index);
869 PoseBasePtr oldStart = designerTrajectory->getUserWaypoint(0)->getPose();
870 std::vector<double>
jointAngles = getNewIkSolutionOfFirstPoint(
871 oldStart, pb, designerTrajectory->getUserWaypoint(0)->getJointAngles());
877 designerTrajectory->insertUserWaypoint(newPoint,
index);
878 designerTrajectory->getUserWaypoint(0)->setJointAngles(
jointAngles);
880 theUniversalMethod(
index);
885 throw NotInitializedException(
"Manager is not intialized",
"Manager");
898 designerTrajectory->getUserWaypoint(
index)->setPose(pb);
902 std::vector<double> newJointAngles;
903 if (designerTrajectory->getNrOfUserWaypoints() > 1)
905 newJointAngles = getNewIkSolutionOfFirstPoint(
906 designerTrajectory->getUserWaypoint(1)->getPose(),
908 designerTrajectory->getUserWaypoint(1)->getJointAngles());
909 if (newJointAngles.size() == 0)
913 designerTrajectory->getUserWaypoint(0)->setPose(pb);
914 designerTrajectory->getUserWaypoint(0)->setJointAngles(newJointAngles);
919 newJointAngles = getNewIkSolutionOfFirstPoint(
920 designerTrajectory->getUserWaypoint(0)->getPose(),
922 designerTrajectory->getUserWaypoint(0)->getJointAngles());
923 if (newJointAngles.size() == 0)
928 uwpTmp->setJointAngles(newJointAngles);
929 uwpTmp->setIKSelection(designerTrajectory->getUserWaypoint(0)->getIKSelection());
930 uwpTmp->setIsTimeOptimalBreakpoint(
931 designerTrajectory->getUserWaypoint(0)->getIsTimeOptimalBreakpoint());
933 designerTrajectory = dtTmp;
936 theUniversalMethod(
index);
940 throw NotInitializedException(
"Manager is not intialized",
"Manager");
954 designerTrajectory->getUserWaypoint(
index)->setIKSelection(ikSelection);
955 theUniversalMethod(
index);
959 throw NotInitializedException(
"Manager is not intialized",
"Manager");
971 if (designerTrajectory->getNrOfUserWaypoints() == 1)
973 isInitialized =
false;
974 designerTrajectory =
nullptr;
979 designerTrajectory->deleteUserWaypoint(
index);
984 designerTrajectory->getUserWaypoint(0)->setTimeOptimalTimestamp(0);
985 designerTrajectory->getUserWaypoint(0)->setUserTimestamp(0);
989 if (designerTrajectory->getNrOfUserWaypoints() == 1)
996 if (
index == designerTrajectory->getNrOfUserWaypoints())
998 theUniversalMethod(
index - 1);
1002 theUniversalMethod(
index);
1008 throw NotInitializedException(
"Manager is not intialized",
"Manager");
1020 designerTrajectory->getTransition(
index)->setInterpolationType(it);
1021 theUniversalMethod(
index);
1025 throw NotInitializedException(
"Manager is not intialized",
"Manager");
1037 designerTrajectory->getUserWaypoint(
index)->setIsTimeOptimalBreakpoint(b);
1038 theUniversalMethod(
index);
1042 throw NotInitializedException(
"Manager is not intialized",
"Manager");
1054 designerTrajectory->getTransition(
index)->setUserDuration(duration);
1056 for (
unsigned int i =
index + 1; i < designerTrajectory->getNrOfUserWaypoints() - 1; i++)
1059 trans->setUserDuration(trans->getUserDuration());
1066 throw NotInitializedException(
"Manager is not intialized",
"Manager");
1077 *(*std::next(mementos.mementoList.begin(), mementos.currentMemento))));
1088 if (newDesignerTrajectory->getNrOfUserWaypoints() > 1 &&
1089 newDesignerTrajectory->getRns()->getName() == rns->getName())
1091 TrajectoryPtr trajectory = newDesignerTrajectory->getTimeOptimalTrajectory();
1094 for (
unsigned int i = 0; i < newDesignerTrajectory->getNrOfUserWaypoints() - 1; i++)
1096 TransitionPtr trans = newDesignerTrajectory->getTransition(i);
1097 double transBeginTime = trans->getStart()->getTimeOptimalTimestamp();
1098 double transEndTime = trans->getEnd()->getTimeOptimalTimestamp();
1099 TrajectoryPtr traj = trajectory->getPart(transBeginTime, transEndTime, 2);
1103 std::vector<std::vector<double>> nodeData;
1104 for (
unsigned int dim = 0; dim < traj->dim(); dim++)
1106 nodeData.push_back(traj->getDimensionData(dim));
1108 std::vector<double> newTimestamps = {0.0};
1109 std::vector<double> oldTimestamps = traj->getTimestamps();
1110 for (
unsigned int j = 1; j < oldTimestamps.size(); j++)
1112 if (oldTimestamps[j] - transBeginTime >= 0)
1114 newTimestamps.push_back(oldTimestamps[j] - transBeginTime);
1118 newTimestamps.push_back(0);
1123 new Trajectory(nodeData, newTimestamps, traj->getDimensionNames()));
1124 trans->setTrajectory(shiftedTraj);
1126 designerTrajectory = newDesignerTrajectory;
1128 if (mementos.mementoList.size() != 0)
1134 mementos.mementoList.push_back(
1137 isInitialized =
true;
1146 return mementos.currentMemento > 0;
1152 if (mementos.mementoList.size() != 0 &&
1153 mementos.currentMemento < mementos.mementoList.size() - 1)