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 "compliantGraspCopyTask.h"
00027
00028 #include <memory>
00029
00030 #include "world.h"
00031 #include "robot.h"
00032 #include "matvec3D.h"
00033 #include "searchState.h"
00034 #include "pr2Gripper.h"
00035
00036 #include "DBPlanner/db_manager.h"
00037 #include "graspit_db_grasp.h"
00038 #include "debug.h"
00039
00040 CompliantGraspCopyTask::CompliantGraspCopyTask(TaskDispatcher *disp, db_planner::DatabaseManager *mgr,
00041 db_planner::TaskRecord rec) : PreGraspCheckTask (disp, mgr, rec)
00042 {
00043
00044 }
00045
00046 void CompliantGraspCopyTask::start()
00047 {
00048
00049 if (!mDBMgr->GetPlanningTaskRecord(mPlanningTask.taskId, &mPlanningTask)) {
00050 DBGA("Failed to get planning record for task");
00051 mStatus = ERROR;
00052 return;
00053 }
00054
00055 loadHand();
00056 if (mStatus == ERROR) return;
00057
00058 if (!mHand->isA("Pr2Gripper2010")) {
00059 DBGA("Compliant copy task only works on the PR2 gripper");
00060 mStatus = ERROR;
00061 return;
00062 }
00063 Pr2Gripper2010* gripper = static_cast<Pr2Gripper2010*>(mHand);
00064
00065 loadObject();
00066 if (mStatus == ERROR) return;
00067
00068
00069 std::vector<db_planner::Grasp*> graspList;
00070 if(!mDBMgr->GetGrasps(*(mPlanningTask.model), mPlanningTask.handName, &graspList)){
00071 DBGA("Load grasps failed");
00072 mStatus = ERROR;
00073 emptyGraspList(graspList);
00074 return;
00075 }
00076
00077 bool success = true;
00078 std::vector<db_planner::Grasp*>::iterator it;
00079 for (it=graspList.begin(); it!=graspList.end(); it++) {
00080 GraspPlanningState *graspState = static_cast<GraspitDBGrasp*>(*it)->getFinalGraspPlanningState();
00081 gripper->setCompliance(Pr2Gripper2010::NONE);
00082 graspState->execute();
00083 DBGA("Compliant copy around finger 0");
00084 if (!compliantCopy(*it, Pr2Gripper2010::FINGER0)) {
00085 success = false;
00086 break;
00087 }
00088 gripper->setCompliance(Pr2Gripper2010::NONE);
00089 graspState->execute();
00090 DBGA("Compliant copy around finger 1");
00091 if (!compliantCopy(*it, Pr2Gripper2010::FINGER1)) {
00092 success = false;
00093 break;
00094 }
00095 }
00096 gripper->setCompliance(Pr2Gripper2010::NONE);
00097
00098 emptyGraspList(graspList);
00099 if (success) mStatus = DONE;
00100 else mStatus = ERROR;
00101 }
00102
00103 bool CompliantGraspCopyTask::compliantCopy(const db_planner::Grasp *grasp, Pr2Gripper2010::ComplianceType compliance)
00104 {
00105
00106
00107 double OPEN_BY = 0.035;
00108 Pr2Gripper2010* gripper = static_cast<Pr2Gripper2010*>(mHand);
00109 gripper->setCompliance(compliance);
00110
00111 std::vector<double> dof(mHand->getNumDOF(),0.0);
00112 std::vector<double> stepSize(mHand->getNumDOF(), M_PI/36.0);
00113 mHand->getDOFVals(&dof[0]);
00114 bool done = false;
00115 transf lastTran = mHand->getTran();
00116 while (!done)
00117 {
00118
00119
00120 for (int d=0; d<mHand->getNumDOF(); d++) {
00121 dof[d] += OPEN_BY;
00122 }
00123 mHand->checkSetDOFVals(&dof[0]);
00124 mHand->moveDOFToContacts(&dof[0], &stepSize[0], true, false);
00125
00126
00127 transf currentTran = mHand->getTran();
00128 if (!similarity(currentTran, lastTran)) {
00129
00130 DBGA("Storing a compliant copy");
00131
00132 gripper->setCompliance(Pr2Gripper2010::NONE);
00133 if (!checkStoreGrasp(grasp)) {
00134 return false;
00135 }
00136 gripper->setCompliance(compliance);
00137
00138 lastTran = currentTran;
00139 }
00140
00141
00142 for (int d=0; d<mHand->getNumDOF(); d++) {
00143 if ( fabs(mHand->getDOF(d)->getVal() - dof[d]) > 1.0e-5 ||
00144 dof[d] == mHand->getDOF(d)->getMin() ||
00145 dof[d] == mHand->getDOF(d)->getMax()) {
00146
00147 done = true;
00148 break;
00149 }
00150 }
00151 }
00152 return true;
00153 }
00154
00155 bool CompliantGraspCopyTask::checkStoreGrasp(const db_planner::Grasp *original)
00156 {
00157
00158 if (!mHand->getWorld()->noCollision()) {
00159 DBGA(" World is in collision");
00160 return true;
00161 }
00162
00163
00164 const GraspitDBGrasp *graspit_original = static_cast<const GraspitDBGrasp*>(original);
00165 std::auto_ptr<GraspitDBGrasp> newGrasp(new GraspitDBGrasp(*graspit_original));
00166
00167 newGrasp->SetCompliantCopy(true);
00168 newGrasp->SetCompliantOriginalId(original->GraspId());
00169
00170 newGrasp->SetClusterRep(false);
00171
00172 GraspPlanningState *graspState = new GraspPlanningState(mHand);
00173 graspState->setPostureType(POSE_DOF, false);
00174 graspState->setPositionType(SPACE_COMPLETE, false);
00175 graspState->setRefTran(mObject->getTran(), false);
00176 graspState->saveCurrentHandState();
00177 newGrasp->setFinalGraspPlanningState(graspState);
00178
00179
00180 GraspPlanningState *preGraspState = new GraspPlanningState(mHand);
00181 preGraspState->setPostureType(POSE_DOF, false);
00182 preGraspState->setPositionType(SPACE_COMPLETE, false);
00183 preGraspState->setRefTran(mObject->getTran(), false);
00184
00185 mHand->saveState();
00186 bool pre_grasp_result = computePreGrasp(newGrasp.get());
00187 preGraspState->saveCurrentHandState();
00188 mHand->restoreState();
00189
00190 if (!pre_grasp_result) {
00191 DBGA(" Pre-grasp creation fails");
00192 return true;
00193 }
00194
00195 if (!mHand->getWorld()->noCollision()) {
00196 DBGA(" World is in collision AFTER PREGRASP COMPUTATION");
00197 return true;
00198 }
00199
00200 newGrasp->setPreGraspPlanningState(preGraspState);
00201
00202 double clearance = mHand->getWorld()->getDist(mHand, mObject);
00203 newGrasp->SetClearance(clearance);
00204
00205
00206 if (!mDBMgr->SaveGrasp(newGrasp.get())) {
00207 DBGA(" Error writing new grasp to database");
00208 return false;
00209 }
00210 return true;
00211 }
00212
00220 bool CompliantGraspCopyTask::similarity(const transf &t1, const transf &t2)
00221 {
00222
00223 double DISTANCE_THRESHOLD = 7.5;
00224
00225 double ANGULAR_THRESHOLD = 0.26;
00226
00227 vec3 dvec = t1.translation() - t2.translation();
00228 double d = dvec.len();
00229 if (d > DISTANCE_THRESHOLD) return false;
00230
00231 Quaternion qvec = t1.rotation() * t2.rotation().inverse();
00232 vec3 axis; double angle;
00233 qvec.ToAngleAxis(angle,axis);
00234 if (angle > M_PI) angle -= 2*M_PI;
00235 if (angle < -M_PI) angle += 2*M_PI;
00236 if (fabs(angle) > ANGULAR_THRESHOLD) return false;
00237
00238 return true;
00239 }
00240
00244 bool CompliantGraspCopyTask::searchSimilarity(const transf &t1, const transf &t2)
00245 {
00246
00247 double DISTANCE_THRESHOLD = 0.01;
00248
00249 vec3 dvec = t1.translation() - t2.translation();
00250 double d = dvec.len() / mObject->getMaxRadius();
00251 Quaternion qvec = t1.rotation() * t2.rotation().inverse();
00252 vec3 axis; double angle;
00253 qvec.ToAngleAxis(angle,axis);
00254
00255 double q = 0.5 * fabs(angle) / M_PI;
00256 if (std::max(d,q) > DISTANCE_THRESHOLD) return false;
00257 return true;
00258 }