planning_scene_interface.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2012, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Ioan Sucan, Sachin Chitta */
36 
39 #include <moveit_msgs/GetPlanningScene.h>
40 #include <moveit_msgs/ApplyPlanningScene.h>
41 #include <ros/ros.h>
42 #include <algorithm>
43 
44 namespace moveit
45 {
46 namespace planning_interface
47 {
48 static const std::string LOGNAME = "planning_scene_interface";
49 
51 {
52 public:
53  explicit PlanningSceneInterfaceImpl(const std::string& ns = "", bool wait = true)
54  {
56  planning_scene_diff_publisher_ = node_handle_.advertise<moveit_msgs::PlanningScene>("planning_scene", 1);
61 
62  if (wait)
63  {
64  waitForService(planning_scene_service_);
65  waitForService(apply_planning_scene_service_);
66  }
67  else
68  {
69  if (!planning_scene_service_.exists() || !apply_planning_scene_service_.exists())
70  {
71  throw std::runtime_error("ROS services not available");
72  }
73  }
74  }
75 
76  std::vector<std::string> getKnownObjectNames(bool with_type)
77  {
78  moveit_msgs::GetPlanningScene::Request request;
79  moveit_msgs::GetPlanningScene::Response response;
80  std::vector<std::string> result;
81  request.components.components = request.components.WORLD_OBJECT_NAMES;
82  if (!planning_scene_service_.call(request, response))
83  return result;
84  if (with_type)
85  {
86  for (std::size_t i = 0; i < response.scene.world.collision_objects.size(); ++i)
87  if (!response.scene.world.collision_objects[i].type.key.empty())
88  result.push_back(response.scene.world.collision_objects[i].id);
89  }
90  else
91  {
92  for (std::size_t i = 0; i < response.scene.world.collision_objects.size(); ++i)
93  result.push_back(response.scene.world.collision_objects[i].id);
94  }
95  return result;
96  }
97 
98  std::vector<std::string> getKnownObjectNamesInROI(double minx, double miny, double minz, double maxx, double maxy,
99  double maxz, bool with_type, std::vector<std::string>& types)
100  {
101  moveit_msgs::GetPlanningScene::Request request;
102  moveit_msgs::GetPlanningScene::Response response;
103  std::vector<std::string> result;
104  request.components.components = request.components.WORLD_OBJECT_GEOMETRY;
105  if (!planning_scene_service_.call(request, response))
106  {
107  ROS_WARN_NAMED(LOGNAME, "Could not call planning scene service to get object names");
108  return result;
109  }
110 
111  for (std::size_t i = 0; i < response.scene.world.collision_objects.size(); ++i)
112  {
113  if (with_type && response.scene.world.collision_objects[i].type.key.empty())
114  continue;
115  if (response.scene.world.collision_objects[i].mesh_poses.empty() &&
116  response.scene.world.collision_objects[i].primitive_poses.empty())
117  continue;
118  bool good = true;
119  for (std::size_t j = 0; j < response.scene.world.collision_objects[i].mesh_poses.size(); ++j)
120  if (!(response.scene.world.collision_objects[i].mesh_poses[j].position.x >= minx &&
121  response.scene.world.collision_objects[i].mesh_poses[j].position.x <= maxx &&
122  response.scene.world.collision_objects[i].mesh_poses[j].position.y >= miny &&
123  response.scene.world.collision_objects[i].mesh_poses[j].position.y <= maxy &&
124  response.scene.world.collision_objects[i].mesh_poses[j].position.z >= minz &&
125  response.scene.world.collision_objects[i].mesh_poses[j].position.z <= maxz))
126  {
127  good = false;
128  break;
129  }
130  for (std::size_t j = 0; j < response.scene.world.collision_objects[i].primitive_poses.size(); ++j)
131  if (!(response.scene.world.collision_objects[i].primitive_poses[j].position.x >= minx &&
132  response.scene.world.collision_objects[i].primitive_poses[j].position.x <= maxx &&
133  response.scene.world.collision_objects[i].primitive_poses[j].position.y >= miny &&
134  response.scene.world.collision_objects[i].primitive_poses[j].position.y <= maxy &&
135  response.scene.world.collision_objects[i].primitive_poses[j].position.z >= minz &&
136  response.scene.world.collision_objects[i].primitive_poses[j].position.z <= maxz))
137  {
138  good = false;
139  break;
140  }
141  if (good)
142  {
143  result.push_back(response.scene.world.collision_objects[i].id);
144  if (with_type)
145  types.push_back(response.scene.world.collision_objects[i].type.key);
146  }
147  }
148  return result;
149  }
150 
151  std::map<std::string, geometry_msgs::Pose> getObjectPoses(const std::vector<std::string>& object_ids)
152  {
153  moveit_msgs::GetPlanningScene::Request request;
154  moveit_msgs::GetPlanningScene::Response response;
155  std::map<std::string, geometry_msgs::Pose> result;
156  request.components.components = request.components.WORLD_OBJECT_GEOMETRY;
157  if (!planning_scene_service_.call(request, response))
158  {
159  ROS_WARN_NAMED(LOGNAME, "Could not call planning scene service to get object names");
160  return result;
161  }
162 
163  for (std::size_t i = 0; i < response.scene.world.collision_objects.size(); ++i)
164  {
165  if (std::find(object_ids.begin(), object_ids.end(), response.scene.world.collision_objects[i].id) !=
166  object_ids.end())
167  {
168  if (response.scene.world.collision_objects[i].mesh_poses.empty() &&
169  response.scene.world.collision_objects[i].primitive_poses.empty())
170  continue;
171  if (!response.scene.world.collision_objects[i].mesh_poses.empty())
172  result[response.scene.world.collision_objects[i].id] =
173  response.scene.world.collision_objects[i].mesh_poses[0];
174  else
175  result[response.scene.world.collision_objects[i].id] =
176  response.scene.world.collision_objects[i].primitive_poses[0];
177  }
178  }
179  return result;
180  }
181 
182  std::map<std::string, moveit_msgs::CollisionObject> getObjects(const std::vector<std::string>& object_ids)
183  {
184  moveit_msgs::GetPlanningScene::Request request;
185  moveit_msgs::GetPlanningScene::Response response;
186  std::map<std::string, moveit_msgs::CollisionObject> result;
187  request.components.components = request.components.WORLD_OBJECT_GEOMETRY;
188  if (!planning_scene_service_.call(request, response))
189  {
190  ROS_WARN_NAMED(LOGNAME, "Could not call planning scene service to get object geometries");
191  return result;
192  }
193 
194  for (std::size_t i = 0; i < response.scene.world.collision_objects.size(); ++i)
195  {
196  if (object_ids.empty() || std::find(object_ids.begin(), object_ids.end(),
197  response.scene.world.collision_objects[i].id) != object_ids.end())
198  {
199  result[response.scene.world.collision_objects[i].id] = response.scene.world.collision_objects[i];
200  }
201  }
202  return result;
203  }
204 
205  std::map<std::string, moveit_msgs::AttachedCollisionObject>
206  getAttachedObjects(const std::vector<std::string>& object_ids)
207  {
208  moveit_msgs::GetPlanningScene::Request request;
209  moveit_msgs::GetPlanningScene::Response response;
210  std::map<std::string, moveit_msgs::AttachedCollisionObject> result;
211  request.components.components = request.components.ROBOT_STATE_ATTACHED_OBJECTS;
212  if (!planning_scene_service_.call(request, response))
213  {
214  ROS_WARN_NAMED(LOGNAME, "Could not call planning scene service to get attached object geometries");
215  return result;
216  }
217 
218  for (std::size_t i = 0; i < response.scene.robot_state.attached_collision_objects.size(); ++i)
219  {
220  if (object_ids.empty() ||
221  std::find(object_ids.begin(), object_ids.end(),
222  response.scene.robot_state.attached_collision_objects[i].object.id) != object_ids.end())
223  {
224  result[response.scene.robot_state.attached_collision_objects[i].object.id] =
225  response.scene.robot_state.attached_collision_objects[i];
226  }
227  }
228  return result;
229  }
230 
231  bool applyPlanningScene(const moveit_msgs::PlanningScene& planning_scene)
232  {
233  moveit_msgs::ApplyPlanningScene::Request request;
234  moveit_msgs::ApplyPlanningScene::Response response;
235  request.scene = planning_scene;
236  if (!apply_planning_scene_service_.call(request, response))
237  {
238  ROS_WARN_NAMED(LOGNAME, "Failed to call ApplyPlanningScene service");
239  return false;
240  }
241  return response.success;
242  }
243 
244  void addCollisionObjects(const std::vector<moveit_msgs::CollisionObject>& collision_objects,
245  const std::vector<moveit_msgs::ObjectColor>& object_colors) const
246  {
247  moveit_msgs::PlanningScene planning_scene;
248  planning_scene.world.collision_objects = collision_objects;
249  planning_scene.object_colors = object_colors;
250 
251  for (size_t i = 0; i < planning_scene.object_colors.size(); ++i)
252  {
253  if (planning_scene.object_colors[i].id.empty() && i < collision_objects.size())
254  planning_scene.object_colors[i].id = collision_objects[i].id;
255  else
256  break;
257  }
258 
259  planning_scene.is_diff = true;
260  planning_scene_diff_publisher_.publish(planning_scene);
261  }
262 
263  void removeCollisionObjects(const std::vector<std::string>& object_ids) const
264  {
265  moveit_msgs::PlanningScene planning_scene;
266  moveit_msgs::CollisionObject object;
267  for (std::size_t i = 0; i < object_ids.size(); ++i)
268  {
269  object.id = object_ids[i];
270  object.operation = object.REMOVE;
271  planning_scene.world.collision_objects.push_back(object);
272  }
273  planning_scene.is_diff = true;
274  planning_scene_diff_publisher_.publish(planning_scene);
275  }
276 
277 private:
279  {
280  ros::Duration time_before_warning(5.0);
281  srv.waitForExistence(time_before_warning);
282  if (!srv.exists())
283  {
284  ROS_WARN_STREAM_NAMED(LOGNAME, "service '" << srv.getService() << "' not advertised yet. Continue waiting...");
285  srv.waitForExistence();
286  }
287  }
288 
293  robot_model::RobotModelConstPtr robot_model_;
294 };
295 
296 PlanningSceneInterface::PlanningSceneInterface(const std::string& ns, bool wait)
297 {
298  impl_ = new PlanningSceneInterfaceImpl(ns, wait);
299 }
300 
302 {
303  delete impl_;
304 }
305 
306 std::vector<std::string> PlanningSceneInterface::getKnownObjectNames(bool with_type)
307 {
308  return impl_->getKnownObjectNames(with_type);
309 }
310 
311 std::vector<std::string> PlanningSceneInterface::getKnownObjectNamesInROI(double minx, double miny, double minz,
312  double maxx, double maxy, double maxz,
313  bool with_type,
314  std::vector<std::string>& types)
315 {
316  return impl_->getKnownObjectNamesInROI(minx, miny, minz, maxx, maxy, maxz, with_type, types);
317 }
318 
319 std::map<std::string, geometry_msgs::Pose>
320 PlanningSceneInterface::getObjectPoses(const std::vector<std::string>& object_ids)
321 {
322  return impl_->getObjectPoses(object_ids);
323 }
324 
325 std::map<std::string, moveit_msgs::CollisionObject>
326 PlanningSceneInterface::getObjects(const std::vector<std::string>& object_ids)
327 {
328  return impl_->getObjects(object_ids);
329 }
330 
331 std::map<std::string, moveit_msgs::AttachedCollisionObject>
332 PlanningSceneInterface::getAttachedObjects(const std::vector<std::string>& object_ids)
333 {
334  return impl_->getAttachedObjects(object_ids);
335 }
336 
337 bool PlanningSceneInterface::applyCollisionObject(const moveit_msgs::CollisionObject& collision_object)
338 {
339  moveit_msgs::PlanningScene ps;
340  ps.robot_state.is_diff = true;
341  ps.is_diff = true;
342  ps.world.collision_objects.reserve(1);
343  ps.world.collision_objects.push_back(collision_object);
344  return applyPlanningScene(ps);
345 }
346 
347 bool PlanningSceneInterface::applyCollisionObject(const moveit_msgs::CollisionObject& collision_object,
348  const std_msgs::ColorRGBA& object_color)
349 {
350  moveit_msgs::PlanningScene ps;
351  ps.robot_state.is_diff = true;
352  ps.is_diff = true;
353  ps.world.collision_objects.reserve(1);
354  ps.world.collision_objects.push_back(collision_object);
355  moveit_msgs::ObjectColor oc;
356  oc.id = collision_object.id;
357  oc.color = object_color;
358  ps.object_colors.push_back(oc);
359  return applyPlanningScene(ps);
360 }
361 
362 bool PlanningSceneInterface::applyCollisionObjects(const std::vector<moveit_msgs::CollisionObject>& collision_objects,
363  const std::vector<moveit_msgs::ObjectColor>& object_colors)
364 {
365  moveit_msgs::PlanningScene ps;
366  ps.robot_state.is_diff = true;
367  ps.is_diff = true;
368  ps.world.collision_objects = collision_objects;
369  ps.object_colors = object_colors;
370 
371  for (size_t i = 0; i < ps.object_colors.size(); ++i)
372  {
373  if (ps.object_colors[i].id.empty() && i < collision_objects.size())
374  ps.object_colors[i].id = collision_objects[i].id;
375  else
376  break;
377  }
378 
379  return applyPlanningScene(ps);
380 }
381 
382 bool PlanningSceneInterface::applyAttachedCollisionObject(const moveit_msgs::AttachedCollisionObject& collision_object)
383 {
384  moveit_msgs::PlanningScene ps;
385  ps.robot_state.is_diff = true;
386  ps.is_diff = true;
387  ps.robot_state.attached_collision_objects.reserve(1);
388  ps.robot_state.attached_collision_objects.push_back(collision_object);
389  return applyPlanningScene(ps);
390 }
391 
393  const std::vector<moveit_msgs::AttachedCollisionObject>& collision_objects)
394 {
395  moveit_msgs::PlanningScene ps;
396  ps.robot_state.is_diff = true;
397  ps.is_diff = true;
398  ps.robot_state.attached_collision_objects = collision_objects;
399  return applyPlanningScene(ps);
400 }
401 
402 bool PlanningSceneInterface::applyPlanningScene(const moveit_msgs::PlanningScene& ps)
403 {
404  return impl_->applyPlanningScene(ps);
405 }
406 
407 void PlanningSceneInterface::addCollisionObjects(const std::vector<moveit_msgs::CollisionObject>& collision_objects,
408  const std::vector<moveit_msgs::ObjectColor>& object_colors) const
409 {
410  impl_->addCollisionObjects(collision_objects, object_colors);
411 }
412 
413 void PlanningSceneInterface::removeCollisionObjects(const std::vector<std::string>& object_ids) const
414 {
415  impl_->removeCollisionObjects(object_ids);
416 }
417 } // namespace planning_interface
418 } // namespace moveit
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
std::map< std::string, geometry_msgs::Pose > getObjectPoses(const std::vector< std::string > &object_ids)
bool applyCollisionObject(const moveit_msgs::CollisionObject &collision_object)
Apply collision object to the planning scene of the move_group node synchronously. Other PlanningSceneMonitors will NOT receive the update unless they subscribe to move_group&#39;s monitored scene.
static const std::string GET_PLANNING_SCENE_SERVICE_NAME
#define ROS_WARN_NAMED(name,...)
bool applyCollisionObjects(const std::vector< moveit_msgs::CollisionObject > &collision_objects, const std::vector< moveit_msgs::ObjectColor > &object_colors=std::vector< moveit_msgs::ObjectColor >())
Apply collision objects to the planning scene of the move_group node synchronously. Other PlanningSceneMonitors will NOT receive the update unless they subscribe to move_group&#39;s monitored scene. If object_colors do not specify an id, the corresponding object id from collision_objects is used.
void wait(int seconds)
bool call(MReq &req, MRes &res)
std::map< std::string, moveit_msgs::CollisionObject > getObjects(const std::vector< std::string > &object_ids=std::vector< std::string >())
Get the objects identified by the given object ids list. If no ids are provided, return all the known...
std::vector< std::string > getKnownObjectNamesInROI(double minx, double miny, double minz, double maxx, double maxy, double maxz, bool with_type, std::vector< std::string > &types)
static const std::string APPLY_PLANNING_SCENE_SERVICE_NAME
std::map< std::string, geometry_msgs::Pose > getObjectPoses(const std::vector< std::string > &object_ids)
Get the poses from the objects identified by the given object ids list.
void publish(const boost::shared_ptr< M > &message) const
bool applyAttachedCollisionObjects(const std::vector< moveit_msgs::AttachedCollisionObject > &attached_collision_objects)
Apply attached collision objects to the planning scene of the move_group node synchronously. Other PlanningSceneMonitors will NOT receive the update unless they subscribe to move_group&#39;s monitored scene.
bool waitForExistence(ros::Duration timeout=ros::Duration(-1))
bool applyPlanningScene(const moveit_msgs::PlanningScene &ps)
Update the planning_scene of the move_group node with the given ps synchronously. Other PlanningScene...
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
constexpr char LOGNAME[]
Definition: moveit_cpp.cpp:51
void removeCollisionObjects(const std::vector< std::string > &object_ids) const
Remove collision objects from the world via /planning_scene.
void removeCollisionObjects(const std::vector< std::string > &object_ids) const
std::map< std::string, moveit_msgs::AttachedCollisionObject > getAttachedObjects(const std::vector< std::string > &object_ids=std::vector< std::string >())
Get the attached objects identified by the given object ids list. If no ids are provided, return all the attached objects.
bool applyAttachedCollisionObject(const moveit_msgs::AttachedCollisionObject &attached_collision_object)
Apply attached collision object to the planning scene of the move_group node synchronously. Other PlanningSceneMonitors will NOT receive the update unless they subscribe to move_group&#39;s monitored scene.
void addCollisionObjects(const std::vector< moveit_msgs::CollisionObject > &collision_objects, const std::vector< moveit_msgs::ObjectColor > &object_colors) const
std::vector< std::string > getKnownObjectNames(bool with_type=false)
Get the names of all known objects in the world. If with_type is set to true, only return objects tha...
std::vector< std::string > getKnownObjectNamesInROI(double minx, double miny, double minz, double maxx, double maxy, double maxz, bool with_type, std::vector< std::string > &types)
Get the names of known objects in the world that are located within a bounding region (specified in t...
std::string getService()
PlanningSceneInterface(const std::string &ns="", bool wait=true)
std::map< std::string, moveit_msgs::AttachedCollisionObject > getAttachedObjects(const std::vector< std::string > &object_ids)
std::map< std::string, moveit_msgs::CollisionObject > getObjects(const std::vector< std::string > &object_ids)
const std::string response
void addCollisionObjects(const std::vector< moveit_msgs::CollisionObject > &collision_objects, const std::vector< moveit_msgs::ObjectColor > &object_colors=std::vector< moveit_msgs::ObjectColor >()) const
Add collision objects to the world via /planning_scene. Make sure object.operation is set to object...
#define ROS_WARN_STREAM_NAMED(name, args)


planning_interface
Author(s): Ioan Sucan
autogenerated on Tue Mar 2 2021 03:52:36