collada_gazebo_gen.cpp
Go to the documentation of this file.
00001 #include <collada_parser/collada_parser.h>
00002 #include <urdf_parser/urdf_parser.h>
00003 #include <urdf/model.h>
00004 
00005 //
00006 #include <iostream>
00007 #include <fstream>
00008 
00009 #undef GAZEBO_1_0
00010 #undef GAZEBO_1_3
00011 //#define GAZEBO_1_0
00012 #define GAZEBO_1_3
00013 
00014 using namespace urdf;
00015 using namespace std;
00016 
00017 int main(int argc, char **argv) {
00018   if (argc != 5){
00019     std::cerr << "Usage: collada_gazebo_gen package_name dir input dae" << std::endl;
00020     std::cerr << "collada file: $(rospack find package_name)/dir/input.dae is needed." << std::endl;
00021     return -1;
00022   }
00023   bool colladafile = false;
00024   std::string package_name(argv[1]);
00025   std::string dir_name(argv[2]);
00026   std::string dae_fname(argv[3]);
00027   std::string prefix(argv[4]);
00028 
00029   ROS_INFO("%s %s %s -> %s",package_name.c_str(),dir_name.c_str(),dae_fname.c_str(), (dir_name + "/" + dae_fname + "." + prefix).c_str());
00030   // get the entire file
00031   std::string xml_string;
00032   std::fstream xml_file((dir_name + "/" + dae_fname + "." + prefix).c_str(), std::fstream::in);
00033   while ( xml_file.good() )
00034   {
00035       std::string line;
00036       std::getline( xml_file, line);
00037       xml_string += (line + "\n");
00038   }
00039   xml_file.close();
00040 
00041   boost::shared_ptr<ModelInterface> robot;
00042   if( xml_string.find("<COLLADA") != std::string::npos )
00043   {
00044     ROS_INFO("Parsing robot collada xml string");
00045     robot = parseCollada(xml_string);
00046     colladafile = true;
00047   }
00048   else
00049   {
00050     ROS_INFO("Parsing robot urdf xml string");
00051     robot = parseURDF(xml_string);
00052   }
00053 
00054   if (!robot){
00055     std::cerr << "ERROR: Model Parsing the xml failed" << std::endl;
00056     return -1;
00057   }
00058 
00059   {
00060     int cnt = 0;
00061     std::ofstream os;
00062     std::string fname = dir_name + "/" + dae_fname + "_transmission.xacro";
00063     os.open(fname.c_str());
00064     if (colladafile) {
00065       os << "<COLLADA xmlns:xacro=\"http://www.ros.org/wiki/xacro\">\n";
00066     } else {
00067       os << "<robot xmlns:xacro=\"http://www.ros.org/wiki/xacro\">\n";
00068     }
00069     os << "  <xacro:property name=\"mechanical_reduction\" value=\"1\" />\n";
00070     os << "  <xacro:property name=\"motor_torque_constant\" value=\"1\" />\n";
00071     os << "  <xacro:property name=\"pulses_per_revolution\" value=\"90000\" />\n\n";
00072     os << "  <xacro:macro name=\"pr2_transmission\" params=\"joint_name link_name\">\n";
00073     os << "    <transmission type=\"pr2_mechanism_model/SimpleTransmission\" name=\"${joint_name}_trans\">\n";
00074     os << "      <actuator name=\"${joint_name}_motor\" />\n";
00075     os << "      <joint name=\"${joint_name}\" />\n";
00076     os << "      <mechanicalReduction>${mechanical_reduction}</mechanicalReduction>\n";
00077     os << "      <motorTorqueConstant>${motor_torque_constant}</motorTorqueConstant>\n";
00078     os << "      <pulsesPerRevolution>${pulses_per_revolution}</pulsesPerRevolution>\n";
00079     os << "    </transmission>\n";
00080     os << "  </xacro:macro>\n";
00081     for ( std::map<std::string,boost::shared_ptr<Joint> >::iterator joint = robot->joints_.begin();
00082           joint != robot->joints_.end(); joint++) {
00083       if ( joint->second->type == Joint::REVOLUTE ||
00084            joint->second->type == Joint::CONTINUOUS ) {
00085         os << "  <xacro:pr2_transmission joint_name=\"" << joint->first;
00086         os << "\" link_name=\"link" << cnt++ << "\"/>\n";
00087       }
00088     }
00089     if (colladafile) {
00090       os << "</COLLADA>";
00091     } else {
00092       os << "</robot>";
00093     }
00094     os.close();
00095   }
00096 
00097   {
00098     std::ofstream os;
00099     std::string fname = dir_name + "/" + dae_fname + "_gazebo.xacro";
00100     os.open(fname.c_str());
00101     if (colladafile) {
00102       os << "<COLLADA xmlns:controller=\"http://playerstage.sourceforge.net/gazebo/xmlschema/#controller\"\n";
00103     } else {
00104       os << "<robot xmlns:controller=\"http://playerstage.sourceforge.net/gazebo/xmlschema/#controller\"\n";
00105     }
00106     os << "         xmlns:xacro=\"http://www.ros.org/wiki/xacro\">\n";
00107 #ifdef GAZEBO_1_0
00108     os << "  <xacro:macro name=\"gazebo_reference\" params=\"reference material gravity\">\n";
00109     os << "    <gazebo reference=\"${reference}\">\n";
00110     os << "      <material>${material}</material>\n";
00111     os << "      <turnGravityOff>${gravity}</turnGravityOff>\n";
00112     os << "    </gazebo>\n";
00113     os << "  </xacro:macro>\n";
00114 #endif
00115 
00116     if (colladafile) {
00117       os << "  <extra type=\"physics\" name=\"gazebo\">\n";
00118       os << "    <technique profile=\"gazebo\">\n";
00119     }
00120 #ifdef GAZEBO_1_0
00121     // old gazebo (gazebo on ROS Fuerte)
00122     os << "      <gazebo>\n";
00123     os << "        <controller:gazebo_ros_controller_manager\n";
00124     os << "           name=\"gazebo_ros_controller_manager\"\n";
00125     os << "           plugin=\"libgazebo_ros_controller_manager.so\">\n";
00126     os << "          <alwaysOn>true</alwaysOn>\n";
00127     os << "          <updateRate>1000.0</updateRate>\n";
00128     os << "        </controller:gazebo_ros_controller_manager>\n";
00129     os << "      </gazebo>\n";
00130 #endif
00131 #ifdef GAZEBO_1_3
00132     os << "  <gazebo>" << endl;
00133     os << "    <!--plugin filename=\"libMultiSenseSLPlugin.so\" name=\"multisense_plugin\" /-->" << endl;
00134     os << "    <!--plugin filename=\"libAtlasPlugin.so\" name=\"atlas_plugin\"/-->" << endl;
00135     os << "    <plugin filename=\"libgazebo_ros_joint_trajectory.so\" name=\"joint_trajectory_plugin\">" << endl;
00136     os << "      <topicName>joint_trajectory</topicName>" << endl;
00137     os << "      <updateRate>1000.0</updateRate>" << endl;
00138     os << "    </plugin>" << endl;
00139     os << "    <plugin filename=\"libgazebo_ros_controller_manager.so\" name=\"gazebo_ros_controller_manager\">" << endl;
00140     os << "      <alwaysOn>true</alwaysOn>" << endl;
00141     os << "      <updateRate>1000.0</updateRate>" << endl;
00142     os << "    </plugin>" << endl;
00143     os << "  </gazebo>" << endl;
00144 #endif
00145 
00146 #ifdef GAZEBO_1_0
00147     for ( std::map<std::string,boost::shared_ptr<Link> >::iterator link = robot->links_.begin();
00148           link != robot->links_.end(); link++) {
00149       os << "      <xacro:gazebo_reference reference=\"" << link->first;
00150       os << "\" material=\"Gazebo/Blue\" gravity=\"False\" />\n";
00151     }
00152 #endif
00153     if (colladafile) {
00154       os << "    </technique>\n";
00155       os << "  </extra>\n";
00156     }
00157 
00158     os << "  <xacro:include filename=\"$(find " << package_name;
00159     os << ")/" << dir_name << "/" << dae_fname << "_transmission.xacro\" />\n";
00160     if (colladafile) {
00161       os << "</COLLADA>\n";
00162     } else {
00163       os << "</robot>\n";
00164     }
00165     os.close();
00166   }
00167 
00168   {
00169     std::ofstream os;
00170     std::string fname = dir_name + "/" + dae_fname + "_controllers.yaml";
00171     os.open(fname.c_str());
00172     os << "fullbody_controller:\n";
00173     os << "  type: robot_mechanism_controllers/JointTrajectoryActionController\n";
00174     os << "  joints:\n";
00175     for ( std::map<std::string,boost::shared_ptr<Joint> >::iterator joint = robot->joints_.begin();
00176           joint != robot->joints_.end(); joint++) {
00177       if ( joint->second->type == Joint::REVOLUTE ||
00178            joint->second->type == Joint::CONTINUOUS ) {
00179         os << "    - " << joint->first << "\n";
00180       }
00181     }
00182     os << "  gains:\n";
00183     for ( std::map<std::string,boost::shared_ptr<Joint> >::iterator joint = robot->joints_.begin();
00184           joint != robot->joints_.end(); joint++) {
00185       if ( joint->second->type == Joint::REVOLUTE ||
00186            joint->second->type == Joint::CONTINUOUS ) {
00187         os << "    " << joint->first << ": {p: 200.0, d: 0.0002, i: 0.0, i_clamp: 0.0}\n"; // ????
00188       }
00189     }
00190     os << "  joint_trajectory_action_node:\n";
00191     os << "    joints:\n";
00192     for ( std::map<std::string,boost::shared_ptr<Joint> >::iterator joint = robot->joints_.begin();
00193           joint != robot->joints_.end(); joint++) {
00194       if ( joint->second->type == Joint::REVOLUTE ||
00195            joint->second->type == Joint::CONTINUOUS ) {
00196         os << "      - " << joint->first << "\n";
00197       }
00198     }
00199     os << "    constraints:\n";
00200     os << "      goal_time: 0.0\n";
00201     for ( std::map<std::string,boost::shared_ptr<Joint> >::iterator joint = robot->joints_.begin();
00202           joint != robot->joints_.end(); joint++) {
00203       if ( joint->second->type == Joint::REVOLUTE ||
00204            joint->second->type == Joint::CONTINUOUS ) {
00205         os << "      " << joint->first << ":\n";
00206         os << "        goal: 0.02\n";
00207       }
00208     }
00209     os.close();
00210   }
00211 
00212   return 0;
00213 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends Defines


collada_tools
Author(s): Yohei Kakiuchi (youhei@jsk.t.u-tokyo.ac.jp)
autogenerated on Sat Mar 23 2013 19:48:48