EigenGraspPlannerNoQt.cpp
Go to the documentation of this file.
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 // #include <timeTest.h>
00044 // #include <guidedPlanner.h>
00045 // #include <loopPlanner.h>
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 //    addAsIdleListener();
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         // PRINTMSG("Thread loop test");
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                 // this means it may have been paused, but becuse we don't have slots here,
00140                 // we don't automatically get notified about the planning state being complete.
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     // lock the world so that it won't change while we set the hand/object and do the planning
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     // for testing we can save the start world to see how it looks before we start planning
00179     // getGraspItSceneManager()->saveGraspItWorld("/home/jenny/test/worlds/startWorld.xml",true);
00180     // getGraspItSceneManager()->saveInventorWorld("/home/jenny/test/worlds/startWorld.iv",true);
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     // lock the world so that it won't change while we do the planning
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     // to be consistent, the graspitEgPlanner object has to be protected by the mutex here as well.
00228     // on the other hand, this prevents the main planning loop from being interrupted. But
00229     // it is not designed to be interrupted anyway....
00230     // TODO at this point we cannot protect graspitEgPlanner object because otherwise the status
00231     // thread does not have access. Find a better solution for this!
00232     // graspitEgPlannerMtx.lock();
00233     graspitEgPlanner->runPlannerLoop();
00234     // graspitEgPlannerMtx.unlock();
00235 
00236     plannerComplete();
00237     PRINTMSG("Planning done.");
00238 
00239     // store results
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 /*void EigenGraspPlannerNoQt::stopPlanner()
00254 {
00255     // this only works if the QT method (asynchronous) is used
00256     graspitEgPlannerMtx.lock();
00257     graspitEgPlanner->pausePlanner();
00258     graspitEgPlannerMtx.unlock();
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     // PRINTMSG("Object position: "<<t.x()<<"/"<<t.y()<<"/"<<t.z());
00282     // PRINTMSG("Object quaternion: "<<q.x<<"/"<<q.y<<"/"<<q.z<<"/"<<q.w);
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     // this gets the DOF values for the 3 fingers. If the hand has only one EigenGrasp which is symmetric,
00311     // the 3 values will be the same.
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     // execute the auto-grasp in "opening" direction
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     // this gets the DOF values for the 3 fingers. If the hand has only one EigenGrasp which is symmetric,
00345     // the 3 values will be the same.
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     // print the EigenGrasp value
00371     for (int k = 0; k < handPosture->getNumVariables(); ++k)
00372     {
00373         // PRINTMSG("EigenGrasp val " << k << ": " << handPosture->getVariable(k)->getValue());
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         // TODO: testing neeed with other state types before we allow this
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         // TODO: testing neeed with other state types before we allow this
00400         PRINTERROR("Check if implementation for other position types than Axis-Angle work!");
00401         return false;
00402     }
00403 
00404     // we assume that the hand has not changed since we started the plannning
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         // Execute grasp so that the correct world is saved
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         // PRINTMSG("Hand transform: " << handTransform);
00489 
00490         EigenTransform objectTransform = getObjectTransform(s);
00491         // PRINTMSG("Object transform: " << objectTransform);
00492 
00493         // Compute hand transform relative to object
00494         // objectTransform * relTransform = handTransform
00495         // relTransform = objectTransform.inverse() * handTransform;
00496         EigenTransform relTransform = objectTransform.inverse() * handTransform;
00497 
00498         // PRINTMSG("Relative transform: " << relTransform);
00499         std::vector<double> graspDOFs;
00500         getGraspJointDOFs(s, graspDOFs);
00501         /*     int k=0;
00502              for (std::vector<double>::const_iterator it=graspDOFs.begin(); it!=graspDOFs.end(); ++it){
00503                  PRINTMSG("Grasp hand DOF "<<k<<": "<<*it);
00504                  ++k;
00505              }
00506         */
00507         std::vector<double> pregraspDOFs;
00508         getPregraspJointDOFs(s, pregraspDOFs);
00509         /*     int k=0;
00510              for (std::vector<double>::const_iterator it=pregraspDOFs.begin(); it!=pregraspDOFs.end(); ++it){
00511                  PRINTMSG("Grasp hand DOF "<<k<<": "<<*it);
00512                  ++k;
00513              }
00514         */
00515 
00516         std::vector<double> egVals;
00517         getEigenGraspValues(s, egVals);
00518         /*      for (std::vector<double>::const_iterator it=egVals.begin(); it!=egVals.end(); ++it){
00519                   PRINTMSG("EigenGrasp value "<<k<<": "<<*it);
00520                   ++k;
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     // force a reset of the planner
00567     graspitEgPlannerMtx.lock();
00568     if (graspitEgPlanner)
00569     {
00570         graspitEgPlanner->invalidateReset();
00571     }  // if graspitEgPlanner is NULL, it will have to be created again in initPlannerType()
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         // this should already be set, but if it wasn't we could do it with this:
00603         // mHand->getGrasp()->setObjectNoUpdate(mObject);
00604         PRINTERROR("Consistency: Currently loaded object to grasp should be the same!");
00605         return false;
00606     }
00607     // mHand->getGrasp()->setGravity(true);
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     // initialize variable types for this search:
00617     for (int i=0; i<graspPlanningState.getNumVariables(); i++) {
00618         // if checkbox in interface is checked:
00619         graspPlanningState.getVariable(i)->setFixed(false);
00620         graspPlanningState.getVariable(i)->setConfidence(0.0);
00621     }*/
00622 
00623     graspitEgPlannerMtx.lock();
00624 
00625     graspitEgPlanner = NULL;
00626 
00627     initSearchType(graspPlanningState, graspitStateType);
00628 
00629     initPlannerType(graspPlanningState, plannerType);
00630 
00631     setPlanningParameters();
00632     // steps
00633     graspitEgPlanner->setMaxSteps(maxPlanningSteps);
00634 
00635     graspitEgPlannerMtx.unlock();
00636 
00637     // print stats of the current grasp planning state
00638     /*    for (int i=0; i<graspPlanningState.getNumVariables(); i++) {
00639             std::string varName=graspPlanningState.getVariable(i)->getName().toStdString();
00640             float varValue=graspPlanningState.getVariable(i)->getValue();
00641             bool varFixed=graspPlanningState.getVariable(i)->isFixed();
00642             float varConf=graspPlanningState.getVariable(i)->getConfidence();
00643             PRINTMSG("Variable: "<<varName<<", value="<<varValue<<", fixed="<<varFixed<<", conf="<<varConf);
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     // tell the planner which GraspPlanningState parameters to use by the graspPlanningState object.
00668     // the graspPlanningState object is only used as template in the functions called below (the object
00669     // is copied by the graspitEgPlanner object)
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     /*case Loop: {
00681         if (graspitEgPlanner) delete graspitEgPlanner;
00682         graspitEgPlanner = new LoopEigenGraspPlannerNoQt(mHand);
00683         ((LoopEigenGraspPlannerNoQt*)graspitEgPlanner)->setModelState(&graspPlanningState);
00684     }
00685     case MultiThreaded: {
00686         if (graspitEgPlanner) delete graspitEgPlanner;
00687         graspitEgPlanner = new GuidedEigenGraspPlannerNoQt(mHand);
00688         ((GuidedEigenGraspPlannerNoQt*)graspitEgPlanner)->setModelState(&graspPlanningState);
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     // contact type
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     // When using QT Signals/Slots, this function is indirectly called from
00742     // graspitEgPlanner->resetEigenGraspPlannerNoQt (last call here) via an emit.
00743     // So do it manually here.
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     FILE *f = fopen("foo.txt","w");
00823     for (int i=0; i<graspitEgPlanner->getListSize(); i++) {
00824         for(int j=i+1; j<graspitEgPlanner->getListSize(); j++) {
00825             float d = graspitEgPlanner->getGrasp(i)->distance( graspitEgPlanner->getGrasp(j) );
00826             fprintf(stderr,"%d -- %d: %f\n",i+1,j+1,d);
00827         }
00828         fprintf(stderr,"\n");
00829         graspitEgPlanner->getGrasp(i)->writeToFile(f);
00830     }
00831     fclose(f);
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         // printEnergy(i);
00863     }
00864 }
00865 
00866 
00867 /*
00868 void EigenGraspPlannerNoQt::inputLoad(const std::string& inputGraspFile)
00869 {
00870     if (inputGraspFile.empty())
00871     {
00872         PRINTERROR("No input grasp file specified");
00873         return;
00874     }
00875 
00876     UNIQUE_RECURSIVE_LOCK(graspitEgPlannerMtx);
00877     if (!graspitEgPlanner)
00878     {
00879         PRINTERROR("Planner is NULL!");
00880         return;
00881     }
00882 
00883     FILE *fp = fopen(inputGraspFile.c_str(), "r");
00884     bool success = true;
00885     if (!fp)
00886     {
00887         PRINTMSG("Failed to open input file!");
00888         success = false;
00889     }
00890     else if (!graspitEgPlanner->getTargetState()->readFromFile(fp))
00891     {
00892         PRINTMSG("Failed to read target from input file!");
00893         success = false;
00894     }
00895     else
00896     {
00897         PRINTMSG("Target values loaded successfully");
00898     }
00899     fclose(fp);
00900     graspitEgPlanner->setInput(INPUT_FILE, success);
00901 }
00902 */


grasp_planning_graspit
Author(s): Jennifer Buehler
autogenerated on Wed May 8 2019 02:53:36