Robowflex  v0.1
Making MoveIt Easy
space.h
Go to the documentation of this file.
1 /* Author: Zachary Kingston */
2 
3 #ifndef ROBOWFLEX_DART_SPACE_
4 #define ROBOWFLEX_DART_SPACE_
5 
6 #include <ompl/base/spaces/RealVectorStateSpace.h>
7 
9 
10 #include <robowflex_dart/robot.h>
11 #include <robowflex_dart/joints.h>
12 
13 namespace robowflex
14 {
15  namespace darts
16  {
17  /** \cond IGNORE */
18  ROBOWFLEX_CLASS_FORWARD(StateSpace)
19  /** \endcond */
20 
21  /** \class robowflex::darts::StateSpacePtr
22  \brief A shared pointer wrapper for robowflex::darts::StateSpace. */
23 
24  /** \class robowflex::darts::StateSpaceConstPtr
25  \brief A const shared pointer wrapper for robowflex::darts::StateSpace. */
26 
27  /** \brief An OMPL state space for robowflex::darts::World. Can do motion planning for any of the
28  * robots or structures in the world.
29  */
30  class StateSpace : public ompl::base::RealVectorStateSpace
31  {
32  public:
33  /** \cond IGNORE */
35  /** \endcond */
36 
37  /** \class robowflex::darts::StateSpace::StateSamplerPtr
38  \brief A shared pointer wrapper for robowflex::darts::StateSpace::StateSampler. */
39 
40  /** \class robowflex::darts::StateSpace::StateSamplerConstPtr
41  \brief A const shared pointer wrapper for robowflex::darts::StateSpace::StateSampler. */
42 
43  /** \brief Sampler for robowflex::darts::StateSpace.
44  */
45  class StateSampler : public ompl::base::RealVectorStateSampler
46  {
47  public:
48  /** \brief Constructor.
49  * \param[in] space Space to sample.
50  */
51  StateSampler(const StateSpace *space);
52 
53  /** \name OMPL StateSampler Methods
54  \{ */
55 
56  void sampleUniform(ompl::base::State *state) override;
57  void sampleUniformNear(ompl::base::State *state, const ompl::base::State *near,
58  double distance) override;
59 
60  /** \} */
61 
62  private:
63  const std::vector<JointPtr> &joints_; ///< Joints to sample from.
64  };
65 
66  /** \brief State type for the robowflex::darts::StateSpace.
67  */
68  class StateType : public ompl::base::RealVectorStateSpace::StateType
69  {
70  public:
71  /** \brief Constructor.
72  * \param[in] n Dimension of state.
73  */
75 
76  Eigen::VectorXd data; ///< Vector for configuration.
77  };
78 
79  // Friendship
80  friend Joint;
81  friend RnJoint;
82  friend StateSampler;
83 
84  /** \name Constructor and Setup
85  \{ */
86 
87  /** \brief Constructor.
88  * \param[in] world World to plan for.
89  */
90  StateSpace(WorldPtr world);
91 
92  /** \brief Add a group to be planned for.
93  * \param[in] name Name of the robot that has the group.
94  * \param[in] group Group to plan for.
95  * \param[in] cyclic If >0, will flatten rotational groups (i.e.,
96  * SO(2), SO(3)) into Rn spaces, where n is \a cyclic.
97  */
98  void addGroup(const std::string &name, const std::string &group, std::size_t cyclic = 0);
99 
100  /** \brief Add a group to be planned for.
101  * \param[in] group_name Name of the new group.
102  * \param[in] joints Joints to add to this group.
103  * \param[in] cyclic If >0, will flatten rotational groups (i.e.,
104  * SO(2), SO(3)) into Rn spaces, where n is \a cyclic.
105  */
106  void addGroupFromJoints(const std::string &group_name,
108  std::size_t cyclic = 0);
109 
110  /** \} */
111 
112  /** \name World State
113  \{ */
114 
115  /** \brief Set the state of a world from an OMPL state.
116  * \param[out] world World to set state of.
117  * \param[in] state State to set world.
118  */
119  void setWorldState(WorldPtr world, const ompl::base::State *state) const;
120 
121  /** \brief Set the state of a world from a configuration
122  * \param[out] world World to set state of.
123  * \param[in] x Configuration to set world.
124  */
125  void setWorldState(WorldPtr world, const Eigen::Ref<const Eigen::VectorXd> &x) const;
126 
127  /** \brief Get the state of a world in an OMPL state.
128  * \param[in] world World to get state of.
129  * \param[out] state State to fill.
130  */
131  void getWorldState(WorldPtr world, ompl::base::State *state) const;
132 
133  /** \brief Get the state of a world in a configuration vector.
134  * \param[in] world World to get state of.
135  * \param[out] x Configuration to fill.
136  */
137  void getWorldState(WorldPtr world, Eigen::Ref<Eigen::VectorXd> x) const;
138 
139  /** \brief Set the state of a world from a configuration of a group.
140  * \param[out] world World to set state of.
141  * \param[in] group_name Name of group to set.
142  * \param[in] x Configuration to set.
143  */
144  void setWorldGroupState(WorldPtr world, const std::string &group_name,
145  const Eigen::Ref<const Eigen::VectorXd> &x) const;
146 
147  /** \brief Get the group state of a world.
148  * \param[in] world World to get state of.
149  * \param[in] group_name Name of group to get.
150  * \param[out] x Configuration to get.
151  */
152  void getWorldGroupState(WorldPtr world, const std::string &group_name,
153  Eigen::Ref<Eigen::VectorXd> x) const;
154 
155  /** \} */
156 
157  /** \name Getters and Setters
158  \{ */
159 
160  /** \brief Get underlying world.
161  * \return The world for the space.
162  */
163  WorldPtr getWorld();
164 
165  /** \brief Get underlying world.
166  * \return The world for the space.
167  */
168  const WorldPtr &getWorldConst() const;
169 
170  /** \brief Get the set of indices that is being planned for. Pairs of skeleton index and joint
171  * index.
172  * \return Vector of indices.
173  */
175 
176  /** \brief Get a vector of the lower joint bounds for the space.
177  * \return The lower bounds of the space.
178  */
179  Eigen::VectorXd getLowerBound() const;
180 
181  /** \brief Get a vector of the upper joint bounds for the space.
182  * \return The upper bounds of the space.
183  */
184  Eigen::VectorXd getUpperBound() const;
185 
186  /** \brief Get a joint that is being planned for.
187  * \return The joint.
188  */
189  JointPtr getJoint(std::size_t index) const;
190 
191  /** \brief Get a joint that is being planned for.
192  * \return The joint.
193  */
194  JointPtr getJoint(const std::string &name) const;
195 
196  /** \brief Get a vector of the joints being planned for.
197  * \return The vector of joints being planned for.
198  */
199  const std::vector<JointPtr> &getJoints() const;
200 
201  /** \brief From a full state, get only the state of a group.
202  * \param[in] group The group to get the state for.
203  * \param[in] state The state to get the group state from.
204  * \param[out] v The group state to set.
205  */
206  void getGroupState(const std::string &group, const ompl::base::State *state,
207  Eigen::Ref<Eigen::VectorXd> v) const;
208 
209  /** \brief Set a (sub)group state in a full state.
210  * \param[in] group The group to set the state for.
211  * \param[out] state The state to set the group state in.
212  * \param[in] v The group state to use.
213  */
214  void setGroupState(const std::string &group, ompl::base::State *state,
215  const Eigen::Ref<const Eigen::VectorXd> &v) const;
216 
217  /** \brief Get the dimension of joint variables for a subgroup.
218  * \return The group's dimension.
219  */
220  std::size_t getGroupDimension(const std::string &group) const;
221 
222  /** \brief Get the names of all groups managed by this space.
223  * \return Names of all the groups.
224  */
225  std::vector<std::string> getGroups() const;
226 
227  /** \brief Get the names of each of the dof controlled by a group.
228  * \return The names of all the controlled dof for a group.
229  */
230  std::vector<std::string> getGroupDofNames(const std::string &group_name) const;
231 
232  /** \} */
233 
234  /** \name OMPL StateSpace Methods
235  \{ */
236 
237  bool isMetricSpace() const override;
238  void enforceBounds(ompl::base::State *state) const override;
239  bool satisfiesBounds(const ompl::base::State *state) const override;
240  double distance(const ompl::base::State *state1, const ompl::base::State *state2) const override;
241  double getMaximumExtent() const override;
242  bool equalStates(const ompl::base::State *state1, const ompl::base::State *state2) const override;
243  void interpolate(const ompl::base::State *from, const ompl::base::State *to, double t,
244  ompl::base::State *state) const override;
245  ompl::base::StateSamplerPtr allocDefaultStateSampler() const override;
246  ompl::base::State *allocState() const override;
247  void freeState(ompl::base::State *state) const override;
248 
249  /** \} */
250 
251  void setMetricSpace(bool metric);
252 
253  protected:
254  void addJoint(const std::string &group_name, const JointPtr &joint);
255  void addJointToGroup(const std::string &group_name, const JointPtr &joint);
256 
257  WorldPtr world_; ///< World to use for planning.
258 
259  std::set<dart::dynamics::Joint *> jointset_; ///< Set of joints used in planning.
260  std::vector<std::size_t> indices_; ///< Vector of indices for planning.
261 
262  bool metric_{true}; ///< Is this space all Rn?
263 
264  std::vector<JointPtr> joints_; ///< Vector of all joints used in planning.
265 
266  ompl::RNG rng_; ///< Random number generator.
267 
268  std::map<std::string, std::vector<JointPtr>> group_joints_; ///< Joints belonging to a group.
270  };
271  } // namespace darts
272 } // namespace robowflex
273 
274 #endif
#define ROBOWFLEX_CLASS_FORWARD(C)
Macro that forward declares a class and defines two shared ptrs types:
Definition: class_forward.h:16
A shared pointer wrapper for robowflex::darts::Joint.
Sampler for robowflex::darts::StateSpace.
Definition: space.h:46
const std::vector< JointPtr > & joints_
Joints to sample from.
Definition: space.h:63
State type for the robowflex::darts::StateSpace.
Definition: space.h:69
Eigen::VectorXd data
Vector for configuration.
Definition: space.h:76
An OMPL state space for robowflex::darts::World. Can do motion planning for any of the robots or stru...
Definition: space.h:31
std::vector< std::size_t > indices_
Vector of indices for planning.
Definition: space.h:260
WorldPtr world_
World to use for planning.
Definition: space.h:257
std::vector< JointPtr > joints_
Vector of all joints used in planning.
Definition: space.h:264
std::set< dart::dynamics::Joint * > jointset_
Set of joints used in planning.
Definition: space.h:259
std::map< std::string, std::vector< JointPtr > > group_joints_
Joints belonging to a group.
Definition: space.h:268
std::map< std::string, std::size_t > group_dimension_
Dimension of the group.
Definition: space.h:269
ompl::RNG rng_
Random number generator.
Definition: space.h:266
A shared pointer wrapper for robowflex::darts::World.
Main namespace. Contains all library classes and functions.
Definition: scene.cpp:25