DesignerTrajectoryManager.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2015-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package ArmarXGuiPlugins::RobotTrajectoryDesigner::Manager
19  * @author Luca Quaer
20  * @date 2018
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #ifndef DESIGNERTRAJECTORYMANAGER_H
26 #define DESIGNERTRAJECTORYMANAGER_H
27 
28 #include <functional>
29 #include <set>
30 
31 #include <Eigen/Core>
32 #include <RobotAPI/interface/core/PoseBase.h>
33 #include <RobotComponents/interface/components/RobotIK.h>
35 #include <MotionPlanning/Planner/BiRrt.h>
36 #include <MotionPlanning/CSpace/CSpaceSampled.h>
37 
38 #include "../Interpolation/InterpolationType.h"
39 #include "../Model/DesignerTrajectory.h"
40 #include "../KinematicSolver.h"
41 #include "../Environment.h"
42 #include "../Interpolation/InterpolationSegmentFactory.h"
43 #include "../TrajectoryCalculation/DesignerTrajectoryCalculator.h"
44 
45 namespace armarx
46 {
47  /**
48  * @brief The DesignerTrajectoryManager class enables easy interaction with the model.
49  *
50  * Using this class, the model is always self-consistent.
51  *
52  * @see DesignerTrajectory
53  */
55  {
56  private:
57  ////////////////////////////////////////////////////////////////////////////////////////////
58  /// Structs
59  ////////////////////////////////////////////////////////////////////////////////////////////
60  ///
61  struct ManipulationInterval
62  {
63  private:
64  std::list<UserWaypointPtr> userWaypointsList;
65  std::list<TransitionPtr> transitionsList;
66 
67  std::set<int> breakpointIndicesSet;
68 
69  // made public
70  //std::vector<UserWaypointPtr> userWaypoints;
71  //std::vector<TransitionPtr> transitions;
72 
73  // made public
74  //std::vector<PoseBasePtr> interpolatedPoses; //including poses of UserWaypoints
75 
76  // this vector holds the new index (in interpolatedPoses) of the UserWaypoints
77  //std::vector<int> newIndexOfUserWaypoint; // e.g.: uwp(0) -> new index 9
78 
79  public:
80  unsigned int upperIntervalLimit;
81  unsigned int lowerIntervalLimit;
82 
83  std::vector<UserWaypointPtr> userWaypoints;
84  std::vector<TransitionPtr> transitions;
85 
86  /* absolute (absolute to all designer trajectory user waypoints)
87  * breakpoint indices of breakpoints in the manipulation interval
88  * BEFORE inserting interpolated poses...
89  * points with index upperIntervalLimit and lowerIntervalLimit included !
90  */
91  std::vector<unsigned int> breakpointIndices;
92 
93  //std::vector<PoseBasePtr> interpolatedPoses; //including poses of UserWaypoints
94  std::vector<std::vector<PoseBasePtr>> interpolatedTransitions;
95 
96  // this vector holds the new index (in interpolatedPoses) of the UserWaypoints
97  std::vector<unsigned int> newIndexOfUserWaypoint; // e.g.: uwp(0) -> new index 9
98 
99  void addFirstUserWaypoint(UserWaypointPtr uw);
100  void pushFront(UserWaypointPtr uw, TransitionPtr t);
101  void pushBack(UserWaypointPtr uw, TransitionPtr t);
102  void addBreakpointIndex(int index);
103  void finishSearch();
104 
105  //int getFirstBreakpointIndex() const;
106  //int getLastBreakpointIndex() const;
107  UserWaypointPtr getUserWaypointByRealIndex(unsigned int i) const;
108  UserWaypointPtr getUserWaypointByZeroBasedIndex(unsigned int i) const;
109  TransitionPtr getTransitionByRealIndex(unsigned int i) const;
110  TransitionPtr getTransitionByZeroBasedIndex(unsigned int i) const;
111 
112  std::vector<std::vector<double>> getUserWaypointsIKSolutions(
113  std::vector<std::vector<double>>& ikSolutions,
114  unsigned int intervalStart,
115  unsigned int intervalEnd);
116  void applyJointAnglesOfUserWaypoints(std::vector<std::vector<double>> ikSolution);
117  };
118 
119  struct InsertTransition
120  {
121  public:
122  unsigned int transitionIndex;
123 
124  // insert transition with transitionIndex between
125  // insertionIndex and insertionIndex + 1
126  unsigned int insertionIndex;
127  };
128 
129  struct Mementos
130  {
131  std::list<DesignerTrajectoryPtr> mementoList;
132  unsigned int currentMemento = 0;
133  };
134 
135  ////////////////////////////////////////////////////////////////////////////////////////////
136  /// Attributes
137  ////////////////////////////////////////////////////////////////////////////////////////////
138  VirtualRobot::RobotNodeSetPtr rns;
139  DesignerTrajectoryPtr designerTrajectory;
140  KinematicSolverPtr kinSolver;
141  bool isInitialized = false;
142  Mementos mementos;
143  static const unsigned int MEMENTO_MAX = 20;
144  static const unsigned int DEFAULT_FRAME_COUNT = 100;
145  static constexpr double MAX_DEVIATION = 0.0001;
146  EnvironmentPtr environment;
147  //CollisionDetectionPtr collisionDetection;
148  ////////////////////////////////////////////////////////////////////////////////////////////
149 
150  /**
151  * @brief saveState
152  */
153  void saveState();
154 
155  /*struct ManipulationInterval
156  {
157  int upperIndex;
158  int lowerIndex;
159  std::vector<UserWaypointPtr> userWaypoints;
160  std::vector<TransitionPtr> transitions;
161 
162  void push(UserWaypointPtr uw, TransitionPtr t);
163 
164  static ManipulationInterval mergeManipulationIntervalHalfs(
165  ManipulationInterval lowerHalf,
166  ManipulationInterval upperHalf);
167  };
168 
169  struct InterpolatedPoints
170  {
171  std::vector<PoseBasePtr> poseBases;
172  std::vector<int> userWaypoint; // e.g.: uwp(0) -> new index 9
173  };*/
174 
175  /**
176  * @brief Calculates one half of the manipulation interval
177  * @param i index where the manipulaion takes place
178  * @param f function specifying the search direction
179  * @param fInv function specifying the inverse search direction
180  * @return interval half which is affected by the manipulation as ManipulationInterval
181  *
182  * @see ManipulationInterval
183  */
184  //ManipulationInterval searchFrom(int i,
185  // std::function<int (int)> f,
186  // std::function<int (int)> fInv);
187 
188  /**
189  * @brief Calculates the whole manipulation interval
190  * @param manipulationIndex index where the manipulation takes place
191  * @return interval which is affected by the manipulation
192  */
193  ManipulationInterval calculateManipulationInterval(
194  unsigned int manipulationIndex);
195 
196  /**
197  * @brief getInterpolationObjects calculates the interpolation object for each transition
198  * in the given manipulation interval
199  * @param mi manipulation interval
200  * @return interpolation objects for each transition
201  *
202  * @see InterpolationSegmentFactory
203  */
204  std::vector<AbstractInterpolationPtr> getInterpolationObjects(
205  ManipulationInterval& mi);
206 
207  /**
208  * @brief Calculates the interpolated points between two userwaypoints according to the
209  * userDuration of the transition between them and the desired frames per second.
210  * @param interpolationObjects interpolation objects
211  * @param fps frames per second
212  * @param ManipulationInterval object to insert the interpolated points into
213  */
214  void calculateInterpolatedPoints(
215  std::vector<AbstractInterpolationPtr> interpolationObjects,
216  unsigned int fps,
217  ManipulationInterval& mi);
218 
219  /**
220  * @brief Checks if the transition with the given index is reachable
221  * @param mi ManipulationInterval
222  * @param transitionIndex transition index
223  * @return true, if transition is reachable;
224  * false, if transition is not reachable
225  */
226  bool checkTransitionReachability(ManipulationInterval& mi, unsigned int transitionIndex);
227 
228  /**
229  * @brief Calls motion planning with a start and end joint angle configuration
230  * @param start joint angle configuration to begin with
231  * @param end joint angle configuration to end with
232  * @return vector of joint angle configurations
233  */
234  /*
235  std::vector<std::vector<double>> motionPlanning(
236  std::vector<double> start,
237  std::vector<double> end);
238  */
239 
240  /**
241  * @brief Calculates the IKSelection of the given InterpolatedPoints.
242  * If transitions are not reachable or have collisions, these transitions get
243  * motion planned. The motion planned IK-solutions are inserted into the
244  * regularly calculated IK-solutions.
245  * The caller of this function passes the parameter 'motionPlanningCalled' to know
246  * if something was motion planned.
247  * @param mi ManipulationInterval
248  * @param motionPlanningCalled boolean flag which indicates if something was motion planned
249  * @return IKSolutions of the given InterpolatedPoints
250  */
251  std::vector<std::vector<double>> calculateIKSolutions(
252  ManipulationInterval& mi/*,
253  bool& motionPlanningCalled*/);
254 
255  /**
256  * @brief Calculates the TimedTrajectory of each breakpoint-interval
257  * @param ikSolutions all ikSolutions of current calculation
258  * @param mi ManipulationInterval
259  * @return returns vector of TimedTrajectories
260  */
261  std::vector<TimedTrajectory> calculateTimeOptimalTrajectories(
262  std::vector<std::vector<double>> ikSolutions,
263  ManipulationInterval& mi);
264 
265  void theUniversalMethod(unsigned int index);
266 
267  std::vector<double> getNewIkSolutionOfFirstPoint(PoseBasePtr oldStart, PoseBasePtr newStart, std::vector<double> jointAnglesOldStart);
268  public:
269  ////////////////////////////////////////////////////////////////////////////////////////////
270  /// Constructor(s)
271  ////////////////////////////////////////////////////////////////////////////////////////////
272  /**
273  * @brief Constructor of this class.
274  * @param rns RobotNodeSet
275  */
276  DesignerTrajectoryManager(std::string rnsName, EnvironmentPtr environment_);
277 
278  ////////////////////////////////////////////////////////////////////////////////////////////
279  /// Public functions
280  ////////////////////////////////////////////////////////////////////////////////////////////
281  /**
282  * @brief Initializes the DesignerTrajectory object of this manager instance.
283  * @param jointAngles start jointangle-configuration of the trajectory
284  */
285  void initializeDesignerTrajectory(std::vector<double>& jointAngles);
286 
287  /**
288  * @brief Adds a UserWaypoint to the end of this manager's DesignerTrajectory.
289  *
290  * To create this UserWaypoint the following parameters are needed:
291  *
292  * @param pose pose of the UserWaypoint
293  */
294  void addWaypoint(const PoseBasePtr pose);
295 
296  /**
297  * @brief Inserts a UserWaypoint at the given position to this manager's DesignerTrajectory.
298  *
299  * To create this UserWaypoint the following parameters are needed:
300  *
301  * @param index position to insert the UserWaypoint into
302  * @param pb pose of the UserWaypoint
303  */
304  void insertWaypoint(unsigned int index, const PoseBasePtr pose);
305 
306  /**
307  * @brief Edits the pose of a UserWaypoint.
308  * @param index index of the UserWaypoint to edit
309  * @param pose new pose
310  */
311  void editWaypointPoseBase(unsigned int index, const PoseBasePtr pose);
312 
313  /**
314  * @brief Edits the IKSelection of a UserWaypoint.
315  * @param index index of the UserWaypoint to edit
316  * @param ikSelection new IKSelection
317  */
319 
320  /**
321  * @brief Deletes a UserWaypoint from this manager's DesignerTrajectory.
322  * @param index index of the UserWaypoint to delete
323  */
324  void deleteWaypoint(unsigned int index);
325 
326  /**
327  * @brief Sets the InterpolationType of a Transition from this manager's DesignerTrajectory.
328  * @param index index of the Transition to edit
329  * @param it new InterpolationType
330  */
331  void setTransitionInterpolation(unsigned int index, InterpolationType it);
332 
333  /**
334  * @brief Sets a UserWaypoint as (not-) breakpoint.
335  * @param index index of the UserWaypoint to edit
336  * @param b true when the selected UserWaypoint should be a breakpoint
337  * false when the selected UserWaypoint should not be a breakpoint
338  */
339  void setWaypointAsBreakpoint(unsigned int index, bool b);
340 
341  /**
342  * @brief Sets the desired duration of a Transition
343  * @param index index of the Transition to edit
344  * @param duration desired duration
345  */
346  void setTransitionUserDuration(unsigned int index, double duration);
347 
348  // returns deep copy of DesignerTrajectory
349  /**
350  * @brief Returns a deep copy of this manager's DesignerTrajectory.
351  * @return a deep copy of this manager's DesignerTrajectory
352  */
354 
355  /**
356  * @brief Overrides the current DesignerTrajectory with the given DesignerTrajectory.
357  * @param designerTrajectory new DesignerTrajectory
358  */
359  bool import(DesignerTrajectoryPtr newDesignerTrajectory);
360 
361  /**
362  * @brief undo
363  * @return
364  */
365  bool undo();
366 
367  /**
368  * @brief redo
369  * @return
370  */
371  bool redo();
372 
373  /**
374  * @brief True if the DesignerTrajectory is initialized
375  * @return isInitialized member
376  */
377  bool getIsInitialized();
378 
379  /**
380  * @brief setCollisionModels Sets the collision models on the collision detection attribute
381  * @param activeCollisionModel The collision model of the RobotNodeSet which gets
382  * manipulated
383  * @param bodyCollisionModels The collision models to check against
384  */
385  /*
386  void setCollisionModels(VirtualRobot::RobotNodeSetPtr activeCollisionModel,
387  std::vector<VirtualRobot::RobotNodeSetPtr> bodyCollisionModels);
388  */
389 
390  bool undoPossible();
391 
392  bool redoPossible();
393 
394  };
395 
396  ////////////////////////////////////////////////////////////////////////////////////////////////
397  /// Typedefinitions
398  ////////////////////////////////////////////////////////////////////////////////////////////////
399  using DesignerTrajectoryManagerPtr = std::shared_ptr<armarx::DesignerTrajectoryManager>;
400 
401  // old typedef (can be deleted, if it is not used anymore)
402  using DesignerTrajectoryManagerStdPtr = std::shared_ptr<armarx::DesignerTrajectoryManager>;
403 }
404 
405 #endif // DESIGNERTRAJECTORYMANAGER_H
armarx::UserWaypointPtr
std::shared_ptr< UserWaypoint > UserWaypointPtr
Definition: UserWaypoint.h:137
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::DesignerTrajectoryManager::editWaypointIKSelection
void editWaypointIKSelection(unsigned int index, VirtualRobot::IKSolver::CartesianSelection ikSelection)
Edits the IKSelection of a UserWaypoint.
Definition: DesignerTrajectoryManager.cpp:944
armarx::DesignerTrajectoryManagerStdPtr
std::shared_ptr< armarx::DesignerTrajectoryManager > DesignerTrajectoryManagerStdPtr
Definition: DesignerTrajectoryManager.h:402
armarx::DesignerTrajectoryManager::undo
bool undo()
undo
Definition: DesignerTrajectoryManager.cpp:514
armarx::DesignerTrajectoryManager::undoPossible
bool undoPossible()
setCollisionModels Sets the collision models on the collision detection attribute
Definition: DesignerTrajectoryManager.cpp:1133
armarx::EnvironmentPtr
std::shared_ptr< Environment > EnvironmentPtr
Definition: Environment.h:29
armarx::DesignerTrajectoryManager
The DesignerTrajectoryManager class enables easy interaction with the model.
Definition: DesignerTrajectoryManager.h:54
armarx::DesignerTrajectoryManager::redo
bool redo()
redo
Definition: DesignerTrajectoryManager.cpp:537
armarx::DesignerTrajectoryManager::setWaypointAsBreakpoint
void setWaypointAsBreakpoint(unsigned int index, bool b)
Sets a UserWaypoint as (not-) breakpoint.
Definition: DesignerTrajectoryManager.cpp:1024
armarx::InterpolationType
InterpolationType
The InterpolationType enum lists all available interpolation types eLinearInterpolation: represents l...
Definition: InterpolationType.h:32
armarx::NJointTaskSpaceDMPControllerMode::CartesianSelection
CartesianSelection
Definition: ControllerInterface.ice:34
armarx::DesignerTrajectoryManager::editWaypointPoseBase
void editWaypointPoseBase(unsigned int index, const PoseBasePtr pose)
Edits the pose of a UserWaypoint.
Definition: DesignerTrajectoryManager.cpp:891
armarx::DesignerTrajectoryManager::redoPossible
bool redoPossible()
Definition: DesignerTrajectoryManager.cpp:1138
armarx::DesignerTrajectoryManager::getIsInitialized
bool getIsInitialized()
True if the DesignerTrajectory is initialized.
Definition: DesignerTrajectoryManager.cpp:560
armarx::TransitionPtr
std::shared_ptr< Transition > TransitionPtr
Definition: Transition.h:141
armarx::DesignerTrajectoryManagerPtr
std::shared_ptr< armarx::DesignerTrajectoryManager > DesignerTrajectoryManagerPtr
Typedefinitions.
Definition: DesignerTrajectoryManager.h:399
armarx::channels::KinematicUnitObserver::jointAngles
const KinematicUnitDatafieldCreator jointAngles("jointAngles")
armarx::DesignerTrajectoryManager::getDesignerTrajectory
DesignerTrajectoryPtr getDesignerTrajectory() const
Returns a deep copy of this manager's DesignerTrajectory.
Definition: DesignerTrajectoryManager.cpp:1063
armarx::DesignerTrajectoryPtr
std::shared_ptr< DesignerTrajectory > DesignerTrajectoryPtr
Definition: DesignerTrajectory.h:165
armarx::KinematicSolverPtr
std::shared_ptr< KinematicSolver > KinematicSolverPtr
Definition: KinematicSolver.h:235
armarx::DesignerTrajectoryManager::initializeDesignerTrajectory
void initializeDesignerTrajectory(std::vector< double > &jointAngles)
Public functions.
Definition: DesignerTrajectoryManager.cpp:812
armarx::DesignerTrajectoryManager::setTransitionUserDuration
void setTransitionUserDuration(unsigned int index, double duration)
Sets the desired duration of a Transition.
Definition: DesignerTrajectoryManager.cpp:1040
armarx::DesignerTrajectoryManager::addWaypoint
void addWaypoint(const PoseBasePtr pose)
Adds a UserWaypoint to the end of this manager's DesignerTrajectory.
Definition: DesignerTrajectoryManager.cpp:840
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::DesignerTrajectoryManager::deleteWaypoint
void deleteWaypoint(unsigned int index)
Deletes a UserWaypoint from this manager's DesignerTrajectory.
Definition: DesignerTrajectoryManager.cpp:960
armarx::DesignerTrajectoryManager::insertWaypoint
void insertWaypoint(unsigned int index, const PoseBasePtr pose)
Inserts a UserWaypoint at the given position to this manager's DesignerTrajectory.
Definition: DesignerTrajectoryManager.cpp:857
armarx::DesignerTrajectoryManager::DesignerTrajectoryManager
DesignerTrajectoryManager(std::string rnsName, EnvironmentPtr environment_)
Constructor(s)
Definition: DesignerTrajectoryManager.cpp:798
armarx::DesignerTrajectoryManager::setTransitionInterpolation
void setTransitionInterpolation(unsigned int index, InterpolationType it)
Sets the InterpolationType of a Transition from this manager's DesignerTrajectory.
Definition: DesignerTrajectoryManager.cpp:1008
PropagateConst.h