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 "tableCheckTask.h"
00027
00028 #include "graspitGUI.h"
00029 #include "ivmgr.h"
00030 #include "world.h"
00031 #include "robot.h"
00032 #include "body.h"
00033 #include "debug.h"
00034 #include "searchState.h"
00035
00036 #include "DBPlanner/db_manager.h"
00037 #include "graspit_db_grasp.h"
00038
00039 TableCheckTask::TableCheckTask(TaskDispatcher *disp, db_planner::DatabaseManager *mgr,
00040 db_planner::TaskRecord rec) : PreGraspCheckTask (disp, mgr, rec)
00041 {
00042
00043 World *world = graspItGUI->getIVmgr()->getWorld();
00044 QString path = QString(getenv("GRASPIT")) + QString("/models/objects/plane.xml");
00045 mTable = world->importBody("Body", path);
00046 if (!mTable) {
00047 DBGA("Failed to load table");
00048 mStatus = ERROR;
00049 }
00050 }
00051
00052 TableCheckTask::~TableCheckTask()
00053 {
00054 World *world = graspItGUI->getIVmgr()->getWorld();
00055 if (mTable) {
00056 world->destroyElement(mTable, true);
00057 }
00058 }
00059
00060 void TableCheckTask::start()
00061 {
00062 if (mStatus == ERROR) return;
00063
00064
00065 if (!mDBMgr->GetPlanningTaskRecord(mPlanningTask.taskId, &mPlanningTask)) {
00066 DBGA("Failed to get planning record for task");
00067 mStatus = ERROR;
00068 return;
00069 }
00070
00071 loadHand();
00072 if (mStatus == ERROR) return;
00073
00074 loadObject();
00075 if (mStatus == ERROR) return;
00076
00077
00078
00079 mTable->setTran( transf( Quaternion::IDENTITY, vec3(0.0, 0.0, -200.0) ) );
00080
00081 transf tr( Quaternion::IDENTITY, vec3(0.0, 0.0, 100.0) );
00082
00083 World *world = graspItGUI->getIVmgr()->getWorld();
00084 world->toggleCollisions(false, mHand, mTable);
00085 mTable->moveTo( tr, 5.0, M_PI/36.0 );
00086 world->toggleCollisions(true, mHand, mTable);
00087 DBGA("Table z location: " << mTable->getTran().translation().z());
00088
00089
00090 std::vector<db_planner::Grasp*> graspList;
00091 if(!mDBMgr->GetGrasps(*(mPlanningTask.model), mPlanningTask.handName, &graspList)){
00092 DBGA("Load grasps failed");
00093 mStatus = ERROR;
00094 emptyGraspList(graspList);
00095 return;
00096 }
00097
00098 bool success = true;
00099 std::vector<db_planner::Grasp*>::iterator it;
00100 for (it=graspList.begin(); it!=graspList.end(); it++) {
00101 if (!checkSetGrasp(*it)) {
00102 success = false;
00103 break;
00104 }
00105 }
00106
00107 emptyGraspList(graspList);
00108 if (success) mStatus = DONE;
00109 else mStatus = ERROR;
00110 }
00111
00112 bool TableCheckTask::checkSetGrasp(db_planner::Grasp *grasp)
00113 {
00114 double distance = getTableClearance(grasp);
00115
00116 if (!mDBMgr->SetGraspTableClearance(grasp, distance)) {
00117 DBGA("Failed to mark table clearance in database");
00118 return false;
00119 }
00120
00121 DBGA("Saved clearance: " << distance);
00122 return true;
00123 }
00124
00125 double TableCheckTask::getTableClearance(db_planner::Grasp *grasp)
00126 {
00127
00128 GraspPlanningState *graspState = static_cast<GraspitDBGrasp*>(grasp)->getFinalGraspPlanningState();
00129 graspState->execute();
00130
00131
00132 World *world = graspItGUI->getIVmgr()->getWorld();
00133 double distance = world->getDist(mHand, mTable);
00134 if (distance < 0) {
00135 DBGA(" Grasp is in collision with table");
00136 return distance;
00137 }
00138 DBGA(" Grasp clearance: " << distance);
00139
00140 if (!computePreGrasp(grasp)) {
00141 DBGA(" Pre-grasp is in collision with table");
00142 return -1.0;
00143 }
00144
00145 double pre_distance = world->getDist(mHand, mTable);
00146 DBGA(" Pre-grasp clearance: " << pre_distance);
00147
00148 return std::min(distance, pre_distance);
00149 }
00150