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>
49 bool storeState(moveit_msgs::SaveRobotStateToWarehouse::Request& request,
52 if (request.name.empty())
54 ROS_ERROR(
"You must specify a name to store a state");
61 bool listStates(moveit_msgs::ListRobotStatesInWarehouse::Request& request,
64 if (request.regex.empty())
75 bool hasState(moveit_msgs::CheckIfRobotStateExistsInWarehouse::Request& request,
76 moveit_msgs::CheckIfRobotStateExistsInWarehouse::Response& response,
83 bool getState(moveit_msgs::GetRobotStateFromWarehouse::Request& request,
88 ROS_ERROR_STREAM(
"No state called '" << request.name <<
"' for robot '" << request.robot <<
"'.");
89 moveit_msgs::RobotState dummy;
95 response.state =
static_cast<const moveit_msgs::RobotState&
>(*state_buffer);
99 bool renameState(moveit_msgs::RenameRobotStateInWarehouse::Request& request,
100 moveit_msgs::RenameRobotStateInWarehouse::Response& ,
105 ROS_ERROR_STREAM(
"No state called '" << request.old_name <<
"' for robot '" << request.robot <<
"'.");
112 bool deleteState(moveit_msgs::DeleteRobotStateFromWarehouse::Request& request,
113 moveit_msgs::DeleteRobotStateFromWarehouse::Response& ,
118 ROS_ERROR_STREAM(
"No state called '" << request.name <<
"' for robot '" << request.robot <<
"'.");
126 template <
typename S,
typename Fn>
128 const std::string& name,
const Fn& fn)
130 return node.
advertiseService<
typename S::Request,
typename S::Response>(
name, [&rs, fn](
auto& req,
auto& res) {
131 return fn(req, res, rs);
135 int main(
int argc,
char** argv)
137 ros::init(argc, argv,
"moveit_warehouse_services");
146 float connection_timeout;
147 int connection_retries;
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);
154 std::unique_ptr<warehouse_ros::DatabaseLoader> db_loader;
159 db_loader = std::make_unique<warehouse_ros::DatabaseLoader>();
160 conn = db_loader->loadDatabase();
161 conn->setParams(host, port, connection_timeout);
163 ROS_INFO(
"Connecting to warehouse on %s:%d", host.c_str(), port);
165 while (!conn->connect())
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)
171 ROS_FATAL(
"Failed to connect too many times, giving up");
176 catch (std::exception& ex)
184 std::vector<std::string> names;
187 ROS_INFO(
"There are no previously stored robot states");
190 ROS_INFO(
"Previously stored robot states:");
191 for (
const std::string&
name : names)
196 moveit_msgs::SaveRobotStateToWarehouse::Response>(
197 "save_robot_state", [&rs](
auto& req,
auto& res) {
return storeState(req, res, rs); });
200 moveit_msgs::ListRobotStatesInWarehouse::Response>(
201 "list_robot_state", [&rs](
auto& req,
auto& res) {
return listStates(req, res, rs); });
204 moveit_msgs::GetRobotStateFromWarehouse::Response>(
205 "get_robot_state", [&rs](
auto& req,
auto& res) {
return getState(req, res, rs); });
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); });
213 moveit_msgs::RenameRobotStateInWarehouse::Response>(
214 "rename_robot_state", [&rs](
auto& req,
auto& res) {
return renameState(req, res, rs); });
217 moveit_msgs::DeleteRobotStateFromWarehouse::Response>(
218 "delete_robot_state", [&rs](
auto& req,
auto& res) {
return deleteState(req, res, rs); });