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 #include "graspTransferCheckTask.h"
00027
00028 #include "graspitGUI.h"
00029 #include "ivmgr.h"
00030 #include "world.h"
00031 #include "robot.h"
00032 #include "body.h"
00033 #include "searchState.h"
00034 #include "DBPlanner/db_manager.h"
00035
00036 #include "preGraspCheckTask.h"
00037
00038 #include "graspit_db_grasp.h"
00039 #include "graspit_db_model.h"
00040
00041 #include "debug.h"
00042
00043 GraspTransferCheckTask::GraspTransferCheckTask(TaskDispatcher *disp, db_planner::DatabaseManager *mgr,
00044 db_planner::TaskRecord rec) : Task (disp, mgr, rec)
00045 {
00046
00047 }
00048
00049
00050 GraspTransferCheckTask::~GraspTransferCheckTask()
00051 {
00052
00053 mObject->getWorld()->destroyElement(mObject, false);
00054
00055
00056 static_cast<GraspitDBModel*>(mPlanningTask.model)->unload();
00057 }
00058
00059 void emptyGraspListHack(std::vector<db_planner::Grasp*> &graspList)
00060 {
00061 while (!graspList.empty()) {
00062 delete graspList.back();
00063 graspList.pop_back();
00064 }
00065 }
00066
00067 void GraspTransferCheckTask::start()
00068 {
00069
00070 if (!mDBMgr->GetPlanningTaskRecord(mPlanningTask.taskId, &mPlanningTask)) {
00071 DBGA("Failed to get planning record for task");
00072 mStatus = ERROR;
00073 return;
00074 }
00075
00076 World *world = graspItGUI->getIVmgr()->getWorld();
00077
00078 if ( !world->getNumHands()) {
00079 QString handPath = GraspitDBGrasp::getHandGraspitPath(QString(mPlanningTask.handName.c_str()));
00080 handPath = QString(getenv("GRASPIT")) + handPath;
00081 DBGA("Grasp transfer task: loading hands from " << handPath.latin1());
00082 mHand1 = static_cast<Hand*>(world->importRobot(handPath));
00083 mHand2 = static_cast<Hand*>(world->importRobot(handPath));
00084 if ( !mHand1 || !mHand2 ) {
00085 DBGA("Failed to load hand(s)");
00086 mStatus = ERROR;
00087 return;
00088 }
00089 } else if ( world->getNumHands()==2 &&
00090 GraspitDBGrasp::getHandDBName(world->getHand(0)) == QString(mPlanningTask.handName.c_str()) &&
00091 GraspitDBGrasp::getHandDBName(world->getHand(1)) == QString(mPlanningTask.handName.c_str()) ) {
00092 mHand1 = world->getHand(0);
00093 mHand2 = world->getHand(1);
00094 } else {
00095 DBGA("Grasp transfer task: wring hand(s) found");
00096 mStatus = ERROR;
00097 return;
00098 }
00099
00100
00101 GraspitDBModel *model = static_cast<GraspitDBModel*>(mPlanningTask.model);
00102 if (model->load(world) != SUCCESS) {
00103 DBGA("Grasp Planning Task: failed to load model");
00104 mStatus = ERROR;
00105 return;
00106 }
00107 mObject = model->getGraspableBody();
00108 mObject->addToIvc();
00109 world->addBody(mObject);
00110
00111
00112 mDBMgr->SetGraspAllocator(new GraspitDBGraspAllocator(mHand1));
00113 std::vector<db_planner::Grasp*> graspList1;
00114 if(!mDBMgr->GetGrasps(*(mPlanningTask.model), mPlanningTask.handName, &graspList1)){
00115 DBGA("Load grasps failed for hand 1");
00116 mStatus = ERROR;
00117 emptyGraspListHack(graspList1);
00118 return;
00119 }
00120
00121 mDBMgr->SetGraspAllocator(new GraspitDBGraspAllocator(mHand2));
00122 std::vector<db_planner::Grasp*> graspList2;
00123 if(!mDBMgr->GetGrasps(*(mPlanningTask.model), mPlanningTask.handName, &graspList2)){
00124 DBGA("Load grasps failed for hand 2");
00125 mStatus = ERROR;
00126 emptyGraspListHack(graspList2);
00127 return;
00128 }
00129
00130
00131 bool success = true;
00132 for (int g1=0; g1<graspList1.size(); g1++) {
00133 for (int g2=g1; g2<graspList2.size(); g2++) {
00134 if (g1==g2) continue;
00135 DBGA("Checking combo " << g1 << " -- " << g2);
00136 if (checkGraspCombo(graspList1[g1], graspList2[g2])) {
00137
00138 if (!mDBMgr->InsertGraspPair(graspList1[g1], graspList2[g2])) {
00139 DBGA(" error inserting pair in database");
00140 success = false;
00141 break;
00142 } else {
00143 DBGA(" inserted in database");
00144 }
00145 }
00146 }
00147 }
00148
00149 emptyGraspListHack(graspList1);
00150 emptyGraspListHack(graspList2);
00151 if (success) mStatus = DONE;
00152 else mStatus = ERROR;
00153 }
00154
00160 bool GraspTransferCheckTask::checkGraspCombo(db_planner::Grasp* grasp1, db_planner::Grasp* grasp2)
00161 {
00162
00163 GraspPlanningState *graspState1 = static_cast<GraspitDBGrasp*>(grasp1)->getFinalGraspPlanningState();
00164 graspState1->execute();
00165 GraspPlanningState *graspState2 = static_cast<GraspitDBGrasp*>(grasp2)->getFinalGraspPlanningState();
00166 graspState2->execute();
00167
00168
00169 World *world = graspItGUI->getIVmgr()->getWorld();
00170 if (!world->noCollision()) {
00171 DBGA(" initial grasps are in collision");
00172 return false;
00173 }
00174
00175
00176 if (!PreGraspCheckTask::preGraspCheck(mHand2)) {
00177 DBGA(" pre grasp 2 fails");
00178 return false;
00179 }
00180
00181
00182 graspState2->execute();
00183
00184
00185 if (!PreGraspCheckTask::preGraspCheck(mHand1)) {
00186 DBGA(" pre grasp 1 fails");
00187 return false;
00188 }
00189
00190 return true;
00191 }