2 #include "../Util/OrientationConversion.h"
9 void TrajectoryController::onInitComponent()
11 ARMARX_INFO <<
"RobotTrajectoryDesigner: TrajectoryController on init";
14 void TrajectoryController::onConnectComponent()
16 ARMARX_INFO <<
"RobotTrajectoryDesigner: TrajectoryController on connect";
19 void TrajectoryController::onDisconnectComponent()
21 ARMARX_INFO <<
"RobotTrajectoryDesigner: TrajectoryController on disconnect";
24 void TrajectoryController::onExitComponent()
26 ARMARX_INFO <<
"RobotTrajectoryDesigner: TrajectoryController on exit";
31 TrajectoryController::TrajectoryController()
37 void TrajectoryController::updateTCP(QString tcp)
39 if (environment->getRobot() == NULL || holder == NULL)
44 VirtualRobot::RobotNodeSetPtr helpRns = environment->getRobot()->getRobotNodeSet(tcp.toStdString());
45 if (manager != NULL && !manager->getIsInitialized())
47 holder->deleteDesignerTrajectoryManager(rns->getName());
48 emit removeTransitionWaypointGui();
49 emit removeTrajectory(QString::fromStdString(rns->getName()));
54 helpCreateDesignerTrajectoryManager();
56 else if (rns == helpRns)
60 else if (holder->rnsIsPartOfExistingRns(helpRns->getName()))
62 if (holder->rnsExists(helpRns->getName()))
64 emit removeTransitionWaypointGui();
65 manager = holder->getTrajectoryManager(helpRns->getName());
68 emit showTrajectory(manager->getDesignerTrajectory());
69 addTransitionWaypointGui(manager->getDesignerTrajectory());
70 emit updateSelectedTCP(QString::fromStdString(rns->getName()));
74 QMessageBox* rnsIsPartOfExistingRns =
new QMessageBox;
75 rnsIsPartOfExistingRns->setWindowTitle(QString::fromStdString(
"Error Message"));
76 rnsIsPartOfExistingRns->setText(QString::fromStdString(
"Selected TCP is part of an existing TCP."));
77 rnsIsPartOfExistingRns->exec();
78 emit updateSelectedTCP(QString::fromStdString(rns->getName()));
83 emit showTrajectory(NULL);
84 emit removeTransitionWaypointGui();
86 helpCreateDesignerTrajectoryManager();
92 void TrajectoryController::updateTransition(
int transition,
int it)
99 emit showTrajectory(manager->getDesignerTrajectory());
100 changeTransitionWaypointGui(manager->getDesignerTrajectory());
101 helpEnableUndoRedo();
104 catch (LocalException& e)
106 helpExceptionMessageBox(e.getReason());
107 TransitionPtr transitionPtr = manager->getDesignerTrajectory()->getTransition(transition);
108 helpChangeTransitionGui(transitionPtr, transition);
110 catch (InvalidArgumentException& e)
112 helpExceptionMessageBox(e.reason);
113 TransitionPtr transitionPtr = manager->getDesignerTrajectory()->getTransition(transition);
114 helpChangeTransitionGui(transitionPtr, transition);
119 void TrajectoryController::updateTransition(
int transition,
double duration)
125 manager->setTransitionUserDuration(transition, duration);
126 emit showTrajectory(manager->getDesignerTrajectory());
127 changeTransitionWaypointGui(manager->getDesignerTrajectory());
128 helpEnableUndoRedo();
131 catch (InvalidArgumentException& e)
133 TransitionPtr transitionPtr = manager->getDesignerTrajectory()->getTransition(transition);
134 helpChangeTransitionGui(transitionPtr, transition);
139 void TrajectoryController::updateWaypoint(
int waypoint, std::vector<double>
values)
145 manager->editWaypointPoseBase(waypoint, parseToPoseBasePtr(
values));
146 emit showTrajectory(manager->getDesignerTrajectory());
147 changeTransitionWaypointGui(manager->getDesignerTrajectory());
148 helpEnableUndoRedo();
151 catch (LocalException& e)
153 UserWaypointPtr waypointPtr = manager->getDesignerTrajectory()->getUserWaypoint(waypoint);
154 helpChangeWaypointGui(waypointPtr, waypoint);
156 catch (InvalidArgumentException& e)
158 UserWaypointPtr waypointPtr = manager->getDesignerTrajectory()->getUserWaypoint(waypoint);
159 helpChangeWaypointGui(waypointPtr, waypoint);
164 void TrajectoryController::updateWaypoint(
int waypoint)
170 manager->editWaypointPoseBase(waypoint, *
new PoseBasePtr(
new Pose(rns->getKinematicRoot()->toLocalCoordinateSystem(rns->getTCP()->getGlobalPose()))));
171 emit showTrajectory(manager->getDesignerTrajectory());
172 changeTransitionWaypointGui(manager->getDesignerTrajectory());
173 helpEnableUndoRedo();
176 catch (LocalException& e)
178 helpExceptionMessageBox(e.getReason());
181 catch (InvalidArgumentException& e)
183 helpExceptionMessageBox(e.reason);
188 void TrajectoryController::updateWaypoint(
int waypoint, PoseBasePtr newPoseBase)
194 manager->editWaypointPoseBase(waypoint, newPoseBase);
195 emit showTrajectory(manager->getDesignerTrajectory());
196 changeTransitionWaypointGui(manager->getDesignerTrajectory());
197 helpEnableUndoRedo();
200 catch (LocalException& e)
202 helpExceptionMessageBox(e.getReason());
203 UserWaypointPtr waypointPtr = manager->getDesignerTrajectory()->getUserWaypoint(waypoint);
204 helpChangeWaypointGui(waypointPtr, waypoint);
206 catch (InvalidArgumentException& e)
208 helpExceptionMessageBox(e.reason);
209 UserWaypointPtr waypointPtr = manager->getDesignerTrajectory()->getUserWaypoint(waypoint);
210 helpChangeWaypointGui(waypointPtr, waypoint);
215 void TrajectoryController::updateWaypoint(
int waypoint,
int cartesianSelection)
222 emit showTrajectory(manager->getDesignerTrajectory());
223 changeTransitionWaypointGui(manager->getDesignerTrajectory());
224 emit cartesianSelectionChanged(manager->getDesignerTrajectory()->getUserWaypoint(waypoint)->getIKSelection());
225 helpEnableUndoRedo();
228 catch (LocalException& e)
230 helpExceptionMessageBox(e.getReason());
231 UserWaypointPtr waypointPtr = manager->getDesignerTrajectory()->getUserWaypoint(waypoint);
232 helpChangeWaypointGui(waypointPtr, waypoint);
234 catch (InvalidArgumentException& e)
236 helpExceptionMessageBox(e.reason);
237 UserWaypointPtr waypointPtr = manager->getDesignerTrajectory()->getUserWaypoint(waypoint);
238 helpChangeWaypointGui(waypointPtr, waypoint);
243 void TrajectoryController::updateWaypoint(
int waypoint,
bool isBreakpoint)
252 manager->setWaypointAsBreakpoint(waypoint, isBreakpoint);
253 emit showTrajectory(manager->getDesignerTrajectory());
254 changeTransitionWaypointGui(manager->getDesignerTrajectory());
255 helpEnableUndoRedo();
257 catch (LocalException& e)
259 helpExceptionMessageBox(e.getReason());
260 UserWaypointPtr waypointPtr = manager->getDesignerTrajectory()->getUserWaypoint(waypoint);
261 helpChangeWaypointGui(waypointPtr, waypoint);
263 catch (InvalidArgumentException& e)
265 helpExceptionMessageBox(e.reason);
266 UserWaypointPtr waypointPtr = manager->getDesignerTrajectory()->getUserWaypoint(waypoint);
267 helpChangeWaypointGui(waypointPtr, waypoint);
271 void TrajectoryController::addWaypoint(
int waypoint,
bool insertAfter)
278 unsigned int helpWaypoint =
static_cast<unsigned>(waypoint);
279 if (!manager->getIsInitialized())
281 std::vector<double> jointValues;
282 for (
double newValue : rns->getJointValues())
284 jointValues.push_back((
double) newValue);
286 manager->initializeDesignerTrajectory(jointValues);
287 emit showTrajectory(manager->getDesignerTrajectory());
288 helpAddWaypointGui(manager->getDesignerTrajectory()->getUserWaypoint(waypoint), waypoint);
289 emit newTrajectory(QString::fromStdString(rns->getName()));
290 emit updateSelectedTCP(QString::fromStdString(rns->getName()));
292 else if (helpWaypoint == manager->getDesignerTrajectory()->getNrOfUserWaypoints() -
static_cast<unsigned>(1) && insertAfter)
296 manager->addWaypoint(*
new PoseBasePtr(
new Pose(rns->getKinematicRoot()->toLocalCoordinateSystem(rns->getTCP()->getGlobalPose()))));
297 emit showTrajectory(manager->getDesignerTrajectory());
298 helpAddWaypointGui(manager->getDesignerTrajectory()->getUserWaypoint(waypoint + 1), waypoint + 1);
299 helpAddTransitionGui(manager->getDesignerTrajectory()->getTransition(waypoint), waypoint);
300 changeTransitionWaypointGui(manager->getDesignerTrajectory());
303 catch (LocalException& e)
305 helpExceptionMessageBox(e.getReason());
307 catch (InvalidArgumentException& e)
309 helpExceptionMessageBox(e.reason);
312 else if (helpWaypoint < manager->getDesignerTrajectory()->getNrOfUserWaypoints() -
static_cast<unsigned>(1) && waypoint >= 0 && insertAfter)
316 manager->insertWaypoint(waypoint + 1, *
new PoseBasePtr(
new Pose(rns->getKinematicRoot()->toLocalCoordinateSystem(rns->getTCP()->getGlobalPose()))));
317 emit showTrajectory(manager->getDesignerTrajectory());
318 helpAddWaypointGui(manager->getDesignerTrajectory()->getUserWaypoint(waypoint + 1), waypoint + 1);
319 helpAddTransitionGui(manager->getDesignerTrajectory()->getTransition(waypoint + 1), waypoint + 1);
320 changeTransitionWaypointGui(manager->getDesignerTrajectory());
323 catch (LocalException& e)
325 helpExceptionMessageBox(e.getReason());
328 else if (helpWaypoint < manager->getDesignerTrajectory()->getNrOfUserWaypoints() && waypoint >= 0 && !insertAfter)
332 manager->insertWaypoint(waypoint, *
new PoseBasePtr(
new Pose(rns->getKinematicRoot()->toLocalCoordinateSystem(rns->getTCP()->getGlobalPose()))));
333 emit showTrajectory(manager->getDesignerTrajectory());
334 helpAddWaypointGui(manager->getDesignerTrajectory()->getUserWaypoint(waypoint), waypoint);
335 helpAddTransitionGui(manager->getDesignerTrajectory()->getTransition(waypoint), waypoint);
336 changeTransitionWaypointGui(manager->getDesignerTrajectory());
339 catch (LocalException& e)
341 helpExceptionMessageBox(e.getReason());
343 catch (InvalidArgumentException& e)
345 helpExceptionMessageBox(e.reason);
349 helpEnableUndoRedo();
352 void TrajectoryController::deleteWaypoint(
int waypoint)
359 if (!manager->getIsInitialized())
364 unsigned int helpWaypoint =
static_cast<unsigned>(waypoint);
365 if (waypoint == 0 && manager->getDesignerTrajectory()->getNrOfUserWaypoints() == 1)
369 manager->deleteWaypoint(waypoint);
370 emit removeTransitionWaypointGui();
372 catch (LocalException& e)
374 helpExceptionMessageBox(e.getReason());
376 catch (InvalidArgumentException& e)
378 helpExceptionMessageBox(e.reason);
381 else if (helpWaypoint == manager->getDesignerTrajectory()->getNrOfUserWaypoints() -
static_cast<unsigned>(1))
385 manager->deleteWaypoint(waypoint);
386 emit showTrajectory(manager->getDesignerTrajectory());
387 emit removeWaypointGui(waypoint);
388 emit removeTransitionGui(waypoint - 1);
389 changeTransitionWaypointGui(manager->getDesignerTrajectory());
392 catch (LocalException& e)
394 helpExceptionMessageBox(e.getReason());
396 catch (InvalidArgumentException& e)
398 helpExceptionMessageBox(e.reason);
401 else if (waypoint >= 0 && helpWaypoint < manager->getDesignerTrajectory()->getNrOfUserWaypoints() -
static_cast<unsigned>(1))
405 manager->deleteWaypoint(waypoint);
406 emit showTrajectory(manager->getDesignerTrajectory());
407 emit removeWaypointGui(waypoint);
408 emit removeTransitionGui(waypoint);
409 changeTransitionWaypointGui(manager->getDesignerTrajectory());
412 catch (LocalException& e)
414 helpExceptionMessageBox(e.getReason());
416 catch (InvalidArgumentException& e)
418 helpExceptionMessageBox(e.reason);
421 helpEnableUndoRedo();
429 if (holder->rnsIsPartOfExistingRns(designerTrajectory->getRns()->getName()))
431 QMessageBox::StandardButton reply;
435 emit removeTrajectory(QString::fromStdString(rns->getName()));
436 helpImportDesignerTrajectory(designerTrajectory);
440 QMessageBox* importFailed =
new QMessageBox;
441 importFailed->setWindowTitle(QString::fromStdString(
"Error Message"));
442 importFailed->setText(QString::fromStdString(
"Import failed."));
443 importFailed->exec();
448 helpImportDesignerTrajectory(designerTrajectory);
452 helpEnableUndoRedo();
457 void TrajectoryController::exportTrajectory(
int fps)
461 std::vector<DesignerTrajectoryPtr> designerTrajectories;
462 for (VirtualRobot::RobotNodeSetPtr
s : environment->getRobot()->getRobotNodeSets())
464 if (holder->rnsExists(
s->getName()) && holder->getTrajectoryManager(
s->getName())->getIsInitialized())
467 designerTrajectories.push_back(holder->getTrajectoryManager(
s->getName())->getDesignerTrajectory());
470 emit exportTrajectory(designerTrajectories);
474 void TrajectoryController::exportTrajectory()
478 std::vector<DesignerTrajectoryPtr> designerTrajectories;
479 for (VirtualRobot::RobotNodeSetPtr
s : environment->getRobot()->getRobotNodeSets())
481 if (holder->rnsExists(
s->getName()) && holder->getTrajectoryManager(
s->getName())->getIsInitialized())
483 designerTrajectories.push_back(holder->getTrajectoryManager(
s->getName())->getDesignerTrajectory());
486 emit exportTrajectory(designerTrajectories);
490 void TrajectoryController::undo()
496 emit removeTransitionWaypointGui();
497 emit showTrajectory(manager->getDesignerTrajectory());
498 addTransitionWaypointGui(manager->getDesignerTrajectory());
501 helpEnableUndoRedo();
506 void TrajectoryController::redo()
512 emit removeTransitionWaypointGui();
513 emit showTrajectory(manager->getDesignerTrajectory());
514 addTransitionWaypointGui(manager->getDesignerTrajectory());
517 helpEnableUndoRedo();
522 void TrajectoryController::playTrajectories()
524 std::vector<DesignerTrajectoryPtr> designerTrajectories;
525 for (VirtualRobot::RobotNodeSetPtr
s : environment->getRobot()->getRobotNodeSets())
527 if (holder->rnsExists(
s->getName()))
529 designerTrajectories.push_back(holder->getTrajectoryManager(
s->getName())->getDesignerTrajectory());
532 emit playTrajectories(designerTrajectories);
537 this->environment = environment;
538 holder = std::make_shared<DesignerTrajectoryHolder>(environment);
541 emit removeTransitionWaypointGui();
542 emit removeAllTrajectories();
543 helpEnableUndoRedo();
547 void TrajectoryController::setActiveColModelName(QString activeColModelName)
549 this->activeColModelName = activeColModelName.toStdString();
553 void TrajectoryController::setBodyColModelsNames(QStringList bodyColModelsNames)
555 this->bodyColModelsNames.clear();
556 for (QString name : bodyColModelsNames)
558 this->bodyColModelsNames.push_back(name.toStdString());
567 std::vector<double> TrajectoryController::parseToCoordinate(PoseBasePtr pose)
569 float x = pose->position->x;
570 float y = pose->position->y;
571 float z = pose->position->z;
573 orientation->qw = pose->orientation->qw;
574 orientation->qx = pose->orientation->qx;
575 orientation->qy = pose->orientation->qy;
576 orientation->qz = pose->orientation->qz;
577 std::vector<double> eulerAngle = OrientationConversion::toEulerAngle(orientation);
578 return {x, y, z, eulerAngle[0], eulerAngle[1], eulerAngle[2]};
581 PoseBasePtr TrajectoryController::parseToPoseBasePtr(std::vector<double>
values)
584 QuaternionBasePtr orientation = OrientationConversion::toQuaternion(
values[3],
values[4],
values[5]);
585 return new FramedPose(position, orientation,
"",
"");
590 void TrajectoryController::helpAddWaypointGui(
UserWaypointPtr waypoint,
int waypointIndex)
592 std::vector<double>
values = parseToCoordinate(waypoint->getPose());
593 bool isBreakpoint = waypoint->getIsTimeOptimalBreakpoint();
594 emit addWaypointGui(waypointIndex,
values, waypoint->getIKSelection(), isBreakpoint);
597 void TrajectoryController::helpAddTransitionGui(
TransitionPtr transition,
int transitionIndex)
599 double duration = transition->getUserDuration();
600 double startTime = transition->getStart()->getUserTimestamp();
602 emit addTransitionGui(transitionIndex, duration, startTime, interpolation);
609 for (
unsigned int i = 0; i < trajectory->getNrOfUserWaypoints(); i++)
612 helpAddWaypointGui(waypoint, i);
615 for (
unsigned int i = 0; i < trajectory->getNrOfUserWaypoints() - 1; i++)
618 helpAddTransitionGui(transition, i);
630 void TrajectoryController::helpChangeTransitionGui(
TransitionPtr transition,
int transitionIndex)
632 double duration = transition->getUserDuration();
633 double startTime = transition->getStart()->getUserTimestamp();
635 emit changeTransitionGui(transitionIndex, duration, startTime, interpolation);
638 void TrajectoryController::helpChangeWaypointGui(
UserWaypointPtr waypoint,
int waypointIndex)
640 std::vector<double>
values = parseToCoordinate(waypoint->getPose());
641 bool isBreakpoint = waypoint->getIsTimeOptimalBreakpoint();
642 emit changeWaypointGui(waypointIndex,
values, waypoint->getIKSelection(), isBreakpoint);
647 for (
unsigned int i = 0; i < trajectory->getNrOfUserWaypoints(); i++)
650 helpChangeWaypointGui(waypoint, i);
653 for (
unsigned int i = 0; i < trajectory->getNrOfUserWaypoints() - 1; i++)
656 helpChangeTransitionGui(transition, i);
664 removeTransitionWaypointGui();
665 holder->import(designerTrajectory,
true);
666 manager = holder->getTrajectoryManager(designerTrajectory->getRns()->getName());
667 rns = manager->getDesignerTrajectory()->getRns();
668 emit rnsChanged(rns);
669 emit showTrajectory(manager->getDesignerTrajectory());
670 emit newTrajectory(QString::fromStdString(rns->getName()));
671 emit updateSelectedTCP(QString::fromStdString(rns->getName()));
672 addTransitionWaypointGui(manager->getDesignerTrajectory());
677 void TrajectoryController::helpCreateDesignerTrajectoryManager()
679 holder->createDesignerTrajectoryManager(rns->getName());
680 manager = holder->getTrajectoryManager(rns->getName());
681 emit rnsChanged(rns);
686 bool TrajectoryController::helpEnableExportPreviewAll()
689 for (VirtualRobot::RobotNodeSetPtr
s : environment->getRobot()->getRobotNodeSets())
691 if (holder->rnsExists(
s->getName()) && holder->getTrajectoryManager(
s->getName())->getIsInitialized())
693 if (holder->getTrajectoryManager(
s->getName())->getDesignerTrajectory()->getNrOfUserWaypoints() > 1)
706 void TrajectoryController::helpEnableUndoRedo()
710 emit enableRedo(manager->redoPossible());
711 emit enableUndo(manager->undoPossible());
715 emit enableRedo(
false);
716 emit enableUndo(
false);
720 void TrajectoryController::helpEnableButtons()
722 emit enableExportButtons(helpEnableExportPreviewAll());
723 emit enablePreviewAll(helpEnableExportPreviewAll());
726 emit enableAdd(
true);
727 if (manager->getIsInitialized())
729 unsigned int nrWaypoints = manager->getDesignerTrajectory()->getNrOfUserWaypoints();
730 emit enableIKSolutionButton(
false);
731 if (nrWaypoints >= 1)
733 emit enableDeleteChange(
true);
734 if (nrWaypoints >= 2)
736 emit enablePreview(
true);
740 emit enablePreview(
false);
745 emit enableDeleteChange(
false);
746 emit enablePreview(
false);
751 emit enableDeleteChange(
false);
752 emit enablePreview(
false);
753 emit enableIKSolutionButton(
true);
758 emit enableAdd(
false);
759 emit enablePreview(
false);
760 emit enableDeleteChange(
false);
761 emit enableIKSolutionButton(
false);
765 void TrajectoryController::helpExceptionMessageBox(std::string errorMessage)
767 QMessageBox* errorMessageBox =
new QMessageBox;
768 errorMessageBox->setWindowTitle(QString::fromStdString(
"Error Message"));
769 errorMessageBox->setText(QString::fromStdString(errorMessage));
770 errorMessageBox->exec();
775 void TrajectoryController::helpCollision()
782 if (holder->isInCollision(activeColModelName, bodyColModelsNames, 100))
784 QMessageBox* changeInterpolationFailed =
new QMessageBox;
785 changeInterpolationFailed->setWindowTitle(QString::fromStdString(
"Information"));
786 changeInterpolationFailed->setText(QString::fromStdString(
"A collision is detected."));
787 changeInterpolationFailed->exec();