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 "preGraspCheckTask.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 "graspit_db_grasp.h"
00037 #include "graspit_db_model.h"
00038
00039 #include "debug.h"
00040
00041 PreGraspCheckTask::PreGraspCheckTask(TaskDispatcher *disp, db_planner::DatabaseManager *mgr,
00042 db_planner::TaskRecord rec) : Task (disp, mgr, rec)
00043 {
00044
00045 }
00046
00047
00048 PreGraspCheckTask::~PreGraspCheckTask()
00049 {
00050
00051 mObject->getWorld()->destroyElement(mObject, false);
00052
00053
00054 static_cast<GraspitDBModel*>(mPlanningTask.model)->unload();
00055 }
00056
00057 void PreGraspCheckTask::emptyGraspList(std::vector<db_planner::Grasp*> &graspList)
00058 {
00059 while (!graspList.empty()) {
00060 delete graspList.back();
00061 graspList.pop_back();
00062 }
00063 }
00064
00065 void PreGraspCheckTask::loadHand()
00066 {
00067 World *world = graspItGUI->getIVmgr()->getWorld();
00068
00069
00070
00071 if (world->getCurrentHand() &&
00072 GraspitDBGrasp::getHandDBName(world->getCurrentHand()) == QString(mPlanningTask.handName.c_str())) {
00073 DBGA("Grasp Planning Task: using currently loaded hand");
00074 mHand = world->getCurrentHand();
00075 } else {
00076 QString handPath = GraspitDBGrasp::getHandGraspitPath(QString(mPlanningTask.handName.c_str()));
00077 handPath = QString(getenv("GRASPIT")) + handPath;
00078 DBGA("Grasp Planning Task: loading hand from " << handPath.latin1());
00079 mHand = static_cast<Hand*>(world->importRobot(handPath));
00080 if ( !mHand ) {
00081 DBGA("Failed to load hand");
00082 mStatus = ERROR;
00083 return;
00084 }
00085 }
00086 mDBMgr->SetGraspAllocator(new GraspitDBGraspAllocator(mHand));
00087
00088
00089 if (mHand->getNumVirtualContacts()==0) {
00090 DBGA("Specified hand does not have virtual contacts defined");
00091 mStatus = ERROR;
00092 return;
00093 }
00094 }
00095
00096 void PreGraspCheckTask::loadObject()
00097 {
00098 World *world = graspItGUI->getIVmgr()->getWorld();
00099
00100 GraspitDBModel *model = static_cast<GraspitDBModel*>(mPlanningTask.model);
00101 if (model->load(world) != SUCCESS) {
00102 DBGA("Grasp Planning Task: failed to load model");
00103 mStatus = ERROR;
00104 return;
00105 }
00106 mObject = model->getGraspableBody();
00107 mObject->addToIvc();
00108 world->addBody(mObject);
00109 }
00110
00111 void PreGraspCheckTask::start()
00112 {
00113
00114 if (!mDBMgr->GetPlanningTaskRecord(mRecord.taskId, &mPlanningTask)) {
00115 DBGA("Failed to get planning record for task id ");
00116 mStatus = ERROR;
00117 return;
00118 }
00119
00120 loadHand();
00121 if (mStatus == ERROR) return;
00122
00123 loadObject();
00124 if (mStatus == ERROR) return;
00125
00126
00127 std::vector<db_planner::Grasp*> graspList;
00128 if(!mDBMgr->GetGrasps(*(mPlanningTask.model), mPlanningTask.handName, &graspList)){
00129 DBGA("Load grasps failed");
00130 mStatus = ERROR;
00131 emptyGraspList(graspList);
00132 return;
00133 }
00134
00135 bool success = true;
00136 std::vector<db_planner::Grasp*>::iterator it;
00137 for (it=graspList.begin(); it!=graspList.end(); it++) {
00138 if (!checkSetGrasp(*it)) {
00139 success = false;
00140 break;
00141 }
00142 }
00143
00144 emptyGraspList(graspList);
00145 if (success) mStatus = DONE;
00146 else mStatus = ERROR;
00147 }
00148
00149 bool PreGraspCheckTask::checkSetGrasp(db_planner::Grasp *grasp)
00150 {
00151 if (!computePreGrasp(grasp)) {
00152 DBGA("Pre-grasp creation fails");
00153
00154 if (!mDBMgr->DeleteGrasp(grasp)) {
00155 DBGA("Failed to delete grasp with id " << grasp->GraspId() << " from database");
00156 return false;
00157 }
00158 return true;
00159 }
00160
00161
00162 if (!mHand->getWorld()->noCollision()) {
00163 DBGA("Collision detected for pre-grasp!");
00164 if (!mDBMgr->DeleteGrasp(grasp)) {
00165 DBGA("Failed to delete grasp with id " << grasp->GraspId() << " from database");
00166 return false;
00167 }
00168 return true;
00169 }
00170
00171
00172 double clearance = mHand->getWorld()->getDist(mHand, mObject);
00173 grasp->SetClearance(clearance);
00174
00175
00176 GraspPlanningState *newPreGrasp = new GraspPlanningState(mHand);
00177 newPreGrasp->setPostureType(POSE_DOF, false);
00178 newPreGrasp->setPositionType(SPACE_COMPLETE, false);
00179 newPreGrasp->setRefTran(mObject->getTran(), false);
00180 newPreGrasp->saveCurrentHandState();
00181
00182 static_cast<GraspitDBGrasp*>(grasp)->setPreGraspPlanningState(newPreGrasp);
00183
00184
00185
00186 if (!mDBMgr->DeleteGrasp(grasp)) {
00187 DBGA("Failed to delete grasp with id " << grasp->GraspId() << " from database");
00188 return false;
00189 }
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206 if (!mDBMgr->SaveGrasp(grasp)) {
00207 DBGA("Failed to save new grasp to database");
00208 return false;
00209 }
00210 DBGA("Pre-grasp inserted");
00211 return true;
00212 }
00213
00214 bool PreGraspCheckTask::computePreGrasp(db_planner::Grasp *grasp)
00215 {
00216
00217 GraspPlanningState *graspState = static_cast<GraspitDBGrasp*>(grasp)->getFinalGraspPlanningState();
00218 graspState->execute();
00219
00220
00221 return preGraspCheck(mHand);
00222 }
00223
00224 bool PreGraspCheckTask::preGraspCheck(Hand *hand)
00225 {
00226
00227
00228 double OPEN_BY = 0.323;
00229
00230 double RETREAT_BY = -100;
00231
00232
00233 std::vector<double> dof(hand->getNumDOF(),0.0);
00234 std::vector<double> stepSize(hand->getNumDOF(), 0.0);
00235 hand->getDOFVals(&dof[0]);
00236 for (int d=0; d<hand->getNumDOF(); d++) {
00237 dof[d] += OPEN_BY;
00238 if (dof[d] < hand->getDOF(d)->getMin()) {
00239 dof[d] = hand->getDOF(d)->getMin();
00240 }
00241 if (dof[d] > hand->getDOF(d)->getMax()) {
00242 dof[d] = hand->getDOF(d)->getMax();
00243 }
00244 stepSize[d] = M_PI/36.0;
00245 }
00246 hand->moveDOFToContacts(&dof[0], &stepSize[0], true, false);
00247
00248
00249 for (int d=0; d<hand->getNumDOF(); d++) {
00250 if ( fabs( dof[d] - hand->getDOF(d)->getVal() ) > 1.0e-5) {
00251 DBGP(" trying to open to " << dof[d] << "; only made it to " << hand->getDOF(d)->getVal());
00252 DBGA(" open gripper fails");
00253 return false;
00254 }
00255 }
00256
00257
00258 if (hand->approachToContact(RETREAT_BY, false)) {
00259
00260 DBGA(" retreat fails");
00261 return false;
00262 }
00263
00264 return true;
00265 }