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 #include <qstring.h>
00038
00039
00040 #include <Inventor/Qt/viewers/SoQtExaminerViewer.h>
00041 #include <Inventor/actions/SoGetBoundingBoxAction.h>
00042 #include <Inventor/actions/SoSearchAction.h>
00043 #include <Inventor/nodes/SoCylinder.h>
00044 #include <Inventor/nodes/SoCone.h>
00045 #include <Inventor/nodes/SoCube.h>
00046 #include <Inventor/nodes/SoSphere.h>
00047 #include <Inventor/SoLists.h>
00048 #include "SoArrow.h"
00049 #include "SoTorquePointer.h"
00050 #include "SoComplexShape.h"
00051 #include <Inventor/fields/SoSFFloat.h>
00052 #include <Inventor/actions/SoGetMatrixAction.h>
00053 #include <Inventor/SbLinear.h>
00054 #include <Inventor/nodes/SoRotation.h>
00055 #include <Inventor/nodes/SoCamera.h>
00056 #include <Inventor/nodes/SoTransformSeparator.h>
00057 #include <Inventor/nodes/SoDirectionalLight.h>
00058 #include <Inventor/sensors/SoIdleSensor.h>
00059 #include <Inventor/nodes/SoMaterial.h>
00060 #include <Inventor/SbColor.h>
00061 #include <Inventor/nodes/SoTransform.h>
00062
00063 #ifdef Q_WS_X11
00064 #include <unistd.h>
00065 #include <sys/time.h>
00066 #endif
00067
00068 #include <list>
00069
00070 #include "ivmgr.h"
00071 #include "world.h"
00072 #include "robot.h"
00073 #include "body.h"
00074 #include "matvec3D.h"
00075 #include "grasp.h"
00076 #include "quality.h"
00077 #include "graspitGUI.h"
00078 #include "grasp_visualization.h"
00079 #include "grasp_tester.h"
00080
00081
00082 #include "debug.h"
00083
00084
00085 #include "profiling.h"
00086 PROF_DECLARE(TOTAL_PLANNER);
00087
00088
00089 extern grasp_tester *myTester;
00090
00094 grasp_tester::grasp_tester() : QObject() {
00095
00096 ivmgr = graspItGUI->getIVmgr();
00097
00098 projectionViewer = NULL;
00099 idleSensor = NULL;
00100 my_hand = NULL;
00101 my_grasp = NULL;
00102 dofs = NULL;
00103 whichQM = -1;
00104 saveToFile = false;
00105 maxItStepNr = MAX_ITERATION_STEPS_PER_GRASP;
00106 backStepSize = BACK_ITERATION_STEP_SIZE;
00107 #ifdef GRASPITDBG
00108 std::cout << "PL_OUT: Tester created." << std::endl;
00109 #endif
00110 }
00111
00116 grasp_tester::~grasp_tester(){
00117 if (my_hand != NULL){
00118 if (dofs) {
00119
00120 my_hand->setTran(origTran);
00121 my_hand->forceDOFVals(dofs);
00122 }
00123 }
00124
00125 if (projectionViewer) delete projectionViewer;
00126 if (dofs) delete [] dofs;
00127 #ifdef GRASPITDBG
00128 std::cout << "PL_OUT: Tester destroyed."<< std::endl;
00129 #endif
00130 }
00131
00135 void
00136 grasp_tester::updateGlobals(){
00137 myViewer = ivmgr->getViewer();
00138 my_world = ivmgr->getWorld();
00139 my_hand = my_world->getCurrentHand();
00140 my_grasp = my_hand->getGrasp();
00141
00142 }
00143
00157 void
00158 grasp_tester::saveGraspsToFile(const QString& filename,bool append)
00159 {
00160 if (saveToFile) graspFile.close();
00161
00162 graspFile.setName(filename);
00163 if (append) {
00164 if (graspFile.open(QIODevice::WriteOnly | QIODevice::Append)) {
00165 graspOut.setDevice(&graspFile);
00166 saveToFile = true;
00167 graspOut << "0 0 0 0 0 0 0 0 -2" << endl;
00168
00169 }
00170 } else {
00171 if (graspFile.open(QIODevice::WriteOnly)) {
00172 graspOut.setDevice(&graspFile);
00173 saveToFile = true;
00174 }
00175 }
00176 }
00177
00182 void
00183 grasp_tester::pauseTests()
00184 {
00185 if (idleSensor!=NULL)
00186 delete idleSensor;
00187 }
00188
00193 void
00194 grasp_tester::continueTests()
00195 {
00196 idleSensor = new SoIdleSensor(testItCB,NULL);
00197 idleSensor->schedule();
00198 }
00199
00215 bool
00216 grasp_tester::callTestIt(std::list<plannedGrasp*>& graspList_in,bool render_in)
00217 {
00218
00219 if (idleSensor) return false;
00220
00221
00222 updateGlobals();
00223
00224
00225 if (whichQM<0){
00226 #ifdef GRASPITDBG
00227 std::cout << "PL_OUT: No quality measure specified. Do that first!" << std::endl;
00228 #endif
00229 return false;
00230 }
00231
00232
00233 if (graspList_in.empty()){
00234 #ifdef GRASPITDBG
00235 std::cout << "PL_OUT: Tester received empty grasp list. Nothing happened." << std::endl;
00236 #endif
00237 return false;
00238 }
00239
00240 nrOfGrasps = graspList_in.size();
00241 actualGraspNr = 0;
00242
00243 graspList = &graspList_in;
00244 render = render_in;
00245
00246
00247 origTran = my_hand->getTran();
00248 dofs = new double[my_hand->getNumDOF()];
00249 for (int i=0; i<my_hand->getNumDOF(); i++){
00250 dofs[i] = my_hand->getDOF(i)->getVal();
00251 }
00252
00253
00254 it_gr = (*graspList).begin();
00255
00256 PROF_RESET_ALL;
00257 PROF_START_TIMER(TOTAL_PLANNER);
00258
00259
00260 idleSensor = new SoIdleSensor(testItCB,NULL);
00261 idleSensor->schedule();
00262
00263 return true;
00264 }
00265
00266
00271 void
00272 grasp_tester::testItCB(void *,SoSensor *){
00273 myTester->testIt();
00274 }
00275
00276
00284 void
00285 grasp_tester::testIt()
00286 {
00287 bool do_iteration;
00288 bool do_save;
00289
00290
00291 #ifdef GRASPITDBG
00292 std::cout<<"PL_OUT: Testing grasp no "<< actualGraspNr++<<" out of "<<
00293 nrOfGrasps<<std::endl;
00294 #endif
00295
00296
00297 if (it_gr != (*graspList).end()){
00298
00299 do_iteration = false;
00300 do_save = false;
00301
00302
00303 #ifdef GRASPITDBG
00304 std::cout << "PL_OUT: vor change color" << std::endl;
00305 #endif
00306
00307 (*it_gr)->get_graspRepresentation()->changeColor(1.,0.,0.);
00308 if (render){
00309 myViewer->render();
00310 projectionViewer->render();
00311 }
00312
00313 #ifdef GRASPITDBG
00314 std::cout << "PL_OUT: Put hand in position" << std::endl;
00315 #endif
00316
00317
00318 if (putIt(*it_gr, render) == SUCCESS){
00319
00320 #ifdef GRASPITDBG
00321 std::cout << "PL_OUT: set preshape" << std::endl;
00322 #endif
00323
00324
00325 preshapeIt((*it_gr)->get_preshape(), render);
00326
00327 #ifdef GRASPITDBG
00328 std::cout << "PL_OUT: test for collisions" << std::endl;
00329 #endif
00330
00331
00332 if (!handCollision()){
00333
00334 #ifdef GRASPITDBG
00335 std::cout << "PL_OUT: move hand towards object" << std::endl;
00336 #endif
00337
00338
00339 if (moveIt((*it_gr)->get_graspDirection(), render)){
00340
00341 #ifdef GRASPITDBG
00342 std::cout << "PL_OUT: check contacts" << std::endl;
00343 #endif
00344
00345 if (checkContactToHand((*it_gr)->get_graspableBody())){
00346
00347
00348 my_hand->autoGrasp(render,1);
00349
00350 my_world->updateGrasps();
00351
00352
00353
00354
00355 (*it_gr)->set_quality(my_grasp->getQM(whichQM)->evaluate());
00356 #ifdef GRASPITDBG
00357 std::cout << "PL_OUT: !!!! whichQM: "<<whichQM<<" quality: "<<(*it_gr)->get_quality()<<std::endl;
00358 #endif
00359 if (saveToFile) saveGrasp((*it_gr)->get_quality());
00360
00361 if ((*it_gr)->get_quality() > QUALITY_MIN_THRESHOLD && (*it_gr)->get_quality() <= 1.0)
00362 do_save = true;
00363 }
00364 else{
00365 do_iteration = true;
00366 }
00367
00368
00369
00370
00371 if (do_iteration ||
00372 ((*it_gr)->get_quality() <= QUALITY_MIN_THRESHOLD)){
00373
00374 if (iteration(**it_gr)){
00375
00376 do_save = true;
00377 }
00378 }
00379 }
00380
00381 #ifdef GRASPITDBG
00382 else std::cout << "PL_OUT: MoveIt failed." << std::endl;
00383 #endif
00384
00385 }
00386 }
00387
00388 #ifdef GRASPITDBG
00389 else
00390 std::cout<<"PL_OUT: putIt failed. Stepping to next grasp." << std::endl;
00391 #endif
00392
00393 if (do_save){
00394
00395
00396 (*it_gr)->get_graspRepresentation()->changeRadius((*it_gr)->get_quality());
00397
00398
00399 savePosition(**it_gr);
00400 }
00401 else
00402 (*it_gr)->remove_graspRepresentation();
00403
00404
00405
00406
00407
00408 if (it_gr != (*graspList).end())
00409 it_gr++;
00410 }
00411
00412
00413
00414 else{
00415
00416 #ifdef GRASPITDBG
00417 std::cout << "PL_OUT: Last grasp reached" << std::endl;
00418 #endif
00419
00420
00421 orderGraspListByQuality(*graspList);
00422 if (saveToFile) {graspFile.close(); saveToFile = false;}
00423
00424
00425 if (idleSensor != NULL)
00426 delete idleSensor;
00427 idleSensor = NULL;
00428
00429 if (render){
00430
00431 my_hand->setTran(origTran);
00432 my_hand->forceDOFVals(dofs);
00433 }
00434
00435 PROF_STOP_TIMER(TOTAL_PLANNER);
00436 PROF_PRINT_ALL;
00437 emit testingComplete();
00438
00439 }
00440 if (idleSensor != NULL)
00441 idleSensor->schedule();
00442
00443 if (!render){
00444
00445 my_hand->setTran(origTran);
00446 my_hand->forceDOFVals(dofs);
00447 }
00448 }
00449
00454 void
00455 grasp_tester::saveGrasp(double quality){
00456 graspOut << my_hand->getTran().translation()[0] << " " <<
00457 my_hand->getTran().translation()[1] << " " <<
00458 my_hand->getTran().translation()[2] << " " <<
00459 my_hand->getTran().rotation().w << " " <<
00460 my_hand->getTran().rotation().x << " " <<
00461 my_hand->getTran().rotation().y << " " <<
00462 my_hand->getTran().rotation().z << " " <<
00463 my_hand->getDOF(0)->getVal() * 180.0/M_PI << " " <<
00464 quality << endl;
00465 }
00466
00470 bool
00471 grasp_tester::handCollision(){
00472
00473 int numCols = my_world->getCollisionReport(&colReport);
00474
00475
00476
00477
00478
00479
00480
00481 return (numCols > 0);
00482
00483 }
00484
00494 bool
00495 grasp_tester::iteration(plannedGrasp& pg)
00496 {
00497 double backStepDist = backStepSize;
00498 transf actTran;
00499 vec3 toVec;
00500
00501 for(int step=0; step<maxItStepNr; step++){
00502
00503
00504 actTran = my_hand->getTran();
00505 toVec = actTran.translation() - backStepDist * pg.get_graspDirection().get_dir() / pg.get_graspDirection().get_dir().len();
00506
00507
00508
00509 transf to = coordinate_transf(position(toVec.x(), toVec.y(), toVec.z()),
00510 ( - pg.get_fixedFingerDirection()) *
00511 pg.get_graspDirection().get_dir(),
00512 - pg.get_fixedFingerDirection());
00513
00514 if (my_hand->setTran(to)) return false;
00515
00516
00517 preshapeIt(pg.get_preshape(), render);
00518
00519 if (render)
00520 myViewer->render();
00521
00522 if (handCollision()) return false;
00523
00524
00525 my_hand->autoGrasp(render,1);
00526
00527 my_world->updateGrasps();
00528
00529
00530
00531
00532
00533
00534 if (!checkContactToHand(pg.get_graspableBody())){
00535 if (my_hand->getName().startsWith("Barrett")){
00536
00537 int allClosed = 0;
00538 for (int i=1; i<4;i++){
00539 if (my_hand->getDOF(i)->getVal() == my_hand->getDOF(i)->getMax()){
00540 allClosed++;
00541 }
00542 }
00543 if (allClosed >= 2)
00544 return false;
00545 }
00546 else return false;
00547 }
00548
00549
00550 pg.set_quality(my_grasp->getQM(whichQM)->evaluate());
00551
00552 if (saveToFile) saveGrasp(pg.get_quality());
00553
00554
00555 if (pg.get_quality() > QUALITY_MIN_THRESHOLD){
00556
00557 return true;
00558 }
00559 }
00560
00561 return false;
00562 }
00563
00568 void
00569 grasp_tester::savePosition(plannedGrasp& pg){
00570 finalGraspPosition fgp;
00571 fgp.set_finalTran(my_hand->getTran());
00572 std::list<double> dl;
00573 for (int i=0;i<my_hand->getNumDOF();i++){
00574 fgp.add_dof(my_hand->getDOF(i)->getVal());
00575 }
00576 pg.set_finalGraspPosition(fgp);
00577 }
00578
00579
00580
00581 #ifdef WIN32
00582
00588 void
00589 sortGrasps(std::list<plannedGrasp*>& grl,
00590 std::list<plannedGrasp*>::iterator left,
00591 std::list<plannedGrasp*>::iterator right,
00592 int size)
00593 {
00594 if (size > 1) {
00595 std::list<plannedGrasp*> tempList;
00596 std::list<plannedGrasp*>::iterator leftCopy = left;
00597 std::list<plannedGrasp*>::iterator mid;
00598 std::list<plannedGrasp*>::iterator midCopy;
00599 int i;
00600
00601 for (mid=left,i=0;i<size/2;i++,mid++);
00602 sortGrasps(grl,left,mid,size/2);
00603 sortGrasps(grl,mid,right,size-size/2);
00604
00605
00606 midCopy = mid;
00607 while (leftCopy != mid && midCopy != right) {
00608 if ((*leftCopy)->get_quality() <= (*midCopy)->get_quality()) {
00609 tempList.push_back(*leftCopy); leftCopy++;
00610 }
00611 else {
00612 tempList.push_back(*midCopy); midCopy++;
00613 }
00614 }
00615 while (leftCopy != mid) {
00616 tempList.push_back(*leftCopy); leftCopy++;
00617 }
00618 while (midCopy != right) {
00619 tempList.push_back(*midCopy); midCopy++;
00620 }
00621 for (leftCopy = tempList.begin();leftCopy!=tempList.end();leftCopy++,left++)
00622 *left = *leftCopy;
00623 }
00624 }
00625 #endif
00626
00631 void
00632 grasp_tester::orderGraspListByQuality(std::list<plannedGrasp*>& grl){
00633
00634 if (grl.empty())
00635 return;
00636
00637 #ifdef WIN32
00638
00639
00640 sortGrasps(grl,grl.begin(),grl.end(),grl.size());
00641 #else
00642 grl.sort(compareGraspQM());
00643 #endif
00644
00645 while((!grl.empty()) && ((*grl.begin())->get_quality() <= 0.0)){
00646 delete *(grl.begin());
00647 grl.pop_front();
00648 }
00649 grl.reverse();
00650
00651 #ifdef GRASPITDBG
00652 std::list<plannedGrasp*>::iterator it;
00653 for (it = grl.begin(); it != grl.end(); it++){
00654 std::cout << "PL_OUT: QM " << (*it)->get_quality() << std::endl;
00655 }
00656 #endif
00657
00658 }
00659
00664 bool
00665 grasp_tester::checkContactToHand(GraspableBody *gb){
00666
00667 std::list<Contact *> contactList = gb->getContacts();
00668 if (contactList.empty()){
00669 return false;
00670 }
00671
00672 std::list<Contact *>::iterator it = contactList.begin();
00673
00674 for (;it!=contactList.end();it++){
00675 if (((*it)->getBody2()->getOwner() == my_hand) ||
00676 ((*it)->getBody1()->getOwner() == my_hand)){
00677 return true;
00678 }
00679 }
00680 return false;
00681 }
00682
00687 bool
00688 grasp_tester::putIt(plannedGrasp* pg, bool render_in){
00689
00690 int result;
00691 transf to =
00692 coordinate_transf(position(pg->get_graspDirection().get_point().x(),
00693 pg->get_graspDirection().get_point().y(),
00694 pg->get_graspDirection().get_point().z()),
00695 ( - pg->get_fixedFingerDirection()) *
00696 pg->get_graspDirection().get_dir(),
00697 - pg->get_fixedFingerDirection());
00698
00699 result = my_hand->setTran(to);
00700 if (render_in)
00701 myViewer->render();
00702
00703 return result;
00704 }
00705
00710 bool
00711 grasp_tester::preshapeIt(preshape p, bool render_in)
00712 {
00713 double a,f1,f2,f3;
00714 p.get_preshape(a,f1,f2,f3);
00715 double v[4];
00716
00717 v[0]=M_PI/180.0 * a;
00718 v[1]=M_PI/180.0 * f1;
00719 v[2]=M_PI/180.0 * f2;
00720 v[3]=M_PI/180.0 * f3;
00721
00722 my_hand->forceDOFVals(v);
00723
00724 if (render_in)
00725 myViewer->render();
00726 return true;
00727 }
00728
00736 bool
00737 grasp_tester::moveIt(cartesianGraspDirection gd, bool render_in)
00738 {
00739
00740 transf to(my_hand->getTran().rotation(),
00741 my_hand->getTran().translation() + 1000 * gd.get_dir());
00742
00743 my_hand->moveTo(to,50*Contact::THRESHOLD,M_PI/36.0);
00744
00745 if (render_in)
00746 myViewer->render();
00747 return true;
00748 }
00749
00757 void
00758 grasp_tester::setupGraspVisWindow(GraspableBody* myBody,SoGroup *prim)
00759 {
00760
00761 if (projectionViewer) {
00762 QWidget *topShell = projectionViewer->getShellWidget();
00763 delete projectionViewer;
00764 delete topShell;
00765 }
00766
00767
00768 updateGlobals();
00769
00770 SoSeparator *VisTop = new SoSeparator();
00771 SoTransformSeparator *lightSep = new SoTransformSeparator();
00772 SoRotation *lightDir = new SoRotation();
00773 SoSeparator *objSep = new SoSeparator();
00774 glRoot = new SoSeparator();
00775
00776 lightDir->rotation.connectFrom(&myViewer->getCamera()->orientation);
00777 lightSep->addChild(lightDir);
00778 lightSep->addChild(myViewer->getHeadlight());
00779
00780 objSep->addChild(myBody->getIVTran());
00781 objSep->addChild(prim);
00782
00783 VisTop->addChild(myViewer->getCamera());
00784 VisTop->addChild(lightSep);
00785 VisTop->addChild(objSep);
00786 VisTop->addChild(glRoot);
00787
00788 projectionViewer = new SoQtRenderArea();
00789 projectionViewer->setTransparencyType(SoGLRenderAction::SORTED_OBJECT_BLEND);
00790 projectionViewer->setBackgroundColor(SbColor(1,1,1));
00791 projectionViewer->setTitle("GraspIt!");
00792
00793 projectionViewer->setSceneGraph(VisTop);
00794 projectionViewer->show();
00795 }
00796
00802 void
00803 grasp_tester::visualizePlannedGrasps(std::list<plannedGrasp*> grList)
00804 {
00805
00806 std::list <plannedGrasp*>::iterator gl_it;
00807 SbMatrix mat1;
00808 SbMatrix mat2;
00809 SbVec3f pos,approach,thumb;
00810 grasp_representation *gRep;
00811
00812 for (gl_it=grList.begin(); gl_it!=grList.end(); gl_it++){
00813
00814 pos.setValue((*gl_it)->get_graspDirection().get_point().x(),
00815 (*gl_it)->get_graspDirection().get_point().y(),
00816 (*gl_it)->get_graspDirection().get_point().z());
00817
00818 approach.setValue((*gl_it)->get_graspDirection().get_dir().x(),
00819 (*gl_it)->get_graspDirection().get_dir().y(),
00820 (*gl_it)->get_graspDirection().get_dir().z());
00821
00822 thumb.setValue((*gl_it)->get_fixedFingerDirection().x(),
00823 (*gl_it)->get_fixedFingerDirection().y(),
00824 (*gl_it)->get_fixedFingerDirection().z());
00825
00826
00827
00828 mat1.setTransform(pos,SbRotation(SbVec3f(0.,1.,0.),approach),
00829 SbVec3f(1.,1.,1.));
00830
00831 mat2.setTransform(pos,SbRotation(SbVec3f(0.,1.,0.),thumb),
00832 SbVec3f(1.,1.,1.));
00833
00834 gRep = new grasp_representation(mat1, mat2, glRoot);
00835 (*gl_it)->set_graspRepresentation(gRep);
00836
00837 }
00838 return;
00839 }
00840
00846 bool
00847 grasp_tester::set_testingParameters(int itStepNr, double stepSize){
00848 if (itStepNr < 0 || stepSize < 0.0)
00849 return false;
00850 maxItStepNr = itStepNr;
00851 backStepSize = stepSize;
00852 return true;
00853 }
00854
00858 void
00859 grasp_tester::get_testingParameters(int& itStepNr, double& stepSize){
00860 itStepNr = maxItStepNr;
00861 stepSize = backStepSize;
00862 return;
00863 }
00864
00865
00866
00867
00868
00869
00870
00871