warehouse_services.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: Dan Greenwald */
36 
37 #include <ros/ros.h>
40 #include <moveit_msgs/SaveRobotStateToWarehouse.h>
41 #include <moveit_msgs/ListRobotStatesInWarehouse.h>
42 #include <moveit_msgs/GetRobotStateFromWarehouse.h>
43 #include <moveit_msgs/CheckIfRobotStateExistsInWarehouse.h>
44 #include <moveit_msgs/DeleteRobotStateFromWarehouse.h>
45 #include <moveit_msgs/RenameRobotStateInWarehouse.h>
46 
47 static const std::string ROBOT_DESCRIPTION = "robot_description";
48 
49 bool storeState(moveit_msgs::SaveRobotStateToWarehouse::Request& request,
50  moveit_msgs::SaveRobotStateToWarehouse::Response& response, moveit_warehouse::RobotStateStorage& rs)
51 {
52  if (request.name.empty())
53  {
54  ROS_ERROR("You must specify a name to store a state");
55  return (response.success = false);
56  }
57  rs.addRobotState(request.state, request.name, request.robot);
58  return (response.success = true);
59 }
60 
61 bool listStates(moveit_msgs::ListRobotStatesInWarehouse::Request& request,
62  moveit_msgs::ListRobotStatesInWarehouse::Response& response, moveit_warehouse::RobotStateStorage& rs)
63 {
64  if (request.regex.empty())
65  {
66  rs.getKnownRobotStates(response.states, request.robot);
67  }
68  else
69  {
70  rs.getKnownRobotStates(request.regex, response.states, request.robot);
71  }
72  return true;
73 }
74 
75 bool hasState(moveit_msgs::CheckIfRobotStateExistsInWarehouse::Request& request,
76  moveit_msgs::CheckIfRobotStateExistsInWarehouse::Response& response,
78 {
79  response.exists = rs.hasRobotState(request.name, request.robot);
80  return true;
81 }
82 
83 bool getState(moveit_msgs::GetRobotStateFromWarehouse::Request& request,
84  moveit_msgs::GetRobotStateFromWarehouse::Response& response, moveit_warehouse::RobotStateStorage& rs)
85 {
86  if (!rs.hasRobotState(request.name, request.robot))
87  {
88  ROS_ERROR_STREAM("No state called '" << request.name << "' for robot '" << request.robot << "'.");
89  moveit_msgs::RobotState dummy;
90  response.state = dummy;
91  return false;
92  }
94  rs.getRobotState(state_buffer, request.name, request.robot);
95  response.state = static_cast<const moveit_msgs::RobotState&>(*state_buffer);
96  return true;
97 }
98 
99 bool renameState(moveit_msgs::RenameRobotStateInWarehouse::Request& request,
100  moveit_msgs::RenameRobotStateInWarehouse::Response& /*response*/,
102 {
103  if (!rs.hasRobotState(request.old_name, request.robot))
104  {
105  ROS_ERROR_STREAM("No state called '" << request.old_name << "' for robot '" << request.robot << "'.");
106  return false;
107  }
108  rs.renameRobotState(request.old_name, request.new_name, request.robot);
109  return true;
110 }
111 
112 bool deleteState(moveit_msgs::DeleteRobotStateFromWarehouse::Request& request,
113  moveit_msgs::DeleteRobotStateFromWarehouse::Response& /*response*/,
115 {
116  if (!rs.hasRobotState(request.name, request.robot))
117  {
118  ROS_ERROR_STREAM("No state called '" << request.name << "' for robot '" << request.robot << "'.");
119  return false;
120  }
121  rs.removeRobotState(request.name, request.robot);
122  return true;
123 }
124 
125 // helper to setup service servers for various RobotStateStorage manipulators
126 template <typename S, typename Fn>
128  const std::string& name, const Fn& fn)
129 {
130  return node.advertiseService<typename S::Request, typename S::Response>(name, [&rs, fn](auto& req, auto& res) {
131  return fn(req, res, rs);
132  });
133 };
134 
135 int main(int argc, char** argv)
136 {
137  ros::init(argc, argv, "moveit_warehouse_services");
138 
140  spinner.start();
141 
142  ros::NodeHandle node;
143  std::string host;
144 
145  int port;
146  float connection_timeout;
147  int connection_retries;
148 
149  node.param<std::string>("warehouse_host", host, "localhost");
150  node.param<int>("warehouse_port", port, 33829);
151  node.param<float>("warehouse_db_connection_timeout", connection_timeout, 5.0);
152  node.param<int>("warehouse_db_connection_retries", connection_retries, 5);
153 
154  std::unique_ptr<warehouse_ros::DatabaseLoader> db_loader;
156 
157  try
158  {
159  db_loader = std::make_unique<warehouse_ros::DatabaseLoader>();
160  conn = db_loader->loadDatabase();
161  conn->setParams(host, port, connection_timeout);
162 
163  ROS_INFO("Connecting to warehouse on %s:%d", host.c_str(), port);
164  int tries = 0;
165  while (!conn->connect())
166  {
167  ++tries;
168  ROS_WARN("Failed to connect to DB on %s:%d (try %d/%d).", host.c_str(), port, tries, connection_retries);
169  if (tries == connection_retries)
170  {
171  ROS_FATAL("Failed to connect too many times, giving up");
172  return 1;
173  }
174  }
175  }
176  catch (std::exception& ex)
177  {
178  ROS_ERROR("%s", ex.what());
179  return 1;
180  }
181 
183 
184  std::vector<std::string> names;
185  rs.getKnownRobotStates(names);
186  if (names.empty())
187  ROS_INFO("There are no previously stored robot states");
188  else
189  {
190  ROS_INFO("Previously stored robot states:");
191  for (const std::string& name : names)
192  ROS_INFO(" * %s", name.c_str());
193  }
194 
195  ros::ServiceServer save_state_server = node.advertiseService<moveit_msgs::SaveRobotStateToWarehouse::Request,
196  moveit_msgs::SaveRobotStateToWarehouse::Response>(
197  "save_robot_state", [&rs](auto& req, auto& res) { return storeState(req, res, rs); });
198 
199  ros::ServiceServer list_state_server = node.advertiseService<moveit_msgs::ListRobotStatesInWarehouse::Request,
200  moveit_msgs::ListRobotStatesInWarehouse::Response>(
201  "list_robot_state", [&rs](auto& req, auto& res) { return listStates(req, res, rs); });
202 
203  ros::ServiceServer get_state_server = node.advertiseService<moveit_msgs::GetRobotStateFromWarehouse::Request,
204  moveit_msgs::GetRobotStateFromWarehouse::Response>(
205  "get_robot_state", [&rs](auto& req, auto& res) { return getState(req, res, rs); });
206 
207  ros::ServiceServer has_state_server =
208  node.advertiseService<moveit_msgs::CheckIfRobotStateExistsInWarehouse::Request,
209  moveit_msgs::CheckIfRobotStateExistsInWarehouse::Response>(
210  "has_robot_state", [&rs](auto& req, auto& res) { return hasState(req, res, rs); });
211 
212  ros::ServiceServer rename_state_server = node.advertiseService<moveit_msgs::RenameRobotStateInWarehouse::Request,
213  moveit_msgs::RenameRobotStateInWarehouse::Response>(
214  "rename_robot_state", [&rs](auto& req, auto& res) { return renameState(req, res, rs); });
215 
216  ros::ServiceServer delete_state_server = node.advertiseService<moveit_msgs::DeleteRobotStateFromWarehouse::Request,
217  moveit_msgs::DeleteRobotStateFromWarehouse::Response>(
218  "delete_robot_state", [&rs](auto& req, auto& res) { return deleteState(req, res, rs); });
219 
221  return 0;
222 }
response
const std::string response
renameState
bool renameState(moveit_msgs::RenameRobotStateInWarehouse::Request &request, moveit_msgs::RenameRobotStateInWarehouse::Response &, moveit_warehouse::RobotStateStorage &rs)
Definition: warehouse_services.cpp:99
ROS_ERROR_STREAM
#define ROS_ERROR_STREAM(args)
boost::shared_ptr
moveit_warehouse::RobotStateStorage
Definition: state_storage.h:82
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
ros.h
moveit_warehouse::RobotStateStorage::addRobotState
void addRobotState(const moveit_msgs::RobotState &msg, const std::string &name, const std::string &robot="")
Definition: state_storage.cpp:67
moveit_warehouse::RobotStateStorage::hasRobotState
bool hasRobotState(const std::string &name, const std::string &robot="") const
Definition: state_storage.cpp:83
state_storage.h
ros::AsyncSpinner
ros::NodeHandle::advertiseService
ServiceServer advertiseService(AdvertiseServiceOptions &ops)
ROBOT_DESCRIPTION
static const std::string ROBOT_DESCRIPTION
Definition: warehouse_services.cpp:47
database_loader.h
moveit_warehouse::RobotStateStorage::renameRobotState
void renameRobotState(const std::string &old_name, const std::string &new_name, const std::string &robot="")
Definition: state_storage.cpp:130
ros::ServiceServer
spinner
void spinner()
name
std::string name
getState
bool getState(moveit_msgs::GetRobotStateFromWarehouse::Request &request, moveit_msgs::GetRobotStateFromWarehouse::Response &response, moveit_warehouse::RobotStateStorage &rs)
Definition: warehouse_services.cpp:83
storeState
bool storeState(moveit_msgs::SaveRobotStateToWarehouse::Request &request, moveit_msgs::SaveRobotStateToWarehouse::Response &response, moveit_warehouse::RobotStateStorage &rs)
Definition: warehouse_services.cpp:49
ROS_ERROR
#define ROS_ERROR(...)
ROS_WARN
#define ROS_WARN(...)
moveit_warehouse::RobotStateStorage::getKnownRobotStates
void getKnownRobotStates(std::vector< std::string > &names, const std::string &robot="") const
Definition: state_storage.cpp:100
ROS_FATAL
#define ROS_FATAL(...)
deleteState
bool deleteState(moveit_msgs::DeleteRobotStateFromWarehouse::Request &request, moveit_msgs::DeleteRobotStateFromWarehouse::Response &, moveit_warehouse::RobotStateStorage &rs)
Definition: warehouse_services.cpp:112
ros::waitForShutdown
ROSCPP_DECL void waitForShutdown()
moveit_warehouse::RobotStateStorage::removeRobotState
void removeRobotState(const std::string &name, const std::string &robot="")
Definition: state_storage.cpp:143
ros::NodeHandle::param
T param(const std::string &param_name, const T &default_val) const
listStates
bool listStates(moveit_msgs::ListRobotStatesInWarehouse::Request &request, moveit_msgs::ListRobotStatesInWarehouse::Response &response, moveit_warehouse::RobotStateStorage &rs)
Definition: warehouse_services.cpp:61
createServiceServer
ros::ServiceServer createServiceServer(moveit_warehouse::RobotStateStorage &rs, ros::NodeHandle &node, const std::string &name, const Fn &fn)
Definition: warehouse_services.cpp:127
hasState
bool hasState(moveit_msgs::CheckIfRobotStateExistsInWarehouse::Request &request, moveit_msgs::CheckIfRobotStateExistsInWarehouse::Response &response, moveit_warehouse::RobotStateStorage &rs)
Definition: warehouse_services.cpp:75
ROS_INFO
#define ROS_INFO(...)
moveit_warehouse::RobotStateStorage::getRobotState
bool getRobotState(RobotStateWithMetadata &msg_m, const std::string &name, const std::string &robot="") const
Get the constraints named name. Return false on failure.
Definition: state_storage.cpp:113
ros::NodeHandle
main
int main(int argc, char **argv)
Definition: warehouse_services.cpp:135


warehouse
Author(s): Ioan Sucan
autogenerated on Mon May 27 2024 02:28:07