Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <ros/ros.h>
00019 #include <urdf/model.h>
00020 #include <fstream>
00021
00022 using namespace std;
00023
00024
00025 std::string exec(const char* cmd) {
00026 FILE* pipe = popen(cmd, "r");
00027 if (!pipe) return "ERROR";
00028 char buffer[128];
00029 std::string result = "";
00030 while(!feof(pipe)) {
00031 if(fgets(buffer, 128, pipe) != NULL)
00032 result += buffer;
00033 }
00034 pclose(pipe);
00035 return result;
00036 }
00037
00038 struct body_data{
00039 urdf::Pose pose;
00040 string name;
00041 string parent;
00042 };
00043 struct joint_data{
00044 string name;
00045 string parent;
00046 string child;
00047 float lower, upper;
00048 urdf::Vector3 axis;
00049 };
00050 int main(int argc, char **argv){
00051
00052 srand(time(NULL));
00053 ros::init(argc, argv, "urdf_openrave",ros::init_options::AnonymousName);
00054
00055 boost::shared_ptr<urdf::Model> urdf_(new urdf::Model);
00056
00057
00058
00059 string file_urdf, file_out;
00060
00061 list<body_data> bodies;
00062 list<joint_data> joints;
00063
00064 map<string,pair<string,string> > arms;
00065
00066 { int i = 1;
00067 while(i < argc){
00068 if(strcmp(argv[i],"--urdf") == 0){
00069 file_urdf = argv[++i];
00070 }else if(strcmp(argv[i],"--output") == 0){
00071 file_out = argv[++i];
00072 }else{
00073 arms.insert(make_pair(string(argv[i]),make_pair(string(argv[i+1]),string(argv[i+2]))));
00074 i+=2;
00075 }
00076 ++i;
00077 }
00078 if(i > argc){
00079 ROS_ERROR("planning_description_generator --urdf PATH [--output PATH] ARM_NAME ARM_ROOT ARM_TIP [...]");
00080 return -1;
00081 }
00082 }
00083
00084 if(arms.empty()){
00085 ROS_ERROR("No kinematic chains specified!");
00086 return -1;
00087 }
00088
00089
00090
00091 string xml;
00092
00093 if(file_urdf.empty()){
00094 stringstream sstream;
00095 string line;
00096 while( getline(cin, line)) {
00097 sstream << line << endl;
00098 }
00099 xml = sstream.str();
00100 } else xml = exec((string("rosrun xacro xacro --inorder ")+file_urdf).c_str());
00101
00102 if(!urdf_->initString(xml))
00103 {
00104 ROS_ERROR_STREAM("Parsing URDF failed!");
00105 return -1;
00106 }
00107
00108 for(map<string,pair<string,string> >::iterator it=arms.begin(); it != arms.end(); ++it){
00109 string link_name = it->second.second;
00110
00111 while( link_name != it->second.first ){
00112 boost::shared_ptr<urdf::Joint > joint = urdf_->getLink(link_name)->parent_joint;
00113 body_data bd;
00114 bd.name = link_name;
00115 bd.parent = joint->parent_link_name;
00116 bd.pose = joint->parent_to_joint_origin_transform;
00117 bodies.push_front(bd);
00118
00119 joint_data jd;
00120 jd.name = joint->name;
00121 jd.child = link_name;
00122 jd.parent = joint->parent_link_name;
00123 jd.axis = joint->axis;
00124 if(joint->limits){
00125 jd.lower = joint->limits->lower;
00126 jd.upper = joint->limits->upper;
00127 }else{
00128 jd.lower = jd.upper = 0;
00129 }
00130 joints.push_front(jd);
00131
00132 link_name = joint->parent_link_name;
00133 }
00134
00135 }
00136
00137
00138 stringstream sout;
00139 sout << "<Robot name=\"" << urdf_->getName() << "\">" << endl;
00140 sout << "\t" << "<KinBody>" << endl;
00141
00142
00143
00144
00145
00146
00147
00148 for(map<string,pair<string,string> >::iterator it=arms.begin(); it != arms.end(); ++it){
00149 sout << "\t" << "\t" << "<Body type=\"dynamic\" name=\"" << it->second.first << "\"/>" << endl;
00150 }
00151 for(list<body_data>::iterator it=bodies.begin(); it != bodies.end(); ++it){
00152 sout << "\t" << "\t" << "<Body type=\"dynamic\" name=\"" << it->name << "\">" << endl;
00153 sout << "\t" << "\t" << "\t" << "<offsetfrom>" << it->parent << "</offsetfrom>" << endl;
00154 sout << "\t" << "\t" << "\t" << "<Translation>" << it->pose.position.x << " "<< it->pose.position.y << " " << it->pose.position.z << "</Translation>" << endl;
00155 sout << "\t" << "\t" << "\t" << "<quat>" << it->pose.rotation.w << " " << it->pose.rotation.x << " "<< it->pose.rotation.y << " " << it->pose.rotation.z << "</quat>" << endl;
00156 sout << "\t" << "\t" << "</Body>" << endl;
00157 }
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167 for(list<joint_data>::iterator it=joints.begin(); it != joints.end(); ++it){
00168 if( it->lower == 0 && it->upper == 0){
00169 sout << "\t" << "\t" << "<Joint type=\"hinge\" enable=\"false\" name=\"" << it->name << "\">" << endl;
00170 }else{
00171 sout << "\t" << "\t" << "<Joint type=\"hinge\" name=\"" << it->name << "\">" << endl;
00172 sout << "\t" << "\t" << "\t" << "<Axis>" << it->axis.x << " "<< it->axis.y << " " << it->axis.z << "</Axis>" << endl;
00173 }
00174 sout << "\t" << "\t" << "\t" << "<limitsrad>" << it->lower << " "<< it->upper << "</limitsrad>" << endl;
00175 sout << "\t" << "\t" "\t" << "<offsetfrom>" << it->child << "</offsetfrom>" << endl;
00176 sout << "\t" << "\t" << "\t" << "<Body>" << it->parent << "</Body>" << endl;
00177 sout << "\t" << "\t" << "\t" << "<Body>" << it->child << "</Body>" << endl;
00178 sout << "\t" << "\t" << "</Joint>" << endl;
00179 }
00180 sout << "\t" << "</KinBody>" << endl;
00181 for(map<string,pair<string,string> >::iterator it=arms.begin(); it != arms.end(); ++it){
00182 sout << "\t" << "<Manipulator name=\"" << it->first <<"\">" << endl;
00183 sout << "\t" << "\t" << "<base>" << it->second.first << "</base>" << endl;
00184 sout << "\t" << "\t" << "<effector>" << it->second.second << "</effector>" << endl;
00185 sout << "\t" << "</Manipulator>" << endl;
00186 }
00187
00188 sout << "</Robot>" << endl;
00189
00190 if(file_out.empty())
00191 cout << sout.str();
00192 else{
00193 ofstream of(file_out.c_str());
00194 of << sout.str();
00195 }
00196
00197 return 0;
00198 }
00199