EigenGraspPlannerClient.cpp
Go to the documentation of this file.
00001 #include <grasp_planning_graspit_ros/EigenGraspPlannerClient.h>
00002 #include <grasp_planning_graspit_ros/LogBindingROS.h>
00003 #include <grasp_planning_graspit_ros/WriteToFile.h>
00004 #include <geometry_msgs/Pose.h>
00005 
00006 #define DEFAULT_ADD_DB_TOPIC "graspit_add_to_db"
00007 #define DEFAULT_LOAD_MODEL_TOPIC "graspit_add_to_db"
00008 #define DEFAULT_SAVE_WORLD_TOPIC "graspit_save_world"
00009 #define DEFAULT_EGPLANNING_TOPIC "graspit_eg_planning"
00010 
00011 
00012 using grasp_planning_graspit_ros::EigenGraspPlannerClient;
00013 
00014 EigenGraspPlannerClient::EigenGraspPlannerClient():
00015     initialized(false)
00016 {
00017     init();
00018 }
00019 EigenGraspPlannerClient::~EigenGraspPlannerClient(){}
00020     
00021 bool EigenGraspPlannerClient::isOK()
00022 {
00023     return initialized && 
00024             eg_planner_client.exists() && eg_planner_client.isValid() &&
00025             add_to_db_client.exists() && add_to_db_client.isValid() &&
00026             load_model_client.exists() && load_model_client.isValid();
00027 }
00028 
00029 void EigenGraspPlannerClient::init()
00030 {
00031     ros::NodeHandle priv("~");
00032     if (!priv.hasParam("add_to_db_service") ||
00033         !priv.hasParam("load_model_service") ||
00034         !priv.hasParam("save_world_service") ||
00035         !priv.hasParam("eg_planning_service"))
00036     {
00037         ROS_ERROR("Have to define the GraspIt! service types as ROS parameters. Won't be able to use EigenGraspPlannerClient.");
00038         return;
00039     }
00040     priv.param<std::string>("add_to_db_service", addToDBService, DEFAULT_ADD_DB_TOPIC);
00041     PRINTMSG("Using add_to_db_service: " << addToDBService);
00042     priv.param<std::string>("load_model_service", loadModelService, DEFAULT_LOAD_MODEL_TOPIC);
00043     PRINTMSG("Using load_model_service: " << loadModelService);
00044     priv.param<std::string>("save_world_service", saveWorldService, DEFAULT_SAVE_WORLD_TOPIC);
00045     PRINTMSG("Using save_world_service: " << saveWorldService);
00046     priv.param<std::string>("eg_planning_service", egPlanningService, DEFAULT_EGPLANNING_TOPIC);
00047     PRINTMSG("Using eg_planning_service: " << egPlanningService);
00048    
00049     add_to_db_client = node.serviceClient<grasp_planning_graspit_msgs::AddToDatabase>(addToDBService);
00050     load_model_client = node.serviceClient<grasp_planning_graspit_msgs::LoadDatabaseModel>(loadModelService);
00051     eg_planner_client = node.serviceClient<moveit_msgs::GraspPlanning>(egPlanningService);
00052     initialized = true;
00053 }
00054 
00055 
00056 int EigenGraspPlannerClient::addRobot(const std::string& modelName, const std::string filename, const std::vector<std::string>& jointNames)
00057 {
00058     if (!isOK())
00059     {
00060         ROS_ERROR("EigenGraspPlannerClient not isOK() properly.");
00061         return -3;
00062     }
00063     PRINTMSG("Adding robot name " << modelName << " in file " << filename);
00064     grasp_planning_graspit_msgs::AddToDatabase srv;
00065     srv.request.filename = filename;
00066     srv.request.isRobot = true;
00067     srv.request.asGraspable = false;
00068     srv.request.modelName = modelName;
00069     srv.request.jointNames = jointNames;
00070 
00071     if (!add_to_db_client.call(srv))
00072     {
00073         PRINTERROR("Failed to call service");
00074         return -1;
00075     }
00076 
00077     if (srv.response.returnCode != grasp_planning_graspit_msgs::AddToDatabase::Response::SUCCESS)
00078     {
00079         PRINTERROR("Could not add the robot to the database. Return code " << srv.response.returnCode);
00080         return -2;
00081     }
00082 
00083     PRINTMSG("Successfully added model to database, got model ID=" << srv.response.modelID);
00084     return srv.response.modelID;
00085 }
00086 
00087 int EigenGraspPlannerClient::addObject(const std::string& modelName, const std::string filename, bool objectGraspable)
00088 {
00089     if (!isOK())
00090     {
00091         ROS_ERROR("EigenGraspPlannerClient not isOK() properly.");
00092         return -3;
00093     }
00094     PRINTMSG("Adding object name " << modelName << " in file " << filename);
00095     grasp_planning_graspit_msgs::AddToDatabase srv;
00096     srv.request.filename = filename;
00097     srv.request.isRobot = false;
00098     srv.request.asGraspable = objectGraspable;
00099     srv.request.modelName = modelName;
00100 
00101     if (!add_to_db_client.call(srv))
00102     {
00103         PRINTERROR("Failed to call service");
00104         return -1;
00105     }
00106 
00107     if (srv.response.returnCode != grasp_planning_graspit_msgs::AddToDatabase::Response::SUCCESS)
00108     {
00109         PRINTERROR("Could not add the object to the database. Return code " << srv.response.returnCode);
00110         return -2;
00111     }
00112 
00113     PRINTMSG("Successfully added object to database, got model ID=" << srv.response.modelID);
00114     return srv.response.modelID;
00115 } 
00116 
00117 int EigenGraspPlannerClient::loadModel(const int modelID, bool clearWorld, const geometry_msgs::Pose& modelPose)
00118 {
00119     if (!isOK())
00120     {
00121         ROS_ERROR("EigenGraspPlannerClient not isOK() properly.");
00122         return -3;
00123     }
00124     //PRINTMSG("Loading model " << modelID << " to graspit world, at pose "<<modelPose<<std::endl<<"Clear others: " << clearWorld);
00125 
00126     grasp_planning_graspit_msgs::LoadDatabaseModel srv;
00127     srv.request.model_id = modelID;
00128     srv.request.model_pose = modelPose;
00129     srv.request.clear_other_models = clearWorld;
00130 
00131     if (!load_model_client.call(srv))
00132     {
00133         PRINTERROR("Failed to call service");
00134         return -1;
00135     }
00136 
00137     if (srv.response.result != grasp_planning_graspit_msgs::LoadDatabaseModel::Response::LOAD_SUCCESS)
00138     {
00139         PRINTERROR("Could load model ID=" << modelID);
00140         return -2;
00141     }
00142 
00143     PRINTMSG("Successfully loaded model ID=" << modelID);
00144     return 0;
00145 }
00146 
00147 
00148 int EigenGraspPlannerClient::plan(const std::string robotModelName, const int objectID,
00149     const geometry_msgs::Pose * newObjectPose,
00150     const std::string& resultsOutputDirectory,
00151     std::vector<moveit_msgs::Grasp>& results)
00152 {
00153     if (!isOK())
00154     {
00155         ROS_ERROR("EigenGraspPlannerClient not isOK() properly.");
00156         return -4;
00157     }
00158     if (resultsOutputDirectory.empty())
00159     {
00160         PRINTMSG("No output path configured, results will be returned in vector only");
00161     }
00162     else if (!makeDirectoryIfNeeded(resultsOutputDirectory))
00163     {
00164         PRINTERROR("Could not create directory "<<resultsOutputDirectory);
00165         return -1;
00166     }
00167 
00168     PRINTMSG("Planning for robot ID=" << robotModelName << " to grasp object ID=" << objectID);
00169 
00170     // Should use a client here to query the database for information about the
00171     // object type. For now, object type information is not used in the planning request,
00172     // as the service looks up the object type itself. So we can leave these on arbitrary values.
00173     object_recognition_msgs::ObjectType dbModelType;
00174     dbModelType.key =  "NotAvailabeYet";
00175     dbModelType.db = "SimpleGraspItDatabase";
00176 
00177     // Here we can set a different pose to put the object at in the current
00178     // graspit world. If this reference frame is "0",
00179     // it uses the object's current pose in the GraspIt! world. If it is "1",
00180     // we will use the pose specified here.
00181     geometry_msgs::PoseStamped modelPose;
00182     if (newObjectPose != NULL)
00183     {
00184         modelPose.pose = *newObjectPose;
00185         modelPose.header.frame_id = "1";
00186     }
00187     else
00188     {
00189         modelPose.header.frame_id = "0";
00190     }
00191 
00192     moveit_msgs::CollisionObject obj;
00193     obj.header = modelPose.header;
00194     obj.id = std::to_string(objectID);
00195     obj.type = dbModelType;
00196     obj.primitive_poses.push_back(modelPose.pose);
00197     PRINTWARN("Temporary hack: model pose for GraspPlanning service is "
00198       << "passed in first primitive pose. This should be handled by /tf "
00199       << "in future. See also issue #40.");
00200 
00201     moveit_msgs::GraspPlanning srv;
00202     srv.request.group_name = robotModelName;
00203     srv.request.target = obj;
00204 
00205     if (!eg_planner_client.call(srv))
00206     {
00207         PRINTERROR("Failed to call service");
00208         return -2;
00209     }
00210 
00211     if (srv.response.error_code.val != moveit_msgs::MoveItErrorCodes::SUCCESS)
00212     {
00213         PRINTERROR("Could do the grasp planning. Error code " << srv.response.error_code.val);
00214         return -3;
00215     }
00216 
00217     PRINTMSG("Successfully finished grasp planning. Have " << srv.response.grasps.size() << " resulting grasps.");
00218     std::vector<moveit_msgs::Grasp>::iterator it;
00219     int i=1;
00220     for (it = srv.response.grasps.begin(); it != srv.response.grasps.end(); ++it)
00221     {
00222         if (!resultsOutputDirectory.empty())
00223         {
00224             std::stringstream filenamePrefix;
00225             filenamePrefix << "Grasp_" << i << ".msg";
00226             ++i;
00227             PRINTMSG("Saving grasp " << i << " to file (dir "
00228               << resultsOutputDirectory << ", prefix " << filenamePrefix.str() << ")");
00229             if (!writeGraspMessage(*it, resultsOutputDirectory, filenamePrefix.str()))
00230             {
00231                 PRINTERROR("Could not save grasp to file "<<filenamePrefix.str());
00232                 continue;
00233             }
00234         }
00235         results.push_back(*it);
00236     }
00237     return 0;
00238 }
00239 


grasp_planning_graspit_ros
Author(s): Jennifer Buehler
autogenerated on Wed May 8 2019 02:53:42