moveit2
The MoveIt Motion Planning Framework for ROS 2.
state_storage.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2012, 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: Ioan Sucan */
36 
38 
39 #include <utility>
40 
41 const std::string moveit_warehouse::RobotStateStorage::DATABASE_NAME = "moveit_robot_states";
42 
43 const std::string moveit_warehouse::RobotStateStorage::STATE_NAME = "state_id";
44 const std::string moveit_warehouse::RobotStateStorage::ROBOT_NAME = "robot_id";
45 
46 static const rclcpp::Logger LOGGER = rclcpp::get_logger("moveit.ros.warehouse.state_storage");
47 
48 using warehouse_ros::Metadata;
49 using warehouse_ros::Query;
50 
51 moveit_warehouse::RobotStateStorage::RobotStateStorage(warehouse_ros::DatabaseConnection::Ptr conn)
52  : MoveItMessageStorage(std::move(conn))
53 {
54  createCollections();
55 }
56 
57 void moveit_warehouse::RobotStateStorage::createCollections()
58 {
59  state_collection_ = conn_->openCollectionPtr<moveit_msgs::msg::RobotState>(DATABASE_NAME, "robot_states");
60 }
61 
63 {
64  state_collection_.reset();
65  conn_->dropDatabase(DATABASE_NAME);
66  createCollections();
67 }
68 
69 void moveit_warehouse::RobotStateStorage::addRobotState(const moveit_msgs::msg::RobotState& msg,
70  const std::string& name, const std::string& robot)
71 {
72  bool replace = false;
73  if (hasRobotState(name, robot))
74  {
75  removeRobotState(name, robot);
76  replace = true;
77  }
78  Metadata::Ptr metadata = state_collection_->createMetadata();
79  metadata->append(STATE_NAME, name);
80  metadata->append(ROBOT_NAME, robot);
81  state_collection_->insert(msg, metadata);
82  RCLCPP_DEBUG(LOGGER, "%s robot state '%s'", replace ? "Replaced" : "Added", name.c_str());
83 }
84 
85 bool moveit_warehouse::RobotStateStorage::hasRobotState(const std::string& name, const std::string& robot) const
86 {
87  Query::Ptr q = state_collection_->createQuery();
88  q->append(STATE_NAME, name);
89  if (!robot.empty())
90  q->append(ROBOT_NAME, robot);
91  std::vector<RobotStateWithMetadata> constr = state_collection_->queryList(q, true);
92  return !constr.empty();
93 }
94 
95 void moveit_warehouse::RobotStateStorage::getKnownRobotStates(const std::string& regex, std::vector<std::string>& names,
96  const std::string& robot) const
97 {
98  getKnownRobotStates(names, robot);
99  filterNames(regex, names);
100 }
101 
102 void moveit_warehouse::RobotStateStorage::getKnownRobotStates(std::vector<std::string>& names,
103  const std::string& robot) const
104 {
105  names.clear();
106  Query::Ptr q = state_collection_->createQuery();
107  if (!robot.empty())
108  q->append(ROBOT_NAME, robot);
109  std::vector<RobotStateWithMetadata> constr = state_collection_->queryList(q, true, STATE_NAME, true);
110  for (RobotStateWithMetadata& state : constr)
111  if (state->lookupField(STATE_NAME))
112  names.push_back(state->lookupString(STATE_NAME));
113 }
114 
116  const std::string& robot) const
117 {
118  Query::Ptr q = state_collection_->createQuery();
119  q->append(STATE_NAME, name);
120  if (!robot.empty())
121  q->append(ROBOT_NAME, robot);
122  std::vector<RobotStateWithMetadata> constr = state_collection_->queryList(q, false);
123  if (constr.empty())
124  return false;
125  else
126  {
127  msg_m = constr.front();
128  return true;
129  }
130 }
131 
132 void moveit_warehouse::RobotStateStorage::renameRobotState(const std::string& old_name, const std::string& new_name,
133  const std::string& robot)
134 {
135  Query::Ptr q = state_collection_->createQuery();
136  q->append(STATE_NAME, old_name);
137  if (!robot.empty())
138  q->append(ROBOT_NAME, robot);
139  Metadata::Ptr m = state_collection_->createMetadata();
140  m->append(STATE_NAME, new_name);
141  state_collection_->modifyMetadata(q, m);
142  RCLCPP_DEBUG(LOGGER, "Renamed robot state from '%s' to '%s'", old_name.c_str(), new_name.c_str());
143 }
144 
145 void moveit_warehouse::RobotStateStorage::removeRobotState(const std::string& name, const std::string& robot)
146 {
147  Query::Ptr q = state_collection_->createQuery();
148  q->append(STATE_NAME, name);
149  if (!robot.empty())
150  q->append(ROBOT_NAME, robot);
151  unsigned int rem = state_collection_->removeMessages(q);
152  RCLCPP_DEBUG(LOGGER, "Removed %u RobotState messages (named '%s')", rem, name.c_str());
153 }
This class provides the mechanism to connect to a database and reads needed ROS parameters when appro...
void renameRobotState(const std::string &old_name, const std::string &new_name, const std::string &robot="")
bool hasRobotState(const std::string &name, const std::string &robot="") const
static const std::string DATABASE_NAME
Definition: state_storage.h:55
void getKnownRobotStates(std::vector< std::string > &names, const std::string &robot="") const
bool getRobotState(RobotStateWithMetadata &msg_m, const std::string &name, const std::string &robot="") const
Get the constraints named name. Return false on failure.
void removeRobotState(const std::string &name, const std::string &robot="")
static const std::string STATE_NAME
Definition: state_storage.h:57
RobotStateStorage(warehouse_ros::DatabaseConnection::Ptr conn)
void addRobotState(const moveit_msgs::msg::RobotState &msg, const std::string &name, const std::string &robot="")
static const std::string ROBOT_NAME
Definition: state_storage.h:58
warehouse_ros::MessageWithMetadata< moveit_msgs::msg::RobotState >::ConstPtr RobotStateWithMetadata
Definition: state_storage.h:47
robot
Definition: pick.py:53
name
Definition: setup.py:7
const rclcpp::Logger LOGGER
Definition: async_test.h:31