Robowflex  v0.1
Making MoveIt Easy
scene.h
Go to the documentation of this file.
1 /* Author: Zachary Kingston */
2 
3 #ifndef ROBOWFLEX_SCENE_
4 #define ROBOWFLEX_SCENE_
5 
6 #include <string>
7 
8 #include <Eigen/Core>
9 #include <Eigen/Geometry>
10 
11 #include <moveit_msgs/PlanningScene.h>
12 
13 #include <moveit/collision_detection/collision_matrix.h> // for collision_detection::AllowedCollisionMatrix
14 #include <moveit/planning_scene/planning_scene.h> // for planning_scene::PlanningScene
15 #include <moveit/robot_state/robot_state.h> // for robot_state::RobotState
16 
19 #include <robowflex_library/id.h>
20 
21 namespace robowflex
22 {
23  /** \cond IGNORE */
25  ROBOWFLEX_CLASS_FORWARD(Geometry);
26  /** \endcond */
27 
28  /** \cond IGNORE */
30  /** \endcond */
31 
32  /** \class robowflex::ScenePtr
33  \brief A shared pointer wrapper for robowflex::Scene. */
34 
35  /** \class robowflex::SceneConstPtr
36  \brief A const shared pointer wrapper for robowflex::Scene. */
37 
38  /** \brief Wrapper class around the planning scene and collision geometry.
39  *
40  * The Scene class is a wrapper around _MoveIt!_'s planning_scene::PlanningScene, providing access to set
41  * and manipulate collision objects, attach and detach objects to the robot, and so on. There are also
42  * utilities to load and save planning scenes from YAML files (toYAMLFile() and fromYAMLFile()).
43  * Note that this class has *its own* robot state, separate from the one in the provided Robot.
44  * Information between this state and the Robot's scratch state are not synchronized, you must do this
45  * manually.
46  */
47  class Scene : public ID
48  {
49  public:
50  /** \brief Constructor.
51  * \param[in] robot Robot to construct planning scene for.
52  */
53  Scene(const RobotConstPtr &robot);
54 
55  /** \brief Constructor.
56  * \param[in] robot Robot to construct planning scene for.
57  */
58  Scene(const robot_model::RobotModelConstPtr &robot);
59 
60  /** \brief Copy Constructor.
61  * \param[in] scene Scene to copy.
62  */
63  Scene(const Scene &scene);
64 
65  /** \brief Assignment Copy Constructor.
66  * \param[in] scene Scene to copy.
67  */
68  void operator=(const Scene &scene);
69 
70  /** \brief Deep Copy.
71  * \return The deep copied planning scene.
72  */
73  ScenePtr deepCopy() const;
74 
75  /** \name Getters and Setters
76  \{ */
77 
78  /** \brief Get a const reference to the planning scene.
79  * \return The planning scene.
80  */
81  const planning_scene::PlanningScenePtr &getSceneConst() const;
82 
83  /** \brief Get a reference to the planning scene.
84  * \return The planning scene.
85  */
86  planning_scene::PlanningScenePtr &getScene();
87 
88  /** \brief Get the message that describes the current planning scene.
89  * \return The planning scene message.
90  */
91  moveit_msgs::PlanningScene getMessage() const;
92 
93  /** \brief Get a reference to the current robot state in the planning scene.
94  * \return The planning scene robot.
95  */
96  robot_state::RobotState &getCurrentState();
97 
98  /** \brief Get a constant reference to the current robot state in the planning scene.
99  * \return The planning scene robot.
100  */
101  const robot_state::RobotState &getCurrentStateConst() const;
102 
103  /** \brief Get the current allowed collision matrix of the planning scene.
104  * \return The allowed collision matrix.
105  */
107 
108  /** \brief Get the current allowed collision matrix of the planning scene.
109  * \return The allowed collision matrix.
110  */
112 
113  /** \brief Set the planning scene to be the same as a message.
114  * \param[in] msg Message to use to set planning scene.
115  * \param[in] diff If true, uses the message as a diff.
116  */
117  void useMessage(const moveit_msgs::PlanningScene &msg, bool diff = false);
118 
119  /** \} */
120 
121  /** \name Collision Object Management
122  \{ */
123 
124  /** \brief Adds or updates collision object in the planning scene.
125  * If the geometry reference is the same, the collision object is updated. Otherwise, the old object
126  * named \a name is deleted and a new one is created.
127  * \param[in] name Name of object to add or update.
128  * \param[in] geometry Geometry of object.
129  * \param[in] pose Pose of object.
130  */
131  void updateCollisionObject(const std::string &name, const GeometryConstPtr &geometry,
132  const RobotPose &pose);
133 
134  /** \brief Returns true if the object \e name is in the scene.
135  * \param[in] name Name of the object to look for.
136  * \return True if object is in scene, false otherwise.
137  */
138  bool hasObject(const std::string &name) const;
139 
140  /** \brief Returns a list of all the names of collision objects in the scene.
141  * \return A list of all the collision objects in the scene.
142  */
144 
145  /** \brief Returns a representation of a collision object in the scene as a Geometry.
146  * If the object has multiple geometries, returns the first.
147  * \param[in] name Name of the object to extract.
148  * \return A representation of the collision object as a Geometry.
149  */
150  GeometryPtr getObjectGeometry(const std::string &name) const;
151 
152  /** \brief Removes an object from the planning scene.
153  * \param[in] name Name of object to remove.
154  */
155  void removeCollisionObject(const std::string &name);
156 
157  /** \brief Get the current pose of a collision object.
158  * If the object has multiple geometries, returns the pose of the first.
159  * \param[in] name Name of object to get pose for.
160  * \return Pose of the object.
161  */
162  RobotPose getObjectPose(const std::string &name) const;
163 
164  /** \brief Get a pose in the global frame relative to a collision object for grasping.
165  * \param[in] name Name of object to get pose for.
166  * \param[in] offset The offset of the grasp in the object's frame.
167  * \return Pose of the grasp offset from the object.
168  */
169  RobotPose getObjectGraspPose(const std::string &name, const RobotPose &offset) const;
170 
171  /** \brief Move all objects according to a transform specified in world frame.
172  * \param[in] transform The transform to move objects in world frame.
173  * \return True on success.
174  */
175  bool moveAllObjectsGlobal(const RobotPose &transform);
176 
177  /** \brief Move all shapes in an object according to the given transform specified in world frame.
178  * \param[in] name Name of the object to move.
179  * \param[in] transform The transform to move the object in world frame.
180  * \return True on success.
181  */
182  bool moveObjectGlobal(const std::string &name, const RobotPose &transform);
183 
184  /** \brief Move all shapes in an object according to the given transform specified in object frame.
185  * If the object has multiple shapes, the first one is considered the local frame.
186  * \param[in] name Name of the object to move.
187  * \param[in] transform The transform to move the object in object frame.
188  * \return True on success.
189  */
190  bool moveObjectLocal(const std::string &name, const RobotPose &transform);
191 
192  /** \brief Get the pose of a particular frame in the scene.
193  * Example, use this to get the pose from /world to /base_link.
194  * \param[in] id The ID of the frame to look for.
195  * \return Pose of the object, Identity if frame is not present.
196  */
197  RobotPose getFramePose(const std::string &id) const;
198 
199  /** \brief Attempts to set the collision detector plugin used by the scene to \a name.
200  * In MoveIt by default, 'Hybrid' is the only plugin defined.
201  * However, you can define your own libraries that contain classes that extend the
202  * collision_detection::CollisionPlugin class, and load them here as well.
203  * See http://moveit.ros.org/documentation/plugins/#collisionplugin for more details.
204  * \param[in] detector_name Name of the collision detection plugin.
205  * \return True if sucessful, false otherwise.
206  */
207  bool setCollisionDetector(const std::string &detector_name) const;
208 
209  /** \brief Attach the named collision object \a name to the default end-effector of the given robot \a
210  * state. Only works if there is one end-effector in the system. Uses all end-effector links as
211  * allowed touch links.
212  * \param[in] name Name of collision to attach.
213  * \param[in] state State of robot the object will be attached to
214  * \return True on success, false on failure.
215  */
216  bool attachObjectToState(robot_state::RobotState &state, const std::string &name) const;
217 
218  /** \brief Attach the named collision object \a name to the link \a ee_link of the given robot \a
219  * state
220  * \param[in] state State of the robot to attach.
221  * \param[in] name Name of object to attach.
222  * \param[in] ee_link Link to attach object to.
223  * \param[in] touch_links Links the object is allowed to touch.
224  * \return True on success, false on failure.
225  */
226  bool attachObjectToState(robot_state::RobotState &state, const std::string &name,
227  const std::string &ee_link,
228  const std::vector<std::string> &touch_links) const;
229 
230  /** \brief Helper function that attaches object to internal state and removes from scene.
231  * \param[in] name Name of object to attach to robot and remove from scene.
232  * \return True on success, false on failure.
233  */
234  bool attachObject(const std::string &name);
235 
236  /** \brief Helper function that attaches object provided state and removes from scene.
237  * \param[in] name Name of object to attach to robot and remove from scene.
238  * \return True on success, false on failure.
239  */
240  bool attachObject(robot_state::RobotState &state, const std::string &name);
241 
242  /** \brief Helper function that attaches object provided state and removes from scene.
243  * \param[in] state State of the robot to attach to.
244  * \param[in] name Name of object to attach to.
245  * \param[in] ee_link Link to attach object to.
246  * \param[in] touch_links Links the object is allowed to touch.
247  * \return True on success, false on failure.
248  */
249  bool attachObject(robot_state::RobotState &state, const std::string &name, const std::string &ee_link,
250  const std::vector<std::string> &touch_links);
251 
252  /** \brief Helper function that detaches the object from the internal scene state.
253  * \param[in] name Name of collision to detach.
254  * \return True on success, false on failure.
255  */
256  bool detachObject(const std::string &name);
257 
258  /** \brief Detach an object \a name from the robot state.
259  * \param[in] state State to detatch the object from.
260  * \param[in] name Name of collision to detach.
261  * \return True on success, false on failure.
262  */
263  bool detachObject(robot_state::RobotState &state, const std::string &name);
264 
265  /** \} */
266 
267  /** \name Checking Collisions
268  \{ */
269 
270  /** \brief Check if a robot state is in collision.
271  * \param[in] state State to check for collision.
272  * \param[in] request Optional request parameters for collision checking.
273  * \return The collision result.
274  */
276  checkCollision(const robot_state::RobotState &state,
277  const collision_detection::CollisionRequest &request = {}) const;
278 
279  /** \brief Get the distance to collision for a robot state.
280  * \param[in] state State to get distance to collision for.
281  * \return The distance of the state to collision.
282  */
283  double distanceToCollision(const robot_state::RobotState &state) const;
284 
285  /** \brief Get the distance to collision to a specific object.
286  * \param[in] state State of the robot.
287  * \param[in] object Object to check against.
288  * \return The distance to collision to the object. On error, returns NaN.
289  */
290  double distanceToObject(const robot_state::RobotState &state, const std::string &object) const;
291 
292  /** \brief Get the distance to collision between two collision objects in the scene.
293  * \param[in] one One of the objects to check.
294  * \param[in] two The other object to check.
295  * \return The distance between the objects. On error, returns NaN.
296  */
297  double distanceBetweenObjects(const std::string &one, const std::string &two) const;
298 
299  /** \brief Get the distance to collision using an ACM.
300  * \param[in] state State of the robot.
301  * \param[in] one acm ACM to use for distance check.
302  * \return The distance between all colliding entries in ACM. On error, returns NaN.
303  */
304  double distanceACM(const robot_state::RobotState &state,
306 
307  /** \brief Disables collision between all entries in the ACM (all robot links and objects in the
308  * scene)
309  * \param[in,out] acm ACM to clear.
310  */
312 
313  /** \brief Get the group state validity callback function that uses this scene.
314  * \param[in] verbose If true, will have verbose collision output.
315  * \return The group state validity function. This function will return true if there is no
316  * collision, and false otherwise.
317  */
319 
320  /** \} */
321 
322  /** \name IO
323  \{ */
324 
325  /** \brief Serialize the current planning scene to a YAML file.
326  * \param[in] file File to serialize planning scene to.
327  * \return True on success, false on failure.
328  */
329  bool toYAMLFile(const std::string &file) const;
330 
331  /** \brief Load a planning scene from a YAML file.
332  * \param[in] file File to load planning scene from.
333  * \return True on success, false on failure.
334  */
335  bool fromYAMLFile(const std::string &file);
336  bool fromOpenRAVEXMLFile(const std::string &file, std::string models_dir = "");
337 
338  /** \} */
339 
340  private:
341  /** \cond IGNORE */
343  /** \endcond */
344 
345  /** \brief Corrects frame mismatches on loaded scenes by using the current root frame instead.
346  * \param[in,out] msg Message to correct frame names for.
347  */
348  void fixCollisionObjectFrame(moveit_msgs::PlanningScene &msg);
349 
350  CollisionPluginLoaderPtr loader_; ///< Plugin loader that sets collision detectors for the scene.
351  planning_scene::PlanningScenePtr scene_; ///< Underlying planning scene.
352  };
353 } // namespace robowflex
354 
355 #endif
#define ROBOWFLEX_CLASS_FORWARD(C)
Macro that forward declares a class and defines two shared ptrs types:
Definition: class_forward.h:16
A const shared pointer wrapper for robowflex::Geometry.
A shared pointer wrapper for robowflex::Geometry.
Adds functionality to uniquely ID a specific class as well as the "version" of that class,...
Definition: id.h:27
A const shared pointer wrapper for robowflex::Robot.
A shared pointer wrapper for robowflex::Scene.
The actual plugin loader for collision plugins. Heavily inspired by code in moveit_ros/planning.
Definition: scene.cpp:29
Wrapper class around the planning scene and collision geometry.
Definition: scene.h:48
double distanceToCollision(const robot_state::RobotState &state) const
Get the distance to collision for a robot state.
Definition: scene.cpp:475
bool moveObjectGlobal(const std::string &name, const RobotPose &transform)
Move all shapes in an object according to the given transform specified in world frame.
Definition: scene.cpp:307
void clearACM(collision_detection::AllowedCollisionMatrix &acm) const
Disables collision between all entries in the ACM (all robot links and objects in the scene)
Definition: scene.cpp:496
planning_scene::PlanningScenePtr scene_
Underlying planning scene.
Definition: scene.h:351
CollisionPluginLoaderPtr loader_
Plugin loader that sets collision detectors for the scene.
Definition: scene.h:350
ScenePtr deepCopy() const
Deep Copy.
Definition: scene.cpp:165
void useMessage(const moveit_msgs::PlanningScene &msg, bool diff=false)
Set the planning scene to be the same as a message.
Definition: scene.cpp:213
double distanceACM(const robot_state::RobotState &state, const collision_detection::AllowedCollisionMatrix &acm) const
Get the distance to collision using an ACM.
Definition: scene.cpp:480
moveit::core::GroupStateValidityCallbackFn getGSVCF(bool verbose) const
Get the group state validity callback function that uses this scene.
Definition: scene.cpp:563
RobotPose getObjectPose(const std::string &name) const
Get the current pose of a collision object. If the object has multiple geometries,...
Definition: scene.cpp:270
void fixCollisionObjectFrame(moveit_msgs::PlanningScene &msg)
Corrects frame mismatches on loaded scenes by using the current root frame instead.
Definition: scene.cpp:223
double distanceToObject(const robot_state::RobotState &state, const std::string &object) const
Get the distance to collision to a specific object.
Definition: scene.cpp:517
bool fromYAMLFile(const std::string &file)
Load a planning scene from a YAML file.
Definition: scene.cpp:589
bool detachObject(const std::string &name)
Helper function that detaches the object from the internal scene state.
Definition: scene.cpp:437
void removeCollisionObject(const std::string &name)
Removes an object from the planning scene.
Definition: scene.cpp:265
bool attachObjectToState(robot_state::RobotState &state, const std::string &name) const
Attach the named collision object name to the default end-effector of the given robot state....
Definition: scene.cpp:354
moveit_msgs::PlanningScene getMessage() const
Get the message that describes the current planning scene.
Definition: scene.cpp:184
collision_detection::AllowedCollisionMatrix & getACM()
Get the current allowed collision matrix of the planning scene.
Definition: scene.cpp:202
std::vector< std::string > getCollisionObjects() const
Returns a list of all the names of collision objects in the scene.
Definition: scene.cpp:247
bool toYAMLFile(const std::string &file) const
Serialize the current planning scene to a YAML file.
Definition: scene.cpp:580
const planning_scene::PlanningScenePtr & getSceneConst() const
Get a const reference to the planning scene.
Definition: scene.cpp:173
RobotPose getFramePose(const std::string &id) const
Get the pose of a particular frame in the scene. Example, use this to get the pose from /world to /ba...
Definition: scene.cpp:333
bool hasObject(const std::string &name) const
Returns true if the object name is in the scene.
Definition: scene.cpp:431
planning_scene::PlanningScenePtr & getScene()
Get a reference to the planning scene.
Definition: scene.cpp:178
robot_state::RobotState & getCurrentState()
Get a reference to the current robot state in the planning scene.
Definition: scene.cpp:191
GeometryPtr getObjectGeometry(const std::string &name) const
Returns a representation of a collision object in the scene as a Geometry. If the object has multiple...
Definition: scene.cpp:253
void operator=(const Scene &scene)
Assignment Copy Constructor.
Definition: scene.cpp:159
Scene(const RobotConstPtr &robot)
Constructor.
Definition: scene.cpp:145
double distanceBetweenObjects(const std::string &one, const std::string &two) const
Get the distance to collision between two collision objects in the scene.
Definition: scene.cpp:535
const collision_detection::AllowedCollisionMatrix & getACMConst() const
Get the current allowed collision matrix of the planning scene.
Definition: scene.cpp:208
RobotPose getObjectGraspPose(const std::string &name, const RobotPose &offset) const
Get a pose in the global frame relative to a collision object for grasping.
Definition: scene.cpp:286
bool attachObject(const std::string &name)
Helper function that attaches object to internal state and removes from scene.
Definition: scene.cpp:403
const robot_state::RobotState & getCurrentStateConst() const
Get a constant reference to the current robot state in the planning scene.
Definition: scene.cpp:197
bool setCollisionDetector(const std::string &detector_name) const
Attempts to set the collision detector plugin used by the scene to name. In MoveIt by default,...
Definition: scene.cpp:341
collision_detection::CollisionResult checkCollision(const robot_state::RobotState &state, const collision_detection::CollisionRequest &request={}) const
Check if a robot state is in collision.
Definition: scene.cpp:466
bool fromOpenRAVEXMLFile(const std::string &file, std::string models_dir="")
Definition: scene.cpp:611
void updateCollisionObject(const std::string &name, const GeometryConstPtr &geometry, const RobotPose &pose)
Adds or updates collision object in the planning scene. If the geometry reference is the same,...
Definition: scene.cpp:230
bool moveAllObjectsGlobal(const RobotPose &transform)
Move all objects according to a transform specified in world frame.
Definition: scene.cpp:294
bool moveObjectLocal(const std::string &name, const RobotPose &transform)
Move all shapes in an object according to the given transform specified in object frame....
Definition: scene.cpp:322
boost::function< bool(RobotState *robot_state, const JointModelGroup *joint_group, const double *joint_group_variable_values)> GroupStateValidityCallbackFn
Functions for loading and animating robots in Blender.
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.