00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include <utility>
00038 #include <algorithm>
00039 #include <string>
00040 #include <functional>
00041
00042 #include <time.h>
00043
00044 #include <boost/foreach.hpp>
00045 #include <boost/lexical_cast.hpp>
00046
00047 #include <boost/shared_ptr.hpp>
00048 using boost::shared_ptr;
00049
00050 #include <ros/ros.h>
00051
00052 #include <object_manipulation_msgs/Grasp.h>
00053 #include <object_manipulation_msgs/GraspableObject.h>
00054 #include <object_manipulation_msgs/GraspPlanning.h>
00055
00056 #include <object_manipulator/tools/grasp_marker_publisher.h>
00057
00058 #include <household_objects_database/objects_database.h>
00059 using household_objects_database::ObjectsDatabasePtr;
00060
00061 #include "probabilistic_grasp_planner/probabilistic_grasp_planner.h"
00062
00063 namespace probabilistic_grasp_planner {
00064
00065 static const std::string PLANNING_MARKERS_OUT_NAME = "grasp_planning_markers";
00066
00067 class ProbabilisticGraspPlannerNode
00068 {
00069 private:
00070
00071 ros::NodeHandle priv_nh_;
00072 ros::NodeHandle root_nh_;
00073
00075 ros::ServiceClient object_detection_srv_;
00077 ros::ServiceClient grasp_planning_srv_;
00078
00080 ros::ServiceServer grasp_execution_srv_;
00081
00083 ros::ServiceServer grasp_test_srv_;
00084
00086 object_manipulator::GraspMarkerPublisher debug_grasp_marker_pub_;
00087
00089 object_manipulator::GraspMarkerPublisher results_grasp_marker_pub_;
00090
00092 object_manipulator::GraspMarkerPublisher rank_grasp_marker_pub_;
00093
00094 ObjectsDatabasePtr database_;
00095
00096 bool show_colored_grasps_;
00097
00099 ProbabilisticGraspPlannerPtr planner_;
00100
00102 bool initializeDatabase();
00103
00105 bool graspPlanningCB(object_manipulation_msgs::GraspPlanning::Request &request,
00106 object_manipulation_msgs::GraspPlanning::Response &response);
00107
00109 bool graspTestCB(object_manipulation_msgs::GraspPlanning::Request &request,
00110 object_manipulation_msgs::GraspPlanning::Response &response);
00111
00112
00113 void visualizeGrasps(const geometry_msgs::PoseStamped &grasp_frame,
00114 const std::vector<object_manipulation_msgs::Grasp> &grasps);
00115
00116 public:
00117 ProbabilisticGraspPlannerNode();
00118 virtual ~ProbabilisticGraspPlannerNode();
00119 };
00120
00121 ProbabilisticGraspPlannerNode::ProbabilisticGraspPlannerNode(): priv_nh_("~"),
00122 root_nh_(""),
00123 debug_grasp_marker_pub_(PLANNING_MARKERS_OUT_NAME, "", 5.0),
00124 results_grasp_marker_pub_("final_sorted_grasp_list", "", 5.0),
00125 rank_grasp_marker_pub_("debug_ranked_grasp_list", "", 5.0)
00126
00127 {
00128 if ( !initializeDatabase() ) ros::shutdown();
00129 planner_.reset(new ProbabilisticGraspPlanner(database_));
00130
00131 planner_->setMarkerPublisher(&results_grasp_marker_pub_, &debug_grasp_marker_pub_, &rank_grasp_marker_pub_);
00132
00133 grasp_execution_srv_ = root_nh_.advertiseService("probabilistic_grasp_planning",
00134 &ProbabilisticGraspPlannerNode::graspPlanningCB, this);
00135
00136 grasp_test_srv_ = root_nh_.advertiseService("test_grasp_on_probabilistic_planner",
00137 &ProbabilisticGraspPlannerNode::graspTestCB, this);
00138 priv_nh_.param<bool>("show_grasps",show_colored_grasps_,false);
00139
00140 ROS_INFO("Robust grasp planner service ready");
00141 }
00142
00143 bool ProbabilisticGraspPlannerNode::initializeDatabase()
00144 {
00145
00146 std::string database_host, database_port, database_user, database_pass, database_name;
00147 root_nh_.param<std::string> ("household_objects_database/database_host", database_host, "uninitialized");
00148
00149 int port_int;
00150 root_nh_.param<int> ("household_objects_database/database_port", port_int, -1);
00151
00152 std::stringstream ss;
00153 ss << port_int;
00154 database_port = ss.str();
00155 root_nh_.param<std::string> ("household_objects_database/database_user", database_user, "uninitialized");
00156 root_nh_.param<std::string> ("household_objects_database/database_pass", database_pass, "uninitialized");
00157 root_nh_.param<std::string> ("household_objects_database/database_name", database_name, "uninitialized");
00158 database_.reset(new household_objects_database::ObjectsDatabase(database_host, database_port, database_user,
00159 database_pass, database_name));
00160 if (!database_->isConnected())
00161 {
00162 ROS_ERROR("Failed to open model database on host %s, port %s, user %s with password %s, database %s",
00163 database_host.c_str(), database_port.c_str(), database_user.c_str(), database_pass.c_str(),
00164 database_name.c_str());
00165 database_.reset();
00166 return false;
00167 }
00168 return true;
00169 }
00170
00171 bool ProbabilisticGraspPlannerNode::graspPlanningCB(object_manipulation_msgs::GraspPlanning::Request &request,
00172 object_manipulation_msgs::GraspPlanning::Response &response)
00173 {
00174
00175 if (request.grasps_to_evaluate.size() > 0)
00176 {
00177 ROS_WARN("Grasps in test list, but grasp planner was called");
00178 }
00179 else
00180 {
00181 bool prune_grasps = true;
00182 planner_->plan(request.arm_name, request.target, response.grasps, show_colored_grasps_,prune_grasps);
00183 }
00184 ROS_INFO("Probabilistic grasp planner: returning %zd grasps", response.grasps.size());
00185 response.error_code.value = response.error_code.SUCCESS;
00186 return true;
00187 }
00188
00189 bool ProbabilisticGraspPlannerNode::graspTestCB(object_manipulation_msgs::GraspPlanning::Request &request,
00190 object_manipulation_msgs::GraspPlanning::Response &response)
00191 {
00192
00193 if (request.grasps_to_evaluate.size() > 0)
00194 {
00195 bool prune_grasps = false;
00196 planner_->plan(request.arm_name, request.target, request.grasps_to_evaluate, show_colored_grasps_, prune_grasps);
00197 response.grasps = request.grasps_to_evaluate;
00198 }
00199 else
00200 {
00201 ROS_WARN("Grasp test service called but no grasps given to test");
00202 }
00203 ROS_INFO("Probabilistic grasp planner: tested %zd grasps", response.grasps.size());
00204 response.error_code.value = response.error_code.SUCCESS;
00205 return true;
00206 }
00207
00208
00209 ProbabilisticGraspPlannerNode::~ProbabilisticGraspPlannerNode()
00210 {
00211 }
00212
00213 }
00214
00215 int main(int argc, char** argv)
00216 {
00217 ros::init(argc, argv, "probabilistic_grasp_planner_node");
00218 probabilistic_grasp_planner::ProbabilisticGraspPlannerNode node;
00219 ros::spin();
00220 return 0;
00221 }