39#include <rclcpp/serialization.hpp>
48using warehouse_ros::Metadata;
49using warehouse_ros::Query;
57void moveit_warehouse::PlanningSceneStorage::createCollections()
59 planning_scene_collection_ =
60 conn_->openCollectionPtr<moveit_msgs::msg::PlanningScene>(DATABASE_NAME,
"planning_scene");
61 motion_plan_request_collection_ =
62 conn_->openCollectionPtr<moveit_msgs::msg::MotionPlanRequest>(DATABASE_NAME,
"motion_plan_request");
63 robot_trajectory_collection_ =
64 conn_->openCollectionPtr<moveit_msgs::msg::RobotTrajectory>(DATABASE_NAME,
"robot_trajectory");
69 planning_scene_collection_.reset();
70 motion_plan_request_collection_.reset();
71 robot_trajectory_collection_.reset();
72 conn_->dropDatabase(DATABASE_NAME);
79 if (hasPlanningScene(scene.name))
81 removePlanningScene(scene.name);
84 Metadata::Ptr metadata = planning_scene_collection_->createMetadata();
85 metadata->append(PLANNING_SCENE_ID_NAME, scene.name);
86 planning_scene_collection_->insert(scene, metadata);
87 RCLCPP_DEBUG(logger_,
"%s scene '%s'", replace ?
"Replaced" :
"Added", scene.name.c_str());
92 Query::Ptr q = planning_scene_collection_->createQuery();
93 q->append(PLANNING_SCENE_ID_NAME, name);
94 std::vector<PlanningSceneWithMetadata> planning_scenes = planning_scene_collection_->queryList(q,
true);
95 return !planning_scenes.empty();
98std::string moveit_warehouse::PlanningSceneStorage::getMotionPlanRequestName(
99 const moveit_msgs::msg::MotionPlanRequest& planning_query,
const std::string& scene_name)
const
102 Query::Ptr q = motion_plan_request_collection_->createQuery();
103 q->append(PLANNING_SCENE_ID_NAME, scene_name);
104 std::vector<MotionPlanRequestWithMetadata> existing_requests = motion_plan_request_collection_->queryList(q,
false);
107 if (existing_requests.empty())
111 rclcpp::Serialization<moveit_msgs::msg::MotionPlanRequest> serializer;
112 rclcpp::SerializedMessage serialized_msg_arg;
113 serializer.serialize_message(&planning_query, &serialized_msg_arg);
114 const size_t serial_size_arg = serialized_msg_arg.size();
115 const void* data_arg = serialized_msg_arg.get_rcl_serialized_message().buffer;
119 auto msg =
static_cast<const moveit_msgs::msg::MotionPlanRequest&
>(*existing_request);
120 rclcpp::SerializedMessage serialized_msg;
121 serializer.serialize_message(&msg, &serialized_msg);
122 const size_t serial_size = serialized_msg.size();
123 const void* data = serialized_msg.get_rcl_serialized_message().buffer;
125 if (serial_size != serial_size_arg)
127 if (memcmp(data_arg, data, serial_size) == 0)
130 return existing_request->lookupString(MOTION_PLAN_REQUEST_ID_NAME);
137 const std::string& scene_name,
138 const std::string& query_name)
140 std::string
id = getMotionPlanRequestName(planning_query, scene_name);
143 if (!query_name.empty() &&
id.empty())
144 removePlanningQuery(scene_name, query_name);
146 if (
id != query_name ||
id.empty())
147 addNewPlanningRequest(planning_query, scene_name, query_name);
151moveit_warehouse::PlanningSceneStorage::addNewPlanningRequest(
const moveit_msgs::msg::MotionPlanRequest& planning_query,
152 const std::string& scene_name,
153 const std::string& query_name)
155 std::string
id = query_name;
158 std::set<std::string> used;
159 Query::Ptr q = motion_plan_request_collection_->createQuery();
160 q->append(PLANNING_SCENE_ID_NAME, scene_name);
161 std::vector<MotionPlanRequestWithMetadata> existing_requests = motion_plan_request_collection_->queryList(q,
true);
163 used.insert(existing_request->lookupString(MOTION_PLAN_REQUEST_ID_NAME));
164 std::size_t index = existing_requests.size();
167 id =
"Motion Plan Request " + std::to_string(index);
169 }
while (used.find(
id) != used.end());
171 Metadata::Ptr metadata = motion_plan_request_collection_->createMetadata();
172 metadata->append(PLANNING_SCENE_ID_NAME, scene_name);
173 metadata->append(MOTION_PLAN_REQUEST_ID_NAME,
id);
174 motion_plan_request_collection_->insert(planning_query, metadata);
175 RCLCPP_DEBUG(logger_,
"Saved planning query '%s' for scene '%s'",
id.c_str(), scene_name.c_str());
180 const moveit_msgs::msg::RobotTrajectory& result,
181 const std::string& scene_name)
183 std::string
id = getMotionPlanRequestName(planning_query, scene_name);
185 id = addNewPlanningRequest(planning_query, scene_name,
"");
186 Metadata::Ptr metadata = robot_trajectory_collection_->createMetadata();
187 metadata->append(PLANNING_SCENE_ID_NAME, scene_name);
188 metadata->append(MOTION_PLAN_REQUEST_ID_NAME,
id);
189 robot_trajectory_collection_->insert(result, metadata);
195 Query::Ptr q = planning_scene_collection_->createQuery();
196 std::vector<PlanningSceneWithMetadata> planning_scenes =
197 planning_scene_collection_->queryList(q,
true, PLANNING_SCENE_ID_NAME,
true);
201 names.push_back(
planning_scene->lookupString(PLANNING_SCENE_ID_NAME));
206 std::vector<std::string>& names)
const
208 getPlanningSceneNames(names);
209 filterNames(regex, names);
213 const std::string& scene_name)
const
216 if (getPlanningScene(scene_m, scene_name))
218 world = scene_m->world;
226 const std::string& scene_name)
const
228 Query::Ptr q = planning_scene_collection_->createQuery();
229 q->append(PLANNING_SCENE_ID_NAME, scene_name);
230 std::vector<PlanningSceneWithMetadata> planning_scenes = planning_scene_collection_->queryList(q,
false);
231 if (planning_scenes.empty())
233 RCLCPP_WARN(logger_,
"Planning scene '%s' was not found in the database", scene_name.c_str());
236 scene_m = planning_scenes.back();
238 const_cast<moveit_msgs::msg::PlanningScene*
>(
static_cast<const moveit_msgs::msg::PlanningScene*
>(scene_m.get()))->name =
244 const std::string& scene_name,
245 const std::string& query_name)
247 Query::Ptr q = motion_plan_request_collection_->createQuery();
248 q->append(PLANNING_SCENE_ID_NAME, scene_name);
249 q->append(MOTION_PLAN_REQUEST_ID_NAME, query_name);
250 std::vector<MotionPlanRequestWithMetadata> planning_queries = motion_plan_request_collection_->queryList(q,
false);
251 if (planning_queries.empty())
253 RCLCPP_ERROR(logger_,
"Planning query '%s' not found for scene '%s'", query_name.c_str(), scene_name.c_str());
258 query_m = planning_queries.front();
264 std::vector<MotionPlanRequestWithMetadata>& planning_queries,
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 planning_queries = motion_plan_request_collection_->queryList(q,
false);
272 const std::string& scene_name)
const
274 Query::Ptr q = motion_plan_request_collection_->createQuery();
275 q->append(PLANNING_SCENE_ID_NAME, scene_name);
276 std::vector<MotionPlanRequestWithMetadata> planning_queries = motion_plan_request_collection_->queryList(q,
true);
280 if (planning_query->lookupField(MOTION_PLAN_REQUEST_ID_NAME))
281 query_names.push_back(planning_query->lookupString(MOTION_PLAN_REQUEST_ID_NAME));
286 std::vector<std::string>& query_names,
287 const std::string& scene_name)
const
289 getPlanningQueriesNames(query_names, scene_name);
293 std::vector<std::string> fnames;
295 for (
const std::string& query_name : query_names)
298 if (std::regex_match(query_name, match, r))
300 fnames.push_back(query_name);
303 query_names.swap(fnames);
308 std::vector<MotionPlanRequestWithMetadata>& planning_queries, std::vector<std::string>& query_names,
309 const std::string& scene_name)
const
311 Query::Ptr q = motion_plan_request_collection_->createQuery();
312 q->append(PLANNING_SCENE_ID_NAME, scene_name);
313 planning_queries = motion_plan_request_collection_->queryList(q,
false);
314 query_names.resize(planning_queries.size());
315 for (std::size_t i = 0; i < planning_queries.size(); ++i)
317 if (planning_queries[i]->lookupField(MOTION_PLAN_REQUEST_ID_NAME))
319 query_names[i] = planning_queries[i]->lookupString(MOTION_PLAN_REQUEST_ID_NAME);
323 query_names[i].clear();
329 std::vector<RobotTrajectoryWithMetadata>& planning_results,
const std::string& scene_name,
330 const moveit_msgs::msg::MotionPlanRequest& planning_query)
const
332 std::string
id = getMotionPlanRequestName(planning_query, scene_name);
335 planning_results.clear();
339 getPlanningResults(planning_results,
id, scene_name);
344 std::vector<RobotTrajectoryWithMetadata>& planning_results,
const std::string& scene_name,
345 const std::string& planning_query)
const
347 Query::Ptr q = robot_trajectory_collection_->createQuery();
348 q->append(PLANNING_SCENE_ID_NAME, scene_name);
349 q->append(MOTION_PLAN_REQUEST_ID_NAME, planning_query);
350 planning_results = robot_trajectory_collection_->queryList(q,
false);
354 const std::string& query_name)
const
356 Query::Ptr q = motion_plan_request_collection_->createQuery();
357 q->append(PLANNING_SCENE_ID_NAME, scene_name);
358 q->append(MOTION_PLAN_REQUEST_ID_NAME, query_name);
359 std::vector<MotionPlanRequestWithMetadata> queries = motion_plan_request_collection_->queryList(q,
true);
360 return !queries.empty();
364 const std::string& new_scene_name)
366 Query::Ptr q = planning_scene_collection_->createQuery();
367 q->append(PLANNING_SCENE_ID_NAME, old_scene_name);
368 Metadata::Ptr m = planning_scene_collection_->createMetadata();
369 m->append(PLANNING_SCENE_ID_NAME, new_scene_name);
370 planning_scene_collection_->modifyMetadata(q, m);
371 RCLCPP_DEBUG(logger_,
"Renamed planning scene from '%s' to '%s'", old_scene_name.c_str(), new_scene_name.c_str());
375 const std::string& old_query_name,
376 const std::string& new_query_name)
378 Query::Ptr q = motion_plan_request_collection_->createQuery();
379 q->append(PLANNING_SCENE_ID_NAME, scene_name);
380 q->append(MOTION_PLAN_REQUEST_ID_NAME, old_query_name);
381 Metadata::Ptr m = motion_plan_request_collection_->createMetadata();
382 m->append(MOTION_PLAN_REQUEST_ID_NAME, new_query_name);
383 motion_plan_request_collection_->modifyMetadata(q, m);
384 RCLCPP_DEBUG(logger_,
"Renamed planning query for scene '%s' from '%s' to '%s'", scene_name.c_str(),
385 old_query_name.c_str(), new_query_name.c_str());
390 removePlanningQueries(scene_name);
391 Query::Ptr q = planning_scene_collection_->createQuery();
392 q->append(PLANNING_SCENE_ID_NAME, scene_name);
393 unsigned int rem = planning_scene_collection_->removeMessages(q);
394 RCLCPP_DEBUG(logger_,
"Removed %u PlanningScene messages (named '%s')", rem, scene_name.c_str());
399 removePlanningResults(scene_name);
400 Query::Ptr q = motion_plan_request_collection_->createQuery();
401 q->append(PLANNING_SCENE_ID_NAME, scene_name);
402 unsigned int rem = motion_plan_request_collection_->removeMessages(q);
403 RCLCPP_DEBUG(logger_,
"Removed %u MotionPlanRequest messages for scene '%s'", rem, scene_name.c_str());
407 const std::string& query_name)
409 removePlanningResults(scene_name, query_name);
410 Query::Ptr q = motion_plan_request_collection_->createQuery();
411 q->append(PLANNING_SCENE_ID_NAME, scene_name);
412 q->append(MOTION_PLAN_REQUEST_ID_NAME, query_name);
413 unsigned int rem = motion_plan_request_collection_->removeMessages(q);
414 RCLCPP_DEBUG(logger_,
"Removed %u MotionPlanRequest messages for scene '%s', query '%s'", rem, scene_name.c_str(),
420 Query::Ptr q = robot_trajectory_collection_->createQuery();
421 q->append(PLANNING_SCENE_ID_NAME, scene_name);
422 unsigned int rem = robot_trajectory_collection_->removeMessages(q);
423 RCLCPP_DEBUG(logger_,
"Removed %u RobotTrajectory messages for scene '%s'", rem, scene_name.c_str());
427 const std::string& query_name)
429 Query::Ptr q = robot_trajectory_collection_->createQuery();
430 q->append(PLANNING_SCENE_ID_NAME, scene_name);
431 q->append(MOTION_PLAN_REQUEST_ID_NAME, query_name);
432 unsigned int rem = robot_trajectory_collection_->removeMessages(q);
433 RCLCPP_DEBUG(logger_,
"Removed %u RobotTrajectory messages for scene '%s', query '%s'", rem, scene_name.c_str(),
This class provides the mechanism to connect to a database and reads needed ROS parameters when appro...
bool getPlanningSceneWorld(moveit_msgs::msg::PlanningSceneWorld &world, const std::string &scene_name) const
void removePlanningScene(const std::string &scene_name)
void addPlanningQuery(const moveit_msgs::msg::MotionPlanRequest &planning_query, const std::string &scene_name, const std::string &query_name="")
void removePlanningQuery(const std::string &scene_name, const std::string &query_name)
static const std::string PLANNING_SCENE_ID_NAME
void getPlanningResults(std::vector< RobotTrajectoryWithMetadata > &planning_results, const std::string &scene_name, const moveit_msgs::msg::MotionPlanRequest &planning_query) const
void removePlanningQueries(const std::string &scene_name)
PlanningSceneStorage(warehouse_ros::DatabaseConnection::Ptr conn)
void addPlanningResult(const moveit_msgs::msg::MotionPlanRequest &planning_query, const moveit_msgs::msg::RobotTrajectory &result, const std::string &scene_name)
void renamePlanningQuery(const std::string &scene_name, const std::string &old_query_name, const std::string &new_query_name)
static const std::string DATABASE_NAME
bool hasPlanningQuery(const std::string &scene_name, const std::string &query_name) const
void getPlanningQueries(std::vector< MotionPlanRequestWithMetadata > &planning_queries, const std::string &scene_name) const
void removePlanningResults(const std::string &scene_name)
void addPlanningScene(const moveit_msgs::msg::PlanningScene &scene)
bool getPlanningQuery(MotionPlanRequestWithMetadata &query_m, const std::string &scene_name, const std::string &query_name)
void renamePlanningScene(const std::string &old_scene_name, const std::string &new_scene_name)
void getPlanningSceneNames(std::vector< std::string > &names) const
void getPlanningQueriesNames(std::vector< std::string > &query_names, const std::string &scene_name) const
bool hasPlanningScene(const std::string &name) const
static const std::string MOTION_PLAN_REQUEST_ID_NAME
bool getPlanningScene(PlanningSceneWithMetadata &scene_m, const std::string &scene_name) const
Get the latest planning scene named scene_name.
warehouse_ros::MessageWithMetadata< moveit_msgs::msg::PlanningScene >::ConstPtr PlanningSceneWithMetadata
warehouse_ros::MessageWithMetadata< moveit_msgs::msg::MotionPlanRequest >::ConstPtr MotionPlanRequestWithMetadata
Main namespace for MoveIt.
This namespace includes the central class for representing planning contexts.