38 #include <boost/regex.hpp>
57 planning_scene_collection_ = conn_->openCollectionPtr<moveit_msgs::PlanningScene>(DATABASE_NAME,
"planning_scene");
58 motion_plan_request_collection_ =
59 conn_->openCollectionPtr<moveit_msgs::MotionPlanRequest>(DATABASE_NAME,
"motion_plan_request");
60 robot_trajectory_collection_ =
61 conn_->openCollectionPtr<moveit_msgs::RobotTrajectory>(DATABASE_NAME,
"robot_trajectory");
66 planning_scene_collection_.reset();
67 motion_plan_request_collection_.reset();
68 robot_trajectory_collection_.reset();
69 conn_->dropDatabase(DATABASE_NAME);
76 if (hasPlanningScene(scene.name))
78 removePlanningScene(scene.name);
81 Metadata::Ptr metadata = planning_scene_collection_->createMetadata();
82 metadata->append(PLANNING_SCENE_ID_NAME, scene.name);
83 planning_scene_collection_->insert(scene, metadata);
84 ROS_DEBUG(
"%s scene '%s'", replace ?
"Replaced" :
"Added", scene.name.c_str());
89 Query::Ptr q = planning_scene_collection_->createQuery();
90 q->append(PLANNING_SCENE_ID_NAME,
name);
91 std::vector<PlanningSceneWithMetadata> planning_scenes = planning_scene_collection_->queryList(q,
true);
92 return !planning_scenes.empty();
97 const std::string& scene_name)
const
100 Query::Ptr q = motion_plan_request_collection_->createQuery();
101 q->append(PLANNING_SCENE_ID_NAME, scene_name);
102 std::vector<MotionPlanRequestWithMetadata> existing_requests = motion_plan_request_collection_->queryList(q,
false);
105 if (existing_requests.empty())
113 const void* data_arg = buffer_arg.get();
117 const size_t serial_size =
119 if (serial_size != serial_size_arg)
124 const void* data = buffer.get();
125 if (memcmp(data_arg, data, serial_size) == 0)
127 return existing_request->lookupString(MOTION_PLAN_REQUEST_ID_NAME);
133 const std::string& scene_name,
134 const std::string& query_name)
136 std::string
id = getMotionPlanRequestName(planning_query, scene_name);
139 if (!query_name.empty() &&
id.empty())
140 removePlanningQuery(scene_name, query_name);
142 if (
id != query_name ||
id.empty())
143 addNewPlanningRequest(planning_query, scene_name, query_name);
147 const moveit_msgs::MotionPlanRequest& planning_query,
const std::string& scene_name,
const std::string& query_name)
149 std::string
id = query_name;
152 std::set<std::string> used;
153 Query::Ptr q = motion_plan_request_collection_->createQuery();
154 q->append(PLANNING_SCENE_ID_NAME, scene_name);
155 std::vector<MotionPlanRequestWithMetadata> existing_requests = motion_plan_request_collection_->queryList(q,
true);
157 used.insert(existing_request->lookupString(MOTION_PLAN_REQUEST_ID_NAME));
158 std::size_t
index = existing_requests.size();
161 id =
"Motion Plan Request " + boost::lexical_cast<std::string>(
index);
163 }
while (used.find(
id) != used.end());
165 Metadata::Ptr metadata = motion_plan_request_collection_->createMetadata();
166 metadata->append(PLANNING_SCENE_ID_NAME, scene_name);
167 metadata->append(MOTION_PLAN_REQUEST_ID_NAME,
id);
168 motion_plan_request_collection_->insert(planning_query, metadata);
169 ROS_DEBUG(
"Saved planning query '%s' for scene '%s'",
id.c_str(), scene_name.c_str());
174 const moveit_msgs::RobotTrajectory& result,
175 const std::string& scene_name)
177 std::string
id = getMotionPlanRequestName(planning_query, scene_name);
179 id = addNewPlanningRequest(planning_query, scene_name,
"");
180 Metadata::Ptr metadata = robot_trajectory_collection_->createMetadata();
181 metadata->append(PLANNING_SCENE_ID_NAME, scene_name);
182 metadata->append(MOTION_PLAN_REQUEST_ID_NAME,
id);
183 robot_trajectory_collection_->insert(result, metadata);
189 Query::Ptr q = planning_scene_collection_->createQuery();
190 std::vector<PlanningSceneWithMetadata> planning_scenes =
191 planning_scene_collection_->queryList(q,
true, PLANNING_SCENE_ID_NAME,
true);
194 names.push_back(
planning_scene->lookupString(PLANNING_SCENE_ID_NAME));
198 std::vector<std::string>& names)
const
200 getPlanningSceneNames(names);
201 filterNames(regex, names);
205 const std::string& scene_name)
const
208 if (getPlanningScene(scene_m, scene_name))
210 world = scene_m->world;
218 const std::string& scene_name)
const
220 Query::Ptr q = planning_scene_collection_->createQuery();
221 q->append(PLANNING_SCENE_ID_NAME, scene_name);
222 std::vector<PlanningSceneWithMetadata> planning_scenes = planning_scene_collection_->queryList(q,
false);
223 if (planning_scenes.empty())
225 ROS_WARN(
"Planning scene '%s' was not found in the database", scene_name.c_str());
228 scene_m = planning_scenes.back();
230 const_cast<moveit_msgs::PlanningScene*
>(
static_cast<const moveit_msgs::PlanningScene*
>(scene_m.get()))->name =
236 const std::string& scene_name,
237 const std::string& query_name)
239 Query::Ptr q = motion_plan_request_collection_->createQuery();
240 q->append(PLANNING_SCENE_ID_NAME, scene_name);
241 q->append(MOTION_PLAN_REQUEST_ID_NAME, query_name);
242 std::vector<MotionPlanRequestWithMetadata> planning_queries = motion_plan_request_collection_->queryList(q,
false);
243 if (planning_queries.empty())
245 ROS_ERROR(
"Planning query '%s' not found for scene '%s'", query_name.c_str(), scene_name.c_str());
250 query_m = planning_queries.front();
256 std::vector<MotionPlanRequestWithMetadata>& planning_queries,
const std::string& scene_name)
const
258 Query::Ptr q = motion_plan_request_collection_->createQuery();
259 q->append(PLANNING_SCENE_ID_NAME, scene_name);
260 planning_queries = motion_plan_request_collection_->queryList(q,
false);
264 const std::string& scene_name)
const
266 Query::Ptr q = motion_plan_request_collection_->createQuery();
267 q->append(PLANNING_SCENE_ID_NAME, scene_name);
268 std::vector<MotionPlanRequestWithMetadata> planning_queries = motion_plan_request_collection_->queryList(q,
true);
271 if (planning_query->lookupField(MOTION_PLAN_REQUEST_ID_NAME))
272 query_names.push_back(planning_query->lookupString(MOTION_PLAN_REQUEST_ID_NAME));
276 std::vector<std::string>& query_names,
277 const std::string& scene_name)
const
279 getPlanningQueriesNames(query_names, scene_name);
283 std::vector<std::string> fnames;
284 boost::regex
r(regex);
285 for (
const std::string& query_name : query_names)
288 if (boost::regex_match(query_name.c_str(), match,
r))
290 fnames.push_back(query_name);
293 query_names.swap(fnames);
298 std::vector<MotionPlanRequestWithMetadata>& planning_queries, std::vector<std::string>& query_names,
299 const std::string& scene_name)
const
301 Query::Ptr q = motion_plan_request_collection_->createQuery();
302 q->append(PLANNING_SCENE_ID_NAME, scene_name);
303 planning_queries = motion_plan_request_collection_->queryList(q,
false);
304 query_names.resize(planning_queries.size());
305 for (std::size_t i = 0; i < planning_queries.size(); ++i)
306 if (planning_queries[i]->lookupField(MOTION_PLAN_REQUEST_ID_NAME))
307 query_names[i] = planning_queries[i]->lookupString(MOTION_PLAN_REQUEST_ID_NAME);
309 query_names[i].clear();
313 std::vector<RobotTrajectoryWithMetadata>& planning_results,
const std::string& scene_name,
314 const moveit_msgs::MotionPlanRequest& planning_query)
const
316 std::string
id = getMotionPlanRequestName(planning_query, scene_name);
318 planning_results.clear();
320 getPlanningResults(planning_results,
id, scene_name);
324 std::vector<RobotTrajectoryWithMetadata>& planning_results,
const std::string& scene_name,
325 const std::string& planning_query)
const
327 Query::Ptr q = robot_trajectory_collection_->createQuery();
328 q->append(PLANNING_SCENE_ID_NAME, scene_name);
329 q->append(MOTION_PLAN_REQUEST_ID_NAME, planning_query);
330 planning_results = robot_trajectory_collection_->queryList(q,
false);
334 const std::string& query_name)
const
336 Query::Ptr q = motion_plan_request_collection_->createQuery();
337 q->append(PLANNING_SCENE_ID_NAME, scene_name);
338 q->append(MOTION_PLAN_REQUEST_ID_NAME, query_name);
339 std::vector<MotionPlanRequestWithMetadata> queries = motion_plan_request_collection_->queryList(q,
true);
340 return !queries.empty();
344 const std::string& new_scene_name)
346 Query::Ptr q = planning_scene_collection_->createQuery();
347 q->append(PLANNING_SCENE_ID_NAME, old_scene_name);
348 Metadata::Ptr m = planning_scene_collection_->createMetadata();
349 m->append(PLANNING_SCENE_ID_NAME, new_scene_name);
350 planning_scene_collection_->modifyMetadata(q, m);
351 ROS_DEBUG(
"Renamed planning scene from '%s' to '%s'", old_scene_name.c_str(), new_scene_name.c_str());
355 const std::string& old_query_name,
356 const std::string& new_query_name)
358 Query::Ptr q = motion_plan_request_collection_->createQuery();
359 q->append(PLANNING_SCENE_ID_NAME, scene_name);
360 q->append(MOTION_PLAN_REQUEST_ID_NAME, old_query_name);
361 Metadata::Ptr m = motion_plan_request_collection_->createMetadata();
362 m->append(MOTION_PLAN_REQUEST_ID_NAME, new_query_name);
363 motion_plan_request_collection_->modifyMetadata(q, m);
364 ROS_DEBUG(
"Renamed planning query for scene '%s' from '%s' to '%s'", scene_name.c_str(), old_query_name.c_str(),
365 new_query_name.c_str());
370 removePlanningQueries(scene_name);
371 Query::Ptr q = planning_scene_collection_->createQuery();
372 q->append(PLANNING_SCENE_ID_NAME, scene_name);
373 unsigned int rem = planning_scene_collection_->removeMessages(q);
374 ROS_DEBUG(
"Removed %u PlanningScene messages (named '%s')", rem, scene_name.c_str());
379 removePlanningResults(scene_name);
380 Query::Ptr q = motion_plan_request_collection_->createQuery();
381 q->append(PLANNING_SCENE_ID_NAME, scene_name);
382 unsigned int rem = motion_plan_request_collection_->removeMessages(q);
383 ROS_DEBUG(
"Removed %u MotionPlanRequest messages for scene '%s'", rem, scene_name.c_str());
387 const std::string& query_name)
389 removePlanningResults(scene_name, query_name);
390 Query::Ptr q = motion_plan_request_collection_->createQuery();
391 q->append(PLANNING_SCENE_ID_NAME, scene_name);
392 q->append(MOTION_PLAN_REQUEST_ID_NAME, query_name);
393 unsigned int rem = motion_plan_request_collection_->removeMessages(q);
394 ROS_DEBUG(
"Removed %u MotionPlanRequest messages for scene '%s', query '%s'", rem, scene_name.c_str(),
400 Query::Ptr q = robot_trajectory_collection_->createQuery();
401 q->append(PLANNING_SCENE_ID_NAME, scene_name);
402 unsigned int rem = robot_trajectory_collection_->removeMessages(q);
403 ROS_DEBUG(
"Removed %u RobotTrajectory messages for scene '%s'", rem, scene_name.c_str());
407 const std::string& query_name)
409 Query::Ptr q = robot_trajectory_collection_->createQuery();
410 q->append(PLANNING_SCENE_ID_NAME, scene_name);
411 q->append(MOTION_PLAN_REQUEST_ID_NAME, query_name);
412 unsigned int rem = robot_trajectory_collection_->removeMessages(q);
413 ROS_DEBUG(
"Removed %u RobotTrajectory messages for scene '%s', query '%s'", rem, scene_name.c_str(),