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
00030 #include "graspit_db_planner.h"
00031
00032 #include <algorithm>
00033
00034
00035 #include "debug.h"
00036 #include "world.h"
00037 #include "robot.h"
00038 #include "grasp.h"
00039 #include "quality.h"
00040 #include "searchState.h"
00041 #include "collisionInterface.h"
00042 #include "DBPlanner/sql_database_manager.h"
00043 #include "graspit_db_grasp.h"
00044 #include "graspit_db_model.h"
00045 #include "DBPlanner/database.h"
00046
00047 GraspitDBPlanner::~GraspitDBPlanner(){
00048 for(int i = 0; i < (int)mTestedGrasps.size(); ++i){
00049 delete mTestedGrasps[i];
00050 }
00051 }
00052
00053
00054 void GraspitDBPlanner::moveBy(vec3 v)
00055 {
00056 transf newTran;
00057
00058 newTran = translate_transf(v * mHand->getApproachTran()) * mHand->getTran();
00059 mHand->setTran(newTran);
00060 }
00061
00062
00063 bool GraspitDBPlanner::moveHandOutOfCollision()
00064 {
00065 CollisionReport colReport;
00066 int numCols;
00067 double offset = -10.0;
00068 std::vector<Body*> justPalm;
00069 justPalm.push_back(mHand->getPalm());
00070
00071
00072 do{
00073 numCols = mHand->getWorld()->getCollisionReport(&colReport, &justPalm);
00074 if (numCols) moveBy(vec3(0.0,0.0,offset));
00075 } while (numCols);
00076
00077
00078 numCols = mHand->getWorld()->getCollisionReport(&colReport);
00079 for (int c=0; c<mHand->getNumChains(); c++) {
00080 if (!mHand->snapChainToContacts(c, colReport)) {
00081
00082
00083
00084 }
00085 }
00086
00087 int safeGuard = 0;
00088 while (++safeGuard < 1000){
00089 numCols = mHand->getWorld()->getCollisionReport(&colReport);
00090 if (numCols){
00091 moveBy(vec3(0.0,0.0,offset));
00092 }
00093 else{
00094 break;
00095 }
00096 }
00097
00098 double step = 10.0;
00099 for( int i = 0; i < 10; i ++) {
00100 if(mHand->approachToContact(step)){
00101 break;
00102 }
00103 }
00104 return true;
00105 }
00106
00107 void GraspitDBPlanner::dynamicsError(const char*)
00108 {
00109 mDynamicsError = true;
00110 }
00111
00112
00113 bool GraspitDBPlanner::testGraspDynamic(DynamicCode *code)
00114 {
00115 if(checkDynBody())
00116 dynamicBodyInit();
00117
00118 int numCols;
00119 CollisionReport colReport;
00120 do{
00121 moveBy(vec3(0.0,0.0,-300));
00122 numCols = mHand->getWorld()->getCollisionReport(&colReport);
00123 } while (numCols);
00124
00125 mHand->autoGrasp(false, -1.0);
00126
00127 int count = 0;
00128 while(!mHand->approachToContact(50) && ++count<10);
00129 if (count >= 10) {
00130 *code = DYNAMIC_APPROACH_FAILED;
00131 return false;
00132 }
00133
00134 bool fingerContact = false;
00135 for (int c=0; c<mHand->getNumChains(); c++) {
00136 for (int l=0; l<mHand->getChain(c)->getNumLinks(); l++) {
00137 if (mHand->getChain(c)->getLink(l)->getNumContacts()) {
00138 fingerContact = true;
00139 break;
00140 }
00141 }
00142 if (fingerContact) break;
00143 }
00144
00145
00146
00147 if (!fingerContact) {
00148 mHand->autoGrasp(false, 1.0, true);
00149 }
00150
00151 numCols = mHand->getWorld()->getCollisionReport(&colReport);
00152 if (numCols) {
00153 *code = DYNAMIC_APPROACH_FAILED;
00154 return false;
00155 }
00156 QObject::connect(mHand->getWorld(), SIGNAL(dynamicsError(const char*)),
00157 this, SLOT(dynamicsError(const char*)));
00158
00159
00160
00161 mHand->getWorld()->resetDynamics();
00162
00163 mHand->getWorld()->resetDynamicWrenches();
00164 mHand->getWorld()->turnOnDynamics();
00165
00166 mHand->autoGrasp(false);
00167
00168
00169 mDynamicsError = false;
00170 int steps=0; int stepFailsafe = 800;
00171 while (1) {
00172 mHand->getWorld()->stepDynamics();
00173 if (mDynamicsError) break;
00174 if (++steps > stepFailsafe) break;
00175 if (!(steps%50)) {DBGA("Dynamic step " << steps);}
00176
00177 }
00178 QObject::disconnect(mHand->getWorld(), SIGNAL(dynamicsError(const char*)),
00179 this, SLOT(dynamicsError(const char*)));
00180
00181 mHand->getWorld()->turnOffDynamics();
00182 if (mDynamicsError) {
00183 DBGA("Dynamics error!");
00184 *code = DYNAMIC_ERROR;
00185 return false;
00186 }
00187 DBGA("Dynamic autograsp complete in " << steps << " steps");
00188 *code = DYNAMIC_SUCCESS;
00189 return true;
00190 }
00191
00192
00193 bool GraspitDBPlanner::testGraspStatic(){
00194
00195 if (!moveHandOutOfCollision()) {
00196 return false;
00197 }
00198
00199 mHand->autoGrasp(true);
00200 return true;
00201 }
00202
00203
00204 bool GraspitDBPlanner::testCurrentGrasp(TestType t, DynamicCode* c){
00205
00206 if(t == STATIC)
00207 return testGraspStatic();
00208 else{
00209 if(!c)
00210 return false;
00211 return testGraspDynamic(c);
00212 }
00213 }
00214
00215 bool GraspitDBPlanner::checkDynBody()
00216 {
00217 return true;
00218 }
00219
00220 void GraspitDBPlanner::dynamicBodyInit(){
00221 }
00222
00223
00224 void GraspitDBPlanner::computeQuality(float &eq, float &vq)
00225 {
00226 CollisionReport colReport;
00227
00228 int numCols = mHand->getWorld()->getCollisionReport(&colReport);
00229
00230 if(numCols>0){
00231 eq = -1.0;
00232 vq = -1.0;
00233 return;
00234 }
00235
00236 if(mEpsQual){
00237 delete mEpsQual;
00238 }
00239 mEpsQual = new QualEpsilon( mHand->getGrasp(), ("Examine_dlg_qm"),"L1 Norm");
00240 if(mVolQual){
00241 delete mVolQual;
00242 }
00243 mVolQual = new QualVolume( mHand->getGrasp(), ("Examine_dlg_qm"),"L1 Norm");
00244 mHand->getWorld()->findAllContacts();
00245 mHand->getWorld()->updateGrasps();
00246
00247 eq = mEpsQual->evaluate();
00248 vq = mVolQual->evaluate();
00249 }
00254 bool GraspitDBPlanner::testGrasps(TestType t,
00255 std::vector<db_planner::Grasp*> graspList,
00256 std::vector<db_planner::Grasp*>* testedGraspList){
00257 mTestedGrasps.clear();
00258 if(testedGraspList)
00259 testedGraspList->clear();
00260
00261
00262 for(int i = 0; i < ((int)graspList.size()); i ++)
00263 {
00264 GraspitDBGrasp* tempGrasp = new GraspitDBGrasp( *static_cast<GraspitDBGrasp*>(graspList[i]) );
00265
00266 static_cast<GraspitDBModel*>(mObject)->getGraspableBody()->setTran(transf::IDENTITY);
00267
00268 GraspPlanningState *tempState = new GraspPlanningState(tempGrasp->getPreGraspPlanningState());
00269 tempState->setRefTran(transf::IDENTITY);
00270 tempGrasp->setPreGraspPlanningState(tempState);
00271
00272 float elmts[16];
00273 if(mAligner->Align(tempGrasp->SourceModel(), *mObject, elmts))
00274 tempGrasp->Transform(elmts);
00275
00276 mHand->getGrasp()->setObject(static_cast<GraspitDBModel*>(mObject)->getGraspableBody());
00277
00278 tempGrasp->getPreGraspPlanningState()->execute();
00279 bool testResult;
00280 DynamicCode dynCode;
00281
00282 if (t == STATIC) {
00283 testResult = testGraspStatic();
00284 dynCode = NO_DYNAMICS;
00285 } else if (t == DYNAMIC) {
00286 testResult = testGraspDynamic(&dynCode);
00287
00288 }
00289 float eq = -1.0, vq;
00290
00291 if (testResult) {
00292 computeQuality(eq, vq);
00293 if (!static_cast<GraspitDBModel*>(mObject)->getGraspableBody()->getNumContacts()) {
00294 dynCode = DYNAMIC_OBJECT_EJECTED;
00295 } else if (eq <= 0.0) {
00296 dynCode = DYNAMIC_NO_FC;
00297 }
00298 }else return false;
00299
00300 if(eq < 0) continue;
00301
00302 GraspPlanningState *newGrasp = new GraspPlanningState(mHand);
00303 newGrasp->setPositionType(SPACE_COMPLETE,false);
00304 newGrasp->setPostureType(POSE_DOF,false);
00305
00306 newGrasp->setRefTran(static_cast<GraspitDBModel*>(mObject)->getGraspableBody()->getTran());
00307 newGrasp->saveCurrentHandState();
00308 newGrasp->setEpsilonQuality(eq);
00309 newGrasp->setVolume(vq);
00310 newGrasp->setIndex(i);
00311
00312 newGrasp->setDistance((double)dynCode);
00313 mTestedGrasps.push_back(newGrasp);
00314
00315 if(!testedGraspList)
00316 continue;
00317
00318 db_planner::Grasp *recordGrasp = static_cast<db_planner::Grasp*>( new GraspitDBGrasp(*tempGrasp) );
00319 tempState = new GraspPlanningState(static_cast<GraspitDBGrasp*>(recordGrasp)->getPreGraspPlanningState());
00320 tempState->copyFrom(newGrasp);
00321 static_cast<GraspitDBGrasp*>(recordGrasp)->setPreGraspPlanningState(tempState);
00322 testedGraspList->push_back(recordGrasp);
00323
00324 delete tempGrasp;
00325 }
00326 if(mTestedGrasps.size() != 0)
00327 return true;
00328 return false;
00329 }
00330
00336 void GraspitDBPlanner::crossCorrelate(const std::vector<db_planner::Model*> modelList,
00337 std::vector<db_planner::Grasp*> graspList)
00338 {
00339 db_planner::Model* objectArchive = mObject;
00340 DBGA("num of modelList: " << modelList.size() << " num of graspList: " << graspList.size());
00341 std::vector<db_planner::Grasp*> temp_graspList;
00342 transf transform;
00343 db_planner::Grasp * grasp;
00344
00345
00346 mHand->getWorld()->destroyElement(static_cast<GraspitDBModel*>(mObject)->getGraspableBody(), false);
00347
00348 for(int i = 0; i < (int)modelList.size(); i ++){
00349 db_planner::Model* m = modelList[i];
00350
00351 if (!static_cast<GraspitDBModel*>(m)->geometryLoaded()) {
00352
00353 static_cast<GraspitDBModel*>(m)->load(mHand->getWorld());
00354 }
00355 mObject = m;
00356
00357 static_cast<GraspitDBModel*>(mObject)->getGraspableBody()->addToIvc();
00358
00359
00360
00361 mHand->getWorld()->addBody(static_cast<GraspitDBModel*>(mObject)->getGraspableBody());
00362
00363 temp_graspList.clear();
00364 for(int k = 0; k < (int)graspList.size(); k ++){
00365 QString neighborModelName = QString(graspList[k]->SourceModel().ModelName().c_str());
00366
00367 grasp = static_cast<db_planner::Grasp*> (new GraspitDBGrasp(*(static_cast<GraspitDBGrasp*>(graspList[k]))));
00368 temp_graspList.push_back(grasp);
00369 }
00370 testGrasps(STATIC, temp_graspList, NULL);
00371
00372 for(int j = 0; j < (int)mTestedGrasps.size(); j ++){
00373 static_cast<GraspitDBGrasp*>(graspList[mTestedGrasps[j]->getIndex()])->addTestScore(mTestedGrasps[j]->getEpsilonQuality());
00374 }
00375 for(int k = 0; k <(int)graspList.size(); k ++){
00376 if(static_cast<GraspitDBGrasp*>(graspList[k])->getNumTestScores()==i) static_cast<GraspitDBGrasp*>(graspList[k])->addTestScore(0.0);
00377 }
00378
00379 for(size_t i = 0; i < temp_graspList.size(); ++i){
00380 delete temp_graspList[i];
00381 }
00382 temp_graspList.clear();
00383
00384 mHand->getWorld()->destroyElement(static_cast<GraspitDBModel*>(mObject)->getGraspableBody(), false);
00385 }
00386
00387 mObject = objectArchive;
00388
00389 static_cast<GraspitDBModel*>(mObject)->getGraspableBody()->addToIvc();
00390
00391
00392
00393 mHand->getWorld()->addBody(static_cast<GraspitDBModel*>(mObject)->getGraspableBody());
00394 }