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 DBGP("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
00181 s->changeHand(mHand, true);
00182 mBestList.push_back(s);
00183
00184
00185
00186 pl->showGrasp(0);
00187 GraspPlanningState *finalGrasp = new GraspPlanningState(pl->getGrasp(0));
00188 finalGrasp->setPositionType(SPACE_COMPLETE);
00189
00190 finalGrasp->setPostureType(POSE_DOF);
00191 finalGrasp->saveCurrentHandState();
00192
00193 finalGrasp->changeHand(mHand, true);
00194
00195 mBestList.push_back(finalGrasp);
00196
00197
00198 GraspPlanningState *s2 = new GraspPlanningState(s);
00199 mAvoidList.push_back(s2);
00200 mHand->getWorld()->getIVRoot()->addChild( s2->getIVRoot() );
00201 if (s2->getEnergy() < 10.0) {
00202 s2->setIVMarkerColor(1,0,0);
00203 } else {
00204 s2->setIVMarkerColor(0.1,0.1,0.1);
00205 }
00206
00207 }
00208 }
00209
00215 void
00216 GuidedPlanner::mainLoop()
00217 {
00218
00219 SimAnn::Result r = mSimAnn->iterate(mCurrentState, mEnergyCalculator);
00220 if (r==SimAnn::FAIL) return;
00221
00222
00223 double bestEnergy;
00224 if ((int)mChildSeeds.size() < mChildSeedSize) {
00225
00226 bestEnergy = mMinChildEnergy;
00227 } else {
00228 bestEnergy = mChildSeeds.back()->getEnergy();
00229 }
00230 if (r==SimAnn::JUMP && mCurrentState->getEnergy() < bestEnergy) {
00231 GraspPlanningState *insertState = new GraspPlanningState(mCurrentState);
00232 DBGP("New solution. Is it a candidate?");
00233 if (!addToListOfUniqueSolutions(insertState,&mChildSeeds,mDistanceThreshold)) {
00234 DBGP("No.");
00235 delete insertState;
00236 } else {
00237 DBGP("Yes");
00238
00239 mHand->getWorld()->getIVRoot()->addChild( insertState->getIVRoot() );
00240 mChildSeeds.sort(GraspPlanningState::compareStates);
00241 DBGP("Queued...");
00242 while ((int)mChildSeeds.size() > mChildSeedSize) {
00243 delete(mChildSeeds.back());
00244 mChildSeeds.pop_back();
00245 }
00246 DBGP("Done.");
00247 }
00248 }
00249
00250 mCurrentStep = mSimAnn->getCurrentStep();
00251 render();
00252
00253 if (mCurrentStep % 100 == 0) {
00254 emit update();
00255 checkChildren();
00256 }
00257 }
00258
00264 void
00265 GuidedPlanner::checkChildren()
00266 {
00267
00268 SimAnnPlanner *pl;
00269 std::vector<SimAnnPlanner*>::iterator it;
00270 it = mChildPlanners.begin();
00271 while(it!=mChildPlanners.end()) {
00272 pl = (*it);
00273 if ( !pl->isActive() ) {
00274 stopChild(pl);
00275 it = mChildPlanners.erase( it );
00276 delete pl;
00277 DBGA("Child stopped.");
00278 } else {
00279 it++;
00280 }
00281 }
00282
00283
00284 if (!isActive()) return;
00285
00286
00287 while ((int)mChildPlanners.size() < mMaxChildren && !mChildSeeds.empty()) {
00288 GraspPlanningState *seed = mChildSeeds.front();
00289 mChildSeeds.pop_front();
00290
00291 mAvoidList.push_back(seed);
00292 startChild(seed);
00293 if (mCurrentState->distance(seed) < mDistanceThreshold) {
00294
00295 resetParameters();
00296 }
00297 }
00298 }