moveit2
The MoveIt Motion Planning Framework for ROS 2.
trajectory_constraints_storage.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: Mario Prats, Ioan Sucan */
36 
38 #include <moveit/utils/logger.hpp>
39 
40 #include <utility>
41 
42 const std::string moveit_warehouse::TrajectoryConstraintsStorage::DATABASE_NAME = "moveit_trajectory_constraints";
43 
47 
48 using warehouse_ros::Metadata;
49 using warehouse_ros::Query;
50 
52  : MoveItMessageStorage(std::move(conn)), logger_(moveit::getLogger("moveit_warehouse_trajectory_constraints_storage"))
53 {
54  createCollections();
55 }
56 
57 void moveit_warehouse::TrajectoryConstraintsStorage::createCollections()
58 {
59  constraints_collection_ =
60  conn_->openCollectionPtr<moveit_msgs::msg::TrajectoryConstraints>(DATABASE_NAME, "trajectory_constraints");
61 }
62 
64 {
65  constraints_collection_.reset();
66  conn_->dropDatabase(DATABASE_NAME);
67  createCollections();
68 }
69 
71  const moveit_msgs::msg::TrajectoryConstraints& msg, const std::string& name, const std::string& robot,
72  const std::string& group)
73 {
74  bool replace = false;
75  if (hasTrajectoryConstraints(name, robot, group))
76  {
77  removeTrajectoryConstraints(name, robot, group);
78  replace = true;
79  }
80  Metadata::Ptr metadata = constraints_collection_->createMetadata();
81  metadata->append(CONSTRAINTS_ID_NAME, name);
82  metadata->append(ROBOT_NAME, robot);
83  metadata->append(CONSTRAINTS_GROUP_NAME, group);
84  constraints_collection_->insert(msg, metadata);
85  RCLCPP_DEBUG(logger_, "%s constraints '%s'", replace ? "Replaced" : "Added", name.c_str());
86 }
87 
89  const std::string& robot,
90  const std::string& group) const
91 {
92  Query::Ptr q = constraints_collection_->createQuery();
93  q->append(CONSTRAINTS_ID_NAME, name);
94  if (!robot.empty())
95  q->append(ROBOT_NAME, robot);
96  if (!group.empty())
97  q->append(CONSTRAINTS_GROUP_NAME, group);
98  std::vector<TrajectoryConstraintsWithMetadata> constr = constraints_collection_->queryList(q, true);
99  return !constr.empty();
100 }
101 
103  std::vector<std::string>& names,
104  const std::string& robot,
105  const std::string& group) const
106 {
107  getKnownTrajectoryConstraints(names, robot, group);
108  filterNames(regex, names);
109 }
110 
112  const std::string& robot,
113  const std::string& group) const
114 {
115  names.clear();
116  Query::Ptr q = constraints_collection_->createQuery();
117  if (!robot.empty())
118  q->append(ROBOT_NAME, robot);
119  if (!group.empty())
120  q->append(CONSTRAINTS_GROUP_NAME, group);
121  std::vector<TrajectoryConstraintsWithMetadata> constr =
122  constraints_collection_->queryList(q, true, CONSTRAINTS_ID_NAME, true);
123  for (TrajectoryConstraintsWithMetadata& traj_constraint : constr)
124  {
125  if (traj_constraint->lookupField(CONSTRAINTS_ID_NAME))
126  names.push_back(traj_constraint->lookupString(CONSTRAINTS_ID_NAME));
127  }
128 }
129 
131  const std::string& name,
132  const std::string& robot,
133  const std::string& group) const
134 {
135  Query::Ptr q = constraints_collection_->createQuery();
136  q->append(CONSTRAINTS_ID_NAME, name);
137  if (!robot.empty())
138  q->append(ROBOT_NAME, robot);
139  if (!group.empty())
140  q->append(CONSTRAINTS_GROUP_NAME, group);
141  std::vector<TrajectoryConstraintsWithMetadata> constr = constraints_collection_->queryList(q, false);
142  if (constr.empty())
143  {
144  return false;
145  }
146  else
147  {
148  msg_m = constr.back();
149  return true;
150  }
151 }
152 
154  const std::string& new_name,
155  const std::string& robot,
156  const std::string& group)
157 {
158  Query::Ptr q = constraints_collection_->createQuery();
159  q->append(CONSTRAINTS_ID_NAME, old_name);
160  if (!robot.empty())
161  q->append(ROBOT_NAME, robot);
162  if (!group.empty())
163  q->append(CONSTRAINTS_GROUP_NAME, group);
164  Metadata::Ptr m = constraints_collection_->createMetadata();
165  m->append(CONSTRAINTS_ID_NAME, new_name);
166  constraints_collection_->modifyMetadata(q, m);
167  RCLCPP_DEBUG(logger_, "Renamed constraints from '%s' to '%s'", old_name.c_str(), new_name.c_str());
168 }
169 
171  const std::string& robot,
172  const std::string& group)
173 {
174  Query::Ptr q = constraints_collection_->createQuery();
175  q->append(CONSTRAINTS_ID_NAME, name);
176  if (!robot.empty())
177  q->append(ROBOT_NAME, robot);
178  if (!group.empty())
179  q->append(CONSTRAINTS_GROUP_NAME, group);
180  unsigned int rem = constraints_collection_->removeMessages(q);
181  RCLCPP_DEBUG(logger_, "Removed %u TrajectoryConstraints messages (named '%s')", rem, name.c_str());
182 }
This class provides the mechanism to connect to a database and reads needed ROS parameters when appro...
TrajectoryConstraintsStorage(warehouse_ros::DatabaseConnection::Ptr conn)
bool hasTrajectoryConstraints(const std::string &name, const std::string &robot="", const std::string &group="") const
void renameTrajectoryConstraints(const std::string &old_name, const std::string &new_name, const std::string &robot="", const std::string &group="")
void getKnownTrajectoryConstraints(std::vector< std::string > &names, const std::string &robot="", const std::string &group="") const
void removeTrajectoryConstraints(const std::string &name, const std::string &robot="", const std::string &group="")
void addTrajectoryConstraints(const moveit_msgs::msg::TrajectoryConstraints &msg, const std::string &name, const std::string &robot="", const std::string &group="")
bool getTrajectoryConstraints(TrajectoryConstraintsWithMetadata &msg_m, const std::string &name, const std::string &robot="", const std::string &group="") const
Get the constraints named name. Return false on failure.
warehouse_ros::MessageWithMetadata< moveit_msgs::msg::TrajectoryConstraints >::ConstPtr TrajectoryConstraintsWithMetadata
Main namespace for MoveIt.
Definition: exceptions.h:43
name
Definition: setup.py:7