moveit2
The MoveIt Motion Planning Framework for ROS 2.
planning_scene_monitor.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2022, Peter David Fagan
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 PickNik Inc. 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: Peter David Fagan */
36 
37 #include "planning_scene_monitor.h"
38 
39 namespace moveit_py
40 {
41 namespace bind_planning_scene_monitor
42 {
43 
44 bool processCollisionObject(const planning_scene_monitor::PlanningSceneMonitorPtr& planning_scene_monitor,
45  moveit_msgs::msg::CollisionObject& collision_object_msg,
46  std::optional<moveit_msgs::msg::ObjectColor> color_msg)
47 {
48  moveit_msgs::msg::CollisionObject::ConstSharedPtr const_ptr =
49  std::make_shared<const moveit_msgs::msg::CollisionObject>(collision_object_msg);
50  if (color_msg)
51  {
52  return planning_scene_monitor->processCollisionObjectMsg(const_ptr, *std::move(color_msg));
53  }
54  return planning_scene_monitor->processCollisionObjectMsg(const_ptr);
55 }
56 
57 bool processAttachedCollisionObjectMsg(const planning_scene_monitor::PlanningSceneMonitorPtr& planning_scene_monitor,
58  moveit_msgs::msg::AttachedCollisionObject& attached_collision_object_msg)
59 {
60  moveit_msgs::msg::AttachedCollisionObject::ConstSharedPtr const_ptr =
61  std::make_shared<const moveit_msgs::msg::AttachedCollisionObject>(attached_collision_object_msg);
62  return planning_scene_monitor->processAttachedCollisionObjectMsg(const_ptr);
63 }
64 
65 LockedPlanningSceneContextManagerRO
66 readOnly(const planning_scene_monitor::PlanningSceneMonitorPtr& planning_scene_monitor)
67 {
69 };
70 
71 LockedPlanningSceneContextManagerRW
72 readWrite(const planning_scene_monitor::PlanningSceneMonitorPtr& planning_scene_monitor)
73 {
75 };
76 
77 const planning_scene::PlanningSceneConstPtr& LockedPlanningSceneContextManagerRO::lockedPlanningSceneRoEnter() const
78 {
80  ->getPlanningScene();
81 }
82 
83 const planning_scene::PlanningScenePtr& LockedPlanningSceneContextManagerRW::lockedPlanningSceneRwEnter()
84 {
85  return planning_scene_monitor_->getPlanningScene();
86 }
87 
89  const py::object& /*value*/,
90  const py::object& /*traceback*/)
91 {
92  ls_ro_.reset();
93 }
94 
96  const py::object& /*value*/,
97  const py::object& /*traceback*/)
98 {
99  ls_rw_.reset();
100 }
101 
102 void initPlanningSceneMonitor(py::module& m)
103 {
104  py::class_<planning_scene_monitor::PlanningSceneMonitor, planning_scene_monitor::PlanningSceneMonitorPtr>(
105  m, "PlanningSceneMonitor", R"(
106  Maintains the internal state of the planning scene.
107  )")
108 
109  .def_property("name", &planning_scene_monitor::PlanningSceneMonitor::getName, nullptr,
110  R"(
111  str: The name of this planning scene monitor.
112  )")
113 
115  R"(
116  Update the transforms for the frames that are not part of the kinematic model using tf.
117 
118  Examples of these frames are the "map" and "odom_combined" transforms. This function is automatically called
119  when data that uses transforms is received.
120  However, this function should also be called before starting a planning request, for example.
121  )")
122 
124  R"(
125  Starts the scene monitor.
126  )")
127 
129  R"(
130  Stops the scene monitor.
131  )")
132 
134  R"(
135  Starts the state monitor.
136  )")
137 
139  R"(
140  Stops the state monitor.
141  )")
143  py::arg("service_name"),
144  R"(
145  Request the planning scene.
146 
147  Args:
148  service_name (str): The name of the service to call.
149  )")
150 
152  R"(
153  Waits for the current robot state to be received.
154  )")
155 
157  R"(
158  Clears the octomap.
159  )")
160  .def("process_collision_object", &moveit_py::bind_planning_scene_monitor::processCollisionObject,
161  py::arg("collision_object_msg"), py::arg("color_msg") = nullptr,
162  R"(
163  Apply a collision object to the planning scene.
164 
165  Args:
166  collision_object_msg (moveit_msgs.msg.CollisionObject): The collision object to apply to the planning scene.
167  )")
168  .def("process_attached_collision_object",
170  py::arg("attached_collision_object_msg"),
171  R"(
172  Apply an attached collision object msg to the planning scene.
173 
174  Args:
175  attached_collision_object_msg (moveit_msgs.msg.AttachedCollisionObject): The attached collision object to apply to the planning scene.
176  )")
177 
179  R"(
180  Returns a read-only context manager for the planning scene.
181  )")
182 
184  R"(
185  Returns a read-write context manager for the planning scene.
186  )");
187 }
188 
189 void initContextManagers(py::module& m)
190 {
191  // In Python we lock the planning scene using a with statement as this allows us to have control over resources.
192  // To this end each of the below manager classes binds special methods __enter__ and __exit__.
193  // LockedPlanningSceneContextManagerRO
194  py::class_<moveit_py::bind_planning_scene_monitor::LockedPlanningSceneContextManagerRO>(
195  m, "LockedPlanningSceneContextManagerRO", R"(
196  A context manager that locks the planning scene for reading.
197  )")
198 
199  .def("__enter__",
201  R"(
202  Special method that is used with the with statement, provides access to a locked plannning scene instance.
203  Returns:
204  :py:class:`moveit_py.core.PlanningScene`: The locked planning scene.
205  )")
206  .def("__exit__",
208  R"(
209  Special method that is used with the with statement, releases the lock on the planning scene.
210  )");
211 
212  // LockedPlanningSceneContextManagerRW
213  py::class_<moveit_py::bind_planning_scene_monitor::LockedPlanningSceneContextManagerRW>(
214  m, "LockedPlanningSceneContextManagerRW", R"(
215  A context manager that locks the planning scene for reading and writing.
216  )")
217 
218  .def("__enter__",
220  py::return_value_policy::take_ownership,
221  R"(
222  Special method that is used with the with statement, provides access to a locked plannning scene instance.
223  Returns:
224  :py:class:`moveit_py.core.PlanningScene`: The locked planning scene.
225  )")
226 
227  .def("__exit__",
229  R"(
230  Special method that is used with the with statement, releases the lock on the planning scene.
231  )");
232 }
233 } // namespace bind_planning_scene_monitor
234 } // namespace moveit_py
const planning_scene_monitor::PlanningSceneMonitorPtr planning_scene_monitor_
const planning_scene::PlanningSceneConstPtr & lockedPlanningSceneRoEnter() const
void lockedPlanningSceneRoExit(const py::object &type, const py::object &value, const py::object &traceback)
std::unique_ptr< const planning_scene_monitor::LockedPlanningSceneRO > ls_ro_
const planning_scene_monitor::PlanningSceneMonitorPtr planning_scene_monitor_
std::unique_ptr< const planning_scene_monitor::LockedPlanningSceneRW > ls_rw_
void lockedPlanningSceneRwExit(const py::object &type, const py::object &value, const py::object &traceback)
PlanningSceneMonitor Subscribes to the topic planning_scene.
const std::string & getName() const
Get the name of this monitor.
void startStateMonitor(const std::string &joint_states_topic=DEFAULT_JOINT_STATES_TOPIC, const std::string &attached_objects_topic=DEFAULT_ATTACHED_COLLISION_OBJECT_TOPIC)
Start the current state monitor.
void startSceneMonitor(const std::string &scene_topic=DEFAULT_PLANNING_SCENE_TOPIC)
Start the scene monitor (ROS topic-based)
bool waitForCurrentRobotState(const rclcpp::Time &t, double wait_time=1.)
Wait for robot state to become more recent than time t.
bool requestPlanningSceneState(const std::string &service_name=DEFAULT_PLANNING_SCENE_SERVICE)
Request a full planning scene state using a service call Be careful not to use this in conjunction wi...
void updateFrameTransforms()
Update the transforms for the frames that are not part of the kinematic model using tf....
bool processAttachedCollisionObjectMsg(const planning_scene_monitor::PlanningSceneMonitorPtr &planning_scene_monitor, moveit_msgs::msg::AttachedCollisionObject &attached_collision_object_msg)
LockedPlanningSceneContextManagerRW readWrite(const planning_scene_monitor::PlanningSceneMonitorPtr &planning_scene_monitor)
bool processCollisionObject(const planning_scene_monitor::PlanningSceneMonitorPtr &planning_scene_monitor, moveit_msgs::msg::CollisionObject &collision_object_msg, std::optional< moveit_msgs::msg::ObjectColor > color_msg)
LockedPlanningSceneContextManagerRO readOnly(const planning_scene_monitor::PlanningSceneMonitorPtr &planning_scene_monitor)