Robowflex  v0.1
Making MoveIt Easy
tsr.h
Go to the documentation of this file.
1 /* Author: Zachary Kingston */
2 
3 #ifndef ROBOWFLEX_DART_TSR_
4 #define ROBOWFLEX_DART_TSR_
5 
6 #include <set>
7 #include <functional>
8 #include <iostream>
9 
10 #include <ompl/base/Constraint.h>
11 
12 #include <dart/dynamics/SimpleFrame.hpp>
13 #include <dart/dynamics/InverseKinematics.hpp>
14 
17 
18 namespace robowflex
19 {
20  namespace darts
21  {
22  /** \cond IGNORE */
24  ROBOWFLEX_CLASS_FORWARD(Structure)
25  ROBOWFLEX_CLASS_FORWARD(StateSpace)
26  /** \endcond */
27 
28  namespace magic
29  {
30  static const double DEFAULT_IK_TOLERANCE = 1e-3;
31  static const std::string ROOT_FRAME = "";
32  static const Eigen::Vector3d DEFAULT_IK_TOLERANCES =
33  Eigen::Vector3d::Constant(DEFAULT_IK_TOLERANCE);
34  } // namespace magic
35 
36  /** \cond IGNORE */
38  /** \endcond */
39 
40  /** \class robowflex::darts::TSRPtr
41  \brief A shared pointer wrapper for robowflex::darts::TSR. */
42 
43  /** \class robowflex::darts::TSRConstPtr
44  \brief A const shared pointer wrapper for robowflex::darts::TSR. */
45 
46  /** \brief A Task Space Region (TSR). TSRs are workspace regions that impose a constraints on a robot.
47  * They are composed of a pose (in some reference frame) and tolerances on position and rotation.
48  * initialize() must be called once all parameters are set.
49  *
50  * In the TSR, there are controlled DoF (through useIndices()) and world DoF (through
51  * useWorldIndices()). The controlled DoF are the DoF inside the structure the TSR's target frame is.
52  * These are the DoF that are used as part of the error, Jacobian, and other computations. However,
53  * the world might be using more DoF, especially if it is a multi-robot system. The world's DoF are
54  * given through useWorldIndices(). The *WorldState allow you to pass a configuration for the entire
55  * world (where each entry in that configuration corresponds to the indices given) and have the input
56  * / output be mapped appropriately to the controlled DoF.
57  *
58  * Note, the TSR creates an IK node in the underlying Dart skeleton for the provided world. If you
59  * have multiple TSRs for the same target frame these will overwrite each other, unless you use a
60  * clone of the World.
61  */
62  class TSR
63  {
65 
66  public:
67  /** \brief The specification of a TSR.
68  */
70  {
71  public:
73 
74  struct
75  {
76  std::string structure; ///< Structure target frame is in.
77  std::string frame; ///< Name of target frame.
78  } target; ///< Target frame.
79 
80  struct
81  {
82  std::string structure; ///< Structure base frame is in.
83  std::string frame{magic::ROOT_FRAME}; ///< Name of base frame.
84  } base; ///< Base frame.
85 
86  RobotPose pose{RobotPose::Identity()}; ///< Pose of TSR.
87 
88  struct
89  {
91  Eigen::Vector3d upper{magic::DEFAULT_IK_TOLERANCES}; ///< Upper position tolerance.
92  Eigen::Vector3d lower{-magic::DEFAULT_IK_TOLERANCES}; ///< Lower position tolerance.
93  } position; ///< Position tolerances.
94 
95  struct
96  {
98  Eigen::Vector3d upper{magic::DEFAULT_IK_TOLERANCES}; ///< Upper orientation tolerance.
99  Eigen::Vector3d lower{-magic::DEFAULT_IK_TOLERANCES}; ///< Lower orientation tolerance.
100  } orientation; ///< Orientation tolerances.
101 
102  std::size_t dimension{6}; ///< Number of constrained dimensions.
103 
104  /** \brief Vector of active constraints. If an index is true, that constraint is active.
105  * Ordered by X-, Y-, Z- orientation constraints, then X-, Y-, Z- position.
106  */
108 
109  std::size_t maxIter{50}; ///< Maximum iterations for solver.
110  double tolerance{magic::DEFAULT_IK_TOLERANCE}; ///< Tolerance on solution for solver.
111 
112  /** \name Constructors
113  \{ */
114 
115  /** \brief Default constructor.
116  */
117  Specification() = default;
118 
119  /** \brief Constructor for basic pose TSR constrained in world frame.
120  * \param[in] structure Structure TSR is on (both target and base frames).
121  * \param[in] target_frame Target frame.
122  * \param[in] position Desired position.
123  * \param[in] rotation Desired orientation.
124  */
125  Specification(const std::string &structure, const std::string &target_frame,
126  const Eigen::Ref<const Eigen::Vector3d> &position,
127  const Eigen::Quaterniond &rotation);
128 
129  /** \} */
130 
131  /** \brief Setting TSR Frame
132  \{ */
133 
134  /** \brief Set the target frame. Sets base frame structure as well if not already set.
135  * \param[in] structure Structure frame is in.
136  * \param[in] frame Frame name.
137  */
138  void setTarget(const std::string &structure, const std::string &frame);
139 
140  /** \brief Set the base frame.
141  * \param[in] structure Structure frame is in.
142  * \param[in] frame Frame name.
143  */
144  void setBase(const std::string &structure, const std::string &frame);
145 
146  /** \brief Set the base and target frame.
147  * \param[in] structure Structure frames are in.
148  * \param[in] target Target frame name.
149  * \param[in] base Base frame name.
150  */
151  void setFrame(const std::string &structure, const std::string &target,
152  const std::string &base = magic::ROOT_FRAME);
153 
154  /** \brief Add a suffix to the structures for the target and base frame.
155  * \param[in] suffix Suffix to add to structures in target and base frame.
156  */
157  void addSuffix(const std::string &suffix);
158 
159  /** \} */
160 
161  /** \brief Setting TSR Pose
162  \{ */
163 
164  /** \brief Set the position of the TSR.
165  * \param[in] position Position vector.
166  */
167  void setPosition(const Eigen::Ref<const Eigen::Vector3d> &position);
168 
169  /** \brief Set the position of the TSR.
170  * \param[in] x X-coordinate.
171  * \param[in] y Y-coordinate.
172  * \param[in] z Z-coordinate.
173  */
174  void setPosition(double x, double y, double z);
175 
176  /** \brief Set the rotation of the TSR.
177  * \param[in] orientation Desired quaternion.
178  */
179  void setRotation(const Eigen::Quaterniond &orientation);
180 
181  /** \brief Set the rotation of the TSR as a quaternion.
182  * \param[in] w W-component of quaternion.
183  * \param[in] x X-component of quaternion.
184  * \param[in] y Y-component of quaternion.
185  * \param[in] z Z-component of quaternion.
186  */
187  void setRotation(double w, double x, double y, double z);
188 
189  /** \brief Set the rotation of the TSR as XYZ Euler angles.
190  * \param[in] x X-component of rotation.
191  * \param[in] y Y-component of rotation.
192  * \param[in] z Z-component of rotation.
193  */
194  void setRotation(double x, double y, double z);
195 
196  /** \brief Set the pose of the TSR.
197  * \param[in] other Pose to use.
198  */
199  void setPose(const RobotPose &other);
200 
201  /** \brief Set the pose of the TSR.
202  * \param[in] position Desired position.
203  * \param[in] rotation Desired rotation.
204  */
205  void setPose(const Eigen::Ref<const Eigen::Vector3d> &position,
206  const Eigen::Quaterniond &rotation);
207 
208  /** \brief Set the pose of the TSR.
209  * \param[in] xp X-coordinate of position.
210  * \param[in] yp Y-coordinate of position.
211  * \param[in] zp Z-coordinate of position.
212  * \param[in] wr W-component of quaternion.
213  * \param[in] xr X-component of quaternion.
214  * \param[in] yr Y-component of quaternion.
215  * \param[in] zr Z-component of quaternion.
216  */
217  void setPose(double xp, double yp, double zp, double wr, double xr, double yr, double zr);
218 
219  /** \brief Set the pose of the TSR for the desired frame in a provided world. Uses world's
220  * current configuration. \param[in] world World to copy transform from.
221  */
222  void setPoseFromWorld(const WorldPtr &world);
223 
224  /** \} */
225 
226  /** \brief Setting Position Tolerances
227  \{ */
228 
229  /** \brief Set the X position tolerance to (-bound, bound).
230  * \param[in] bound Bound to set.
231  */
232  void setXPosTolerance(double bound);
233 
234  /** \brief Set the Y position tolerance to (-bound, bound).
235  * \param[in] bound Bound to set.
236  */
237  void setYPosTolerance(double bound);
238 
239  /** \brief Set the Z position tolerance to (-bound, bound).
240  * \param[in] bound Bound to set.
241  */
242  void setZPosTolerance(double bound);
243 
244  /** \brief Set the X position tolerance to (lower, upper).
245  * \param[in] lower Lower bound.
246  * \param[in] upper Upper bound.
247  */
248  void setXPosTolerance(double lower, double upper);
249 
250  /** \brief Set the Y position tolerance to (lower, upper).
251  * \param[in] lower Lower bound.
252  * \param[in] upper Upper bound.
253  */
254  void setYPosTolerance(double lower, double upper);
255 
256  /** \brief Set the Z position tolerance to (lower, upper).
257  * \param[in] lower Lower bound.
258  * \param[in] upper Upper bound.
259  */
260  void setZPosTolerance(double lower, double upper);
261 
262  /** \brief Set no position tolerance on the X-axis.
263  */
264  void setNoXPosTolerance();
265 
266  /** \brief Set no position tolerance on the Y-axis.
267  */
268  void setNoYPosTolerance();
269 
270  /** \brief Set no position tolerance on the Z-axis.
271  */
272  void setNoZPosTolerance();
273 
274  /** \brief Set no position tolerance at all.
275  */
276  void setNoPosTolerance();
277 
278  /** \} */
279 
280  /** \brief Setting Orientation Tolerances
281  \{ */
282 
283  /** \brief Set the X orientation tolerance to (-bound, bound).
284  * \param[in] bound Bound to set.
285  */
286  void setXRotTolerance(double bound);
287 
288  /** \brief Set the Y orientation tolerance to (-bound, bound).
289  * \param[in] bound Bound to set.
290  */
291  void setYRotTolerance(double bound);
292 
293  /** \brief Set the Z orientation tolerance to (-bound, bound).
294  * \param[in] bound Bound to set.
295  */
296  void setZRotTolerance(double bound);
297 
298  /** \brief Set the X orientation tolerance to (lower, upper).
299  * \param[in] lower Lower bound.
300  * \param[in] upper Upper bound.
301  */
302  void setXRotTolerance(double lower, double upper);
303 
304  /** \brief Set the Y orientation tolerance to (lower, upper).
305  * \param[in] lower Lower bound.
306  * \param[in] upper Upper bound.
307  */
308  void setYRotTolerance(double lower, double upper);
309 
310  /** \brief Set the Z orientation tolerance to (lower, upper).
311  * \param[in] lower Lower bound.
312  * \param[in] upper Upper bound.
313  */
314  void setZRotTolerance(double lower, double upper);
315 
316  /** \brief Set no orientation tolerance on the X-axis.
317  */
318  void setNoXRotTolerance();
319 
320  /** \brief Set no orientation tolerance on the Y-axis.
321  */
322  void setNoYRotTolerance();
323 
324  /** \brief Set no orientation tolerance on the Z-axis.
325  */
326  void setNoZRotTolerance();
327 
328  /** \brief Set no orientation tolerance at all.
329  */
330  void setNoRotTolerance();
331 
332  /** \} */
333 
334  /** \brief Getters and Informative Methods
335  \{ */
336 
337  /** \brief Get the current desired position.
338  * \return Position vector.
339  */
340  Eigen::Vector3d getPosition() const;
341 
342  /** \brief Get the current desired orientation.
343  * \return Orientation quaternion.
344  */
345  Eigen::Quaterniond getRotation() const;
346 
347  /** \brief Get the current desired orientation.
348  * \return Orientation as XYZ Euler angles.
349  */
350  Eigen::Vector3d getEulerRotation() const;
351 
352  /** \brief Returns true if TSR is position constrained.
353  * \return True if position constrained.
354  */
355  bool isPositionConstrained() const;
356 
357  /** \brief Returns true if TSR is orientation constrained.
358  * \return True if orientation constrained.
359  */
360  bool isRotationConstrained() const;
361 
362  /** \brief Returns true if TSR is a relative reference frame (not the world).
363  * \return True if this is a relative reference frame.
364  */
365  bool isRelative() const;
366 
367  /** \brief Print out this TSR information.
368  * \param[in] out Output stream.
369  */
370  void print(std::ostream &out) const;
371 
372  /** \} */
373 
374  /** \brief Operations
375  \{ */
376 
377  /** \brief Compute the intersection of this TSR with the \a other.
378  * \return True if successful, false on failure.
379  */
380  bool intersect(const Specification &other);
381 
382  /** \} */
383 
384  private:
385  /** \brief Fixes bounds so they are correct.
386  */
387  void fixBounds();
388 
389  /** \brief Compute and return constraint dimension of the TSR.
390  * \return Dimension of TSR.
391  */
392  std::size_t getDimension() const;
393 
394  /** \brief Checks if two values correspond to a position constraint.
395  * \return If (lower, upper) is bounded or not.
396  */
397  bool isPosConstrained(double lower, double upper) const;
398 
399  /** \brief Checks if two values correspond to a orientation constraint.
400  * \return If (lower, upper) is bounded or not.
401  */
402  bool isRotConstrained(double lower, double upper) const;
403  };
404 
405  /** \name Constructor and Initialization
406  \{ */
407 
408  /** \brief Constructor.
409  * \param[in] world World to apply TSR to.
410  * \param[in] spec Specification for TSR.
411  */
412  TSR(const WorldPtr &world, const Specification &spec);
413 
414  /** \brief Destructor. Disables IK on node if created.
415  */
416  ~TSR();
417 
418  /** \brief Set the world used by this TSR.
419  * \param[in] world New world to use.
420  */
421  void setWorld(const WorldPtr &world);
422 
423  /** \brief Clears the initialization of this TSR.
424  */
425  void clear();
426 
427  /** \brief Initialize this TSR. Creates IK node attached to body node in world.
428  */
429  void initialize();
430 
431  /** \} */
432 
433  /** \name DoF Indexing
434  \{ */
435 
436  /** \brief Use the joints in a robot's group. Robot used is the target frame's structure.
437  * \param[in] name Name of group to use.
438  */
439  void useGroup(const std::string &name);
440 
441  /** \brief Use DoF indices for TSR computation.
442  * \param[in] indices Indices to use.
443  */
444  void useIndices(const std::vector<std::size_t> &indices);
445 
446  /** \brief Gets the transformation from the specification's base to the TSR's frame.
447  * \return RobotPose representing the transform.
448  */
449  robowflex::RobotPose getTransformToFrame() const;
450 
451  /** \brief Use World DoF indices for TSR computation. World indices are pairs of skeleton index
452  * and DoF index.
453  * \param[in] indices Indices to use.
454  */
455  void useWorldIndices(const std::vector<std::pair<std::size_t, std::size_t>> &indices);
456 
457  /** \brief Output world indices. TSR information for *WorldState methods uses this set
458  * of world indices. World indices are pairs of skeleton index and DoF index.
459  * \param[in] indices Indices to use.
460  */
461  void setWorldIndices(const std::vector<std::pair<std::size_t, std::size_t>> &indices);
462 
463  /** \brief Compute the set of world indices used based on the underlying active indices.
464  * \return World indices corresponding to this TSR's current indices.
465  */
466  std::vector<std::pair<std::size_t, std::size_t>> computeWorldIndices() const;
467 
468  /** \brief Get the skeleton index for the target frame's structure.
469  * \return The index of the skeleton this TSR is targeting.
470  */
471  std::size_t getSkeletonIndex();
472 
473  /** \brief Get the set of indices used for TSR computation.
474  */
475  const std::vector<std::size_t> &getIndices() const;
476 
477  /** \brief Get the set of output world indices used.
478  */
479  const std::vector<std::pair<std::size_t, std::size_t>> &getWorldIndices() const;
480 
481  /** \brief From a configuration of controlled indices, map that configuration into a world
482  * configuration. That is, map \a state into \a world according to the set indices and world
483  * indices.
484  * \param[out] world Output world configuration.
485  * \param[in] state Input configuration.
486  */
487  void toBijection(Eigen::Ref<Eigen::VectorXd> world,
488  const Eigen::Ref<const Eigen::VectorXd> &state) const;
489 
490  /** \brief From a world configuration, map that configuration into a controlled DoF
491  * configuration. That is, map \a world into \a state according to the set indices and world
492  * indices.
493  * \param[out] state Output configuration.
494  * \param[in] world Input world configuration.
495  */
496  void fromBijection(Eigen::Ref<Eigen::VectorXd> state,
497  const Eigen::Ref<const Eigen::VectorXd> &world) const;
498 
499  /** \} */
500 
501  /** \name Getters
502  \{ */
503 
504  /** \brief Get the error dimension of this TSR.
505  * \return The error dimension of the TSR.
506  */
507  std::size_t getDimension() const;
508 
509  /** \brief Get the number of controlled DoF for this TSR.
510  * \return The number of DoF for this TSR.
511  */
512  std::size_t getNumDofs() const;
513 
514  /** \brief Get the number of world DoF for this TSR.
515  * \return The number of world DoF for this TSR.
516  */
517  std::size_t getNumWorldDofs() const;
518 
519  /** \} */
520 
521  /** \name Specification and Updates
522  \{ */
523 
524  /** \brief Get the specification for this TSR.
525  * \return The TSR's specification.
526  */
527  Specification &getSpecification();
528 
529  /** \brief If the pose of the specification is updated, call this to update underlying IK solver.
530  */
531  void updatePose();
532 
533  /** \brief If the bounds of the specification are updated, call this to update underlying IK
534  * solver.
535  */
536  void updateBounds();
537 
538  /** \brief If the solver parameters of the specification are updated, call this to update
539  * underlying IK solver.
540  */
541  void updateSolver();
542 
543  /** \} */
544 
545  /** \name Error / Function Computation
546  \{ */
547 
548  /** \brief Get the current error given the world state, with all values.
549  * \param[out] error Error value for all TSRs.
550  */
551  void getErrorWorldRaw(Eigen::Ref<Eigen::VectorXd> error) const;
552 
553  /** \brief Get the current error given the world state.
554  * \param[out] error Error value.
555  */
556  void getErrorWorld(Eigen::Ref<Eigen::VectorXd> error) const;
557 
558  /** \brief Get the error given a provided world configuration.
559  * \param[in] world World configuration (from world indices).
560  * \param[out] error Error value.
561  */
562  void getErrorWorldState(const Eigen::Ref<const Eigen::VectorXd> &world,
563  Eigen::Ref<Eigen::VectorXd> error) const;
564 
565  /** \brief Get the error given a configuration of the controlled DoF.
566  * \param[in] state Controlled DoF configuration.
567  * \param[out] error Error value.
568  */
569  void getError(const Eigen::Ref<const Eigen::VectorXd> &state,
570  Eigen::Ref<Eigen::VectorXd> error) const;
571 
572  /** \} */
573 
574  /** \name Jacobian Computation
575  \{ */
576 
577  /** \brief Get the current Jacobian given the world state.
578  * \param[out] jacobian Jacobian value.
579  */
580  void getJacobianWorld(Eigen::Ref<Eigen::MatrixXd> jacobian) const;
581 
582  /** \brief Get the Jacobian given a provided world configuration.
583  * \param[in] world World configuration (from world indices).
584  * \param[out] jacobian Jacobian value.
585  */
586  void getJacobianWorldState(const Eigen::Ref<const Eigen::VectorXd> &world,
587  Eigen::Ref<Eigen::MatrixXd> jacobian) const;
588 
589  /** \brief Get the Jacobian given a configuration of the controlled DoF.
590  * \param[in] state Controlled DoF configuration.
591  * \param[out] jacobian Jacobian value.
592  */
593  void getJacobian(const Eigen::Ref<const Eigen::VectorXd> &state,
594  Eigen::Ref<Eigen::MatrixXd> jacobian) const;
595 
596  /** \} */
597 
598  /** \name Distance Computation
599  \{ */
600 
601  /** \brief Get the current distance to satisfaction given the world state.
602  * \return Distance to TSR satisfaction.
603  */
604  double distanceWorld() const;
605 
606  /** \brief Get the distance to satisfaction given a provided world configuration.
607  * \param[in] world World configuration (from world indices).
608  * \return Distance to TSR satisfaction.
609  */
610  double distanceWorldState(const Eigen::Ref<const Eigen::VectorXd> &world) const;
611 
612  /** \brief Get the distance to satisfaction given a configuration of the controlled DoF.
613  * \param[in] state Controlled DoF configuration.
614  * \return Distance to TSR satisfaction.
615  */
616  double distance(const Eigen::Ref<const Eigen::VectorXd> &state) const;
617 
618  /** \} */
619 
620  /** \name Solving for Satisfying Configurations
621  \{ */
622 
623  /** \brief Solve for a satisfying configuration given the current state of the world.
624  * \return True on success, false on failure.
625  */
626  bool solveWorld();
627 
628  /** \brief Solve for a satisfying configuration given an initial world configuration.
629  * \param[in] world World configuration (from world indices).
630  * \return True on success, false on failure.
631  */
632  bool solveWorldState(Eigen::Ref<Eigen::VectorXd> world);
633 
634  /** \brief Solve for a satisfying configuration given an initial configuration of the controlled
635  * DoF.
636  * \param[in] state Controlled DoF configuration.
637  * \return True on success, false on failure.
638  */
639  bool solve(Eigen::Ref<Eigen::VectorXd> state);
640 
641  /** \brief Solve using gradient descent for a satisfying configuration given the current state of
642  * the world. \return True on success, false on failure.
643  */
644  bool solveGradientWorld();
645 
646  /** \brief Solve using gradient descent for a satisfying configuration given an initial world
647  * configuration.
648  * \param[in] world World configuration (from world indices).
649  * \return True on success, false on failure.
650  */
651  bool solveGradientWorldState(Eigen::Ref<Eigen::VectorXd> world);
652 
653  /** \brief Solve using gradient descent for a satisfying configuration given an initial
654  * configuration of the controlled DoF.
655  * \param[in] state Controlled DoF configuration.
656  * \return True on success, false on failure.
657  */
658  bool solveGradient(Eigen::Ref<Eigen::VectorXd> state);
659 
660  /** \} */
661 
662  /** \name Getting and Setting Position
663  \{ */
664 
665  /** \brief Set the positions of the world given a world configuration.
666  * \param[in] world World configuration.
667  */
668  void setPositionsWorldState(const Eigen::Ref<const Eigen::VectorXd> &world) const;
669 
670  /** \brief Set the positions of the world given a configuration.
671  * \param[in] state Controlled DoF configuration.
672  */
673  void setPositions(const Eigen::Ref<const Eigen::VectorXd> &state) const;
674 
675  /** \brief Get the positions of the world into a world configuration.
676  * \param[out] world World configuration.
677  */
678  void getPositionsWorldState(Eigen::Ref<Eigen::VectorXd> world) const;
679 
680  /** \brief Get the positions of the world given into a configuration.
681  * \param[out] state Controlled DoF configuration.
682  */
683  void getPositions(Eigen::Ref<Eigen::VectorXd> state) const;
684 
685  /** \} */
686 
687  private:
688  /** \brief Compute the mapping from world indices to controlled DoF indices.
689  */
690  void computeBijection();
691 
692  WorldPtr world_; ///< Underlying world.
693  Specification spec_; ///< TSR specification.
694 
695  std::size_t skel_index_; ///< Index of controlled skeleton.
696  std::vector<std::size_t> indices_; ///< Controlled indices.
698  std::vector<std::size_t> bijection_; ///< Mapping between controlled to world indices. The ith
699  ///< entry contains the mapping of state[i] =
700  ///< world[bijection_[i]]
701 
702  std::shared_ptr<dart::dynamics::SimpleFrame> frame_{nullptr}; ///< Target frame of TSR.
703  dart::dynamics::BodyNode *tnd_{nullptr}; ///< Target body node.
704  std::shared_ptr<dart::dynamics::InverseKinematics> ik_{nullptr}; ///< Inverse kinematics module.
705  dart::dynamics::InverseKinematics::TaskSpaceRegion *tsr_{nullptr}; ///< TSR.
706  };
707 
708  /** \cond IGNORE */
710  /** \endcond */
711 
712  /** \class robowflex::darts::TSRSetPtr
713  \brief A shared pointer wrapper for robowflex::darts::TSRSet. */
714 
715  /** \class robowflex::darts::TSRSetConstPtr
716  \brief A const shared pointer wrapper for robowflex::darts::TSRSet. */
717 
718  /** \brief Manager for a set of TSR constraints. Attempts to reduce redundancy and combines errors and
719  * Jacobians together.
720  *
721  * Will "stack" the error values and Jacobians together. For example, given two TSRs \a "a" and \a
722  * "b" in the set, the error vector will be [ea, eb] where \a ea and \a eb are \a a and \a b's error
723  * vector. The Jacobian will also correspond to this stacked error vector.
724  */
725  class TSRSet
726  {
727  public:
728  /** \name Constructor and Initialization
729  \{ */
730 
731  /** \brief Constructor.
732  * \param[in] world World to use.
733  * \param[in] tsr TSR to add to set.
734  */
735  TSRSet(const WorldPtr &world, const TSRPtr &tsr);
736 
737  /** \brief Constructor.
738  * \param[in] world World to use.
739  * \param[in] tsrs TSRs to add to set.
740  * \param[in] intersect If true, tries to simplify TSR set.
741  */
742  TSRSet(const WorldPtr &world, const std::vector<TSRPtr> &tsrs, bool intersect = true);
743 
744  /** \brief Set the world used by this TSR set.
745  * \param[in] world New world to use.
746  */
747  void setWorld(const WorldPtr &world);
748 
749  /** \brief Add a TSR to the set.
750  * \param[in] tsr TSR to add.
751  * \param[in] intersect If true, tries to simplify TSR set.
752  * \param[in] weight Weight to use for this TSR.
753  */
754  void addTSR(const TSRPtr &tsr, bool intersect = true, double weight = 1.0);
755 
756  /** \brief Initialize this set of TSRs
757  */
758  void initialize();
759 
760  /** \brief Update the solver information (iterations, tolerance, etc.)
761  */
762  void updateSolver();
763 
764  /** \} */
765 
766  /** \name DoF Indexing
767  \{ */
768 
769  /** \brief Use the joints in a robot's group. Robot used is the target frame's structure.
770  * \param[in] name Name of group to use.
771  */
772  void useGroup(const std::string &name);
773 
774  /** \brief Use DoF indices for all TSRs computation.
775  * \param[in] indices Indices to use.
776  */
777  void useIndices(const std::vector<std::size_t> &indices);
778 
779  /** \brief Use World DoF indices for all TSRs computation. World indices are pairs of skeleton
780  * index and DoF index. \param[in] indices Indices to use.
781  */
782  void useWorldIndices(const std::vector<std::pair<std::size_t, std::size_t>> &indices);
783 
784  /** \brief Output world indices. TSR information for *WorldState methods uses this set
785  * of world indices. World indices are pairs of skeleton index and DoF index.
786  * \param[in] indices Indices to use.
787  */
788  void setWorldIndices(const std::vector<std::pair<std::size_t, std::size_t>> &indices);
789 
790  /** \brief Get the world indices used.
791  * \return World indices.
792  */
793  const std::vector<std::pair<std::size_t, std::size_t>> &getWorldIndices() const;
794 
795  /** \brief Set the upper limit on joints given their index in the world configuration.
796  * \param[in] upper Upper bounds on joints.
797  */
798  void setWorldUpperLimits(const Eigen::Ref<const Eigen::VectorXd> &upper);
799 
800  /** \brief Set the lower limit on joints given their index in the world configuration.
801  * \param[in] lower Lower bounds on joints.
802  */
803  void setWorldLowerLimits(const Eigen::Ref<const Eigen::VectorXd> &lower);
804 
805  /** \brief Compute the upper and lower limits from the skeleton.
806  */
807  void computeLimits();
808 
809  /** \} */
810 
811  /** \name Getters
812  \{ */
813 
814  /** \brief Get the current world state.
815  * \param[out] world State to fill.
816  */
817  void getPositionsWorldState(Eigen::Ref<Eigen::VectorXd> world) const;
818 
819  /** \brief Get the error dimension of this set of TSRs.
820  * \return The error dimension of the set of TSRs.
821  */
822  std::size_t getDimension() const;
823 
824  /** \brief Get the number of TSRs in the set.
825  * \return The number of TSRs.
826  */
827  std::size_t numTSRs() const;
828 
829  /** \brief Get the TSRs that form the set.
830  * \return The set of TSRs.
831  */
832  const std::vector<TSRPtr> &getTSRs() const;
833 
834  /** \brief Get the numerical tolerance for solving.
835  * \return The tolerance.
836  */
837  double getTolerance() const;
838 
839  /** \brief Set the maximum tolerance used for solving.
840  * \param[in] tolerance New tolerance.
841  */
842  void setTolerance(double tolerance);
843 
844  /** \brief Get the maximum iterations allowed.
845  * \return The maximum solver iterations.
846  */
847  std::size_t getMaxIterations() const;
848 
849  /** \brief Set the maximum iterations used for solving.
850  * \param[in] iterations Max iterations to use.
851  */
852  void setMaxIterations(std::size_t iterations);
853 
854  /** \brief Add a suffix to the end of all structures in the TSRs in the set.
855  * \param[in] suffix Suffix to add.
856  */
857  void addSuffix(const std::string &suffix);
858 
859  /** \brief Set the step parameter in the solver.
860  * \param[in] step Step parameter.
861  */
862  void setStep(double step);
863 
864  /** \brief Get the solver step parameter.
865  * \return The solver step parameter.
866  */
867  double getStep() const;
868 
869  /** \brief Set the limit parameter in the solver.
870  * \param[in] limit Limit parameter.
871  */
872  void setLimit(double limit);
873 
874  /** \brief Get the solver limit parameter.
875  * \return The solver limit parameter.
876  */
877  double getLimit() const;
878 
879  /** \brief Set the damping parameter in the solver.
880  * \param[in] damping Damping parameter.
881  */
882  void setDamping(double damping);
883 
884  /** \brief Get the solver damping parameter.
885  * \return The solver damping parameter.
886  */
887  double getDamping() const;
888 
889  /** \brief Set if damping is used in SVD solving.
890  * \param[in] damping Damping value.
891  */
892  void useDamping(bool damping);
893 
894  /** \brief Use SVD for solving.
895  */
896  void useSVD();
897 
898  /** \brief Use QR for solving.
899  */
900  void useQR();
901 
902  /** \} */
903 
904  /** \name Error / Function Computation
905  \{ */
906 
907  /** \brief Get the current error given the world state.
908  * \param[out] error Error value for all TSRs.
909  */
910  void getErrorWorld(Eigen::Ref<Eigen::VectorXd> error) const;
911 
912  /** \brief Get the error given a provided world configuration.
913  * \param[in] world World configuration (from world indices).
914  * \param[out] error Error value for all TSRs.
915  */
916  void getErrorWorldState(const Eigen::Ref<const Eigen::VectorXd> &world,
917  Eigen::Ref<Eigen::VectorXd> error) const;
918 
919  /** \} */
920 
921  /** \name Jacobian Computation
922  \{ */
923 
924  /** \brief Get the Jacobian given a provided world configuration.
925  * \param[in] world World configuration (from world indices).
926  * \param[out] jacobian Jacobian value for all TSRs.
927  */
928  void getJacobianWorldState(const Eigen::Ref<const Eigen::VectorXd> &world,
929  Eigen::Ref<Eigen::MatrixXd> jacobian) const;
930 
931  /** \} */
932 
933  /** \name Distance Computation
934  \{ */
935 
936  /** \brief Get the current distance to satisfaction given the world state.
937  * \return Distance to TSR set satisfaction.
938  */
939  double distanceWorld() const;
940 
941  /** \brief Get the distance to satisfaction given a provided world configuration.
942  * \param[in] world World configuration (from world indices).
943  * \return Distance to TSR set satisfaction.
944  */
945  double distanceWorldState(const Eigen::Ref<const Eigen::VectorXd> &world) const;
946 
947  /** \} */
948 
949  /** \name Solving for Satisfying Configurations
950  \{ */
951 
952  /** \brief Solve for a satisfying configuration given the current state of the world.
953  * \return True on success, false on failure.
954  */
955  bool solveWorld();
956 
957  /** \brief Solve for a satisfying configuration given an initial world configuration.
958  * \param[in] world World configuration (from world indices).
959  * \return True on success, false on failure.
960  */
961  bool solveWorldState(Eigen::Ref<Eigen::VectorXd> world);
962 
963  /** \brief Solve using gradient descent for a satisfying configuration given an initial world
964  * configuration.
965  * \param[in] world World configuration (from world indices).
966  * \return True on success, false on failure.
967  */
968  bool solveGradientWorldState(Eigen::Ref<Eigen::VectorXd> world);
969 
970  /** \} */
971 
972  /** \brief Print out the TSRs in this set.
973  * \param[in] out Output stream.
974  */
975  void print(std::ostream &out) const;
976 
977  private:
978  /** \brief Enforce upper and lower bounds on a world configuration.
979  * \param[in,out] world World configuration to enforce.
980  */
981  void enforceBoundsWorld(Eigen::Ref<Eigen::VectorXd> world) const;
982 
983  WorldPtr world_; ///< World to use.
984  std::set<std::size_t> skel_indices_; ///< All skeleton indices used by members of the set.
985 
986  bool qr_{false}; ///< If true, use QR in gradient solve. Else, SVD.
987  bool damped_{true}; ///< If true, use damped SVD.
988  double step_{1.0}; ///< Step scaling in gradient.
989  double limit_{1.}; ///< Step size limit.
990  double damping_{1e-8}; ///< Damping factor.
991  double tolerance_{magic::DEFAULT_IK_TOLERANCE}; ///< Tolerance for solving.
992  std::size_t maxIter_{50}; ///< Maximum iterations to use for solving.
993 
994  std::vector<TSRPtr> tsrs_; ///< Set of TSRs
995  std::vector<double> weights_; ///< Weights on TSRs
996  std::size_t dimension_{0}; ///< Total error dimension of set.
997 
998  Eigen::VectorXd upper_; ///< Upper bounds on world configuration.
999  Eigen::VectorXd lower_; ///< Lower bounds on world configuration.
1000  };
1001 
1002  /** \cond IGNORE */
1004  /** \endcond */
1005 
1006  /** \class robowflex::darts::TSRConstraintPtr
1007  \brief A shared pointer wrapper for robowflex::darts::TSRConstraint. */
1008 
1009  /** \class robowflex::darts::TSRConstraintConstPtr
1010  \brief A const shared pointer wrapper for robowflex::darts::TSRConstraint. */
1011 
1012  /** \brief An OMPL constraint for TSRs.
1013  * Under the hood, creates a TSRSet that is used for all computation. Make sure that the robot state
1014  * space has all groups setup before creation of this constraint.
1015  */
1016  class TSRConstraint : public ompl::base::Constraint
1017  {
1018  public:
1019  /** \brief Constructor for a single TSR.
1020  * \param[in] space Robot state space.
1021  * \param[in] tsr TSR to use in constraint.
1022  */
1023  TSRConstraint(const StateSpacePtr &space, const TSRPtr &tsr);
1024 
1025  /** \brief Constructor for multiple TSRs.
1026  * \param[in] space Robot state space.
1027  * \param[in] tsrs TSRs to use in constraint.
1028  */
1029  TSRConstraint(const StateSpacePtr &space, const std::vector<TSRPtr> &tsrs);
1030 
1031  /** \brief Constructor for a TSRSet.
1032  * \param[in] space Robot state space.
1033  * \param[in] tsr TSRs to use in constraint.
1034  */
1035  TSRConstraint(const StateSpacePtr &space, const TSRSetPtr &tsr);
1036 
1037  /** \brief Get TSR set for this constraint.
1038  * \return The constraint's TSR set.
1039  */
1040  TSRSetPtr getSet();
1041 
1042  void function(const Eigen::Ref<const Eigen::VectorXd> &x,
1043  Eigen::Ref<Eigen::VectorXd> out) const override;
1044 
1045  void jacobian(const Eigen::Ref<const Eigen::VectorXd> &x,
1046  Eigen::Ref<Eigen::MatrixXd> out) const override;
1047 
1048  bool project(Eigen::Ref<Eigen::VectorXd> x) const override;
1049 
1050  /** \brief Public options.
1051  */
1052  struct
1053  {
1054  bool use_gradient{false};
1055  } options;
1056 
1057  protected:
1058  StateSpacePtr space_; ///< Robot state space.
1059  TSRSetPtr tsr_; ///< Set of TSR constraints.
1060  };
1061  } // namespace darts
1062 } // namespace robowflex
1063 
1064 #endif
#define ROBOWFLEX_CLASS_FORWARD(C)
Macro that forward declares a class and defines two shared ptrs types:
Definition: class_forward.h:16
#define ROBOWFLEX_EIGEN
Macro for classes with fixed width Eigen classes.
Definition: class_forward.h:25
A shared pointer wrapper for robowflex::darts::StateSpace.
An OMPL constraint for TSRs. Under the hood, creates a TSRSet that is used for all computation....
Definition: tsr.h:1017
TSRSetPtr tsr_
Set of TSR constraints.
Definition: tsr.h:1059
StateSpacePtr space_
Robot state space.
Definition: tsr.h:1058
A shared pointer wrapper for robowflex::darts::TSR.
A shared pointer wrapper for robowflex::darts::TSRSet.
Manager for a set of TSR constraints. Attempts to reduce redundancy and combines errors and Jacobians...
Definition: tsr.h:726
Eigen::VectorXd upper_
Upper bounds on world configuration.
Definition: tsr.h:998
Eigen::VectorXd lower_
Lower bounds on world configuration.
Definition: tsr.h:999
WorldPtr world_
World to use.
Definition: tsr.h:983
std::vector< double > weights_
Weights on TSRs.
Definition: tsr.h:995
std::set< std::size_t > skel_indices_
All skeleton indices used by members of the set.
Definition: tsr.h:984
std::vector< TSRPtr > tsrs_
Set of TSRs.
Definition: tsr.h:994
The specification of a TSR.
Definition: tsr.h:70
std::string structure
Structure target frame is in.
Definition: tsr.h:76
std::string frame
Name of target frame.
Definition: tsr.h:77
Specification()=default
Default constructor.
A Task Space Region (TSR). TSRs are workspace regions that impose a constraints on a robot....
Definition: tsr.h:63
std::vector< std::size_t > indices_
Controlled indices.
Definition: tsr.h:696
std::vector< std::pair< std::size_t, std::size_t > > world_indices_
World indices.
Definition: tsr.h:697
std::vector< std::size_t > bijection_
Definition: tsr.h:698
WorldPtr world_
Underlying world.
Definition: tsr.h:692
std::size_t skel_index_
Index of controlled skeleton.
Definition: tsr.h:695
Specification spec_
TSR specification.
Definition: tsr.h:693
A shared pointer wrapper for robowflex::darts::World.
static const double DEFAULT_IK_TOLERANCE
Definition: tsr.h:30
static const Eigen::Vector3d DEFAULT_IK_TOLERANCES
Definition: tsr.h:32
static const std::string ROOT_FRAME
Definition: tsr.h:31
Main namespace. Contains all library classes and functions.
Definition: scene.cpp:25
std::decay< decltype(std::declval< moveit::core::Transforms >().getTransform("")) >::type RobotPose
A pose (point in SE(3)) used in various functions. Defined from what MoveIt! uses.
Definition: adapter.h:24
void print(PropagationDistanceField &pdf, int numX, int numY, int numZ)