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 
34 
35 #include <RobotAPI/interface/core/PoseBase.h>
36 
37 #include <RobotComponents/interface/components/RobotIK.h>
38 
39 #include "../Environment.h"
40 #include "../Interpolation/InterpolationSegmentFactory.h"
41 #include "../Interpolation/InterpolationType.h"
42 #include "../KinematicSolver.h"
43 #include "../Model/DesignerTrajectory.h"
44 #include "../TrajectoryCalculation/DesignerTrajectoryCalculator.h"
45 #include <MotionPlanning/CSpace/CSpaceSampled.h>
46 #include <MotionPlanning/Planner/BiRrt.h>
47 
48 namespace armarx
49 {
50  /**
51  * @brief The DesignerTrajectoryManager class enables easy interaction with the model.
52  *
53  * Using this class, the model is always self-consistent.
54  *
55  * @see DesignerTrajectory
56  */
58  {
59  private:
60  ////////////////////////////////////////////////////////////////////////////////////////////
61  /// Structs
62  ////////////////////////////////////////////////////////////////////////////////////////////
63  ///
64  struct ManipulationInterval
65  {
66  private:
67  std::list<UserWaypointPtr> userWaypointsList;
68  std::list<TransitionPtr> transitionsList;
69 
70  std::set<int> breakpointIndicesSet;
71 
72  // made public
73  //std::vector<UserWaypointPtr> userWaypoints;
74  //std::vector<TransitionPtr> transitions;
75 
76  // made public
77  //std::vector<PoseBasePtr> interpolatedPoses; //including poses of UserWaypoints
78 
79  // this vector holds the new index (in interpolatedPoses) of the UserWaypoints
80  //std::vector<int> newIndexOfUserWaypoint; // e.g.: uwp(0) -> new index 9
81 
82  public:
83  unsigned int upperIntervalLimit;
84  unsigned int lowerIntervalLimit;
85 
86  std::vector<UserWaypointPtr> userWaypoints;
87  std::vector<TransitionPtr> transitions;
88 
89  /* absolute (absolute to all designer trajectory user waypoints)
90  * breakpoint indices of breakpoints in the manipulation interval
91  * BEFORE inserting interpolated poses...
92  * points with index upperIntervalLimit and lowerIntervalLimit included !
93  */
94  std::vector<unsigned int> breakpointIndices;
95 
96  //std::vector<PoseBasePtr> interpolatedPoses; //including poses of UserWaypoints
97  std::vector<std::vector<PoseBasePtr>> interpolatedTransitions;
98 
99  // this vector holds the new index (in interpolatedPoses) of the UserWaypoints
100  std::vector<unsigned int> newIndexOfUserWaypoint; // e.g.: uwp(0) -> new index 9
101 
102  void addFirstUserWaypoint(UserWaypointPtr uw);
103  void pushFront(UserWaypointPtr uw, TransitionPtr t);
104  void pushBack(UserWaypointPtr uw, TransitionPtr t);
105  void addBreakpointIndex(int index);
106  void finishSearch();
107 
108  //int getFirstBreakpointIndex() const;
109  //int getLastBreakpointIndex() const;
110  UserWaypointPtr getUserWaypointByRealIndex(unsigned int i) const;
111  UserWaypointPtr getUserWaypointByZeroBasedIndex(unsigned int i) const;
112  TransitionPtr getTransitionByRealIndex(unsigned int i) const;
113  TransitionPtr getTransitionByZeroBasedIndex(unsigned int i) const;
114 
115  std::vector<std::vector<double>>
116  getUserWaypointsIKSolutions(std::vector<std::vector<double>>& ikSolutions,
117  unsigned int intervalStart,
118  unsigned int intervalEnd);
119  void applyJointAnglesOfUserWaypoints(std::vector<std::vector<double>> ikSolution);
120  };
121 
122  struct InsertTransition
123  {
124  public:
125  unsigned int transitionIndex;
126 
127  // insert transition with transitionIndex between
128  // insertionIndex and insertionIndex + 1
129  unsigned int insertionIndex;
130  };
131 
132  struct Mementos
133  {
134  std::list<DesignerTrajectoryPtr> mementoList;
135  unsigned int currentMemento = 0;
136  };
137 
138  ////////////////////////////////////////////////////////////////////////////////////////////
139  /// Attributes
140  ////////////////////////////////////////////////////////////////////////////////////////////
141  VirtualRobot::RobotNodeSetPtr rns;
142  DesignerTrajectoryPtr designerTrajectory;
143  KinematicSolverPtr kinSolver;
144  bool isInitialized = false;
145  Mementos mementos;
146  static const unsigned int MEMENTO_MAX = 20;
147  static const unsigned int DEFAULT_FRAME_COUNT = 100;
148  static constexpr double MAX_DEVIATION = 0.0001;
149  EnvironmentPtr environment;
150  //CollisionDetectionPtr collisionDetection;
151  ////////////////////////////////////////////////////////////////////////////////////////////
152 
153  /**
154  * @brief saveState
155  */
156  void saveState();
157 
158  /*struct ManipulationInterval
159  {
160  int upperIndex;
161  int lowerIndex;
162  std::vector<UserWaypointPtr> userWaypoints;
163  std::vector<TransitionPtr> transitions;
164 
165  void push(UserWaypointPtr uw, TransitionPtr t);
166 
167  static ManipulationInterval mergeManipulationIntervalHalfs(
168  ManipulationInterval lowerHalf,
169  ManipulationInterval upperHalf);
170  };
171 
172  struct InterpolatedPoints
173  {
174  std::vector<PoseBasePtr> poseBases;
175  std::vector<int> userWaypoint; // e.g.: uwp(0) -> new index 9
176  };*/
177 
178  /**
179  * @brief Calculates one half of the manipulation interval
180  * @param i index where the manipulaion takes place
181  * @param f function specifying the search direction
182  * @param fInv function specifying the inverse search direction
183  * @return interval half which is affected by the manipulation as ManipulationInterval
184  *
185  * @see ManipulationInterval
186  */
187  //ManipulationInterval searchFrom(int i,
188  // std::function<int (int)> f,
189  // std::function<int (int)> fInv);
190 
191  /**
192  * @brief Calculates the whole manipulation interval
193  * @param manipulationIndex index where the manipulation takes place
194  * @return interval which is affected by the manipulation
195  */
196  ManipulationInterval calculateManipulationInterval(unsigned int manipulationIndex);
197 
198  /**
199  * @brief getInterpolationObjects calculates the interpolation object for each transition
200  * in the given manipulation interval
201  * @param mi manipulation interval
202  * @return interpolation objects for each transition
203  *
204  * @see InterpolationSegmentFactory
205  */
206  std::vector<AbstractInterpolationPtr> getInterpolationObjects(ManipulationInterval& mi);
207 
208  /**
209  * @brief Calculates the interpolated points between two userwaypoints according to the
210  * userDuration of the transition between them and the desired frames per second.
211  * @param interpolationObjects interpolation objects
212  * @param fps frames per second
213  * @param ManipulationInterval object to insert the interpolated points into
214  */
215  void calculateInterpolatedPoints(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>
262  calculateTimeOptimalTrajectories(std::vector<std::vector<double>> ikSolutions,
263  ManipulationInterval& mi);
264 
265  void theUniversalMethod(unsigned int index);
266 
267  std::vector<double> getNewIkSolutionOfFirstPoint(PoseBasePtr oldStart,
268  PoseBasePtr newStart,
269  std::vector<double> jointAnglesOldStart);
270 
271  public:
272  ////////////////////////////////////////////////////////////////////////////////////////////
273  /// Constructor(s)
274  ////////////////////////////////////////////////////////////////////////////////////////////
275  /**
276  * @brief Constructor of this class.
277  * @param rns RobotNodeSet
278  */
279  DesignerTrajectoryManager(std::string rnsName, EnvironmentPtr environment_);
280 
281  ////////////////////////////////////////////////////////////////////////////////////////////
282  /// Public functions
283  ////////////////////////////////////////////////////////////////////////////////////////////
284  /**
285  * @brief Initializes the DesignerTrajectory object of this manager instance.
286  * @param jointAngles start jointangle-configuration of the trajectory
287  */
288  void initializeDesignerTrajectory(std::vector<double>& jointAngles);
289 
290  /**
291  * @brief Adds a UserWaypoint to the end of this manager's DesignerTrajectory.
292  *
293  * To create this UserWaypoint the following parameters are needed:
294  *
295  * @param pose pose of the UserWaypoint
296  */
297  void addWaypoint(const PoseBasePtr pose);
298 
299  /**
300  * @brief Inserts a UserWaypoint at the given position to this manager's DesignerTrajectory.
301  *
302  * To create this UserWaypoint the following parameters are needed:
303  *
304  * @param index position to insert the UserWaypoint into
305  * @param pb pose of the UserWaypoint
306  */
307  void insertWaypoint(unsigned int index, const PoseBasePtr pose);
308 
309  /**
310  * @brief Edits the pose of a UserWaypoint.
311  * @param index index of the UserWaypoint to edit
312  * @param pose new pose
313  */
314  void editWaypointPoseBase(unsigned int index, const PoseBasePtr pose);
315 
316  /**
317  * @brief Edits the IKSelection of a UserWaypoint.
318  * @param index index of the UserWaypoint to edit
319  * @param ikSelection new IKSelection
320  */
321  void editWaypointIKSelection(unsigned int index,
323 
324  /**
325  * @brief Deletes a UserWaypoint from this manager's DesignerTrajectory.
326  * @param index index of the UserWaypoint to delete
327  */
328  void deleteWaypoint(unsigned int index);
329 
330  /**
331  * @brief Sets the InterpolationType of a Transition from this manager's DesignerTrajectory.
332  * @param index index of the Transition to edit
333  * @param it new InterpolationType
334  */
335  void setTransitionInterpolation(unsigned int index, InterpolationType it);
336 
337  /**
338  * @brief Sets a UserWaypoint as (not-) breakpoint.
339  * @param index index of the UserWaypoint to edit
340  * @param b true when the selected UserWaypoint should be a breakpoint
341  * false when the selected UserWaypoint should not be a breakpoint
342  */
343  void setWaypointAsBreakpoint(unsigned int index, bool b);
344 
345  /**
346  * @brief Sets the desired duration of a Transition
347  * @param index index of the Transition to edit
348  * @param duration desired duration
349  */
350  void setTransitionUserDuration(unsigned int index, double duration);
351 
352  // returns deep copy of DesignerTrajectory
353  /**
354  * @brief Returns a deep copy of this manager's DesignerTrajectory.
355  * @return a deep copy of this manager's DesignerTrajectory
356  */
358 
359  /**
360  * @brief Overrides the current DesignerTrajectory with the given DesignerTrajectory.
361  * @param designerTrajectory new DesignerTrajectory
362  */
363  bool import(DesignerTrajectoryPtr newDesignerTrajectory);
364 
365  /**
366  * @brief undo
367  * @return
368  */
369  bool undo();
370 
371  /**
372  * @brief redo
373  * @return
374  */
375  bool redo();
376 
377  /**
378  * @brief True if the DesignerTrajectory is initialized
379  * @return isInitialized member
380  */
381  bool getIsInitialized();
382 
383  /**
384  * @brief setCollisionModels Sets the collision models on the collision detection attribute
385  * @param activeCollisionModel The collision model of the RobotNodeSet which gets
386  * manipulated
387  * @param bodyCollisionModels The collision models to check against
388  */
389  /*
390  void setCollisionModels(VirtualRobot::RobotNodeSetPtr activeCollisionModel,
391  std::vector<VirtualRobot::RobotNodeSetPtr> bodyCollisionModels);
392  */
393 
394  bool undoPossible();
395 
396  bool redoPossible();
397  };
398 
399  ////////////////////////////////////////////////////////////////////////////////////////////////
400  /// Typedefinitions
401  ////////////////////////////////////////////////////////////////////////////////////////////////
402  using DesignerTrajectoryManagerPtr = std::shared_ptr<armarx::DesignerTrajectoryManager>;
403 
404  // old typedef (can be deleted, if it is not used anymore)
405  using DesignerTrajectoryManagerStdPtr = std::shared_ptr<armarx::DesignerTrajectoryManager>;
406 } // namespace armarx
407 
408 #endif // DESIGNERTRAJECTORYMANAGER_H
armarx::UserWaypointPtr
std::shared_ptr< UserWaypoint > UserWaypointPtr
Definition: UserWaypoint.h:142
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:945
armarx::DesignerTrajectoryManagerStdPtr
std::shared_ptr< armarx::DesignerTrajectoryManager > DesignerTrajectoryManagerStdPtr
Definition: DesignerTrajectoryManager.h:405
armarx::DesignerTrajectoryManager::undo
bool undo()
undo
Definition: DesignerTrajectoryManager.cpp:503
armarx::DesignerTrajectoryManager::undoPossible
bool undoPossible()
setCollisionModels Sets the collision models on the collision detection attribute
Definition: DesignerTrajectoryManager.cpp:1144
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:57
armarx::DesignerTrajectoryManager::redo
bool redo()
redo
Definition: DesignerTrajectoryManager.cpp:527
armarx::DesignerTrajectoryManager::setWaypointAsBreakpoint
void setWaypointAsBreakpoint(unsigned int index, bool b)
Sets a UserWaypoint as (not-) breakpoint.
Definition: DesignerTrajectoryManager.cpp:1030
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:890
armarx::DesignerTrajectoryManager::redoPossible
bool redoPossible()
Definition: DesignerTrajectoryManager.cpp:1150
armarx::DesignerTrajectoryManager::getIsInitialized
bool getIsInitialized()
True if the DesignerTrajectory is initialized.
Definition: DesignerTrajectoryManager.cpp:551
armarx::TransitionPtr
std::shared_ptr< Transition > TransitionPtr
Definition: Transition.h:143
armarx::DesignerTrajectoryManagerPtr
std::shared_ptr< armarx::DesignerTrajectoryManager > DesignerTrajectoryManagerPtr
Typedefinitions.
Definition: DesignerTrajectoryManager.h:402
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:1071
armarx::DesignerTrajectoryPtr
std::shared_ptr< DesignerTrajectory > DesignerTrajectoryPtr
Definition: DesignerTrajectory.h:163
armarx::KinematicSolverPtr
std::shared_ptr< KinematicSolver > KinematicSolverPtr
Definition: KinematicSolver.h:287
armarx::DesignerTrajectoryManager::initializeDesignerTrajectory
void initializeDesignerTrajectory(std::vector< double > &jointAngles)
Public functions.
Definition: DesignerTrajectoryManager.cpp:808
armarx::DesignerTrajectoryManager::setTransitionUserDuration
void setTransitionUserDuration(unsigned int index, double duration)
Sets the desired duration of a Transition.
Definition: DesignerTrajectoryManager.cpp:1047
armarx::DesignerTrajectoryManager::addWaypoint
void addWaypoint(const PoseBasePtr pose)
Adds a UserWaypoint to the end of this manager's DesignerTrajectory.
Definition: DesignerTrajectoryManager.cpp:836
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::DesignerTrajectoryManager::deleteWaypoint
void deleteWaypoint(unsigned int index)
Deletes a UserWaypoint from this manager's DesignerTrajectory.
Definition: DesignerTrajectoryManager.cpp:964
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:854
armarx::DesignerTrajectoryManager::DesignerTrajectoryManager
DesignerTrajectoryManager(std::string rnsName, EnvironmentPtr environment_)
Constructor(s)
Definition: DesignerTrajectoryManager.cpp:795
armarx::DesignerTrajectoryManager::setTransitionInterpolation
void setTransitionInterpolation(unsigned int index, InterpolationType it)
Sets the InterpolationType of a Transition from this manager's DesignerTrajectory.
Definition: DesignerTrajectoryManager.cpp:1013
PropagateConst.h