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
00030 #include "graspit_db_grasp.h"
00031 #include <QSqlQuery>
00032 #include <QtSql>
00033 #include <list>
00034
00035 #include "searchState.h"
00036 #include "matvec3D.h"
00037 #include "body.h"
00038 #include "robot.h"
00039
00040 #include "debug.h"
00041
00043 bool initializeHandObjectState(const std::vector<double>& joints, const std::vector<double>& position,
00044 GraspPlanningState *state)
00045 {
00046 if(joints.size() == 0) return false;
00047 state->setPostureType(POSE_DOF);
00048 state->getPosture()->readFromArray(const_cast<std::vector<double>&>(joints));
00049 state->setPositionType(SPACE_COMPLETE);
00050 state->getPosition()->readFromArray(const_cast<std::vector<double>&>(position));
00051 return true;
00052 }
00053
00054 GraspitDBGrasp::~GraspitDBGrasp(){
00055 delete mPreGrasp;
00056 delete mFinalGrasp;
00057 }
00058
00060 GraspitDBGrasp::GraspitDBGrasp(const GraspitDBGrasp& grasp2) : db_planner::Grasp(grasp2), mHand(grasp2.mHand) {
00061 mPreGrasp = new GraspPlanningState(grasp2.mPreGrasp->getHand());
00062 mPreGrasp->copyFrom(grasp2.mPreGrasp);
00063 mFinalGrasp = new GraspPlanningState(grasp2.mFinalGrasp->getHand());
00064 mFinalGrasp->copyFrom(grasp2.mFinalGrasp);
00065 mTestScores = grasp2.mTestScores;
00066 }
00067
00069 bool GraspitDBGrasp::SetGraspParameters(const std::vector<double>& prejoint,
00070 const std::vector<double>& prepos,
00071 const std::vector<double>& finjoint,
00072 const std::vector<double>& finpos){
00073
00074 mPreGrasp = new GraspPlanningState(mHand);
00075 initializeHandObjectState(prejoint, prepos, mPreGrasp);
00076
00077 mFinalGrasp = new GraspPlanningState(mHand);
00078 initializeHandObjectState(finjoint, finpos, mFinalGrasp);
00079 return true;
00080 }
00081
00083 bool GraspitDBGrasp::Transform(const float array[16]){
00084
00085 mat3 m;
00086 m[0] = array[0];
00087 m[1] = array[1];
00088 m[2] = array[2];
00089 m[3] = array[4];
00090 m[4] = array[5];
00091 m[5] = array[6];
00092 m[6] = array[8];
00093 m[7] = array[9];
00094 m[8] = array[10];
00095 vec3 v;
00096 v[0] = array[3];
00097 v[1] = array[7];
00098 v[2] = array[11];
00099 transf transform;
00100 transform.set(m,v);
00101
00102 std::vector<double> position;
00103
00104 PositionState* ps = mPreGrasp->getPosition();
00105 ps->setTran(ps->getCoreTran() * transform);
00106 for(int i = 0; i < ps->getNumVariables(); ++i){
00107 position.push_back(ps->getVariable(i)->getValue());
00108 }
00109 SetPregraspPosition(position);
00110
00111 position.clear();
00112 ps = mFinalGrasp->getPosition();
00113 ps->setTran(ps->getCoreTran() * transform);
00114 for(int i = 0; i < ps->getNumVariables(); ++i){
00115 position.push_back(ps->getVariable(i)->getValue());
00116 }
00117 SetFinalgraspPosition(position);
00118
00119 return true;
00120 }
00121
00123 double GraspitDBGrasp::getTestAverageScore(){
00124 double sum = 0;
00125 for(int i = 0; i < (int)mTestScores.size(); i ++){
00126 sum += mTestScores[i];
00127 }
00128 if((int)mTestScores.size()>0){
00129 return sum/(double)mTestScores.size();
00130 }
00131 else{
00132 return 0;
00133 }
00134 }
00135
00136 void GraspitDBGrasp::setPreGraspPlanningState(GraspPlanningState* p){
00137 delete mPreGrasp;
00138 mPreGrasp = p;
00139
00140 const PositionState *positionS = p->readPosition();
00141 const PostureState *postureS = p->readPosture();
00142
00143 std::vector<double> position, joints;
00144 for(int i = 0; i < positionS->getNumVariables(); ++i){
00145 position.push_back(positionS->getVariable(i)->getValue());
00146 }
00147 for(int i = 0; i < postureS->getNumVariables(); ++i){
00148 joints.push_back(postureS->getVariable(i)->getValue());
00149 }
00150 this->SetPregraspJoints(joints);
00151 this->SetPregraspPosition(position);
00152 }
00153
00154 void GraspitDBGrasp::setFinalGraspPlanningState(GraspPlanningState* p){
00155 delete mFinalGrasp;
00156 mFinalGrasp = p;
00157
00158 const PositionState *positionS = p->readPosition();
00159 const PostureState *postureS = p->readPosture();
00160
00161 std::vector<double> pos, joints, contacts;
00162 for(int i = 0; i < positionS->getNumVariables(); ++i){
00163 pos.push_back(positionS->getVariable(i)->getValue());
00164 }
00165 for(int i = 0; i < postureS->getNumVariables(); ++i){
00166 joints.push_back(postureS->getVariable(i)->getValue());
00167 }
00168 std::list<position>* tmpContacts;
00169 tmpContacts = p->getContacts();
00170 for(std::list<position>::iterator it = tmpContacts->begin(); it!= tmpContacts->end(); ++it){
00171 contacts.push_back((*it).x());
00172 contacts.push_back((*it).y());
00173 contacts.push_back((*it).z());
00174 }
00175 this->SetFinalgraspJoints(joints);
00176 this->SetFinalgraspPosition(pos);
00177 this->SetContacts(contacts);
00178 }
00179
00181 QString GraspitDBGrasp::getHandDBName(Hand* h)
00182 {
00183 QString handName = h->getName();
00184 int material = h->getFinger(0)->getLink(0)->getMaterial();
00185 QString hand_db_name;
00186 if(handName == QString("Barrett")){
00187 if(material == h->getWorld()->getMaterialIdx("rubber")){
00188 hand_db_name = QString("BARRETT_RUBBER");
00189 } else if(material == graspItGUI->getIVmgr()->getWorld()->getMaterialIdx("plastic")){
00190 hand_db_name = QString("BARRETT_PLASTIC");
00191 } else if(material == h->getWorld()->getMaterialIdx("wood")){
00192 hand_db_name = QString("BARRETT_WOOD");
00193 }
00194 }else if(handName == QString("HumanHand20DOF")){
00195 hand_db_name = QString("HUMAN");
00196 }else if(handName == QString("pr2_gripper") ) {
00197 hand_db_name = QString("WILLOW_GRIPPER");
00198 }else if (handName == QString("pr2_gripper_2008")){
00199 hand_db_name = QString("WILLOW_GRIPPER_2008");
00200 }else if(handName == QString("pr2_gripper_2010")){
00201 hand_db_name = QString("WILLOW_GRIPPER_2010");
00202 }else if(handName == QString("McGrip") || handName == QString("McGrip_optim")){
00203 hand_db_name = QString("MC_GRIP");
00204 }else if(handName == QString("McHand")){
00205 hand_db_name = QString("GRIPPER_DESIGN_1");
00206 }else if(handName == QString("gripper_design_4")){
00207 hand_db_name = QString("GRIPPER_DESIGN_4");
00208 }else {
00209 std::cout << "Wrong hand name detected: " << handName.latin1() <<
00210 ". Acceptable GRASPIT_hand_names are: Barrett, HumanHand20DOF, pr2_gripper" <<
00211 ", pr_gripper_2008, pr2_gripper_2010" << std::endl;
00212 hand_db_name = QString::null;
00213 }
00214 return hand_db_name;
00215 }
00216
00217 QString GraspitDBGrasp::getHandGraspitPath(QString handDBName)
00218 {
00219 QString path;
00220 if (handDBName=="BARRETT_RUBBER") {
00221 path = "/models/robots/Barrett/Barrett.xml";
00222 } else if (handDBName=="BARRETT_PLASTIC") {
00223 path = "/models/robots/Barrett/Barrett.xml";
00224 } else if (handDBName=="BARRETT_WOOD") {
00225 path = "/models/robots/Barrett/Barrett.xml";
00226 } else if (handDBName=="BARRETT_HUMAN") {
00227 path = "/models/robots/HumanHand/HumanHand20DOF.xml";
00228 } else if (handDBName=="WILLOW_GRIPPER") {
00229 path = "/models/robots/pr2_gripper/pr2_gripper.xml";
00230 } else if (handDBName=="WILLOW_GRIPPER_2008") {
00231 path = "/models/robots/pr2_gripper/pr2_gripper_2008.xml";
00232 } else if (handDBName=="WILLOW_GRIPPER_2010") {
00233 path = "/models/robots/pr2_gripper/pr2_gripper_2010.xml";
00234 } else if (handDBName=="MC_GRIP") {
00235 path = "/models/robots/McHand/McGrip.xml";
00236 } else {
00237 DBGA("Cannot convert database hand name " << handDBName.latin1() <<
00238 " to GraspIt path");
00239 path = QString::null;
00240 }
00241 return path;
00242
00243 }