grasp_planning.cpp
Go to the documentation of this file.
00001 #ifdef DOXYGEN_SHOULD_SKIP_THIS
00002 
00021 #endif  // DOXYGEN_SHOULD_SKIP_THIS
00022 
00023 #include <grasp_planning_graspit/GraspItSceneManagerHeadless.h>
00024 #include <grasp_planning_graspit/LogBinding.h>
00025 #include <grasp_planning_graspit/EigenGraspPlanner.h>
00026 #include <grasp_planning_graspit/EigenGraspResult.h>
00027 
00028 #include <string>
00029 #include <set>
00030 #include <stdio.h>
00031 #include <execinfo.h>
00032 #include <signal.h>
00033 #include <stdlib.h>
00034 #include <unistd.h>
00035 #include <vector>
00036 
00037 #include <boost/program_options/options_description.hpp>
00038 #include <boost/program_options/variables_map.hpp>
00039 #include <boost/program_options/parsers.hpp>
00040 
00041 // if this is defined, the EigenGraspPlannerNoQt implementation
00042 // is used. If not defined, EigenGraspPlannern implementation is used.
00043 // #define USE_EIGENGRASP_NOQT
00044 
00045 #ifdef USE_EIGENGRASP_NOQT
00046 // This needs to be changed to remove graspit include for StateType
00047 // #include <grasp_planning_graspit/EigenGraspPlannerNoQt.h>
00048 #endif  // USE_EIGENGRASP_NOQT
00049 
00050 
00054 void print_trace(void)
00055 {
00056     void *array[10];
00057     size_t size;
00058     char **strings;
00059     size_t i;
00060 
00061     size = backtrace(array, 10);
00062     strings = backtrace_symbols(array, size);
00063 
00064     printf("Obtained %zd stack frames.\n", size);
00065 
00066     for (i = 0; i < size; i++)
00067         printf("%s\n", strings[i]);
00068 
00069     free(strings);
00070 }
00071 
00072 
00073 void handler(int sig)
00074 {
00075     print_trace();
00076     exit(1);
00077 }
00078 
00079 
00080 boost::program_options::options_description getOptions()
00081 {
00082     // Declare the supported options.
00083     boost::program_options::options_description desc("Allowed options");
00084     desc.add_options()
00085     ("help", "produce help message")
00086     ("dir", boost::program_options::value<std::string>(), "set output directory for resulting files")
00087     ("wld", boost::program_options::value<std::string>(), "filename for the world file")
00088     ("rob", boost::program_options::value<std::string>(), "filename for the robot file -- ALTERNATIVE to parameter wld!")
00089     ("obj", boost::program_options::value<std::string>(), "filename for the object file -- ALTERNATIVE to parameter wld!")
00090     ("iter", boost::program_options::value<int>(), "Maximum number of iterations for the planning algorithm")
00091     ("obj-pos", boost::program_options::value<std::vector<float> >()->multitoken(), "Position of the object relative to the robot: Specify one x, y and z value.")
00092     ("save-separate", "if this flag is set, robot and object files will be saved separately in addition to the normal result.");
00093     return desc;
00094 }
00095 
00096 boost::program_options::variables_map loadParams(int argc, char ** argv)
00097 {
00098     boost::program_options::options_description optDesc = getOptions();
00099     boost::program_options::variables_map vm;
00100     boost::program_options::store(boost::program_options::command_line_parser(argc, argv).options(optDesc).style(
00101         boost::program_options::command_line_style::unix_style ^ boost::program_options::command_line_style::allow_short).run(), vm);
00102     boost::program_options::notify(vm);
00103     return vm;
00104 }
00105 
00106 bool loadParams(int argc, char ** argv, std::string& worldFilename, std::string& robotFilename,
00107                 std::string& objectFilename, std::string& outputDirectory, bool& saveSeparate, Eigen::Vector3d& objPos,
00108                 int& maxIterations)
00109 {
00110     saveSeparate = false;
00111     worldFilename.clear();
00112     robotFilename.clear();
00113     objectFilename.clear();
00114     outputDirectory.clear();
00115 
00116     boost::program_options::variables_map vm;
00117     try
00118     {
00119         vm = loadParams(argc, argv);
00120     }
00121     catch (std::exception const& e)
00122     {
00123         PRINTERROR("Exception caught: " << e.what());
00124         return false;
00125     }
00126     catch (...)
00127     {
00128         PRINTERROR("Exception caught");
00129         return false;
00130     }
00131 
00132     boost::program_options::options_description desc = getOptions();
00133     // desc=getOptions();
00134 
00135     if (vm.count("help"))
00136     {
00137         PRINTMSG(desc);
00138         return false;
00139     }
00140 
00141     if (vm.count("dir") < 1)
00142     {
00143         PRINTERROR("Must specify an output directory");
00144         PRINTMSG(desc);
00145         return false;
00146     }
00147 
00148     if (vm.count("wld") && (vm.count("rob") || vm.count("obj")))
00149     {
00150         PRINTERROR("Cannot specify a world and a robot and/or object at the same time.");
00151         PRINTMSG(desc);
00152         return false;
00153     }
00154 
00155     if (!vm.count("wld") && !vm.count("rob"))
00156     {
00157         PRINTERROR("Have to specify either a robot or a world.");
00158         PRINTMSG(desc);
00159         return false;
00160     }
00161 
00162     if (vm.count("rob") != vm.count("obj"))
00163     {
00164         PRINTERROR("If you specify a robot, you also have to specify an object, and vice versa.");
00165         PRINTMSG(desc);
00166         return false;
00167     }
00168 
00169 
00170     if (vm.count("rob") > 1)
00171     {
00172         PRINTERROR("You can only specify one robot at this stage.");
00173         PRINTMSG(desc);
00174         return false;
00175     }
00176 
00177     if (vm.count("obj") > 1)
00178     {
00179         PRINTERROR("You can only specify one object at this stage.");
00180         PRINTMSG(desc);
00181         return false;
00182     }
00183 
00184     if (vm.count("obj") != vm.count("rob"))
00185     {
00186         PRINTERROR("If you specify a robot, you should also specify an object.");
00187         PRINTMSG(desc);
00188         return false;
00189     }
00190 
00191     if (vm.count("wld"))
00192     {
00193         worldFilename = vm["wld"].as<std::string>();
00194         PRINTMSG("World file is " << worldFilename);
00195     }
00196     if (vm.count("rob"))
00197     {
00198         robotFilename = vm["rob"].as<std::string>();
00199         PRINTMSG("Robot file is " << robotFilename);
00200     }
00201     if (vm.count("obj"))
00202     {
00203         objectFilename = vm["obj"].as<std::string>();
00204         PRINTMSG("Object file is " << objectFilename);
00205     }
00206     if (vm.count("dir"))
00207     {
00208         outputDirectory = vm["dir"].as<std::string>();
00209         PRINTMSG("Output dir is " << outputDirectory);
00210     }
00211 
00212     if (vm.count("iter"))
00213     {
00214         maxIterations = vm["iter"].as<int>();
00215         PRINTMSG("Number of iterations: " << maxIterations);
00216         if (maxIterations < 35000)
00217         {
00218             PRINTWARN("Planning is not working well with max iterations < 35000");
00219         }
00220     }
00221 
00222 
00223     if (vm.count("obj-pos"))
00224     {
00225         std::vector<float> vals=vm["obj-pos"].as<std::vector<float> >();
00226         if (vals.size()!=3)
00227         {
00228             PRINTERROR("Must specify 3 values for --obj-pos: x, y and z (specified "<<vals.size()<<")");
00229             PRINTMSG(desc);
00230         }
00231         PRINTMSG("Using initial object pose "<<vals[0]<<", "<<vals[1]<<", "<<vals[2]);
00232         objPos=Eigen::Vector3d(vals[0],vals[1],vals[2]);
00233     }
00234 
00235     if (vm.count("save-separate"))
00236     {
00237         saveSeparate=true;
00238     }
00239 
00240     return true;
00241 }
00242 
00243 
00244 int main(int argc, char **argv)
00245 {
00246     signal(SIGSEGV, handler);
00247     signal(SIGABRT, handler);
00248 
00249     PRINT_INIT_STD();
00250 
00251     std::string worldFilename;
00252     std::string robotFilename;
00253     std::string objectFilename;
00254     std::string outputDirectory;
00255     bool saveSeparate;
00256     Eigen::Vector3d objPos;
00257     int maxPlanningSteps = 50000;
00258 
00259     if (!loadParams(argc, argv, worldFilename, robotFilename, objectFilename, outputDirectory, saveSeparate, objPos, maxPlanningSteps))
00260     {
00261         PRINTERROR("Could not read arguments");
00262         return 1;
00263     }
00264 
00265     PRINTMSG("Creating planner");
00266 
00267     std::string name = "EigenGraspPlanner1";  // TODO make parameter
00268     SHARED_PTR<GraspIt::GraspItSceneManager> graspitMgr(new GraspIt::GraspItSceneManagerHeadless());
00269 
00270 #ifdef USE_EIGENGRASP_NOQT
00271     SHARED_PTR<GraspIt::EigenGraspPlannerNoQt> p(new GraspIt::EigenGraspPlannerNoQt(name, graspitMgr));
00272 #else
00273     SHARED_PTR<GraspIt::EigenGraspPlanner> p(new GraspIt::EigenGraspPlanner(name, graspitMgr));
00274 #endif
00275 
00276     // TODO parameterize:
00277     // Names for robot and object if not loaded from a world file.
00278     // If loaded from a world file, will be overwritten.
00279     std::string useRobotName="Robot1";
00280     std::string useObjectName="Object1";
00281 
00282     if (!worldFilename.empty())
00283     {
00284         PRINTMSG("Loading world");
00285         graspitMgr->loadWorld(worldFilename);
00286         std::vector<std::string> robs = graspitMgr->getRobotNames();       
00287         std::vector<std::string> objs = graspitMgr->getObjectNames(true);
00288         if (robs.empty())
00289         {
00290             PRINTERROR("No robots loaded");
00291             return 1;
00292         }
00293         if (objs.empty())
00294         {
00295             PRINTERROR("No graspable objects loaded");
00296             return 1;
00297         }
00298         if (robs.size()!=1)
00299         {
00300             PRINTERROR("Exactly 1 robot should have been loaded");
00301             return 1;
00302         }
00303         if (objs.size()!=1)
00304         {
00305             PRINTERROR("Exactly 1 graspable object should have been loaded");
00306             return 1;
00307         }
00308         useRobotName=robs.front();
00309         useObjectName=objs.front();
00310         PRINTMSG("Using robot "<<useRobotName<<" and object "<<useObjectName);
00311     }
00312     else
00313     {
00314         // TODO add an option to set the transforms.
00315         // For now, they're put in the origin. For the planning, this should not really matter...
00316         GraspIt::EigenTransform robotTransform;
00317         GraspIt::EigenTransform objectTransform;
00318         robotTransform.setIdentity();
00319         objectTransform.setIdentity();
00320         objectTransform.translate(objPos);
00321         // objectTransform.translate(Eigen::Vector3d(100,0,0));
00322         std::string robotName(useRobotName); 
00323         std::string objectName(useObjectName);
00324         if ((graspitMgr->loadRobot(robotFilename, robotName, robotTransform) != 0) ||
00325                 (graspitMgr->loadObject(objectFilename, objectName, true, objectTransform)))
00326         {
00327             PRINTERROR("Could not load robot or object");
00328             return 1;
00329         }
00330     }
00331     
00332     
00333     bool createDir = true;
00334     bool saveIV = true;
00335     bool forceWrite = createDir;  // only enforce if creating dir is also allowed
00336     
00337     // in case one wants to view the initial world before planning, save it:
00338     graspitMgr->saveGraspItWorld(outputDirectory + "/startWorld.xml", createDir);
00339     graspitMgr->saveInventorWorld(outputDirectory + "/startWorld.iv", createDir);
00340 
00341     if (saveSeparate)
00342     {
00343         graspitMgr->saveRobotAsInventor(outputDirectory + "/robotStartpose.iv", useRobotName, createDir, forceWrite);
00344         graspitMgr->saveObjectAsInventor(outputDirectory + "/object.iv", useObjectName, createDir, forceWrite);
00345     }
00346 
00347     int repeatPlanning = 1;
00348     int keepMaxPlanningResults = 3;
00349     bool finishWithAutograsp = false;
00350     p->plan(maxPlanningSteps, repeatPlanning, keepMaxPlanningResults, finishWithAutograsp);
00351 
00352     PRINTMSG("Saving results as world files");
00353 
00354     bool saveWorld = true;
00355 
00356     std::string resultsWorldDirectory = outputDirectory;
00357     std::string filenamePrefix = "world";
00358     p->saveResultsAsWorldFiles(resultsWorldDirectory, filenamePrefix, saveWorld, saveIV, createDir, saveSeparate);
00359 
00360     std::vector<GraspIt::EigenGraspResult> allGrasps;
00361     p->getResults(allGrasps);
00362 
00363     PRINTMSG("Grasp results:");
00364     std::vector<GraspIt::EigenGraspResult>::iterator it;
00365     for (it = allGrasps.begin(); it != allGrasps.end(); ++it)
00366     {
00367         PRINTMSG(*it);
00368     }
00369 
00370     PRINTMSG("Quitting program.");
00371     return 1;
00372 }


grasp_planning_graspit
Author(s): Jennifer Buehler
autogenerated on Wed May 8 2019 02:53:36