GraspTable.cpp
Go to the documentation of this file.
00001 
00033 #include <ros/ros.h>
00034 #include <GraspTable.h>
00035 
00036 void GraspTable::ReadDoubleValue(TiXmlElement* xml, const char * tag, double * value)
00037 {
00038   TiXmlHandle handle(xml);
00039   TiXmlElement* element=handle.FirstChild(tag).Element();
00040   sscanf(element->GetText(), "%lf", value);
00041 }
00042 
00043 void GraspTable::ReadJoint(TiXmlElement* xml, const char * tag, std::vector<double> & values)
00044 {
00045   TiXmlHandle handle(xml);
00046   TiXmlElement* element=handle.FirstChild(tag).Element();
00047   values.resize(16);
00048   ReadDoubleValue(element, "ThumbAngle",           &(values.data()[0]));
00049   ReadDoubleValue(element, "ThumbBase",            &(values.data()[1]));
00050   ReadDoubleValue(element, "ThumbProximal",        &(values.data()[2]));
00051   ReadDoubleValue(element, "ThumbDistal",          &(values.data()[3]));
00052   ReadDoubleValue(element, "FirstFingerBase",      &(values.data()[4]));
00053   ReadDoubleValue(element, "FirstFingerProximal",  &(values.data()[5]));
00054   ReadDoubleValue(element, "FirstFingerDistal",    &(values.data()[6]));
00055   ReadDoubleValue(element, "MiddleFingerBase",     &(values.data()[7]));
00056   ReadDoubleValue(element, "MiddleFingerProximal", &(values.data()[8]));
00057   ReadDoubleValue(element, "MiddleFingerDistal",   &(values.data()[9]));
00058   ReadDoubleValue(element, "RingFingerBase",       &(values.data()[10]));
00059   ReadDoubleValue(element, "RingFingerProximal",   &(values.data()[11]));
00060   ReadDoubleValue(element, "RingFingerDistal",     &(values.data()[12]));
00061   ReadDoubleValue(element, "LittleFingerBase",     &(values.data()[13]));
00062   ReadDoubleValue(element, "LittleFingerProximal", &(values.data()[14]));
00063   ReadDoubleValue(element, "LittleFingerDistal",   &(values.data()[15]));
00064 }
00065 
00066 void GraspTable::ReadPose(TiXmlElement* xml, const char * tag, std::vector<double> & values)
00067 {
00068   TiXmlHandle handle(xml);
00069   TiXmlElement* element=handle.FirstChild(tag).Element();
00070   values.resize(6);
00071   ReadDoubleValue(element, "PositionX", &(values.data()[0]));
00072   ReadDoubleValue(element, "PositionY", &(values.data()[1]));
00073   ReadDoubleValue(element, "PositionZ", &(values.data()[2]));
00074   ReadDoubleValue(element, "Roll",      &(values.data()[3]));
00075   ReadDoubleValue(element, "Pitch",     &(values.data()[4]));
00076   ReadDoubleValue(element, "Yaw",       &(values.data()[5]));
00077 }
00078 
00079 int GraspTable::ReadFromFile(const char * filename, GraspTableObject* graspTableObject)
00080 {
00081   TiXmlDocument doc(filename);
00082   if (graspTableObject == NULL)
00083   {
00084         printf("GraspTable::ReadFromFile:Error,  argument error%s\n",filename);
00085         return -3;
00086   }
00087   if (!doc.LoadFile())
00088   {
00089         printf("GraspTable::ReadFromFile:Error, could not read %s\n",filename);
00090     return -1;
00091   }
00092   //printf ("Readig file %s\n",filename);
00093   TiXmlHandle root_handle(&doc);
00094   TiXmlHandle grasp_list_handle=root_handle.FirstChild("GraspList");
00095   TiXmlElement* number_element=grasp_list_handle.FirstChildElement("Number").Element();
00096   // total number of grasps in this file
00097   int number;
00098   sscanf(number_element->GetText(), "%d", &number);
00099 
00100   if (graspTableObject->Init(number) != 0)
00101   {
00102         printf("GraspTable::ReadFromFile:Error, could not allocate GraspTableObject\n");
00103         return -2;
00104   }
00105   for (int i=0; i<number; i++)
00106     {
00107       TiXmlHandle grasp_handle=grasp_list_handle.ChildElement("Grasp", i);
00108       TiXmlElement* grasp_element=grasp_handle.Element();
00109       double quality;
00110       grasp_element->QueryDoubleAttribute("Quality", &quality);
00111       
00112           Grasp * newGrasp = new Grasp();
00113           newGrasp->SetGraspId(i);
00114         
00115       std::vector<double> values;
00116 
00117       ReadPose(grasp_element, "ApproachPose", values);
00118           newGrasp->SetTCPPreGraspPose(values);
00119       
00120           ReadPose(grasp_element, "GraspPose", values);
00121           newGrasp->SetTCPGraspPose(values);
00122 
00123       ReadJoint(grasp_element, "ApproachJoint", values);
00124           newGrasp->SetHandPreGraspConfig(values);
00125       
00126           ReadJoint(grasp_element, "GraspJoint", values);
00127           newGrasp->SetHandGraspConfig(values);
00128       
00129           ReadJoint(grasp_element, "GraspOptimalJoint", values);
00130           newGrasp->SetHandOptimalGraspConfig(values);
00131 
00132           graspTableObject->AddGrasp(newGrasp);
00133     }
00134 
00135         return 0;
00136 }
00137 
00138 
00139 int GraspTable::Init(char* object_table_file,unsigned int table_size)
00140 {
00141         FILE* f = fopen(object_table_file,"r");
00142         int numberOfObjects = 0;
00143 
00144         if (f==NULL)
00145         {
00146                 printf("GraspTable::Error, Object Table File not found :%s\n",object_table_file);
00147                 return -1;
00148 
00149         }
00150         fscanf(f,"%d\n",&numberOfObjects);
00151         m_GraspTable.resize(table_size); //range of DESIRE class ids
00152         for (unsigned int i=0;i < table_size;i++)
00153         {
00154                 m_GraspTable[i] = NULL;
00155         }
00156         for (int obj=0; obj <numberOfObjects; obj++)
00157         {
00158                 char GraspTableFileName[500];
00159                 int objectClassId = 0;
00160                 fscanf(f,"%d, %s\n",&objectClassId,GraspTableFileName);
00161 //~ #####################################################################################################################
00162                 std::string str ("/GraspTable.txt");
00163                 std::string object_table_file_str=object_table_file;
00164                 std::string::iterator end= object_table_file_str.end();
00165                 std::string::iterator begin= object_table_file_str.end()-str.size();
00166                 object_table_file_str.erase(begin, end);
00167                 object_table_file_str+=GraspTableFileName;
00168                 strncpy(GraspTableFileName, object_table_file_str.c_str(), sizeof(GraspTableFileName));
00169                 GraspTableFileName[sizeof(GraspTableFileName) - 1] = 0;
00170 //~ #####################################################################################################################
00171                 printf("GraspTable::Init: Trying to read grasp table for object %d from file %s ...\n",objectClassId,GraspTableFileName);
00172                 GraspTableObject * graspTableObject = new GraspTableObject();
00173                 graspTableObject->SetObjectClassId(objectClassId);
00174                 if (ReadFromFile(GraspTableFileName,graspTableObject)==0)
00175                 {
00176                         printf("successful\n");
00177                         AddGraspTableObject(graspTableObject);
00178                 }
00179                 else
00180                 {
00181                         printf("failed\n");
00182                 }
00183         }
00184         return 5;
00185 }
00186 
00187 void GraspTable::AddGraspTableObject(GraspTableObject* object)
00188 {
00189         unsigned int objectClassId = object->GetObjectClassId();
00190         if (objectClassId < m_GraspTable.size())
00191         {
00192                 m_GraspTable[objectClassId]=object;
00193         }
00194         else
00195         {
00196                 printf("GraspTable::AddGraspTableObject: Error, class id larger than table size!\n");
00197         }
00198 }
00199 
00200 
00201 Grasp* GraspTable::GetNextGrasp(unsigned int objectClassId)
00202 {
00203         Grasp* retVal = NULL;
00204         if (objectClassId < m_GraspTable.size() && m_GraspTable[objectClassId] != NULL)
00205         {
00206                         if ((objectClassId != m_lastObjectClassId))
00207                         {
00208                                         m_lastObjectClassId=objectClassId;
00209                                         m_GraspTable[objectClassId]->ResetGraspReadPtr();
00210                         }
00211                         retVal =  m_GraspTable[objectClassId]->GetNextGrasp();
00212         }
00213         return retVal;
00214 }
00215 
00216 void GraspTable::ResetReadPtr(unsigned int object_class_id)
00217 {
00218 
00219         if (object_class_id < m_GraspTable.size())
00220         {
00221                 m_GraspTable[object_class_id]->ResetGraspReadPtr();
00222         } 
00223 }
00224 
00225 Grasp * GraspTable::GetGrasp(unsigned int objectClassId, unsigned int & grasp_id)
00226 {
00227         Grasp* retVal = NULL;
00228         if (objectClassId < m_GraspTable.size() && m_GraspTable[objectClassId] != NULL)
00229         {
00230                 retVal=m_GraspTable[objectClassId]->GetGrasp(grasp_id);
00231         }
00232         return retVal;
00233 }
00234 
00235 
00236 
00237 
00238 
00239 
00240 
00241 
00242 
00243 
00244 
00245 
00246 
00247 
00248 


cob_pick_place_action
Author(s): Felix Messmer
autogenerated on Wed Aug 26 2015 11:01:29