moveit2
The MoveIt Motion Planning Framework for ROS 2.
wrap_python_planning_scene_interface.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2013, 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 */
36 
41 
42 #include <boost/python.hpp>
43 #include <Python.h>
44 
47 namespace bp = boost::python;
48 
49 namespace moveit
50 {
51 namespace planning_interface
52 {
53 class PlanningSceneInterfaceWrapper : protected py_bindings_tools::ROScppInitializer, public PlanningSceneInterface
54 {
55 public:
56  // ROSInitializer is constructed first, and ensures ros::init() was called, if needed
57  PlanningSceneInterfaceWrapper(const std::string& ns = "")
58  : py_bindings_tools::ROScppInitializer(), PlanningSceneInterface(ns)
59  {
60  }
61 
62  bp::list getKnownObjectNamesPython(bool with_type = false)
63  {
64  return py_bindings_tools::listFromString(getKnownObjectNames(with_type));
65  }
66 
67  bp::list getKnownObjectNamesInROIPython(double minx, double miny, double minz, double maxx, double maxy, double maxz,
68  bool with_type = false)
69  {
70  return py_bindings_tools::listFromString(getKnownObjectNamesInROI(minx, miny, minz, maxx, maxy, maxz, with_type));
71  }
72 
73  bp::dict getObjectPosesPython(const bp::list& object_ids)
74  {
75  std::map<std::string, geometry_msgs::Pose> ops = getObjectPoses(py_bindings_tools::stringFromList(object_ids));
76  std::map<std::string, py_bindings_tools::ByteString> ser_ops;
77  for (std::map<std::string, geometry_msgs::Pose>::const_iterator it = ops.begin(); it != ops.end(); ++it)
78  ser_ops[it->first] = py_bindings_tools::serializeMsg(it->second);
79 
80  return py_bindings_tools::dictFromType(ser_ops);
81  }
82 
83  bp::dict getObjectsPython(const bp::list& object_ids)
84  {
85  std::map<std::string, moveit_msgs::msg::CollisionObject> objs =
86  getObjects(py_bindings_tools::stringFromList(object_ids));
87  std::map<std::string, py_bindings_tools::ByteString> ser_objs;
88  for (std::map<std::string, moveit_msgs::msg::CollisionObject>::const_iterator it = objs.begin(); it != objs.end();
89  ++it)
90  ser_objs[it->first] = py_bindings_tools::serializeMsg(it->second);
91 
92  return py_bindings_tools::dictFromType(ser_objs);
93  }
94 
95  bp::dict getAttachedObjectsPython(const bp::list& object_ids)
96  {
97  std::map<std::string, moveit_msgs::msg::AttachedCollisionObject> aobjs =
98  getAttachedObjects(py_bindings_tools::stringFromList(object_ids));
99  std::map<std::string, py_bindings_tools::ByteString> ser_aobjs;
100  for (std::map<std::string, moveit_msgs::msg::AttachedCollisionObject>::const_iterator it = aobjs.begin();
101  it != aobjs.end(); ++it)
102  ser_aobjs[it->first] = py_bindings_tools::serializeMsg(it->second);
103 
104  return py_bindings_tools::dictFromType(ser_aobjs);
105  }
106 
107  bool applyPlanningScenePython(const py_bindings_tools::ByteString& ps_str)
108  {
109  moveit_msgs::msg::PlanningScene ps_msg;
110  py_bindings_tools::deserializeMsg(ps_str, ps_msg);
111  return applyPlanningScene(ps_msg);
112  }
113 };
114 
115 static void wrap_planning_scene_interface()
116 {
117  bp::class_<PlanningSceneInterfaceWrapper> planning_scene_class("PlanningSceneInterface",
118  bp::init<bp::optional<std::string>>());
119 
120  planning_scene_class.def("get_known_object_names", &PlanningSceneInterfaceWrapper::getKnownObjectNamesPython);
121  planning_scene_class.def("get_known_object_names_in_roi",
122  &PlanningSceneInterfaceWrapper::getKnownObjectNamesInROIPython);
123  planning_scene_class.def("get_object_poses", &PlanningSceneInterfaceWrapper::getObjectPosesPython);
124  planning_scene_class.def("get_objects", &PlanningSceneInterfaceWrapper::getObjectsPython);
125  planning_scene_class.def("get_attached_objects", &PlanningSceneInterfaceWrapper::getAttachedObjectsPython);
126  planning_scene_class.def("apply_planning_scene", &PlanningSceneInterfaceWrapper::applyPlanningScenePython);
127 }
128 } // namespace planning_interface
129 } // namespace moveit
130 
131 BOOST_PYTHON_MODULE(_moveit_planning_scene_interface)
132 {
133  using namespace moveit::planning_interface;
134  wrap_planning_scene_interface();
135 }
136 
Simple interface to MoveIt components.
Definition: moveit_cpp.h:203
boost::python::list listFromString(const std::vector< std::string > &v)
boost::python::dict dictFromType(const std::map< std::string, T > &v)
ByteString serializeMsg(const T &msg)
Convert a ROS message to a Python Bytestring.
std::vector< std::string > stringFromList(const boost::python::object &values)
void deserializeMsg(const ByteString &data, T &msg)
Convert a Python Bytestring to a ROS message.
Main namespace for MoveIt.
Definition: exceptions.h:43
This namespace includes the base class for MoveIt planners.
BOOST_PYTHON_MODULE(_moveit_roscpp_initializer)