moveit2
The MoveIt Motion Planning Framework for ROS 2.
Loading...
Searching...
No Matches
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
38
39namespace moveit_py
40{
41namespace bind_planning_scene_monitor
42{
43
44bool 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
57bool 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
65LockedPlanningSceneContextManagerRO
66readOnly(const planning_scene_monitor::PlanningSceneMonitorPtr& planning_scene_monitor)
67{
69};
70
71LockedPlanningSceneContextManagerRW
72readWrite(const planning_scene_monitor::PlanningSceneMonitorPtr& planning_scene_monitor)
73{
75};
76
77const planning_scene::PlanningSceneConstPtr& LockedPlanningSceneContextManagerRO::lockedPlanningSceneRoEnter() const
78{
80 ->getPlanningScene();
81}
82
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
102void 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 )")
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 py::arg("scene"),
180 R"(
181 Called to update the planning scene with a new message.
182
183 Args:
184 scene (moveit_msgs.msg.PlanningScene): The new planning scene message.
185 )")
186
188 R"(
189 Returns a read-only context manager for the planning scene.
190 )")
191
193 R"(
194 Returns a read-write context manager for the planning scene.
195 )");
196}
197
198void initContextManagers(py::module& m)
199{
200 // In Python we lock the planning scene using a with statement as this allows us to have control over resources.
201 // To this end each of the below manager classes binds special methods __enter__ and __exit__.
202 // LockedPlanningSceneContextManagerRO
203 py::class_<moveit_py::bind_planning_scene_monitor::LockedPlanningSceneContextManagerRO>(
204 m, "LockedPlanningSceneContextManagerRO", R"(
205 A context manager that locks the planning scene for reading.
206 )")
207
208 .def("__enter__",
210 R"(
211 Special method that is used with the with statement, provides access to a locked plannning scene instance.
212 Returns:
213 :py:class:`moveit_py.core.PlanningScene`: The locked planning scene.
214 )")
215 .def("__exit__",
217 R"(
218 Special method that is used with the with statement, releases the lock on the planning scene.
219 )");
220
221 // LockedPlanningSceneContextManagerRW
222 py::class_<moveit_py::bind_planning_scene_monitor::LockedPlanningSceneContextManagerRW>(
223 m, "LockedPlanningSceneContextManagerRW", R"(
224 A context manager that locks the planning scene for reading and writing.
225 )")
226
227 .def("__enter__",
229 py::return_value_policy::take_ownership,
230 R"(
231 Special method that is used with the with statement, provides access to a locked plannning scene instance.
232 Returns:
233 :py:class:`moveit_py.core.PlanningScene`: The locked planning scene.
234 )")
235
236 .def("__exit__",
238 R"(
239 Special method that is used with the with statement, releases the lock on the planning scene.
240 )");
241}
242} // namespace bind_planning_scene_monitor
243} // 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.
bool newPlanningSceneMessage(const moveit_msgs::msg::PlanningScene &scene)
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...
const std::string & getName() const
Get the name of this monitor.
const planning_scene::PlanningScenePtr & getPlanningScene()
Avoid this function! Returns an unsafe pointer to the current planning scene.
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)