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
00027
00028
00029
00030
00031
00032
00037
00038 #include <stdio.h>
00039 #include <iostream>
00040
00041 #include <QFile>
00042 #include <QTextStream>
00043
00044
00045 #include <Inventor/actions/SoGetBoundingBoxAction.h>
00046 #include <Inventor/actions/SoSearchAction.h>
00047 #include <Inventor/nodes/SoCylinder.h>
00048 #include <Inventor/nodes/SoCone.h>
00049 #include <Inventor/nodes/SoCube.h>
00050 #include <Inventor/nodes/SoSphere.h>
00051 #include <Inventor/SoLists.h>
00052 #include "SoArrow.h"
00053 #include "SoTorquePointer.h"
00054 #include "SoComplexShape.h"
00055 #include <Inventor/fields/SoSFFloat.h>
00056 #include <Inventor/actions/SoGetMatrixAction.h>
00057 #include <Inventor/SbLinear.h>
00058 #include <Inventor/nodes/SoSelection.h>
00059
00060 #ifdef Q_WS_X11
00061 #include <unistd.h>
00062 #endif
00063
00064
00065 #include "graspitGUI.h"
00066 #include "contact.h"
00067 #include "world.h"
00068 #include "ivmgr.h"
00069 #include "robot.h"
00070 #include "body.h"
00071 #include "grasp.h"
00072
00073
00074 #include <list>
00075
00076
00077 #include "matvecIO.h"
00078 #include "grasp_coordinates.h"
00079 #include "grasp_directions.h"
00080 #include "grasp_preshape.h"
00081 #include "grasp_visualization.h"
00082 #include "grasp_grasps.h"
00083 #include "grasp_planner.h"
00084 #include "grasp_tester.h"
00085 #include "grasp_presenter.h"
00086
00087 #include "grasp_manager.h"
00088
00089
00090
00091
00092
00093 grasp_tester *myTester;
00094
00099 grasp_manager::grasp_manager(){
00100 graspsNotShown = 0;
00101 renderIt = false;
00102 doIteration = false;
00103 itQualThresh = ITERATION_QUALITY_THRESH;
00104 maxdist = MAX_GRASP_DISTANCE_IN_ITERATION;
00105
00106 myPlanner = new grasp_planner();
00107 myTester = new grasp_tester();
00108 myPresent = new grasp_presenter();
00109
00110 #ifdef GRASPITDBG
00111 std::cout << "PL_OUT: Grasp Manager built." << std::endl;
00112 #endif
00113 }
00114
00119 grasp_manager::~grasp_manager(){
00120 std::list<plannedGrasp *>::iterator gptr;
00121
00122 if (!graspList.empty())
00123 for (gptr = graspList.begin();gptr!=graspList.end();gptr++)
00124 delete *gptr;
00125
00126 delete myPlanner;
00127 delete myTester;
00128 delete myPresent;
00129
00130 #ifdef GRASPITDBG
00131 std::cout << "PL_OUT: Grasp manager destroyed." << std::endl;
00132 #endif
00133 }
00134
00135
00142 void
00143 grasp_manager::loadPrimitives()
00144 {
00145
00146 SoInput myInput;
00147 char prDir[256];
00148 QString directory = QString(getenv("GRASPIT"))+
00149 QString("/models/objects/primitives/");
00150 QString filename = my_body->getFilename().section('/',-1);
00151
00152
00153 filename = filename.section('.',-2,-2) + ".iv";
00154 QString path = directory + filename;
00155
00156 printf("Loading primitive %s.\n",path.latin1());
00157
00158 if (!(myInput.openFile(path.latin1()))) {
00159 pr_error("could not open primitives file!");
00160 primitives = my_body->getIVGeomRoot();
00161 printf ("%s\n",prDir);
00162 printf("Setting primitive root node to original object.\n");
00163 }
00164 else {
00165 primitives = SoDB::readAll(&myInput);
00166 myInput.closeFile();
00167 if (primitives == NULL) {
00168 printf("Load Primitive didnt work, although file seems to exist.\n");
00169 printf("Setting primitive root node to original object.\n");
00170 primitives = my_body->getIVGeomRoot();
00171 }
00172 else {
00173 primitives->ref();
00174 }
00175 }
00176
00177 }
00178
00201 int
00202 grasp_manager::readCandidateGraspsFile(const QString& filename)
00203 {
00204 cartesian_coordinates pt,dir,thumbdir;
00205 cartesianGraspDirection gd;
00206 plannedGrasp *pg;
00207 double spreadAngle;
00208 std::list<plannedGrasp *>::iterator gptr;
00209
00210
00211 QFile file(filename);
00212
00213 if (!file.open(QIODevice::ReadOnly))
00214 return 1;
00215
00216 QTextStream stream( &file );
00217
00218 my_body = graspItGUI->getIVmgr()->getWorld()->getCurrentHand()->getGrasp()->getObject();
00219
00220 if (my_body == NULL){
00221 int whichObject = 0;
00222 if (graspItGUI->getIVmgr()->getWorld()->getNumGB())
00223 my_body = graspItGUI->getIVmgr()->getWorld()->getGB(whichObject);
00224 else{
00225 #ifdef GRASPITDBG
00226 std::cout << "PL_OUT: Nothing selected and no graspable bodies exist. Stop." << std::endl;
00227 #endif
00228 return 1;
00229 }
00230 }
00231
00232 preshape compPreshape;
00233 if (!graspList.empty())
00234 for (gptr = graspList.begin();gptr!=graspList.end();gptr++)
00235 delete *gptr;
00236
00237 graspList.clear();
00238
00239 while (!stream.atEnd()) {
00240 stream >> pt >> dir >> thumbdir>>spreadAngle; stream.readLine();
00241 #ifdef GRASPITDBG
00242 std::cout << "read " << pt << dir << thumbdir<<spreadAngle<<std::endl;
00243 #endif
00244 gd.set_point(pt);
00245 gd.set_dir(dir);
00246 pg = new plannedGrasp(gd);
00247 pg->set_fixedFingerDirection(thumbdir);
00248 pg->set_graspableBody(my_body);
00249 compPreshape.set_preshape(spreadAngle,0,0,0);
00250 pg->set_preshape(compPreshape);
00251
00252 graspList.push_back(pg);
00253 }
00254
00255 #ifdef GRASPITDBG
00256 std::cout << "PL_OUT: "<<graspList.size()<<" grasps read."<<std::endl;
00257 #endif
00258
00259 nrOfPlannedGrasps = graspList.size();
00260
00261
00262 primitives = my_body->getIVGeomRoot();
00263
00264 myTester->setupGraspVisWindow( my_body,primitives);
00265 myTester->visualizePlannedGrasps(graspList);
00266
00267 return 0;
00268 }
00269
00276 void
00277 grasp_manager::generateGrasps(){
00278
00279 std::list<plannedGrasp*> tmpList;
00280
00281 my_body = graspItGUI->getIVmgr()->getWorld()->getCurrentHand()->getGrasp()->getObject();
00282
00283 if (my_body == NULL){
00284 int whichObject = 0;
00285 if (graspItGUI->getIVmgr()->getWorld()->getNumGB())
00286 my_body = graspItGUI->getIVmgr()->getWorld()->getGB(whichObject);
00287 else{
00288 #ifdef GRASPITDBG
00289 std::cout << "PL_OUT: Nothing selected and no graspable bodies exist. Stop." << std::endl;
00290 #endif
00291 return;
00292 }
00293 }
00294
00295 loadPrimitives();
00296
00297
00298
00299
00300 tmpList = std::list<plannedGrasp*>(graspList);
00301
00302
00303 graspList = myPlanner->planIt(my_body,primitives);
00304
00305
00306 if (doIteration)
00307 compareGraspListsByDist(graspList, tmpList);
00308 nrOfPlannedGrasps = graspList.size();
00309
00310 myTester->setupGraspVisWindow( my_body,primitives);
00311 myTester->visualizePlannedGrasps(graspList);
00312 }
00313
00317 void
00318 grasp_manager::testGrasps(){
00319
00320 myTester->callTestIt(graspList, renderIt);
00321 graspsNotShown=1;
00322 }
00323
00334 void
00335 grasp_manager::compareGraspListsByDist(std::list<plannedGrasp*>& newList, std::list<plannedGrasp*> criteriaList){
00336 std::list<plannedGrasp*>::iterator it = newList.begin();
00337 std::list<plannedGrasp*>::iterator it_tmp;
00338 std::list<plannedGrasp*>::iterator cr_l_end = criteriaList.end();
00339
00340 bool doErase;
00341 int size = newList.size();
00342
00343 if (criteriaList.empty() || newList.empty())
00344 return;
00345
00346 for(int i=0; i<size; i++){
00347
00348 doErase = true;
00349
00350 for (std::list<plannedGrasp*>::iterator cr_it = criteriaList.begin(); cr_it != cr_l_end; cr_it++){
00351 if((*cr_it)->get_quality() >= itQualThresh){
00352 double dist = double((*cr_it)->distanceTo(**it));
00353 #ifdef GRASPITDBG
00354 std::cout << "PL_OUT: Dist is " << dist << std::endl;
00355 #endif
00356 if (dist <= maxdist){
00357 doErase = false;
00358 break;
00359 }
00360 }
00361 }
00362
00363 if (doErase){
00364 it_tmp = it;
00365 if (it!=newList.end())
00366 it++;
00367 delete (*it_tmp);
00368 newList.erase(it_tmp);
00369 }
00370 else if (it!=newList.end())
00371 it++;
00372 else break;
00373 }
00374 return;
00375 }
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00412 void
00413 grasp_manager::showGrasps(int next)
00414 {
00415
00416 if (!graspList.empty()){
00417
00418 if (graspsNotShown){
00419
00420
00421 myPresent->takeList(graspList);
00422 graspsNotShown=0;
00423 nrOfStableGrasps = graspList.size();
00424
00425 #ifdef GRASPITDBG
00426 std::cout << "PL_OUT: " << std::endl << "PL_OUT: " << std::endl;
00427 std::cout << "PL_OUT: Result grasp generation:" << std::endl;
00428 std::cout << "PL_OUT: Nr of tested grasps " << nrOfPlannedGrasps << std::endl;
00429 std::cout << "PL_OUT: Nr of found stable grasps " << nrOfStableGrasps << std::endl;
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443 std::cout << "PL_OUT: " << std::endl;
00444 std::cout << "PL_OUT: " << std::endl;
00445 #endif
00446 }
00447
00448
00449 myPresent->showGrasp(next, renderIt);
00450 }
00451
00452 #ifdef GRASPITDBG
00453 else std::cout << "PL_OUT: No grasps planned yet. Nothing to show." << std::endl;
00454 #endif
00455 }
00456
00461 void
00462 grasp_manager::chooseGrasp(){
00463 myPresent->chooseGrasp();
00464 }
00465
00470 void
00471 grasp_manager::set_render(bool in){
00472 renderIt = in;
00473 }
00474
00478 bool
00479 grasp_manager::get_render()const{
00480 return renderIt;
00481 }
00482
00489 void
00490 grasp_manager::set_doIteration(bool in){
00491 doIteration = in;
00492 }
00493
00497 bool
00498 grasp_manager::get_doIteration()const{
00499 return doIteration;
00500 }
00501
00508 bool
00509 grasp_manager::set_iterationParameters(double a,double b){
00510 if(a>1.0 || a<0.0 || b>1.0 || b<0.0)
00511 return false;
00512 itQualThresh = a;
00513 maxdist = b;
00514 return true;
00515 }
00516
00521 void
00522 grasp_manager::get_iterationParameters(double& a,double& b)const{
00523 a = itQualThresh;
00524 b = maxdist;
00525 return;
00526 }
00527
00531 grasp_planner*
00532 grasp_manager::get_graspPlanner()const{
00533 return myPlanner;
00534 }
00535
00539 grasp_tester*
00540 grasp_manager::get_graspTester()const{
00541 return myTester;
00542 }