Skip to content

Commit ae73248

Browse files
committed
Implement issue moveit#630
1 parent 72d8edb commit ae73248

File tree

4 files changed

+130
-21
lines changed

4 files changed

+130
-21
lines changed

planning_interface/planning_scene_interface/include/moveit/planning_scene_interface/planning_scene_interface.h

+13-4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#define MOVEIT_PLANNING_INTERFACE_PLANNING_SCENE_INTERFACE_
3939

4040
#include <moveit/robot_state/robot_state.h>
41+
#include <moveit_msgs/ObjectColor.h>
4142
#include <moveit_msgs/CollisionObject.h>
4243
#include <moveit_msgs/AttachedCollisionObject.h>
4344

@@ -72,13 +73,21 @@ class PlanningSceneInterface
7273
return getKnownObjectNamesInROI(minx, miny, minz, maxx, maxy, maxz, with_type, empty_vector_string);
7374
};
7475

76+
/** \brief Get the poses from the objects identified by the given object ids list. */
7577
std::map<std::string, geometry_msgs::Pose> getObjectPoses(const std::vector<std::string> &object_ids);
7678

77-
/** \brief Add collision objects to the world
78-
Make sure object.operation is set to object.ADD*/
79-
void addCollisionObjects(const std::vector<moveit_msgs::CollisionObject> &collision_objects) const;
79+
/** \brief Get the objects identified by the given object ids list. If no ids are provided, return all the known objects. */
80+
std::map<std::string, moveit_msgs::CollisionObject> getObjects(const std::vector<std::string> &object_ids = std::vector<std::string>());
8081

81-
/** \brief Remove collision objects from the world*/
82+
/** \brief Get the attached objects identified by the given object ids list. If no ids are provided, return all the attached objects. */
83+
std::map<std::string, moveit_msgs::AttachedCollisionObject> getAttachedObjects(const std::vector<std::string> &object_ids = std::vector<std::string>());
84+
85+
/** \brief Add collision objects to the world.
86+
Make sure object.operation is set to object.ADD. */
87+
void addCollisionObjects(const std::vector<moveit_msgs::CollisionObject> &collision_objects,
88+
const std::vector<moveit_msgs::ObjectColor> &object_colors = std::vector<moveit_msgs::ObjectColor>()) const;
89+
90+
/** \brief Remove collision objects from the world */
8291
void removeCollisionObjects(const std::vector<std::string> &object_ids) const;
8392

8493
/**@}*/

planning_interface/planning_scene_interface/src/planning_scene_interface.cpp

+71-15
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <moveit/move_group/capability_names.h>
3939
#include <moveit_msgs/GetPlanningScene.h>
4040
#include <ros/ros.h>
41+
#include <algorithm>
4142

4243
namespace moveit
4344
{
@@ -140,29 +141,73 @@ class PlanningSceneInterface::PlanningSceneInterfaceImpl
140141
return result;
141142
}
142143

143-
for(std::size_t i=0; i < object_ids.size(); ++i)
144+
for (std::size_t i = 0; i < response.scene.world.collision_objects.size(); ++i)
144145
{
145-
for (std::size_t j=0; j < response.scene.world.collision_objects.size() ; ++j)
146+
if (std::find(object_ids.begin(), object_ids.end(), response.scene.world.collision_objects[i].id) != object_ids.end())
146147
{
147-
if(response.scene.world.collision_objects[j].id == object_ids[i])
148-
{
149-
if (response.scene.world.collision_objects[j].mesh_poses.empty() &&
150-
response.scene.world.collision_objects[j].primitive_poses.empty())
151-
continue;
152-
if(!response.scene.world.collision_objects[j].mesh_poses.empty())
153-
result[response.scene.world.collision_objects[j].id] = response.scene.world.collision_objects[j].mesh_poses[0];
154-
else
155-
result[response.scene.world.collision_objects[j].id] = response.scene.world.collision_objects[j].primitive_poses[0];
156-
}
148+
if (response.scene.world.collision_objects[i].mesh_poses.empty() &&
149+
response.scene.world.collision_objects[i].primitive_poses.empty())
150+
continue;
151+
if(!response.scene.world.collision_objects[i].mesh_poses.empty())
152+
result[response.scene.world.collision_objects[i].id] = response.scene.world.collision_objects[i].mesh_poses[0];
153+
else
154+
result[response.scene.world.collision_objects[i].id] = response.scene.world.collision_objects[i].primitive_poses[0];
155+
}
156+
}
157+
return result;
158+
}
159+
160+
std::map<std::string, moveit_msgs::CollisionObject> getObjects(const std::vector<std::string> &object_ids)
161+
{
162+
moveit_msgs::GetPlanningScene::Request request;
163+
moveit_msgs::GetPlanningScene::Response response;
164+
std::map<std::string, moveit_msgs::CollisionObject> result;
165+
request.components.components = request.components.WORLD_OBJECT_GEOMETRY;
166+
if (!planning_scene_service_.call(request, response))
167+
{
168+
ROS_WARN("Could not call planning scene service to get object geometries");
169+
return result;
170+
}
171+
172+
for (std::size_t i = 0; i < response.scene.world.collision_objects.size(); ++i)
173+
{
174+
if (object_ids.empty() || std::find(object_ids.begin(), object_ids.end(), response.scene.world.collision_objects[i].id) != object_ids.end())
175+
{
176+
result[response.scene.world.collision_objects[i].id] = response.scene.world.collision_objects[i];
177+
}
178+
}
179+
return result;
180+
}
181+
182+
std::map<std::string, moveit_msgs::AttachedCollisionObject> getAttachedObjects(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::AttachedCollisionObject> result;
187+
request.components.components = request.components.ROBOT_STATE_ATTACHED_OBJECTS;
188+
if (!planning_scene_service_.call(request, response))
189+
{
190+
ROS_WARN("Could not call planning scene service to get attached object geometries");
191+
return result;
192+
}
193+
194+
for (std::size_t i = 0; i < response.scene.robot_state.attached_collision_objects.size(); ++i)
195+
{
196+
if (object_ids.empty() ||
197+
std::find(object_ids.begin(), object_ids.end(), response.scene.robot_state.attached_collision_objects[i].object.id) != object_ids.end())
198+
{
199+
result[response.scene.robot_state.attached_collision_objects[i].object.id] = response.scene.robot_state.attached_collision_objects[i];
157200
}
158201
}
159202
return result;
160203
}
161204

162-
void addCollisionObjects(const std::vector<moveit_msgs::CollisionObject> &collision_objects) const
205+
void addCollisionObjects(const std::vector<moveit_msgs::CollisionObject> &collision_objects,
206+
const std::vector<moveit_msgs::ObjectColor> &object_colors) const
163207
{
164208
moveit_msgs::PlanningScene planning_scene;
165209
planning_scene.world.collision_objects = collision_objects;
210+
planning_scene.object_colors = object_colors;
166211
planning_scene.is_diff = true;
167212
planning_scene_diff_publisher_.publish(planning_scene);
168213
}
@@ -214,9 +259,20 @@ std::map<std::string, geometry_msgs::Pose> PlanningSceneInterface::getObjectPose
214259
return impl_->getObjectPoses(object_ids);
215260
}
216261

217-
void PlanningSceneInterface::addCollisionObjects(const std::vector<moveit_msgs::CollisionObject> &collision_objects) const
262+
std::map<std::string, moveit_msgs::CollisionObject> PlanningSceneInterface::getObjects(const std::vector<std::string> &object_ids)
263+
{
264+
return impl_->getObjects(object_ids);
265+
}
266+
267+
std::map<std::string, moveit_msgs::AttachedCollisionObject> PlanningSceneInterface::getAttachedObjects(const std::vector<std::string> &object_ids)
268+
{
269+
return impl_->getAttachedObjects(object_ids);
270+
}
271+
272+
void PlanningSceneInterface::addCollisionObjects(const std::vector<moveit_msgs::CollisionObject> &collision_objects,
273+
const std::vector<moveit_msgs::ObjectColor> &object_colors) const
218274
{
219-
return impl_->addCollisionObjects(collision_objects);
275+
return impl_->addCollisionObjects(collision_objects, object_colors);
220276
}
221277

222278
void PlanningSceneInterface::removeCollisionObjects(const std::vector<std::string> &object_ids) const

planning_interface/planning_scene_interface/src/wrap_python_planning_scene_interface.cpp

+36-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <moveit/planning_scene_interface/planning_scene_interface.h>
3838
#include <moveit/py_bindings_tools/roscpp_initializer.h>
3939
#include <moveit/py_bindings_tools/py_conversions.h>
40+
#include <moveit/py_bindings_tools/serialize_msg.h>
4041

4142
#include <boost/function.hpp>
4243
#include <boost/python.hpp>
@@ -64,12 +65,42 @@ class PlanningSceneInterfaceWrapper : protected py_bindings_tools::ROScppInitial
6465

6566
bp::list getKnownObjectNamesPython(bool with_type = false)
6667
{
67-
return moveit::py_bindings_tools::listFromString(getKnownObjectNames(with_type));
68+
return py_bindings_tools::listFromString(getKnownObjectNames(with_type));
6869
}
6970

7071
bp::list getKnownObjectNamesInROIPython(double minx, double miny, double minz, double maxx, double maxy, double maxz, bool with_type = false)
7172
{
72-
return moveit::py_bindings_tools::listFromString(getKnownObjectNamesInROI(minx, miny, minz, maxx, maxy, maxz, with_type));
73+
return py_bindings_tools::listFromString(getKnownObjectNamesInROI(minx, miny, minz, maxx, maxy, maxz, with_type));
74+
}
75+
76+
bp::dict getObjectPosesPython(bp::list object_ids)
77+
{
78+
std::map<std::string, geometry_msgs::Pose> ops = getObjectPoses(py_bindings_tools::stringFromList(object_ids));
79+
std::map<std::string, std::string> ser_ops;
80+
for (std::map<std::string, geometry_msgs::Pose>::const_iterator it = ops.begin(); it != ops.end(); ++it)
81+
ser_ops[it->first] = py_bindings_tools::serializeMsg(it->second);
82+
83+
return py_bindings_tools::dictFromType(ser_ops);
84+
}
85+
86+
bp::dict getObjectsPython(bp::list object_ids)
87+
{
88+
std::map<std::string, moveit_msgs::CollisionObject> objs = getObjects(py_bindings_tools::stringFromList(object_ids));
89+
std::map<std::string, std::string> ser_objs;
90+
for (std::map<std::string, moveit_msgs::CollisionObject>::const_iterator it = objs.begin(); it != objs.end(); ++it)
91+
ser_objs[it->first] = py_bindings_tools::serializeMsg(it->second);
92+
93+
return py_bindings_tools::dictFromType(ser_objs);
94+
}
95+
96+
bp::dict getAttachedObjectsPython(const bp::list &object_ids)
97+
{
98+
std::map<std::string, moveit_msgs::AttachedCollisionObject> aobjs = getAttachedObjects(py_bindings_tools::stringFromList(object_ids));
99+
std::map<std::string, std::string> ser_aobjs;
100+
for (std::map<std::string, moveit_msgs::AttachedCollisionObject>::const_iterator it = aobjs.begin(); it != aobjs.end(); ++it)
101+
ser_aobjs[it->first] = py_bindings_tools::serializeMsg(it->second);
102+
103+
return py_bindings_tools::dictFromType(ser_aobjs);
73104
}
74105

75106
};
@@ -80,6 +111,9 @@ static void wrap_planning_scene_interface()
80111

81112
PlanningSceneClass.def("get_known_object_names", &PlanningSceneInterfaceWrapper::getKnownObjectNamesPython);
82113
PlanningSceneClass.def("get_known_object_names_in_roi", &PlanningSceneInterfaceWrapper::getKnownObjectNamesInROIPython);
114+
PlanningSceneClass.def("get_object_poses", &PlanningSceneInterfaceWrapper::getObjectPosesPython);
115+
PlanningSceneClass.def("get_objects", &PlanningSceneInterfaceWrapper::getObjectsPython);
116+
PlanningSceneClass.def("get_attached_objects", &PlanningSceneInterfaceWrapper::getAttachedObjectsPython);
83117
}
84118

85119
}

planning_interface/py_bindings_tools/include/moveit/py_bindings_tools/py_conversions.h

+10
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <boost/python.hpp>
4141
#include <string>
4242
#include <vector>
43+
#include <map>
4344

4445
namespace moveit
4546
{
@@ -65,6 +66,15 @@ boost::python::list listFromType(const std::vector<T>& v)
6566
return l;
6667
}
6768

69+
template<typename T>
70+
boost::python::dict dictFromType(const std::map<std::string, T>& v)
71+
{
72+
boost::python::dict d;
73+
for (typename std::map<std::string, T>::const_iterator it = v.begin(); it != v.end(); ++it)
74+
d[it->first] = it->second;
75+
return d;
76+
}
77+
6878
std::vector<double> doubleFromList(const boost::python::list &values)
6979
{
7080
return typeFromList<double>(values);

0 commit comments

Comments
 (0)