contacts_generator_node.cpp
Go to the documentation of this file.
00001 
00019 #include <ros/ros.h>
00020 
00021 #include <urdf2inventor/Helpers.h>
00022 #include <urdf2graspit/Urdf2Graspit.h>
00023 #include <urdf2graspit/ContactsGenerator.h>
00024 #include <urdf2graspit/FileIO.h>
00025 #include <string>
00026 #include <vector>
00027 
00028 int main(int argc, char** argv)
00029 {
00030     ros::init(argc, argv, "urdf2graspit", ros::init_options::AnonymousName);
00031     ros::NodeHandle priv("~");
00032     ros::NodeHandle pub("");
00033 
00034     if (argc < 6)
00035     {
00036         ROS_ERROR("Not enough arguments!");
00037         ROS_INFO_STREAM("Usage: " << argv[0] <<
00038                         " <urdf-file> <output-directory> <palm link name>" <<
00039                         " <finger joint name 0> ... <finger joint name n>");
00040         return 0;
00041     }
00042 
00043     // set parameters
00044 
00045     std::string urdf_filename = std::string(argv[1]);
00046     ROS_INFO("URDF file: %s", urdf_filename.c_str());
00047 
00048     std::string outputDir = std::string(argv[2]);
00049     ROS_INFO("Output dir: %s", outputDir.c_str());
00050 
00051     std::string palmLinkName = std::string(argv[3]);
00052     ROS_INFO("Hand root: %s", palmLinkName.c_str());
00053 
00054     std::vector<std::string> roots;
00055     for (unsigned int i = 4; i < argc; ++i)
00056     {
00057         std::string fingerJoint = std::string(argv[i]);
00058         ROS_INFO("Joint: %s", fingerJoint.c_str());
00059         roots.push_back(fingerJoint);
00060     }
00061 
00062     std::string outputMaterial = "plastic";
00063     double scaleFactor = 1000;
00064     priv.param<double>("scale_factor", scaleFactor, scaleFactor);
00065     ROS_INFO("scale_factor: <%f>", scaleFactor);
00066     
00067     bool negateJointMoves=false;
00068     priv.param<bool>("negate_joint_movement", negateJointMoves, negateJointMoves);
00069     ROS_INFO("negate_joint_movement: <%d>", negateJointMoves);
00070 
00071     // if this is true, the DH axes are displayed. Otherwise, the URDF axes are displayed.
00072     bool displayDHAxes=false;
00073     priv.param<bool>("display_dh_axes", displayDHAxes, displayDHAxes);
00074     ROS_INFO("negate_joint_movement: <%d>", displayDHAxes);
00075 
00076     // An axis and angle (degrees) can be specified which will transform *all*
00077     // visuals (not links, but their visuals!) within their local coordinate system.
00078     // This can be used to correct transformation errors which may have been 
00079     // introduced in converting meshes from one format to the other, losing orientation information
00080     // For example, .dae has an "up vector" definition which may have been ignored.
00081     float visCorrAxX=0;
00082     priv.param<float>("visual_corr_axis_x", visCorrAxX, visCorrAxX);
00083     float visCorrAxY=0;
00084     priv.param<float>("visual_corr_axis_y", visCorrAxY, visCorrAxY);
00085     float visCorrAxZ=0;
00086     priv.param<float>("visual_corr_axis_z", visCorrAxZ, visCorrAxZ);
00087     float visCorrAxAngle=0;
00088     priv.param<float>("visual_corr_axis_angle", visCorrAxAngle, visCorrAxAngle);
00089     urdf2graspit::Urdf2GraspIt::EigenTransform addVisualTrans(Eigen::AngleAxisd(visCorrAxAngle*M_PI/180, Eigen::Vector3d(visCorrAxX,visCorrAxY,visCorrAxZ)));
00090 
00091     std::string useFilename;
00092     priv.param<std::string>("filename", useFilename, useFilename);
00093 
00094     ROS_INFO("### Getting DH parameters...");
00095     
00096     urdf2inventor::Urdf2Inventor::UrdfTraverserPtr traverser_conv(new urdf_traverser::UrdfTraverser());
00097     urdf2graspit::Urdf2GraspIt converter(traverser_conv, scaleFactor, negateJointMoves);
00098     
00099     if (!converter.loadModelFromFile(urdf_filename))
00100     {
00101         ROS_ERROR("Could not load the model into the contacts generator");
00102         return 0;
00103     }
00104 
00105     if (!converter.prepareModelForDenavitHartenberg(palmLinkName))
00106     {
00107         ROS_ERROR("Could not prepare model for DH parameters");
00108         return 0;
00109     }
00110     std::vector<urdf2graspit::DHParam> dh_parameters;
00111     if (!converter.getDHParams(dh_parameters, palmLinkName))
00112     {
00113         ROS_ERROR("Could not retrieve DH parameters from model");
00114         return 0;
00115     }
00116 
00117     ROS_INFO("### Generating contacts ");
00118     urdf2inventor::Urdf2Inventor::UrdfTraverserPtr traverser_cont(new urdf_traverser::UrdfTraverser());
00119     urdf2graspit::ContactsGenerator contGen(traverser_cont, scaleFactor);
00120     if (!contGen.loadModelFromFile(urdf_filename))
00121     {
00122         ROS_ERROR("Could not load the model into the contacts generator");
00123         return 0;
00124     }
00125     float coefficient = 0.2;
00126     bool addAxes = true;
00127     float axRad=0.0015;
00128     float axLen=0.015;
00129     bool facesCCW=true;
00130     if (!contGen.generateContactsWithViewer(roots,
00131         palmLinkName, coefficient, dh_parameters, addAxes,
00132         displayDHAxes, axRad, axLen, addVisualTrans, facesCCW))
00133     {
00134         ROS_ERROR("Could not generate contacts");
00135         return 0;
00136     }
00137 
00138     std::string contacts = contGen.getContactsFileContent(traverser_cont->getModelName());
00139     ROS_INFO_STREAM("Contacts generated."); // <<": "<<contacts);
00140 
00141     urdf2graspit::FileIO fileIO(outputDir, contGen.getOutStructure());
00142     if (!fileIO.initOutputDir(traverser_cont->getModelName()))
00143     {
00144         ROS_ERROR_STREAM("Could not initialize output directory "
00145             <<outputDir<<" for robot "<<traverser_cont->getModelName());
00146         return 0;
00147     }
00148     if (!fileIO.writeContacts(traverser_cont->getModelName(), contacts, useFilename))
00149     {
00150         ROS_ERROR("Could not write files");
00151         return 0;
00152     }
00153     
00154     ROS_INFO("Cleaning up...");
00155     converter.cleanup();
00156     contGen.cleanup();
00157 
00158     ROS_INFO("Done.");
00159     return 0;
00160 }


urdf2graspit
Author(s): Jennifer Buehler
autogenerated on Wed May 8 2019 02:53:45