Robowflex  v0.1
Making MoveIt Easy
robowflex_dart/include/robowflex_dart/planning.h
Go to the documentation of this file.
1 /* Author: Zachary Kingston */
2 
3 #ifndef ROBOWFLEX_DART_PLANNING_
4 #define ROBOWFLEX_DART_PLANNING_
5 
6 #include <set>
7 #include <Eigen/Dense>
8 
9 #include <ompl/base/goals/GoalLazySamples.h>
10 #include <ompl/base/Constraint.h>
11 #include <ompl/geometric/SimpleSetup.h>
12 
13 #include <moveit_msgs/MotionPlanRequest.h>
14 
16 #include <robowflex_dart/space.h>
17 
18 namespace robowflex
19 {
20  namespace darts
21  {
22  /** \cond IGNORE */
25  ROBOWFLEX_CLASS_FORWARD(TSRConstraint)
27  /** \endcond */
28 
29  /** \cond IGNORE */
31  ROBOWFLEX_CLASS_FORWARD(JointRegionGoal)
32  ROBOWFLEX_CLASS_FORWARD(PlanBuilder)
33  /** \endcond */
34 
35  /** \class robowflex::darts::TSRGoalPtr
36  \brief A shared pointer wrapper for robowflex::darts::TSRGoal. */
37 
38  /** \class robowflex::darts::TSRGoalConstPtr
39  \brief A const shared pointer wrapper for robowflex::darts::TSRGoal. */
40 
41  /** \class robowflex::darts::JointRegionGoalPtr
42  \brief A shared pointer wrapper for robowflex::darts::JointRegionGoal. */
43 
44  /** \class robowflex::darts::JointRegionGoalConstPtr
45  \brief A const shared pointer wrapper for robowflex::darts::JointRegionGoal. */
46 
47  /** \brief Helper class to manage extracting states from a possibly constrained state space.
48  */
50  {
51  public:
52  /** \brief Constructor.
53  */
54  ConstraintExtractor() = default;
55 
56  /** \brief Constructor.
57  * \param[in] si The space information.
58  */
59  ConstraintExtractor(const ompl::base::SpaceInformationPtr &si);
60 
61  /** \name State Access
62  * \{ */
63 
64  /** \brief Extract underlying state from a base state.
65  * \param[in] state State.
66  * \return Underlying robot state.
67  */
68  StateSpace::StateType *toState(ompl::base::State *state) const;
69 
70  /** \brief Extract underlying state from a base state.
71  * \param[in] state State.
72  * \return Underlying robot state.
73  */
74  const StateSpace::StateType *toStateConst(const ompl::base::State *state) const;
75 
76  /** \brief Access the underlying state from a constrained OMPL state.
77  * \param[in] state Constrained state to access.
78  * \return The underlying state.
79  */
80  StateSpace::StateType *fromConstrainedState(ompl::base::State *state) const;
81 
82  /** \brief Access the underlying state from a constrained OMPL state.
83  * \param[in] state Constrained state to access.
84  * \return The underlying state.
85  */
86  const StateSpace::StateType *fromConstrainedStateConst(const ompl::base::State *state) const;
87 
88  /** \brief Access the underlying state from an unconstrained OMPL state.
89  * \param[in] state Unconstrained state to access.
90  * \return The underlying state.
91  */
92  StateSpace::StateType *fromUnconstrainedState(ompl::base::State *state) const;
93 
94  /** \brief Access the underlying state from an unconstrained OMPL state.
95  * \param[in] state Unconstrained state to access.
96  * \return The underlying state.
97  */
98  const StateSpace::StateType *fromUnconstrainedStateConst(const ompl::base::State *state) const;
99 
100  /** \} */
101 
102  /** \brief Gets the underlying state space from the space information.
103  * \return State space.
104  */
105  const StateSpace *getSpace() const;
106 
107  /** \brief Set space information used for constraint extraction.
108  * \param[in] si The space information.
109  */
110  void setSpaceInformation(const ompl::base::SpaceInformationPtr &si);
111 
112  private:
113  ompl::base::SpaceInformationPtr space_info_; ///< Space Information.
114  bool is_constrained_{false}; ///< Is the underlying space constrained?
115  };
116 
117  /** \brief A sampleable goal region for OMPL for a set of TSRs.
118  * Samples goals in a separate thread using a clone of the world.
119  */
120  class TSRGoal : public ompl::base::GoalLazySamples, public ConstraintExtractor
121  {
122  public:
123  /** \name Constructors.
124  \{ */
125 
126  /** \brief Constructor.
127  * \param[in] si Space information.
128  * \param[in] world World to use.
129  * \param[in] tsrs TSRs to sample.
130  */
131  TSRGoal(const ompl::base::SpaceInformationPtr &si, const WorldPtr &world,
132  const std::vector<TSRPtr> &tsrs);
133 
134  /** \brief Constructor.
135  * \param[in] si Space information.
136  * \param[in] world World to use.
137  * \param[in] tsr TSR to sample.
138  */
139  TSRGoal(const ompl::base::SpaceInformationPtr &si, const WorldPtr &world, const TSRPtr tsr);
140 
141  /** \brief Constructor.
142  * \param[in] builder Plan builder to use as base for goal.
143  * \param[in] tsr TSR to sample.
144  */
145  TSRGoal(const PlanBuilder &builder, TSRPtr tsr);
146 
147  /** \brief Constructor.
148  * \param[in] builder Plan builder to use as base for goal.
149  * \param[in] tsrs TSRs to sample.
150  */
151  TSRGoal(const PlanBuilder &builder, const std::vector<TSRPtr> &tsrs);
152 
153  /** \brief Destructor. Stops sampling thread.
154  */
155  ~TSRGoal();
156 
157  /** \} */
158 
159  /** \brief Sampling routine. Generates IK samples from the TSR goal.
160  * \param[in] gls This class.
161  * \param[in] state State to sample.
162  */
163  bool sample(const ompl::base::GoalLazySamples *gls, ompl::base::State *state);
164 
165  /** \brief Distance to the goal.
166  * \param[in] state State to measure.
167  */
168  double distanceGoal(const ompl::base::State *state) const override;
169 
170  /** \brief Get the TSR set of this goal region.
171  * \return The TSR set.
172  */
174 
175  /** \brief Public options.
176  */
177  struct
178  {
179  bool use_gradient{false}; ///< Use gradient-based TSR solver.
180  std::size_t max_samples{10}; ///< Maximum samples.
182 
183  private:
184  WorldPtr world_; ///< World used.
185  TSRSetPtr tsr_; ///< TSR set to sample from.
186  ompl::base::StateSamplerPtr sampler_; ///< State sampler.
187  };
188 
189  /** \brief A joint space goal volume.
190  */
191  class JointRegionGoal : public ompl::base::GoalSampleableRegion, public ConstraintExtractor
192  {
193  public:
194  JointRegionGoal(const PlanBuilder &builder, const Eigen::Ref<const Eigen::VectorXd> &state,
195  double tolerance = 0.);
196 
197  JointRegionGoal(const PlanBuilder &builder, const Eigen::Ref<const Eigen::VectorXd> &lower,
198  const Eigen::Ref<const Eigen::VectorXd> &upper);
199 
201 
202  void sampleGoal(ompl::base::State *state) const override;
203  unsigned int maxSampleCount() const override;
204  double distanceGoal(const ompl::base::State *state) const override;
205 
206  private:
207  mutable ompl::RNG rng_;
208 
209  ompl::base::State *lower_;
210  ompl::base::State *upper_;
211  };
212 
213  /** \class robowflex::darts::PlanBuilderPtr
214  \brief A shared pointer wrapper for robowflex::darts::PlanBuilder. */
215 
216  /** \class robowflex::darts::PlanBuilderConstPtr
217  \brief A const shared pointer wrapper for robowflex::darts::PlanBuilder. */
218 
219  /** \brief A helper class to setup common OMPL structures for planning.
220  */
222  {
223  public:
224  /** \name Setup and Initialization.
225  \{ */
226 
227  /** \brief Constructor.
228  * \param[in] world World to use for planning.
229  */
231 
232  /** \brief Initialize OMPL structures. Only call this once all constraints, groups, and other
233  * parameters have been set for the builder.
234  */
235  void initialize();
236 
237  /** \brief Calls setup routines on OMPL structures. Call after all setup is done and builder is
238  * initialized.
239  */
240  void setup();
241 
242  /** \} */
243 
244  /** \name MoveIt Planning Messages
245  \{ */
246 
247  /** \brief Get workspace bounds from a planning request.
248  * \param[in] msg Planning request message.
249  */
250  void getWorkspaceBoundsFromMessage(const moveit_msgs::MotionPlanRequest &msg);
251 
252  /** \brief Set group for planning from message.
253  * \param[in] robot_name Robot name to use message on.
254  * \param[in] msg Planning request message.
255  */
256  void getGroupFromMessage(const std::string &robot_name,
257  const moveit_msgs::MotionPlanRequest &msg);
258 
259  /** \brief Get the start state from the request message.
260  * \param[in] robot_name Robot name to use message on.
261  * \param[in] msg Planning request message.
262  */
263  void getStartFromMessage(const std::string &robot_name,
264  const moveit_msgs::MotionPlanRequest &msg);
265 
266  /** \brief Get the set of path constraints from the request message.
267  * \param[in] robot_name Robot name to use message on.
268  * \param[in] msg Planning request message.
269  */
270  void getPathConstraintsFromMessage(const std::string &robot_name,
271  const moveit_msgs::MotionPlanRequest &msg);
272 
273  /** \brief Get the goal constraints from the planning request message.
274  * \param[in] robot_name Robot name to use message on.
275  * \param[in] msg Planning request message.
276  */
277  ompl::base::GoalPtr getGoalFromMessage(const std::string &robot_name,
278  const moveit_msgs::MotionPlanRequest &msg);
279 
280  /** \brief Get a TSR from an position constraint.
281  * \param[in] robot_name Robot name to use message on.
282  * \param[in] msg Position constraint.
283  */
284  TSRPtr fromPositionConstraint(const std::string &robot_name,
285  const moveit_msgs::PositionConstraint &msg) const;
286 
287  /** \brief Get a TSR from an orientation constraint.
288  * \param[in] robot_name Robot name to use message on.
289  * \param[in] msg Orientation constraint.
290  */
291  TSRPtr fromOrientationConstraint(const std::string &robot_name,
292  const moveit_msgs::OrientationConstraint &msg) const;
293 
294  /** \brief Get a joint region goal from a message.
295  * \param[in] msgs Joint constraint messages to create region goal.
296  * \return The joint region goal.
297  */
300 
301  /** \brief Use all of the planning request message to setup motion planning.
302  * \param[in] robot_name Robot name to use message on.
303  * \param[in] msg Planning request message.
304  */
305  ompl::base::GoalPtr fromMessage(const std::string &robot_name,
306  const moveit_msgs::MotionPlanRequest &msg);
307 
308  /** \} */
309 
310  /** \name Other Settings
311  \{ */
312 
313  /** \brief Add a robot's planning group to the controlled DoF in the state space.
314  * \param[in] robot Name of the robot.
315  * \param[in] name Name of the group in the robot to add.
316  * \param[in] cyclic If > 0, if the group contains cyclic joints (SO(2), SO(3)) these will be
317  * modeled as Rn.
318  */
319  void addGroup(const std::string &robot, const std::string &name, std::size_t cyclic = 0);
320 
321  /** \brief Adds a TSR as a path constraint to the problem.
322  * \param[in] tsr Path constraint.
323  */
324  void addConstraint(const TSRPtr &tsr);
325 
326  /** \} */
327 
328  /** \name Start Configuration
329  \{ */
330 
331  /** \brief Set the start configuration from the current state of the world.
332  */
334 
335  /** \brief Set the start configuration from a configuration.
336  * \param[in] q Configuration.
337  */
338  void setStartConfiguration(const Eigen::Ref<const Eigen::VectorXd> &q);
339 
340  /** \brief Set the start configuration from a configuration.
341  * \param[in] q Configuration.
342  */
344 
345  /** \brief Sample a valid start configuration.
346  */
348 
349  /** \} */
350 
351  /** \name Goals
352  \{ */
353 
354  /** \brief Set the goal configuration from the current state of the world.
355  */
357 
358  /** \brief Set the goal configuration from a configuration.
359  * \param[in] q Configuration.
360  */
362  getGoalConfiguration(const Eigen::Ref<const Eigen::VectorXd> &q);
363 
364  /** \brief Set the goal configuration from a configuration.
365  * \param[in] q Configuration.
366  */
368 
369  /** \brief Set a TSR as the goal for the planning problem (will create a TSRGoal)
370  * \param[in] tsr TSR for the goal.
371  */
372  TSRGoalPtr getGoalTSR(const TSRPtr &tsr);
373 
374  /** \brief Set a set of TSRs as the goal for the planning problem (will create a TSRGoal)
375  * \param[in] tsrs TSRs for the goal.
376  */
378 
379  /** \brief Sample a valid goal configuration.
380  */
382 
383  /** \brief Set the goal for the problem.
384  * \param[in] goal Goal to use.
385  */
386  void setGoal(const ompl::base::GoalPtr &goal);
387 
388  /** \} */
389 
390  /** \name Other Functions
391  \{ */
392 
393  /** \brief Sample a valid state.
394  * \return a Valid state.
395  */
396  ompl::base::State *sampleState() const;
397 
398  /** \brief Get the solution path from a successful plan
399  * \param[in] simplify Simplify the solution.
400  * \param[in] interpolate Interpolate the solution.
401  */
402  ompl::geometric::PathGeometric getSolutionPath(bool simplify = true,
403  bool interpolate = true) const;
404 
405  /** \} */
406 
407  StateSpacePtr rspace{nullptr}; ///< Underlying Robot State Space.
408  ompl::base::StateSpacePtr space{nullptr}; ///< Actual OMPL State Space (might be constrained).
409  ompl::base::SpaceInformationPtr rinfo{nullptr}; ///< Underlying Space Information.
410  ompl::base::SpaceInformationPtr info{nullptr}; ///< Actual Space Information.
411  ompl::geometric::SimpleSetupPtr ss{nullptr}; ///< Simple Setup.
412  WorldPtr world; ///< World used for planning.
413 
414  std::vector<TSRPtr> path_constraints; ///< Path Constraints.
415  TSRConstraintPtr constraint{nullptr}; ///< OMPL Constraint for Path Constraints.
416 
417  Eigen::VectorXd start; ///< Start configuration.
418 
419  /** \brief Hyperparameter options.
420  */
421  struct
422  {
423  /** \brief Constrained planning options.
424  */
425  struct
426  {
427  double delta{0.2}; ///< Manifold delta.
428  double lambda{5.0}; ///< Manifold lambda.
431 
432  protected:
433  ompl::base::GoalPtr goal_{nullptr}; ///< Desired goal.
434 
435  /** \brief Initialize when path constraints are present.
436  */
437  void initializeConstrained();
438 
439  /** \brief Initialize when no path constraints are present.
440  */
442 
443  /** \brief Set the state validity checker on the simple setup.
444  */
446 
447  /** \brief Get a state validity checker for the unconstrained space.
448  * \return The state validity checker.
449  */
450  ompl::base::StateValidityCheckerFn getSVCUnconstrained();
451 
452  /** \brief Get a state validity checker for the unconstrained space.
453  * \return The state validity checker.
454  */
455  ompl::base::StateValidityCheckerFn getSVCConstrained();
456  };
457  } // namespace darts
458 } // namespace robowflex
459 
460 #endif
#define ROBOWFLEX_CLASS_FORWARD(C)
Macro that forward declares a class and defines two shared ptrs types:
Definition: class_forward.h:16
Helper class to manage extracting states from a possibly constrained state space.
ompl::base::SpaceInformationPtr space_info_
Space Information.
ConstraintExtractor()=default
Constructor.
A shared pointer wrapper for robowflex::darts::JointRegionGoal.
JointRegionGoal(const PlanBuilder &builder, const Eigen::Ref< const Eigen::VectorXd > &state, double tolerance=0.)
unsigned int maxSampleCount() const override
void sampleGoal(ompl::base::State *state) const override
double distanceGoal(const ompl::base::State *state) const override
A helper class to setup common OMPL structures for planning.
void setGoal(const ompl::base::GoalPtr &goal)
Set the goal for the problem.
void setStartConfigurationFromWorld()
Set the start configuration from the current state of the world.
TSRPtr fromOrientationConstraint(const std::string &robot_name, const moveit_msgs::OrientationConstraint &msg) const
Get a TSR from an orientation constraint.
std::shared_ptr< ompl::base::GoalStates > sampleGoalConfiguration()
Sample a valid goal configuration.
void addConstraint(const TSRPtr &tsr)
Adds a TSR as a path constraint to the problem.
void setup()
Calls setup routines on OMPL structures. Call after all setup is done and builder is initialized.
void setStartConfiguration(const Eigen::Ref< const Eigen::VectorXd > &q)
Set the start configuration from a configuration.
void getPathConstraintsFromMessage(const std::string &robot_name, const moveit_msgs::MotionPlanRequest &msg)
Get the set of path constraints from the request message.
void initialize()
Initialize OMPL structures. Only call this once all constraints, groups, and other parameters have be...
void initializeConstrained()
Initialize when path constraints are present.
ompl::base::SpaceInformationPtr info
Actual Space Information.
ompl::base::StateValidityCheckerFn getSVCUnconstrained()
Get a state validity checker for the unconstrained space.
TSRConstraintPtr constraint
OMPL Constraint for Path Constraints.
TSRGoalPtr getGoalTSR(const TSRPtr &tsr)
Set a TSR as the goal for the planning problem (will create a TSRGoal)
ompl::geometric::PathGeometric getSolutionPath(bool simplify=true, bool interpolate=true) const
Get the solution path from a successful plan.
ompl::geometric::SimpleSetupPtr ss
Simple Setup.
StateSpacePtr rspace
Underlying Robot State Space.
void addGroup(const std::string &robot, const std::string &name, std::size_t cyclic=0)
Add a robot's planning group to the controlled DoF in the state space.
struct robowflex::darts::PlanBuilder::@1::@2 constraints
Constrained planning options.
void getWorkspaceBoundsFromMessage(const moveit_msgs::MotionPlanRequest &msg)
Get workspace bounds from a planning request.
void setStateValidityChecker()
Set the state validity checker on the simple setup.
void sampleStartConfiguration()
Sample a valid start configuration.
std::vector< TSRPtr > path_constraints
Path Constraints.
struct robowflex::darts::PlanBuilder::@1 options
Hyperparameter options.
void getStartFromMessage(const std::string &robot_name, const moveit_msgs::MotionPlanRequest &msg)
Get the start state from the request message.
std::shared_ptr< ompl::base::GoalStates > getGoalConfigurationFromWorld()
Set the goal configuration from the current state of the world.
ompl::base::GoalPtr fromMessage(const std::string &robot_name, const moveit_msgs::MotionPlanRequest &msg)
Use all of the planning request message to setup motion planning.
TSRPtr fromPositionConstraint(const std::string &robot_name, const moveit_msgs::PositionConstraint &msg) const
Get a TSR from an position constraint.
ompl::base::StateSpacePtr space
Actual OMPL State Space (might be constrained).
void initializeUnconstrained()
Initialize when no path constraints are present.
ompl::base::State * sampleState() const
Sample a valid state.
ompl::base::StateValidityCheckerFn getSVCConstrained()
Get a state validity checker for the unconstrained space.
JointRegionGoalPtr fromJointConstraints(const std::vector< moveit_msgs::JointConstraint > &msgs) const
Get a joint region goal from a message.
ompl::base::GoalPtr getGoalFromMessage(const std::string &robot_name, const moveit_msgs::MotionPlanRequest &msg)
Get the goal constraints from the planning request message.
ompl::base::SpaceInformationPtr rinfo
Underlying Space Information.
std::shared_ptr< ompl::base::GoalStates > getGoalConfiguration(const Eigen::Ref< const Eigen::VectorXd > &q)
Set the goal configuration from a configuration.
void getGroupFromMessage(const std::string &robot_name, const moveit_msgs::MotionPlanRequest &msg)
Set group for planning from message.
A shared pointer wrapper for robowflex::darts::StateSpace.
State type for the robowflex::darts::StateSpace.
Definition: space.h:69
An OMPL state space for robowflex::darts::World. Can do motion planning for any of the robots or stru...
Definition: space.h:31
A shared pointer wrapper for robowflex::darts::TSRConstraint.
A shared pointer wrapper for robowflex::darts::TSRGoal.
A sampleable goal region for OMPL for a set of TSRs. Samples goals in a separate thread using a clone...
struct robowflex::darts::TSRGoal::@0 options
Public options.
ompl::base::StateSamplerPtr sampler_
State sampler.
double distanceGoal(const ompl::base::State *state) const override
Distance to the goal.
~TSRGoal()
Destructor. Stops sampling thread.
TSRSetPtr getTSRSet()
Get the TSR set of this goal region.
bool sample(const ompl::base::GoalLazySamples *gls, ompl::base::State *state)
Sampling routine. Generates IK samples from the TSR goal.
TSRGoal(const ompl::base::SpaceInformationPtr &si, const WorldPtr &world, const std::vector< TSRPtr > &tsrs)
Constructor.
A shared pointer wrapper for robowflex::darts::TSR.
A shared pointer wrapper for robowflex::darts::TSRSet.
A shared pointer wrapper for robowflex::darts::World.
Functions for loading and animating robots in Blender.
Main namespace. Contains all library classes and functions.
Definition: scene.cpp:25