urdf2model.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 * 
00004 *  Copyright (c) 2008, Willow Garage, Inc.
00005 *  All rights reserved.
00006 * 
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 * 
00011 *   * Redistributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of the Willow Garage nor the names of its
00018 *     contributors may be used to endorse or promote products derived
00019 *     from this software without specific prior written permission.
00020 * 
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
00033 *********************************************************************/
00034 
00035 #include <cstdio>
00036 #include <cstdlib>
00037 
00038 #include <vector>
00039 #include <string>
00040 #include <sstream>
00041 
00042 #include "ros/ros.h"
00043 #include <boost/program_options.hpp>
00044 
00045 #include <gazebo/urdf2gazebo.h>
00046 
00047 void usage(const char *progname)
00048 {
00049     printf("\nUsage: %s [options]\n", progname);
00050     printf("  Example: %s --help\n",progname);
00051     printf("  Example: %s -f my_urdf_file.urdf -o gazebo_model.xml\n",progname);
00052 }
00053 
00054 namespace po = boost::program_options;
00055 
00056 int main(int argc, char **argv)
00057 {
00058   // print usage
00059   if (argc < 2)
00060   {
00061     usage(argv[0]);
00062     exit(1);
00063   }
00064 
00065   // Initial pose of model
00066   double initial_x = 0;
00067   double initial_y = 0;
00068   double initial_z = 0;
00069   double initial_rx = 0;
00070   double initial_ry = 0;
00071   double initial_rz = 0;
00072 
00073   // parse options
00074   po::options_description v_desc("Allowed options");
00075   v_desc.add_options()
00076     ("help,h" , "produce this help message")
00077     ("input-filename,f" , po::value<std::string>() , "read input model from file, not from the parameter server, exclusive with -p option.")
00078     ("output-filename,o" , po::value<std::string>() , "write converted gazebo model xml to a file instead, model is not spawned in simulation")
00079     ("model-name" , po::value<std::string>() , "Change name of Gazebo model")
00080     ("skip-joint-limits,s" , "do not enforce joint limits during urdf-->gazebo conversion.")
00081     ("init-x,x" , po::value<double>() , "set initial x position of model, defaults to 0.")
00082     ("init-y,y" , po::value<double>() , "set initial y position of model, defaults to 0.")
00083     ("init-z,z" , po::value<double>() , "set initial z position of model, defaults to 0.")
00084     ("init-roll,R" , po::value<double>() , "set initial roll (rx) of model in radians, defaults to 0. Application orders are r-p-y.")
00085     ("init-pitch,P" , po::value<double>() , "set initial pitch (ry) of model in radians, defaults to 0. Application orders are r-p-y.")
00086     ("init-yaw,Y" , po::value<double>() , "set initial yaw (rz) of model in radians, defaults to 0. Application orders are r-p-y.");
00087 
00088   
00089   po::options_description desc("Allowed options");
00090   desc.add(v_desc);
00091   
00092   po::positional_options_description p_desc;
00093   
00094   po::variables_map vm;
00095   po::store(po::command_line_parser(argc, argv).options(desc).positional(p_desc).run(), vm);
00096   po::notify(vm);
00097   
00098   if (vm.count("help"))
00099   {
00100     usage(argv[0]);
00101     std::cout << v_desc << std::endl;
00102     exit(1);
00103   }
00104   
00105   // set flag to enforce joint limits, this is hardcoded to true because we do want a model with
00106   // joint limits enforced.
00107   bool enforce_limits = true;
00108   if (vm.count("skip-joint-limits"))
00109     enforce_limits = false;
00110   
00111   std::string file_in;
00112   if (vm.count("input-filename"))
00113     file_in = vm["input-filename"].as<std::string>();
00114   else
00115     file_in.clear();
00116   
00117   std::string file_out;
00118   if (vm.count("output-filename"))
00119   {
00120     file_out = vm["output-filename"].as<std::string>();
00121   }  
00122 
00123   std::string model_name;
00124   if (vm.count("model-name"))
00125   {
00126     model_name = vm["model-namename"].as<std::string>();
00127   }  
00128 
00129   // Initial pose of model 
00130   if (vm.count("init-x"))
00131   {
00132     initial_x = vm["init-x"].as<double>();
00133     ROS_DEBUG("x: %f",initial_x);
00134   }
00135   if (vm.count("init-y"))
00136   {
00137     initial_y = vm["init-y"].as<double>();
00138     ROS_DEBUG("y: %f",initial_y);
00139   }
00140   if (vm.count("init-z"))
00141   {
00142     initial_z = vm["init-z"].as<double>();
00143     ROS_DEBUG("z: %f",initial_z);
00144   }
00145   if (vm.count("init-roll"))
00146   {
00147     initial_rx = vm["init-roll"].as<double>();
00148     ROS_DEBUG("init-roll: %f",initial_rx);
00149   }
00150   if (vm.count("init-pitch"))
00151   {
00152     initial_ry = vm["init-pitch"].as<double>();
00153     ROS_DEBUG("init-pitch: %f",initial_ry);
00154   }
00155   if (vm.count("init-yaw"))
00156   {
00157     initial_rz = vm["init-yaw"].as<double>();
00158     ROS_DEBUG("init-yaw: %f",initial_rz);
00159   }
00160 
00161   // setup initial pose vectors
00162   urdf::Vector3 initial_xyz(initial_x,           initial_y,           initial_z);
00163   urdf::Vector3 initial_rpy(initial_rx*180/M_PI, initial_ry*180/M_PI, initial_rz*180/M_PI);
00164       
00165   if (file_out.empty())
00166   {
00167     ROS_ERROR("Must specify output filename for gazebo model");
00168     exit(2);
00169   }
00170 
00171   // get input xml
00172   std::string xml_string;
00173   if (file_in.empty())
00174   {
00175     ROS_ERROR("Unable to load robot model from filename");
00176     exit(2);
00177   }
00178 
00179   // read urdf / gazebo model xml from file
00180   TiXmlDocument xml_in(file_in);
00181   xml_in.LoadFile();
00182   // copy tixml to a string
00183   std::ostringstream stream;
00184   stream << xml_in;
00185   xml_string = stream.str();
00186   
00187   // convert xml string to tinyxml object
00188   TiXmlDocument xml_doc;
00189   xml_doc.Parse(xml_string.c_str());
00190 
00191   // resulting gazebo model XML will be stored here
00192   TiXmlDocument gazebo_model_xml;
00193   
00194   // check root xml element to determine if conversion is necessary
00195   if (!xml_doc.FirstChild("robot"))
00196   {
00197     ROS_ERROR("Unable to determine input xml type: input urdf xml must begin with <robot>");
00198     exit(2);
00199   }
00200   
00201   // do convertion / manipulate xml
00202   // convert urdf to gazebo model
00203   urdf2gazebo::URDF2Gazebo u2g;
00204   u2g.convert(xml_doc, gazebo_model_xml, enforce_limits, initial_xyz, initial_rpy, model_name);
00205   
00206   // output filename is specified, save converted Gazebo Model XML to a file
00207   if (!gazebo_model_xml.SaveFile(file_out))
00208   {
00209     printf("Unable to save gazebo model in %s\n", file_out.c_str());  
00210     exit(3);
00211   }
00212   
00213   return 0;
00214 }
00215 


gazebo
Author(s): Nate Koenig, Andrew Howard, with contributions from many others. See web page for a full credits llist.
autogenerated on Sun Jan 5 2014 11:34:52