00001 //###################################################################### 00002 // 00003 // GraspIt! 00004 // Copyright (C) 2002-2009 Columbia University in the City of New York. 00005 // All rights reserved. 00006 // 00007 // GraspIt! is free software: you can redistribute it and/or modify 00008 // it under the terms of the GNU General Public License as published by 00009 // the Free Software Foundation, either version 3 of the License, or 00010 // (at your option) any later version. 00011 // 00012 // GraspIt! is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 // 00017 // You should have received a copy of the GNU General Public License 00018 // along with GraspIt!. If not, see <http://www.gnu.org/licenses/>. 00019 // 00020 // Author(s): Matei T. Ciocarlie 00021 // 00022 // $Id: onLineGraspInterface.cpp,v 1.8 2009/05/07 19:57:26 cmatei Exp $ 00023 // 00024 //###################################################################### 00025 00026 #include "robot.h" 00027 #include "matvec3D.h" 00028 #include "barrett.h" 00029 #include "onLineGraspInterface.h" 00030 #include "searchState.h" 00031 00032 #ifdef HARDWARE_LIB 00033 #include "BarrettHand.h" 00034 #endif 00035 00036 #include "debug.h" 00037 00038 OnLineGraspInterface::OnLineGraspInterface(Hand *h) 00039 { 00040 mHand = h; 00041 mBarrettHand = NULL; 00042 mAction = ACTION_PLAN; 00043 } 00044 00045 void 00046 OnLineGraspInterface::useRealBarrettHand(bool s) 00047 { 00048 if (s) { 00049 if ( !mHand->isA("Barrett") ) { 00050 DBGA("Can't use real hand: this is not a Barrett!"); 00051 mBarrettHand = NULL; 00052 return; 00053 } 00054 mBarrettHand = ((Barrett*)mHand)->getRealHand(); 00055 } else { 00056 mBarrettHand = NULL; 00057 } 00058 } 00059 00060 00068 bool 00069 OnLineGraspInterface::getSuggestedDOF(const GraspPlanningState *s, double *initialDof, double *finalDof) 00070 { 00071 s->readPosture()->getHandDOF( finalDof ); 00072 mHand->forceDOFVals( finalDof ); 00073 00074 //close fingers gradually as we move closer to the target state 00075 transf handTran = mHand->getTran(); 00076 transf solTran = s->getTotalTran(); 00077 vec3 app = handTran.translation() - solTran.translation(); 00078 double dist = app.len(); 00079 00080 //first find how much we should open the fingers, based on distance from solution 00081 double openFingers = dist / 200.0; 00082 DBGP("Open fingers to " << openFingers); 00083 if (!mHand->quickOpen(openFingers)) { 00084 DBGP("Open finger position not found"); 00085 return false; 00086 } 00087 mHand->getDOFVals(finalDof); 00088 00089 //also check if we can get from here to there 00090 mHand->forceDOFVals(initialDof); 00091 if ( mHand->checkDOFPath(finalDof, 0.16) ) { 00092 return true; 00093 } else { 00094 DBGP("Open finger found, but not reachable"); 00095 return false; 00096 } 00097 } 00098 00103 GraspPlanningState* 00104 OnLineGraspInterface::updateHand(const std::list<GraspPlanningState*> *solutionList) 00105 { 00106 if (mAction != ACTION_PLAN) return NULL; 00107 00108 double *finalDof = new double[mHand->getNumDOF()]; 00109 double *savedDof = new double[mHand->getNumDOF()]; 00110 mHand->getDOFVals( savedDof); 00111 00112 GraspPlanningState *s = NULL; 00113 std::list<GraspPlanningState*>::const_iterator it = solutionList->begin(); 00114 while (it!=solutionList->end()) { 00115 //try the grasps in the order that they are presented. 00116 if ( (*it)->getDistance() > 1.0) { 00117 //don't use solutions that are too far away. 00118 break; 00119 } 00120 if ( getSuggestedDOF(*it,savedDof, finalDof) ) { 00121 //if I can find a way to shape the fingers for this grasps, this is the one 00122 s = (*it); 00123 break; 00124 } 00125 it++; 00126 } 00127 if (s) { 00128 DBGP("Update hand success"); 00129 mHand->forceDOFVals(finalDof); 00130 #ifdef HARDWARE_LIB 00131 if (mBarrettHand) { 00132 mBarrettHand->MoveTogether( finalDof ); 00133 } 00134 #endif 00135 } else { 00136 DBGP("Update hand failed"); 00137 mHand->forceDOFVals(savedDof); 00138 } 00139 delete finalDof; 00140 delete savedDof; 00141 return s; 00142 } 00143 00144 void 00145 OnLineGraspInterface::action(ActionType a) 00146 { 00147 switch(a) { 00148 case ACTION_PLAN: 00149 break; 00150 case ACTION_GRASP: 00151 mHand->autoGrasp(false); 00152 #ifdef HARDWARE_LIB 00153 if (mBarrettHand) { 00154 while (mBarrettHand->isBusy()); 00155 mBarrettHand->Close(BARRETT_ALL_FINGERS); 00156 } 00157 #endif 00158 break; 00159 case ACTION_OPEN: 00160 mHand->autoGrasp(false,-1.0); 00161 #ifdef HARDWARE_LIB 00162 if (mBarrettHand) { 00163 while (mBarrettHand->isBusy()); 00164 mBarrettHand->Open(BARRETT_ALL_FINGERS); 00165 } 00166 #endif 00167 break; 00168 }; 00169 mAction = a; 00170 }