grasp_planning_add_model_client.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 <vector>
00025 
00026 #include <stdio.h>
00027 #include <execinfo.h>
00028 #include <signal.h>
00029 #include <stdlib.h>
00030 #include <unistd.h>
00031 
00032 #include <ros/ros.h>
00033 
00034 #include <grasp_planning_graspit_msgs/AddToDatabase.h>
00035 
00036 #include <grasp_planning_graspit_ros/LogBindingROS.h>
00037 
00038 
00042 void print_trace(void)
00043 {
00044     void *array[10];
00045     size_t size;
00046     char **strings;
00047     size_t i;
00048 
00049     size = backtrace(array, 10);
00050     strings = backtrace_symbols(array, size);
00051 
00052     printf("Obtained %zd stack frames.\n", size);
00053 
00054     for (i = 0; i < size; i++)
00055         printf("%s\n", strings[i]);
00056 
00057     free(strings);
00058 }
00059 
00060 
00061 void handler(int sig)
00062 {
00063     print_trace();
00064     ros::shutdown();
00065     exit(1);
00066 }
00067 
00068 void printHelp(const char * progName)
00069 {
00070     PRINTMSG("Usage: " << progName << " <model name> <model file> <as robot: 'true' or 'false'> [<for objects: as graspable, 'true' or 'false'>]");
00071 }
00072 
00073 int run(int argc, char **argv)
00074 {
00075     if (argc < 4)
00076     {
00077         printHelp(argv[0]);
00078         return 0;
00079     }
00080 
00081     const std::string falseStr("false");
00082     const std::string trueStr("true");
00083 
00084     const std::string modelName(argv[1]);
00085     const std::string filename(argv[2]);
00086     const std::string asRobotArg(argv[3]);
00087 
00088     PRINTMSG("Adding model name " << modelName << " in file " << filename << ". As robot: " << asRobotArg);
00089 
00090     bool asRobotOpt = false;
00091     if (asRobotArg == trueStr)
00092     {
00093         asRobotOpt = true;
00094     }
00095     else if (asRobotArg != falseStr)
00096     {
00097         PRINTERROR("Must specify whether to load as robot with " << trueStr << " or " << falseStr);
00098         printHelp(argv[0]);
00099         return 1;
00100     }
00101 
00102 
00103     int jnIdx = 4;  // index in argv where joint names start
00104     bool asGraspableOpt = false;
00105     if (!asRobotOpt)
00106     {
00107         if (argc < 5)
00108         {
00109             PRINTERROR("If loading an object, you must specify whether it is to be loaded as graspable");
00110             printHelp(argv[0]);
00111             return 1;
00112         }
00113         const std::string asGraspableArg(argv[4]);
00114         jnIdx = 5;
00115         if (asGraspableArg == trueStr)
00116         {
00117             PRINTMSG("Loading object as graspable");
00118             asGraspableOpt = true;
00119         }
00120         else if (asGraspableArg != falseStr)
00121         {
00122             PRINTERROR("Must specify whether to load as graspable object with " << trueStr << " or " << falseStr);
00123             printHelp(argv[0]);
00124             return 1;
00125         }
00126     }
00127 
00128     if (asRobotOpt && (jnIdx >= argc))
00129     {
00130         PRINTERROR("Must specify at least one joint name for a robot!");
00131         printHelp(argv[0]);
00132         return 1;
00133     }
00134 
00135     std::vector<std::string> jointNames;
00136     for (int i = jnIdx; i < argc; ++i)
00137     {
00138         jointNames.push_back(std::string(argv[i]));
00139     }
00140 
00141 
00142     // TODO parameterize this
00143     std::string addToDBTopic = "graspit_add_to_database";
00144 
00145     ros::NodeHandle n;
00146     ros::ServiceClient client = n.serviceClient<grasp_planning_graspit_msgs::AddToDatabase>(addToDBTopic);
00147 
00148     grasp_planning_graspit_msgs::AddToDatabase srv;
00149     srv.request.filename = filename;
00150     srv.request.isRobot = (asRobotOpt == 1);
00151     srv.request.asGraspable = (asGraspableOpt == 1);
00152     srv.request.modelName = modelName;
00153     srv.request.jointNames = jointNames;
00154 
00155     if (!client.call(srv))
00156     {
00157         PRINTERROR("Failed to call service");
00158         return 1;
00159     }
00160 
00161     if (srv.response.returnCode != grasp_planning_graspit_msgs::AddToDatabase::Response::SUCCESS)
00162     {
00163         PRINTERROR("Could not add the robot to the database. Return code " << srv.response.returnCode);
00164         return 1;
00165     }
00166 
00167     PRINTMSG("Successfully added model to database, got model ID=" << srv.response.modelID);
00168     return 0;
00169 }
00170 
00171 
00172 int main(int argc, char **argv)
00173 {
00174     signal(SIGSEGV, handler);
00175     signal(SIGABRT, handler);
00176 
00177     ros::init(argc, argv, "database_test", ros::init_options::AnonymousName);
00178 
00179     bool useRosLogging = true;
00180     if (useRosLogging)
00181     {
00182         PRINT_INIT_ROS();
00183     }
00184     else
00185     {
00186         PRINT_INIT_STD();
00187     }
00188 
00189     int ret = run(argc, argv);
00190     // PRINTMSG("Result of run(): "<<ret);
00191     return ret;
00192 }


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