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 "graspPlanningTask.h"
00027
00028 #include <QString>
00029
00030 #include "mytools.h"
00031 #include "graspitGUI.h"
00032 #include "ivmgr.h"
00033 #include "world.h"
00034 #include "robot.h"
00035 #include "body.h"
00036 #include "searchState.h"
00037 #include "loopPlanner.h"
00038 #include "DBPlanner/db_manager.h"
00039
00040 #include "graspit_db_grasp.h"
00041 #include "graspit_db_model.h"
00042
00043 #include "debug.h"
00044
00045 GraspPlanningTask::GraspPlanningTask(TaskDispatcher *disp,
00046 db_planner::DatabaseManager *mgr,
00047 db_planner::TaskRecord rec) :
00048 Task(disp, mgr, rec),
00049 mObject(NULL),
00050 mPlanner(NULL)
00051 {
00052 }
00053
00054 GraspPlanningTask::~GraspPlanningTask()
00055 {
00056
00057 if (mObject) {
00058 mObject->getWorld()->destroyElement(mObject, false);
00059
00060
00061 static_cast<GraspitDBModel*>(mPlanningTask.model)->unload();
00062 }
00063 delete mPlanner;
00064 }
00065
00066 void GraspPlanningTask::start()
00067 {
00068
00069 if (!mDBMgr->GetPlanningTaskRecord(mRecord.taskId, &mPlanningTask)) {
00070 DBGA("Failed to get planning record for task");
00071 mStatus = ERROR;
00072 return;
00073 }
00074
00075 World *world = graspItGUI->getIVmgr()->getWorld();
00076
00077
00078
00079 if (world->getCurrentHand() &&
00080 GraspitDBGrasp::getHandDBName(world->getCurrentHand()) == QString(mPlanningTask.handName.c_str())) {
00081 DBGA("Grasp Planning Task: using currently loaded hand");
00082 mHand = world->getCurrentHand();
00083 } else {
00084 QString handPath = GraspitDBGrasp::getHandGraspitPath(QString(mPlanningTask.handName.c_str()));
00085 handPath = QString(getenv("GRASPIT")) + handPath;
00086 DBGA("Grasp Planning Task: loading hand from " << handPath.latin1());
00087 mHand = static_cast<Hand*>(world->importRobot(handPath));
00088 if ( !mHand ) {
00089 DBGA("Failed to load hand");
00090 mStatus = ERROR;
00091 return;
00092 }
00093 }
00094
00095 if (mHand->getNumVirtualContacts()==0) {
00096 DBGA("Specified hand does not have virtual contacts defined");
00097 mStatus = ERROR;
00098 return;
00099 }
00100
00101
00102 GraspitDBModel *model = static_cast<GraspitDBModel*>(mPlanningTask.model);
00103 if (model->load(world) != SUCCESS) {
00104 DBGA("Grasp Planning Task: failed to load model");
00105 mStatus = ERROR;
00106 return;
00107 }
00108 mObject = model->getGraspableBody();
00109 mObject->addToIvc();
00110 world->addBody(mObject);
00111
00112
00113 GraspPlanningState seed(mHand);
00114 seed.setObject(mObject);
00115 seed.setPositionType(SPACE_AXIS_ANGLE);
00116 seed.setPostureType(POSE_EIGEN);
00117 seed.setRefTran(mObject->getTran());
00118 seed.reset();
00119
00120 mPlanner = new LoopPlanner(mHand);
00121 QObject::connect(mPlanner, SIGNAL(loopUpdate()), this, SLOT(plannerLoopUpdate()));
00122 QObject::connect(mPlanner, SIGNAL(complete()), this, SLOT(plannerComplete()));
00123
00124 mPlanner->setEnergyType(ENERGY_CONTACT);
00125 mPlanner->setContactType(CONTACT_PRESET);
00126 mPlanner->setMaxSteps(65000);
00127 mPlanner->setRepeat(true);
00128
00129 if (mPlanningTask.taskTime >= 0){
00130 mPlanner->setMaxTime(mPlanningTask.taskTime);
00131 } else {
00132 mPlanner->setMaxTime(-1);
00133 }
00134 static_cast<SimAnnPlanner*>(mPlanner)->setModelState(&seed);
00135
00136 if (!mPlanner->resetPlanner()) {
00137 DBGA("Grasp Planning Task: failed to reset planner");
00138 mStatus = ERROR;
00139 return ;
00140 }
00141
00142
00143 mDBMgr->SetGraspAllocator(new GraspitDBGraspAllocator(mHand));
00144 std::vector<db_planner::Grasp*> graspList;
00145 if(!mDBMgr->GetGrasps(*(mPlanningTask.model), mPlanningTask.handName, &graspList)){
00146
00147 DBGA("No grasps found in database for model " << mPlanningTask.model->ModelName());
00148 }
00149
00150 for (size_t i=0; i<graspList.size(); i++) {
00151 GraspPlanningState *state = new GraspPlanningState(static_cast<GraspitDBGrasp*>(graspList[i])
00152 ->getFinalGraspPlanningState() );
00153 state->setObject(mObject);
00154 state->setPositionType(SPACE_AXIS_ANGLE, true);
00155
00156 state->setPostureType(POSE_EIGEN, true);
00157 static_cast<LoopPlanner*>(mPlanner)->addToAvoidList(state);
00158 }
00159 while (!graspList.empty()) {
00160 delete graspList.back();
00161 graspList.pop_back();
00162 }
00163 mLastSolution = mPlanner->getListSize();
00164 DBGA("Planner starting off with " << mLastSolution << " solutions");
00165 mPlanner->startPlanner();
00166 mStatus = RUNNING;
00167 }
00168
00169 void GraspPlanningTask::plannerComplete()
00170 {
00171
00172 plannerLoopUpdate();
00173
00174 mStatus = DONE;
00175 }
00176
00177 void GraspPlanningTask::plannerLoopUpdate()
00178 {
00179 if (mStatus != RUNNING) return;
00180
00181 for(int i=mLastSolution; i<mPlanner->getListSize(); i++) {
00182
00183 GraspPlanningState *sol = new GraspPlanningState(mPlanner->getGrasp(i));
00184
00185
00186 sol->setPositionType(SPACE_COMPLETE,true);
00187
00188
00189 sol->setPostureType(POSE_DOF,true);
00190
00191 if (!saveGrasp(sol)) {
00192 DBGA("Grasp Planning Task: failed to save solution to dbase");
00193 mStatus = ERROR;
00194 break;
00195 }
00196 }
00197 if (mStatus == ERROR) {
00198
00199
00200
00201
00202 mPlanner->setMaxSteps(0);
00203 } else {
00204 DBGA(mPlanner->getListSize() - mLastSolution << " solutions saved to database");
00205 }
00206 mLastSolution = mPlanner->getListSize();
00207 }
00208
00209 bool GraspPlanningTask::saveGrasp(const GraspPlanningState *gps)
00210 {
00211 GraspitDBModel* dbModel= mObject->getDBModel();
00212 assert(dbModel);
00213
00214 db_planner::Grasp* grasp = new db_planner::Grasp;
00215
00216 std::vector<double> contacts = grasp->GetContacts();
00217
00218 grasp->SetSourceModel( *(static_cast<db_planner::Model*>(dbModel)) );
00219 grasp->SetHandName(GraspitDBGrasp::getHandDBName(mHand).toStdString());
00220 grasp->SetEpsilonQuality(0.0);
00221 grasp->SetVolumeQuality(0.0);
00222 grasp->SetEnergy(gps->getEnergy());
00223 grasp->SetClearance(0.0);
00224 grasp->SetClusterRep(false);
00225
00226 grasp->SetSource("EIGENGRASPS");
00227
00228 std::vector<double> tempArray;
00229
00230 for(int i = 0; i < gps->readPosture()->getNumVariables(); ++i){
00231 tempArray.push_back(gps->readPosture()->readVariable(i));
00232 }
00233 grasp->SetPregraspJoints(tempArray);
00234 grasp->SetFinalgraspJoints(tempArray);
00235
00236
00237 tempArray.clear();
00238 for(int i = 0; i < gps->readPosition()->getNumVariables(); ++i){
00239 tempArray.push_back(gps->readPosition()->readVariable(i));
00240 }
00241 grasp->SetPregraspPosition(tempArray);
00242 grasp->SetFinalgraspPosition(tempArray);
00243
00244
00245
00246 tempArray.clear();
00247 grasp->SetContacts(tempArray);
00248
00249 std::vector<db_planner::Grasp*> graspList;
00250 graspList.push_back(grasp);
00251
00252 bool result = mDBMgr->SaveGrasps(graspList);
00253 delete grasp;
00254 return result;
00255 }