grasp_planning_node.cpp
Go to the documentation of this file.
00001 #ifdef DOXYGEN_SHOULD_SKIP_THIS
00002 
00021 #endif  // DOXYGEN_SHOULD_SKIP_THIS
00022 
00023 #include <string>
00024 #include <set>
00025 #include <vector>
00026 
00027 #include <stdio.h>
00028 #include <execinfo.h>
00029 #include <signal.h>
00030 #include <stdlib.h>
00031 #include <unistd.h>
00032 
00033 #include <ros/ros.h>
00034 #include <grasp_planning_graspit/GraspItSceneManagerHeadless.h>
00035 #include <grasp_planning_graspit/LogBinding.h>
00036 #include <grasp_planning_graspit/EigenGraspPlanner.h>
00037 #include <grasp_planning_graspit/EigenGraspResult.h>
00038 #include <grasp_planning_graspit_ros/LogBindingROS.h>
00039 
00040 
00044 void print_trace(void)
00045 {
00046     void *array[10];
00047     size_t size;
00048     char **strings;
00049     size_t i;
00050 
00051     size = backtrace(array, 10);
00052     strings = backtrace_symbols(array, size);
00053 
00054     printf("Obtained %zd stack frames.\n", size);
00055 
00056     for (i = 0; i < size; i++)
00057         printf("%s\n", strings[i]);
00058 
00059     free(strings);
00060 }
00061 
00062 
00063 void handler(int sig)
00064 {
00065     print_trace();
00066     ros::shutdown();
00067     exit(1);
00068 }
00069 
00089 int main(int argc, char **argv)
00090 {
00091     signal(SIGSEGV, handler);
00092     signal(SIGABRT, handler);
00093 
00094     ros::init(argc, argv, "grasp_planning_graspit", ros::init_options::AnonymousName);
00095     ros::NodeHandle priv("~");
00096     ros::NodeHandle pub("");
00097 
00098 
00099     bool useRosLogging = true;
00100     if (useRosLogging)
00101     {
00102         PRINT_INIT_ROS();
00103     }
00104     else
00105     {
00106         PRINT_INIT_STD();
00107     }
00108 
00109 
00110     ROS_INFO_STREAM("Reading parameters from namespace " << priv.getNamespace());
00111 
00112     std::string outputDirectory;
00113     if (!priv.hasParam("results_output_directory"))
00114     {
00115         PRINTERROR("Could not read parameter results_output_directory");
00116         return 1;
00117     }
00118     priv.param<std::string>("results_output_directory", outputDirectory, outputDirectory);
00119     PRINTMSG("Results output directory: " << outputDirectory);
00120 
00121     bool useWorld = false;
00122     if (!priv.hasParam("use_world_file"))
00123     {
00124         PRINTERROR("Could not read parameter use_world_file");
00125         return 1;
00126     }
00127     priv.param<bool>("use_world_file", useWorld, useWorld);
00128 
00129     std::string worldFilename;
00130     std::string robotFilename;
00131     std::string objectFilename;
00132 
00133     if (useWorld)
00134     {
00135         PRINTMSG("Using world file");
00136         if (!priv.hasParam("graspit_world_file"))
00137         {
00138             PRINTERROR("Could not read parameter graspit_world_file");
00139             return 1;
00140         }
00141         priv.param<std::string>("graspit_world_file", worldFilename, worldFilename);
00142         PRINTMSG("Using world file " << worldFilename);
00143     }
00144     else
00145     {
00146         PRINTMSG("Using robot and object file");
00147         if (!priv.hasParam("graspit_robot_file"))
00148         {
00149             PRINTERROR("Could not read parameter graspit_robot_file");
00150             return 1;
00151         }
00152         priv.param<std::string>("graspit_robot_file", robotFilename, robotFilename);
00153         PRINTMSG("Using robot file " << robotFilename);
00154 
00155         if (!priv.hasParam("graspit_object_file"))
00156         {
00157             PRINTERROR("Could not read parameter graspit_object_file");
00158             return 1;
00159         }
00160         priv.param<std::string>("graspit_object_file", objectFilename, objectFilename);
00161     }
00162 
00163     int maxPlanningSteps;
00164     priv.param<int>("max_planning_steps", maxPlanningSteps, 50000);
00165     PRINTMSG("Using max number of planning steps: " << maxPlanningSteps);
00166 
00167     int repeatPlanning;
00168     priv.param<int>("num_repeat_planning", repeatPlanning, 1);
00169     PRINTMSG("Repeating planning: " << repeatPlanning);
00170 
00171 
00172     int numKeepResults;
00173     priv.param<int>("default_num_keep_results", numKeepResults, 5);
00174     PRINTMSG("Using default number of results kept: " << numKeepResults);
00175     
00176     bool finishWithAutograsp;
00177     priv.param<bool>("default_finish_with_autograsp", finishWithAutograsp, false);
00178     PRINTMSG("Finish with auto-grasp by default: " << finishWithAutograsp);
00179 
00180     bool saveResultFilesInventor;
00181     priv.param<bool>("save_result_files_inventor", saveResultFilesInventor, true);
00182     PRINTMSG("Save result files inventor: " << saveResultFilesInventor);
00183 
00184     bool saveResultFilesGraspit;
00185     priv.param<bool>("save_result_files_graspit", saveResultFilesGraspit, true);
00186     PRINTMSG("Save result files graspit: " << saveResultFilesGraspit);
00187 
00188     PRINTMSG("Creating planner");
00189     std::string name = "EigenGraspPlanner1";  // TODO make parameter
00190     SHARED_PTR<GraspIt::GraspItSceneManager> graspitMgr(new GraspIt::GraspItSceneManagerHeadless());
00191 
00192     SHARED_PTR<GraspIt::EigenGraspPlanner> p(new GraspIt::EigenGraspPlanner(name, graspitMgr));
00193 
00194     if (!worldFilename.empty())
00195     {
00196         PRINTMSG("Loading world");
00197         graspitMgr->loadWorld(worldFilename);
00198     }
00199     else
00200     {
00201         // TODO add an option to specify a transform for the object.
00202         // For now, they're put in the origin. For the planning, this should not really matter...
00203         GraspIt::EigenTransform robotTransform;
00204         GraspIt::EigenTransform objectTransform;
00205         robotTransform.setIdentity();
00206         objectTransform.setIdentity();
00207         // objectTransform.translate(Eigen::Vector3d(100,0,0));
00208         std::string robotName("Robot1");  // TODO parameterize
00209         std::string objectName("Object1");  // TODO parameterize
00210         if ((graspitMgr->loadRobot(robotFilename, robotName, robotTransform) != 0) ||
00211                 (graspitMgr->loadObject(objectFilename, objectName, true, objectTransform) != 0))
00212         {
00213             PRINTERROR("Could not load robot or object");
00214             return 0;
00215         }
00216 
00217         // in case one wants to view the initial world before planning, save it:
00218         graspitMgr->saveGraspItWorld(outputDirectory + "/worlds/startWorld.xml", true);
00219         graspitMgr->saveInventorWorld(outputDirectory + "/worlds/startWorld.iv", true);
00220     }
00221 
00222     PRINTMSG("Now planning...");
00223     if (!p->plan(maxPlanningSteps, repeatPlanning, numKeepResults, finishWithAutograsp))
00224     {
00225         PRINTERROR("Could not plan.");
00226         return 0;
00227     }
00228 
00229     PRINTMSG("Saving results as world files");
00230 
00231     std::string resultsWorldDirectory = outputDirectory + "/worlds";
00232     std::string filenamePrefix = "world";
00233     p->saveResultsAsWorldFiles(resultsWorldDirectory, filenamePrefix, true, true);
00234 
00235     std::vector<GraspIt::EigenGraspResult> allGrasps;
00236     p->getResults(allGrasps);
00237 
00238     PRINTMSG("Grasp results:");
00239     std::vector<GraspIt::EigenGraspResult>::iterator it;
00240     for (it = allGrasps.begin(); it != allGrasps.end(); ++it)
00241     {
00242         PRINTMSG(*it);
00243     }
00244 
00245 
00246     PRINTMSG("Quitting program.");
00247     return 0;
00248 }


grasp_planning_graspit_ros
Author(s): Jennifer Buehler
autogenerated on Wed May 8 2019 02:53:42