00001
00021 #include <grasp_planning_graspit/EigenGraspPlannerNoQt.h>
00022 #include <grasp_planning_graspit/LogBinding.h>
00023 #include <grasp_planning_graspit/PrintHelpers.h>
00024 #include <grasp_planning_graspit/EigenGraspResult.h>
00025 #include <grasp_planning_graspit/GraspItHelpers.h>
00026
00027 #include <string>
00028 #include <vector>
00029 #include <sstream>
00030
00031 #include <matvec3D.h>
00032 #include <world.h>
00033 #include <robot.h>
00034 #include <body.h>
00035 #include <grasp.h>
00036 #include <eigenGrasp.h>
00037 #include <EGPlanner/search.h>
00038 #include <EGPlanner/searchState.h>
00039 #include <EGPlanner/energy/searchEnergy.h>
00040 #include <EGPlanner/egPlanner.h>
00041 #include <EGPlanner/simAnn.h>
00042 #include <EGPlanner/simAnnPlanner.h>
00043
00044
00045
00046
00047 #include <QWidget>
00048 #include <Inventor/Qt/SoQt.h>
00049 #include <Inventor/actions/SoWriteAction.h>
00050 #include <Inventor/actions/SoGetBoundingBoxAction.h>
00051 #include <Inventor/sensors/SoIdleSensor.h>
00052
00053 #include <boost/filesystem.hpp>
00054
00055 #include <EGPlanner/search.h>
00056
00057 using GraspIt::EigenGraspPlannerNoQt;
00058 using GraspIt::Log;
00059
00060 EigenGraspPlannerNoQt::EigenGraspPlannerNoQt(const std::string& name, const SHARED_PTR<GraspItSceneManager>& intr):
00061 GraspItAccessor(name, intr),
00062 graspitEgPlanner(NULL),
00063 graspitStateType(SPACE_AXIS_ANGLE),
00064 graspitSearchEnergyType("CONTACT_ENERGY"),
00065 useContacts(true)
00066 {
00067 statusThread = new THREAD_CONSTR(statusThreadLoop, this);
00068
00069 PRINTMSG("#######################################################");
00070
00071 }
00072
00073 EigenGraspPlannerNoQt::~EigenGraspPlannerNoQt()
00074 {
00075 PRINTMSG("EigenGrasp planner destructor");
00076
00077 removeFromIdleListeners();
00078
00079 if (statusThread)
00080 {
00081 statusThread->detach();
00082 delete statusThread;
00083 statusThread = NULL;
00084 }
00085
00086 graspitEgPlannerMtx.lock();
00087 if (graspitEgPlanner)
00088 {
00089 delete graspitEgPlanner;
00090 graspitEgPlanner = NULL;
00091 }
00092 graspitEgPlannerMtx.unlock();
00093
00094 PRINTMSG("Exit EigenGrasp planner destructor");
00095 }
00096
00097
00098 void EigenGraspPlannerNoQt::onSceneManagerShutdown()
00099 {
00100 if (statusThread)
00101 {
00102 statusThread->detach();
00103 delete statusThread;
00104 statusThread = NULL;
00105 }
00106
00107 graspitEgPlannerMtx.lock();
00108 if (graspitEgPlanner)
00109 {
00110 delete graspitEgPlanner;
00111 graspitEgPlanner = NULL;
00112 }
00113 graspitEgPlannerMtx.unlock();
00114 }
00115
00116 void EigenGraspPlannerNoQt::idleEventFromSceneManager()
00117 {
00118 }
00119
00120
00121
00122 void EigenGraspPlannerNoQt::statusThreadLoop(EigenGraspPlannerNoQt * _this)
00123 {
00124 PRINTMSG("Enter STATUS thread loop");
00125 float slept = 0;
00126 while (true)
00127 {
00128 SLEEP(0.5);
00129
00130 _this->graspitEgPlannerMtx.lock();
00131 if (_this->graspitEgPlanner)
00132 {
00133 if (_this->graspitEgPlanner->isActive())
00134 {
00135 _this->plannerUpdate();
00136 }
00137 else if (_this->graspitEgPlanner->isReady())
00138 {
00139
00140
00141 _this->graspitEgPlanner->stopPlanner();
00142 }
00143 }
00144 _this->graspitEgPlannerMtx.unlock();
00145 }
00146 PRINTMSG("Exit STATUS thread loop");
00147 }
00148
00149
00150 bool EigenGraspPlannerNoQt::plan(const std::string& handName, const std::string& objectName,
00151 const EigenTransform * objectPose,
00152 const int maxPlanningSteps, const PlannerType& planType)
00153 {
00154 if (!getGraspItSceneManager()->isInitialized())
00155 {
00156 PRINTERROR("Graspit scene manager not initialized. Cannot do planning.");
00157 return false;
00158 }
00159
00160
00161 UNIQUE_RECURSIVE_LOCK lock = getUniqueWorldLock();
00162 if (getGraspItSceneManager()->setCurrentHand(handName) != 0)
00163 {
00164 PRINTERROR("Could not set current hand " << handName);
00165 return false;
00166 }
00167 if (getGraspItSceneManager()->setCurrentGraspableObject(objectName) != 0)
00168 {
00169 PRINTERROR("Could not set current object " << objectName);
00170 return false;
00171 }
00172 if (objectPose && (getGraspItSceneManager()->moveObject(objectName, *objectPose) != 0))
00173 {
00174 PRINTERROR("Could not set the object pose");
00175 return false;
00176 }
00177
00178
00179
00180
00181
00182 return plan(maxPlanningSteps, planType);
00183 }
00184
00185
00186 bool EigenGraspPlannerNoQt::plan(const int maxPlanningSteps, const PlannerType& planType)
00187 {
00188 if (!getGraspItSceneManager()->isInitialized())
00189 {
00190 PRINTERROR("Graspit scene manager not initialized. Cannot do planning.");
00191 return false;
00192 }
00193
00194
00195 UNIQUE_RECURSIVE_LOCK lock = getUniqueWorldLock();
00196
00197 PRINTMSG("Initializing planning...");
00198 if (!initPlanner(maxPlanningSteps, planType))
00199 {
00200 PRINTERROR("Could not initialize planner.");
00201 return false;
00202 }
00203
00204 {
00205 UNIQUE_RECURSIVE_LOCK(graspitEgPlannerMtx);
00206 if (!graspitEgPlanner)
00207 {
00208 PRINTERROR("EigenGraspPlannerNoQt not initialized");
00209 return false;
00210 }
00211
00212 if (graspitEgPlanner->isActive())
00213 {
00214 PRINTERROR("EigenGraspPlannerNoQt is already active, you cannot call plan()");
00215 return false;
00216 }
00217
00218 if (!graspitEgPlanner->isReady())
00219 {
00220 PRINTERROR("EigenGraspPlannerNoQt is not ready.");
00221 return false;
00222 }
00223 }
00224
00225 PRINTMSG("Now initiating planning.");
00226
00227
00228
00229
00230
00231
00232
00233 graspitEgPlanner->runPlannerLoop();
00234
00235
00236 plannerComplete();
00237 PRINTMSG("Planning done.");
00238
00239
00240 graspitEgPlannerMtx.lock();
00241
00242 results.clear();
00243 int numGrasps = graspitEgPlanner->getListSize();
00244 for (int i = 0; i < numGrasps; ++i)
00245 {
00246 const GraspPlanningState *s = graspitEgPlanner->getGrasp(i);
00247 results.push_back(s);
00248 }
00249 graspitEgPlannerMtx.unlock();
00250 return true;
00251 }
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261 GraspIt::EigenTransform EigenGraspPlannerNoQt::getHandTransform(const GraspPlanningState * s) const
00262 {
00263 const PositionState* handPosition = s->readPosition();
00264 EigenTransform handTransform = getEigenTransform(handPosition->getCoreTran());
00265 return handTransform;
00266 }
00267
00268 GraspIt::EigenTransform EigenGraspPlannerNoQt::getObjectTransform(const GraspPlanningState * s) const
00269 {
00270 GraspableBody * object = s->getObject();
00271 if (!object)
00272 {
00273 PRINTERROR("Object not initialized");
00274 return EigenTransform::Identity();
00275 }
00276
00277 transf pose = object->getTran();
00278 Quaternion q = pose.rotation();
00279 vec3 t = pose.translation();
00280
00281
00282
00283
00284 EigenTransform objectTransform;
00285 objectTransform.setIdentity();
00286 Eigen::Vector3d eObjectTranslation(t.x(), t.y(), t.z());
00287 Eigen::Quaterniond eObjectQuaternion(q.w, q.x, q.y, q.z);
00288
00289 objectTransform = objectTransform.translate(eObjectTranslation);
00290 objectTransform = objectTransform.rotate(eObjectQuaternion);
00291
00292 return objectTransform;
00293 }
00294
00295
00296 void EigenGraspPlannerNoQt::getGraspJointDOFs(const GraspPlanningState * s, std::vector<double>& dofs) const
00297 {
00298 if (!s->getHand())
00299 {
00300 PRINTERROR("Hand is NULL!");
00301 return;
00302 }
00303 const PostureState* handPosture = s->readPosture();
00304 if (!handPosture)
00305 {
00306 PRINTERROR("Posture is NULL!");
00307 return;
00308 }
00309
00310
00311
00312 const int numDOF = s->getHand()->getNumDOF();
00313 double * _dofs = new double[numDOF];
00314 handPosture->getHandDOF(_dofs);
00315 for (int k = 0; k < numDOF; ++k)
00316 {
00317 dofs.push_back(_dofs[k]);
00318 }
00319 }
00320
00321
00322 void EigenGraspPlannerNoQt::getPregraspJointDOFs(const GraspPlanningState * s, std::vector<double>& dofs) const
00323 {
00324 GraspPlanningState sCopy(s);
00325
00326 if (!sCopy.getHand())
00327 {
00328 PRINTERROR("Hand is NULL!");
00329 return;
00330 }
00331
00332
00333 sCopy.execute();
00334 sCopy.getHand()->autoGrasp(false,-0.1,true);
00335 sCopy.saveCurrentHandState();
00336
00337 const PostureState* handPosture = sCopy.readPosture();
00338 if (!handPosture)
00339 {
00340 PRINTERROR("Posture is NULL!");
00341 return;
00342 }
00343
00344
00345
00346 const int numDOF = sCopy.getHand()->getNumDOF();
00347 double * _dofs = new double[numDOF];
00348 handPosture->getHandDOF(_dofs);
00349 for (int k = 0; k < numDOF; ++k)
00350 {
00351 dofs.push_back(_dofs[k]);
00352 }
00353 }
00354
00355
00356
00357
00358
00359
00360 void EigenGraspPlannerNoQt::getEigenGraspValues(const GraspPlanningState * s, std::vector<double>& egVals) const
00361 {
00362 if (!s->readPosture())
00363 {
00364 PRINTERROR("Posture is NULL!");
00365 return;
00366 }
00367
00368 const PostureState* handPosture = s->readPosture();
00369
00370
00371 for (int k = 0; k < handPosture->getNumVariables(); ++k)
00372 {
00373
00374 egVals.push_back(handPosture->getVariable(k)->getValue());
00375 }
00376 }
00377
00378 bool EigenGraspPlannerNoQt::checkStateValidity(const GraspPlanningState * s) const
00379 {
00380 if (!s->readPosture() || !s->readPosition())
00381 {
00382 PRINTERROR("Posture or Position is NULL!");
00383 return false;
00384 }
00385
00386 const PostureState* handPosture = s->readPosture();
00387 const PositionState* handPosition = s->readPosition();
00388
00389 StateType postureType = handPosture->getType();
00390 if (postureType != POSE_EIGEN)
00391 {
00392
00393 PRINTERROR("Check if implementation for other pose types than Eigen work!");
00394 return false;
00395 }
00396 StateType positionType = handPosition->getType();
00397 if (positionType != SPACE_AXIS_ANGLE)
00398 {
00399
00400 PRINTERROR("Check if implementation for other position types than Axis-Angle work!");
00401 return false;
00402 }
00403
00404
00405 const Hand * mHand = readCurrentHand();
00406 if (!mHand)
00407 {
00408 PRINTERROR("Hand is NULL!");
00409 return false;
00410 }
00411 if (mHand != s->getHand())
00412 {
00413 PRINTERROR("We have changed hand pointer!!!");
00414 return false;
00415 }
00416
00417 return true;
00418 }
00419
00420
00421 bool EigenGraspPlannerNoQt::saveResultsAsWorldFiles(const std::string& inDirectory,
00422 const std::string& fileNamePrefix, bool asGraspIt, bool asInventor, bool createDir)
00423 {
00424 if (!asGraspIt && !asInventor)
00425 {
00426 PRINTERROR("Have to specify either to save results as GraspIt or as Inventor files");
00427 return false;
00428 }
00429
00430 if (!getGraspItSceneManager()->isInitialized())
00431 {
00432 PRINTERROR("Graspit scene manager not initialized.");
00433 return false;
00434 }
00435
00436 int numGrasps = results.size();
00437 for (int i = 0; i < numGrasps; ++i)
00438 {
00439 const GraspPlanningState * s = results[i];
00440 if (!s)
00441 {
00442 PRINTERROR("GraspPlanningState is NULL!");
00443 return false;
00444 }
00445
00446 if (!checkStateValidity(s))
00447 {
00448 PRINTERROR("Cannot work with the state of grasp " << i);
00449 return false;
00450 }
00451
00452
00453 s->execute();
00454
00455 std::stringstream _wFilename;
00456 _wFilename << inDirectory << "/" << fileNamePrefix << "_" << (i + 1);
00457 std::string wFilename = _wFilename.str();
00458 if (asGraspIt && !getGraspItSceneManager()->saveGraspItWorld(wFilename + ".xml", createDir))
00459 {
00460 PRINTERROR("GraspIt could not save world file " << i);
00461 return false;
00462 }
00463 if (asInventor && !getGraspItSceneManager()->saveInventorWorld(wFilename + ".iv", createDir))
00464 {
00465 PRINTERROR("GraspIt could not save world file " << i);
00466 return false;
00467 }
00468 }
00469 return true;
00470 }
00471
00472
00473 void EigenGraspPlannerNoQt::getResults(std::vector<EigenGraspResult>& allGrasps) const
00474 {
00475 int numGrasps = results.size();
00476
00477 for (int i = 0; i < numGrasps; ++i)
00478 {
00479 const GraspPlanningState * s = results[i];
00480
00481 if (!checkStateValidity(s))
00482 {
00483 PRINTERROR("Cannot work with this state");
00484 continue;
00485 }
00486
00487 EigenTransform handTransform = getHandTransform(s);
00488
00489
00490 EigenTransform objectTransform = getObjectTransform(s);
00491
00492
00493
00494
00495
00496 EigenTransform relTransform = objectTransform.inverse() * handTransform;
00497
00498
00499 std::vector<double> graspDOFs;
00500 getGraspJointDOFs(s, graspDOFs);
00501
00502
00503
00504
00505
00506
00507 std::vector<double> pregraspDOFs;
00508 getPregraspJointDOFs(s, pregraspDOFs);
00509
00510
00511
00512
00513
00514
00515
00516 std::vector<double> egVals;
00517 getEigenGraspValues(s, egVals);
00518
00519
00520
00521
00522 allGrasps.push_back(EigenGraspResult(relTransform, graspDOFs, pregraspDOFs, egVals,
00523 s->isLegal(), s->getEpsilonQuality(), s->getVolume(), s->getEnergy()));
00524 }
00525 }
00526
00527 void EigenGraspPlannerNoQt::initSearchType(GraspPlanningState& graspPlanningState, const StateType& st)
00528 {
00529 graspPlanningState.setPositionType(st);
00530
00531 GraspableBody * mObject = getCurrentGraspableBody();
00532 if (!mObject)
00533 {
00534 PRINTERROR("Object is NULL!");
00535 return;
00536 }
00537
00538 switch (st)
00539 {
00540 case SPACE_AXIS_ANGLE:
00541 case SPACE_COMPLETE:
00542 case SPACE_ELLIPSOID:
00543 {
00544 graspPlanningState.setRefTran(mObject->getTran());
00545 break;
00546 }
00547 case SPACE_APPROACH:
00548 {
00549 Hand * mHand = getCurrentHand();
00550 if (!mHand)
00551 {
00552 PRINTERROR("Hand is NULL!");
00553 return;
00554 }
00555 graspPlanningState.setRefTran(mHand->getTran());
00556 break;
00557 }
00558 default:
00559 {
00560 PRINTERROR("Unsupported search type");
00561 }
00562 }
00563
00564 graspPlanningState.reset();
00565
00566
00567 graspitEgPlannerMtx.lock();
00568 if (graspitEgPlanner)
00569 {
00570 graspitEgPlanner->invalidateReset();
00571 }
00572 graspitEgPlannerMtx.unlock();
00573 }
00574
00575
00576
00577
00578 bool EigenGraspPlannerNoQt::initPlanner(const int maxPlanningSteps, const PlannerType& plannerType)
00579 {
00580 Hand * mHand = getCurrentHand();
00581 GraspableBody * mObject = getCurrentGraspableBody();
00582 if (!mHand || !mObject)
00583 {
00584 PRINTERROR("Cannot initialize planner if no current hand and/or object is loaded");
00585 return false;
00586 }
00587
00588 if (!mHand->getEigenGrasps())
00589 {
00590 PRINTERROR("Current hand has no EigenGrasp information!");
00591 return false;
00592 }
00593
00594 if (!mHand->getGrasp())
00595 {
00596 PRINTERROR("Grasp is NULL!");
00597 return false;
00598 }
00599
00600 if (mHand->getGrasp()->getObject() != mObject)
00601 {
00602
00603
00604 PRINTERROR("Consistency: Currently loaded object to grasp should be the same!");
00605 return false;
00606 }
00607
00608 mHand->getGrasp()->setGravity(false);
00609 GraspPlanningState graspPlanningState(mHand);
00610 graspPlanningState.setObject(mObject);
00611 graspPlanningState.setPositionType(graspitStateType);
00612 graspPlanningState.setRefTran(mObject->getTran());
00613 graspPlanningState.reset();
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623 graspitEgPlannerMtx.lock();
00624
00625 graspitEgPlanner = NULL;
00626
00627 initSearchType(graspPlanningState, graspitStateType);
00628
00629 initPlannerType(graspPlanningState, plannerType);
00630
00631 setPlanningParameters();
00632
00633 graspitEgPlanner->setMaxSteps(maxPlanningSteps);
00634
00635 graspitEgPlannerMtx.unlock();
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646 return true;
00647 }
00648
00649
00650
00651
00652
00653 void EigenGraspPlannerNoQt::initPlannerType(const GraspPlanningState& graspPlanningState, const PlannerType &pt)
00654 {
00655 PRINTMSG("Initializing planner type");
00656
00657
00658 Hand * mHand = getCurrentHand();
00659 if (!mHand)
00660 {
00661 PRINTERROR("Hand is NULL!");
00662 return;
00663 }
00664
00665 UNIQUE_RECURSIVE_LOCK(graspitEgPlannerMtx);
00666
00667
00668
00669
00670 switch (pt)
00671 {
00672 case SimAnn:
00673 {
00674 if (graspitEgPlanner) delete graspitEgPlanner;
00675 SimAnnPlanner * planner = new SimAnnPlanner(mHand);
00676 planner->setModelState(&graspPlanningState);
00677 graspitEgPlanner = planner;
00678 break;
00679 }
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690 default:
00691 {
00692 PRINTERROR("Unknown planner type requested");
00693 return;
00694 }
00695 }
00696
00697 plannerReset();
00698 }
00699
00700
00701
00702 void EigenGraspPlannerNoQt::setPlanningParameters()
00703 {
00704 UNIQUE_RECURSIVE_LOCK(graspitEgPlannerMtx);
00705 if (!graspitEgPlanner)
00706 {
00707 PRINTERROR("Planner is NULL!");
00708 return;
00709 }
00710
00711 graspitEgPlanner->setEnergyType(graspitSearchEnergyType);
00712
00713
00714 if (useContacts)
00715 {
00716 graspitEgPlanner->setContactType(CONTACT_PRESET);
00717 }
00718 else
00719 {
00720 graspitEgPlanner->setContactType(CONTACT_LIVE);
00721 }
00722 }
00723
00724
00725
00726
00727 void EigenGraspPlannerNoQt::plannerReset()
00728 {
00729 UNIQUE_RECURSIVE_LOCK(graspitEgPlannerMtx);
00730 if (!graspitEgPlanner)
00731 {
00732 PRINTERROR("Planner is NULL!");
00733 return;
00734 }
00735
00736 assert(graspitEgPlanner);
00737 setPlanningParameters();
00738
00739 graspitEgPlanner->resetPlanner();
00740
00741
00742
00743
00744 plannerUpdate();
00745 }
00746
00747
00748 void EigenGraspPlannerNoQt::plannerUpdate()
00749 {
00750 PRINTMSG("=== EigenGraspPlannerNoQt update ===");
00751 updateResults();
00752 }
00753
00754
00755 void EigenGraspPlannerNoQt::plannerComplete()
00756 {
00757 printPlanningResults();
00758 }
00759
00760 void EigenGraspPlannerNoQt::updateResults()
00761 {
00762 UNIQUE_RECURSIVE_LOCK(graspitEgPlannerMtx);
00763 if (!graspitEgPlanner)
00764 {
00765 PRINTERROR("Planner is NULL!");
00766 return;
00767 }
00768
00769 int nStep = graspitEgPlanner->getCurrentStep();
00770 float runTime = graspitEgPlanner->getRunningTime();
00771 PRINTMSG("Current Step: " << nStep);
00772 PRINTMSG("Current time: " << runTime);
00773
00774 printResult(0, false);
00775 }
00776
00777 void EigenGraspPlannerNoQt::printResult(int i, bool detailed)
00778 {
00779 UNIQUE_RECURSIVE_LOCK(graspitEgPlannerMtx);
00780 if (!graspitEgPlanner)
00781 {
00782 PRINTERROR("Planner is NULL!");
00783 return;
00784 }
00785
00786 int d = graspitEgPlanner->getListSize();
00787 int rank, size, iteration;
00788 double energy;
00789
00790 int displayState = i;
00791
00792 if (d == 0)
00793 {
00794 displayState = rank = size = energy = iteration = 0;
00795 }
00796 else if (displayState < 0)
00797 {
00798 displayState = 0;
00799 }
00800 else if (displayState >= d)
00801 {
00802 displayState = d - 1;
00803 }
00804
00805 const GraspPlanningState *s = NULL;
00806
00807 if (d != 0)
00808 {
00809 s = graspitEgPlanner->getGrasp(displayState);
00810 if (!s)
00811 {
00812 PRINTERROR("GraspPlanningState is NULL!");
00813 return;
00814 }
00815 rank = displayState + 1;
00816 size = d;
00817 iteration = s->getItNumber();
00818 energy = s->getEnergy();
00819 }
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834 PRINTMSG("Rank: " << rank << "/" << size);
00835 PRINTMSG("Iteration: " << iteration);
00836 PRINTMSG("Energy: " << energy);
00837
00838 if (s && detailed)
00839 {
00840 PRINTMSG("Detailed state:");
00841 s->printState();
00842 }
00843 }
00844
00845
00846 void EigenGraspPlannerNoQt::printPlanningResults()
00847 {
00848 PRINTMSG("########## Final results ############");
00849 UNIQUE_RECURSIVE_LOCK(graspitEgPlannerMtx);
00850 if (!graspitEgPlanner)
00851 {
00852 PRINTERROR("Planner is NULL!");
00853 return;
00854 }
00855 int numResults = graspitEgPlanner->getListSize();
00856 for (int i = 0; i < numResults; ++i)
00857 {
00858 PRINTMSG("---------------------");
00859 PRINTMSG("--- Result #" << (i + 1) << " ---");
00860 PRINTMSG("---------------------");
00861 printResult(i, true);
00862
00863 }
00864 }
00865
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902