00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
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
00059 if (argc < 2)
00060 {
00061 usage(argv[0]);
00062 exit(1);
00063 }
00064
00065
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
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
00106
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
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
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
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
00180 TiXmlDocument xml_in(file_in);
00181 xml_in.LoadFile();
00182
00183 std::ostringstream stream;
00184 stream << xml_in;
00185 xml_string = stream.str();
00186
00187
00188 TiXmlDocument xml_doc;
00189 xml_doc.Parse(xml_string.c_str());
00190
00191
00192 TiXmlDocument gazebo_model_xml;
00193
00194
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
00202
00203 urdf2gazebo::URDF2Gazebo u2g;
00204 u2g.convert(xml_doc, gazebo_model_xml, enforce_limits, initial_xyz, initial_rpy, model_name);
00205
00206
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