Robowflex  v0.1
Making MoveIt Easy
structure.h
Go to the documentation of this file.
1 /* Author: Zachary Kingston */
2 
3 #ifndef ROBOWFLEX_DART_STRUCTURE_
4 #define ROBOWFLEX_DART_STRUCTURE_
5 
6 #include <dart/dynamics/BoxShape.hpp>
7 #include <dart/dynamics/CylinderShape.hpp>
8 #include <dart/dynamics/SphereShape.hpp>
9 #include <dart/dynamics/MeshShape.hpp>
10 #include <dart/dynamics/RevoluteJoint.hpp>
11 #include <dart/dynamics/PrismaticJoint.hpp>
12 #include <dart/dynamics/FreeJoint.hpp>
13 #include <dart/dynamics/WeldJoint.hpp>
14 #include <dart/dynamics/Skeleton.hpp>
15 
18 #include <robowflex_library/tf.h>
19 
20 namespace robowflex
21 {
22  /** \cond IGNORE */
24  ROBOWFLEX_CLASS_FORWARD(Geometry)
25  /** \endcond */
26 
27  namespace darts
28  {
29  namespace magic
30  {
31  static const double DEFAULT_DENSITY = 1000.;
32  static const double DEFAULT_DAMPING = 0.001;
33  static const double DEFAULT_RESTITUTION = 1;
34  }; // namespace magic
35 
36  /** \cond IGNORE */
38  /** \endcond */
39 
40  /** \cond IGNORE */
42  /** \endcond */
43 
44  /** \class robowflex::darts::StructurePtr
45  \brief A shared pointer wrapper for robowflex::darts::Structure. */
46 
47  /** \class robowflex::darts::StructureConstPtr
48  \brief A const shared pointer wrapper for robowflex::darts::Structure. */
49 
50  /** \brief Wrapper class for a dart::dynamics::Skeleton.
51  */
52  class Structure
53  {
54  public:
55  /** \name Constructors
56  \{ */
57 
58  /** \brief Create an empty structure.
59  * \param[in] name Name of the structure.
60  */
61  Structure(const std::string &name);
62 
63  /** \brief Copy a MoveIt (robowflex::Scene) into a structure.
64  * \param[in] name Name of the structure.
65  * \param[in] scene Scene to copy.
66  */
67  Structure(const std::string &name, const SceneConstPtr &scene);
68 
69  /** \brief Destructor.
70  */
71  virtual ~Structure() = default;
72 
73  /** \brief Clones this structure with a new name.
74  * \param[in] newName Name for clone of the structure.
75  * \return The cloned structure.
76  */
77  StructurePtr cloneStructure(const std::string &newName) const;
78 
79  /** \} */
80 
81  /** \name Getters and Setters
82  \{ */
83 
84  /** \brief Get the name of this structure.
85  * \return Name of the structure.
86  */
87  const std::string &getName() const;
88 
89  /** \brief Get the ACM for the structure.
90  * \return The ACM.
91  */
92  ACMPtr getACM();
93 
94  /** \brief Get the ACM for the structure.
95  * \return The ACM.
96  */
97  const ACMPtr &getACMConst() const;
98 
99  /** \brief Set the skeleton for the structure.
100  * \param[in] skeleton The new skeleton.
101  */
102  void setSkeleton(const dart::dynamics::SkeletonPtr &skeleton);
103 
104  /** \brief Get the underlying skeleton for the structure.
105  * \return The skeleton.
106  */
107  dart::dynamics::SkeletonPtr &getSkeleton();
108 
109  /** \brief Get the underlying skeleton for the structure.
110  * \return The skeleton.
111  */
112  const dart::dynamics::SkeletonPtr &getSkeletonConst() const;
113 
114  /** \brief Dumps the structure of the skeleton to a GraphViz file.
115  * \param[in] out Stream to output to.
116  * \param[in] standalone If false, does not include graph header.
117  */
118  void dumpGraphViz(std::ostream &out, bool standalone = true);
119 
120 
121  /** \} */
122 
123  /** \name Getting and Setting Configurations
124  \{ */
125 
126  /** \brief Set the value of a 1-DoF joint in the structure.
127  * \param[in] name Name of joint.
128  * \param[in] value Value to set joint.
129  */
130  void setJoint(const std::string &name, double value);
131 
132  /** \brief Set the value of a n-DoF joint in the structure.
133  * \param[in] name Name of joint.
134  * \param[in] value Value to set joint.
135  */
136  void setJoint(const std::string &name, const Eigen::Ref<const Eigen::VectorXd> &value);
137 
138  /** \brief Solve the current whole-body IK problem imposed on the structure.
139  * \return Returns true on success false on failure.
140  */
141  bool solveIK();
142 
143  /** \brief Set the DoF at \a index to \a value.
144  * \param[in] index Index of DoF to set.
145  * \param[in] value Value to set DoF.
146  */
147  void setDof(unsigned int index, double value);
148 
149  /** \} */
150 
151  /** \name Modifying Frames
152  \{ */
153 
154  /** \brief Get the joint names for this structure.
155  * \return The names of all the joints in this structure.
156  */
157  std::vector<std::string> getJointNames() const;
158 
159  /** \brief Get a reference to the joint in the structure.
160  * \param[in] joint_name Name of the joint to retrieve.
161  * \return The joint if it exists, nullptr otherwise.
162  */
163  dart::dynamics::Joint *getJoint(const std::string &joint_name) const;
164 
165  /** \brief Get a body node within the structure.
166  * \param[in] name Name of the node to retrieve.
167  * \return The body node if it exists, nullptr otherwise. If name is empty, returns root.
168  */
169  dart::dynamics::BodyNode *getFrame(const std::string &name = "") const;
170 
171  /** \brief Get the root frame of this structure.
172  * \return The root node.
173  */
174  dart::dynamics::BodyNode *getRootFrame() const;
175 
176  /** \brief Reparents the child node to the parent node.
177  * \param[in] child Child to reparent.
178  * \param[in] parent Name of new parent for the child frame.
179  */
180  void reparentFreeFrame(dart::dynamics::BodyNode *child, const std::string &parent = "");
181 
182  /** \brief Set the transform from a joint to its parent.
183  * \param[in] name Name of joint to set transform of.
184  * \param[in] tf Transform to set.
185  */
186  void setJointParentTransform(const std::string &name, const RobotPose &tf);
187 
188  /** \brief Update or add a collision object.
189  * \param[in] name Name of object to add.
190  * \param[in] geometry Geometry of object.
191  * \param[in] pose Pose of object to set.
192  */
193  void updateCollisionObject(const std::string &name, const GeometryPtr &geometry,
194  const robowflex::RobotPose &pose);
195 
196  /** \} */
197 
198  /** \name Constructing Frames
199  \{ */
200 
201  /** \brief Add a new frame attached to a revolute joint to this structure.
202  * \param[in] properties Joint properties to use for joint.
203  * \param[in] shape Shape to attach to frame.
204  * \param[in] parent Parent frame to attach new joint to.
205  * \return The created joint and body node.
206  */
208  addRevoluteFrame(const dart::dynamics::RevoluteJoint::Properties &properties, //
209  const dart::dynamics::ShapePtr &shape, //
210  dart::dynamics::BodyNode *parent = nullptr);
211 
212  /** \brief Add a new frame attached to a prismatic joint to this structure.
213  * \param[in] properties Joint properties to use for joint.
214  * \param[in] shape Shape to attach to frame.
215  * \param[in] parent Parent frame to attach new joint to.
216  * \return The created joint and body node.
217  */
219  addPrismaticFrame(const dart::dynamics::PrismaticJoint::Properties &properties, //
220  const dart::dynamics::ShapePtr &shape, //
221  dart::dynamics::BodyNode *parent = nullptr);
222 
223  /** \brief Add a new frame attached to a free joint to this structure.
224  * \param[in] properties Joint properties to use for joint.
225  * \param[in] shape Shape to attach to frame.
226  * \param[in] parent Parent frame to attach new joint to.
227  * \return The created joint and body node.
228  */
230  addFreeFrame(const dart::dynamics::FreeJoint::Properties &properties, //
231  const dart::dynamics::ShapePtr &shape, //
232  dart::dynamics::BodyNode *parent = nullptr);
233 
234  /** \brief Add a new frame attached to a fixed joint to this structure.
235  * \param[in] properties Joint properties to use for joint.
236  * \param[in] shape Shape to attach to frame.
237  * \param[in] parent Parent frame to attach new joint to.
238  * \return The created joint and body node.
239  */
241  addWeldedFrame(const dart::dynamics::WeldJoint::Properties &properties, //
242  const dart::dynamics::ShapePtr &shape, //
243  dart::dynamics::BodyNode *parent = nullptr);
244 
245  /** \brief Add a ground plane.
246  * \param[in] z Z- height of the plane.
247  * \param[in] radius X- and Y- width of the plane.
248  */
249  void addGround(double z = 0., double radius = 10.);
250 
251  /** \} */
252 
253  protected:
254  /** \brief Create a shape node on a body.
255  * \param[in,out] body Body to add shape node to.
256  * \param[in] shape Shape to add to body.
257  */
258  void createShapeNode(dart::dynamics::BodyNode *body, const dart::dynamics::ShapePtr &shape);
259 
260  const std::string name_{"robot"}; ///< Name of the structure.
261  dart::dynamics::SkeletonPtr skeleton_{nullptr}; ///< Underlying skeleton.
262  ACMPtr acm_; ///< ACM for structure.
263  };
264 
265  /** \brief Convert a robowflex::Geometry to a Dart Shape.
266  * \param[in] geometry Geometry to convert.
267  * \return Shape from geometry.
268  */
269  dart::dynamics::ShapePtr makeGeometry(const GeometryPtr &geometry);
270 
271  /** \brief Create a box.
272  * \param[in] v Dimensions of the box.
273  * \return The box shape.
274  */
275  std::shared_ptr<dart::dynamics::BoxShape> makeBox(const Eigen::Ref<const Eigen::Vector3d> &v);
276 
277  /** \brief Create a box.
278  * \param[in] x X dimension of box.
279  * \param[in] y Y dimension of box.
280  * \param[in] z Z dimension of box.
281  * \return The box shape.
282  */
283  std::shared_ptr<dart::dynamics::BoxShape> makeBox(double x, double y, double z);
284 
285  /** \brief Create a cylinder.
286  * \param[in] radius Radius of cylinder.
287  * \param[in] height Height of the cylinder.
288  * \return The cylinder shape.
289  */
290  std::shared_ptr<dart::dynamics::CylinderShape> makeCylinder(double radius, double height);
291 
292  /** \brief Create a sphere.
293  * \param[in] radius Radius of sphere.
294  * \return The sphere shape.
295  */
297 
298  /** \brief Create a mesh from a robowflex::Geometry that contains a mesh.
299  * \param[in] geometry Geometry with a mesh to convert.
300  * \return The mesh shape.
301  */
303 
304  /** \brief Create a circle's arcsector from one angle to another, with a specified radius.
305  * \param[in] low Lower bound, in radians.
306  * \param[in] high Upper bound, in radians.
307  * \param[in] inner_radius Inner segment radius.
308  * \param[in] outer_radius Outer segment radius.
309  * \param[in] resolution Number of segments.
310  * \return The mesh shape.
311  */
313  double inner_radius, double outer_radius,
314  std::size_t resolution = 32);
315 
316  /** \brief Sets the color of the shapes on a body node.
317  * \param[in] node Node to set color of.
318  * \param[in] color Color to set.
319  */
320  void setColor(dart::dynamics::BodyNode *node, const Eigen::Vector4d &color);
321  } // namespace darts
322 } // namespace robowflex
323 
324 #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::Geometry.
A const shared pointer wrapper for robowflex::Scene.
A shared pointer wrapper for robowflex::darts::ACM.
Allowable collision matrix for robowflex::darts::Structure.
Definition: acm.h:34
A shared pointer wrapper for robowflex::darts::Structure.
Definition: structure.h:34
Wrapper class for a dart::dynamics::Skeleton.
Definition: structure.h:53
ACMPtr acm_
ACM for structure.
Definition: structure.h:262
virtual ~Structure()=default
Destructor.
static const double DEFAULT_RESTITUTION
Definition: structure.h:33
static const double DEFAULT_DAMPING
Definition: structure.h:32
static const double DEFAULT_DENSITY
Definition: structure.h:31
std::shared_ptr< dart::dynamics::CylinderShape > makeCylinder(double radius, double height)
Create a cylinder.
Definition: structure.cpp:380
void setColor(dart::dynamics::BodyNode *node, const Eigen::Vector4d &color)
Sets the color of the shapes on a body node.
Definition: structure.cpp:550
std::shared_ptr< dart::dynamics::MeshShape > makeArcsegment(double low, double high, double inner_radius, double outer_radius, std::size_t resolution=32)
Create a circle's arcsector from one angle to another, with a specified radius.
Definition: structure.cpp:419
std::shared_ptr< dart::dynamics::SphereShape > makeSphere(double radius)
Create a sphere.
Definition: structure.cpp:385
dart::dynamics::ShapePtr makeGeometry(const GeometryPtr &geometry)
Convert a robowflex::Geometry to a Dart Shape.
Definition: structure.cpp:348
std::shared_ptr< dart::dynamics::MeshShape > makeMesh(const GeometryPtr &geometry)
Create a mesh from a robowflex::Geometry that contains a mesh.
Definition: structure.cpp:390
std::shared_ptr< dart::dynamics::BoxShape > makeBox(const Eigen::Ref< const Eigen::Vector3d > &v)
Create a box.
Definition: structure.cpp:370
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
Functions for loading and animating scenes in Blender.