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 "robot.h"
00027 #include "world.h"
00028 #include "guidedPlanner.h"
00029 #include "searchState.h"
00030 #include "searchEnergy.h"
00031 #include "simAnn.h"
00032
00033
00034 #include "debug.h"
00035
00036 GuidedPlanner::GuidedPlanner(Hand *h)
00037 {
00038 mHand = h;
00039 init();
00040 mEnergyCalculator = new ClosureSearchEnergy();
00041 mEnergyCalculator->setType(ENERGY_CONTACT_QUALITY);
00042 ((ClosureSearchEnergy*)mEnergyCalculator)->setAvoidList( &mAvoidList );
00043 mSimAnn = new SimAnn();
00044 mChildClones = true;
00045 mChildThreads = true;
00046 mMaxChildren = 1;
00047 mRepeat = true;
00048
00049
00050 mBestListSize = 20;
00051 mChildSeedSize = 20;
00052 mDistanceThreshold = 0.3f;
00053 mMinChildEnergy = -0.1f;
00054 mChildEnergyType = ENERGY_STRICT_AUTOGRASP;
00055 mMaxChildSteps = 200;
00056
00057 ((ClosureSearchEnergy*)mEnergyCalculator)->setThreshold(mDistanceThreshold);
00058 }
00059
00060 GuidedPlanner::~GuidedPlanner()
00061 {
00062 while (!mAvoidList.empty()) {
00063 delete mAvoidList.front();
00064 mAvoidList.pop_front();
00065 }
00066 while (!mChildSeeds.empty()) {
00067 delete mChildSeeds.front();
00068 mChildSeeds.pop_front();
00069 }
00070 }
00071
00076 void
00077 GuidedPlanner::startPlanner()
00078 {
00079 mCurrentState->getObject()->showFrictionCones(false);
00080 SimAnnPlanner::startPlanner();
00081 }
00082
00086 void
00087 GuidedPlanner::stopPlanner()
00088 {
00089 for (int i=0; i<(int)mChildPlanners.size(); i++) {
00090 mChildPlanners[i]->stopPlanner();
00091 }
00092 checkChildren();
00093 SimAnnPlanner::stopPlanner();
00094 }
00095
00096 void
00097 GuidedPlanner::pausePlanner()
00098 {
00099 SimAnnPlanner::pausePlanner();
00100
00101 for (int i=0; i<(int)mChildPlanners.size(); i++) {
00102 mChildPlanners[i]->stopPlanner();
00103 }
00104 checkChildren();
00105 mCurrentState->getObject()->showFrictionCones(true);
00106 }
00107
00108 bool
00109 GuidedPlanner::resetPlanner()
00110 {
00111 while (!mAvoidList.empty()) {
00112 delete mAvoidList.back();
00113 mAvoidList.pop_back();
00114 }
00115 return SimAnnPlanner::resetPlanner();
00116 }
00117
00127 void
00128 GuidedPlanner::startChild(const GraspPlanningState *seed)
00129 {
00130 DBGP("Creating a child...");
00131 SimAnnPlanner *child = new SimAnnPlanner(mHand);
00132 if (mChildThreads) {
00133 child->startThread();
00134 child->showClone(false);
00135 }else if (mChildClones) {
00136 child->createAndUseClone();
00137 }
00138 DBGA("Child created (and started)");
00139 child->setEnergyType(mChildEnergyType);
00140 if (mChildEnergyType == ENERGY_CONTACT) {
00141 child->setContactType(CONTACT_PRESET);
00142 }
00143 child->setAnnealingParameters(ANNEAL_STRICT);
00144 child->setMaxSteps(mMaxChildSteps);
00145
00146 child->setModelState(seed);
00147 child->resetPlanner();
00148 child->getTargetState()->copyFrom(seed);
00149 child->getTargetState()->getPosition()->setAllConfidences(0.65);
00150 child->getTargetState()->getPosition()->setAllFixed(true);
00151 child->getTargetState()->getPosture()->setAllConfidences(0.5);
00152 child->getTargetState()->getPosture()->setAllFixed(true);
00153
00154 child->startPlanner();
00155 mChildPlanners.push_back(child);
00156
00157 seed->setIVMarkerColor(0,1,0);
00158 }
00159
00169 void
00170 GuidedPlanner::stopChild(SimAnnPlanner *pl)
00171 {
00172 DBGA("Child has finished!");
00173
00174 pl->stopPlanner();
00175 DBGP("Thread has stopped.");
00176 int j = pl->getListSize();
00177 if (j) {
00178
00179 GraspPlanningState *s = new GraspPlanningState( pl->getGrasp(0) );
00180 s->printState();
00181
00182 s->changeHand(mHand, true);
00183 mBestList.push_back(s);
00184
00185
00186
00187 pl->showGrasp(0);
00188 GraspPlanningState *finalGrasp = new GraspPlanningState(pl->getGrasp(0));
00189 finalGrasp->setPositionType(SPACE_COMPLETE);
00190
00191 finalGrasp->setPostureType(POSE_DOF);
00192 finalGrasp->saveCurrentHandState();
00193
00194 finalGrasp->changeHand(mHand, true);
00195
00196 mBestList.push_back(finalGrasp);
00197
00198
00199 GraspPlanningState *s2 = new GraspPlanningState(s);
00200 mAvoidList.push_back(s2);
00201 mHand->getWorld()->getIVRoot()->addChild( s2->getIVRoot() );
00202 if (s2->getEnergy() < 10.0) {
00203 s2->setIVMarkerColor(1,0,0);
00204 } else {
00205 s2->setIVMarkerColor(0.1,0.1,0.1);
00206 }
00207 DBGA("Enrgy from child: " << s2->getEnergy());
00208
00209 }
00210 else
00211 {
00212 DBGA("Child has no solutions");
00213 }
00214 }
00215
00221 void
00222 GuidedPlanner::mainLoop()
00223 {
00224
00225 SimAnn::Result r = mSimAnn->iterate(mCurrentState, mEnergyCalculator);
00226 if (r==SimAnn::FAIL) return;
00227
00228
00229 double bestEnergy;
00230 if ((int)mChildSeeds.size() < mChildSeedSize) {
00231
00232 bestEnergy = mMinChildEnergy;
00233 } else {
00234 bestEnergy = mChildSeeds.back()->getEnergy();
00235 }
00236 if (r==SimAnn::JUMP && mCurrentState->getEnergy() < bestEnergy) {
00237 GraspPlanningState *insertState = new GraspPlanningState(mCurrentState);
00238 DBGP("New solution. Is it a candidate?");
00239 if (!addToListOfUniqueSolutions(insertState,&mChildSeeds,mDistanceThreshold)) {
00240 DBGP("No.");
00241 delete insertState;
00242 } else {
00243 DBGP("Yes");
00244
00245 mHand->getWorld()->getIVRoot()->addChild( insertState->getIVRoot() );
00246 mChildSeeds.sort(GraspPlanningState::compareStates);
00247 DBGP("Queued...");
00248 while ((int)mChildSeeds.size() > mChildSeedSize) {
00249 delete(mChildSeeds.back());
00250 mChildSeeds.pop_back();
00251 }
00252 DBGP("Done.");
00253 }
00254 }
00255
00256 mCurrentStep = mSimAnn->getCurrentStep();
00257 render();
00258
00259 if (mCurrentStep % 100 == 0) {
00260 emit update();
00261 checkChildren();
00262 }
00263 }
00264
00270 void
00271 GuidedPlanner::checkChildren()
00272 {
00273
00274 SimAnnPlanner *pl;
00275 std::vector<SimAnnPlanner*>::iterator it;
00276 it = mChildPlanners.begin();
00277 while(it!=mChildPlanners.end()) {
00278 pl = (*it);
00279 if ( !pl->isActive() ) {
00280 stopChild(pl);
00281 it = mChildPlanners.erase( it );
00282 delete pl;
00283 DBGA("Child stopped.");
00284 } else {
00285 it++;
00286 }
00287 }
00288
00289
00290 if (!isActive()) return;
00291
00292
00293 while ((int)mChildPlanners.size() < mMaxChildren && !mChildSeeds.empty()) {
00294 GraspPlanningState *seed = mChildSeeds.front();
00295 mChildSeeds.pop_front();
00296
00297 mAvoidList.push_back(seed);
00298 startChild(seed);
00299 if (mCurrentState->distance(seed) < mDistanceThreshold) {
00300
00301 resetParameters();
00302 }
00303 }
00304 }