model.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 /* Author: Wim Meeussen */
00036 
00037 #include "urdf/model.h"
00038 
00039 #include <ros/ros.h>
00040 
00041 /* we include the default parser for plain URDF files; 
00042    other parsers are loaded via plugins (if available) */
00043 #include <urdf_parser/urdf_parser.h>
00044 #include <urdf_parser_plugin/parser.h>
00045 #include <pluginlib/class_loader.h>
00046 
00047 #include <boost/algorithm/string.hpp>
00048 #include <boost/scoped_ptr.hpp>
00049 #include <boost/thread.hpp>
00050 
00051 #include <vector>
00052 #include <fstream>
00053 #include <iostream>
00054 
00055 namespace urdf{
00056 
00057 static bool IsColladaData(const std::string& data)
00058 {
00059   return data.find("<COLLADA") != std::string::npos;
00060 }
00061 
00062 
00063 bool Model::initFile(const std::string& filename)
00064 {
00065 
00066   // get the entire file
00067   std::string xml_string;
00068   std::fstream xml_file(filename.c_str(), std::fstream::in);
00069   if (xml_file.is_open())
00070   {
00071     while ( xml_file.good() )
00072     {
00073       std::string line;
00074       std::getline( xml_file, line);
00075       xml_string += (line + "\n");
00076     }
00077     xml_file.close();
00078     return Model::initString(xml_string);
00079   }
00080   else
00081   {
00082     ROS_ERROR("Could not open file [%s] for parsing.",filename.c_str());
00083     return false;
00084   }
00085 
00086 }
00087 
00088 
00089 bool Model::initParam(const std::string& param)
00090 {
00091   ros::NodeHandle nh;
00092   std::string xml_string;
00093   
00094   // gets the location of the robot description on the parameter server
00095   std::string full_param;
00096   if (!nh.searchParam(param, full_param)){
00097     ROS_ERROR("Could not find parameter %s on parameter server", param.c_str());
00098     return false;
00099   }
00100 
00101   // read the robot description from the parameter server
00102   if (!nh.getParam(full_param, xml_string)){
00103     ROS_ERROR("Could not read parameter %s on parameter server", full_param.c_str());
00104     return false;
00105   }
00106   return Model::initString(xml_string);
00107 }
00108 
00109 bool Model::initXml(TiXmlDocument *xml_doc)
00110 {
00111   if (!xml_doc)
00112   {
00113     ROS_ERROR("Could not parse the xml document");
00114     return false;
00115   }
00116 
00117   std::stringstream ss;
00118   ss << *xml_doc;
00119 
00120   return Model::initString(ss.str());
00121 }
00122 
00123 bool Model::initXml(TiXmlElement *robot_xml)
00124 {
00125   if (!robot_xml)
00126   {
00127     ROS_ERROR("Could not parse the xml element");
00128     return false;
00129   }
00130 
00131   std::stringstream ss;
00132   ss << (*robot_xml);
00133 
00134   return Model::initString(ss.str());
00135 }
00136 
00137 bool Model::initString(const std::string& xml_string)
00138 {
00139   urdf::ModelInterfaceSharedPtr model;
00140 
00141   // necessary for COLLADA compatibility
00142   if( IsColladaData(xml_string) ) {
00143     ROS_DEBUG("Parsing robot collada xml string");
00144 
00145     static boost::mutex PARSER_PLUGIN_LOCK;
00146     static boost::scoped_ptr<pluginlib::ClassLoader<urdf::URDFParser> > PARSER_PLUGIN_LOADER;
00147     boost::mutex::scoped_lock _(PARSER_PLUGIN_LOCK);
00148 
00149     try
00150     {
00151       if (!PARSER_PLUGIN_LOADER)
00152         PARSER_PLUGIN_LOADER.reset(new pluginlib::ClassLoader<urdf::URDFParser>("urdf_parser_plugin", "urdf::URDFParser"));
00153       const std::vector<std::string> &classes = PARSER_PLUGIN_LOADER->getDeclaredClasses();
00154       bool found = false;
00155       for (std::size_t i = 0 ; i < classes.size() ; ++i)
00156         if (classes[i].find("urdf/ColladaURDFParser") != std::string::npos)
00157         {
00158           boost::shared_ptr<urdf::URDFParser> instance = PARSER_PLUGIN_LOADER->createInstance(classes[i]);
00159           if (instance)
00160             model = instance->parse(xml_string);
00161           found = true;
00162           break;
00163         }
00164       if (!found)
00165         ROS_ERROR_STREAM("No URDF parser plugin found for Collada files. Did you install the corresponding package?");
00166     }
00167     catch(pluginlib::PluginlibException& ex)
00168     {
00169       ROS_ERROR_STREAM("Exception while creating planning plugin loader " << ex.what() << ". Will not parse Collada file.");
00170     }
00171   }
00172   else {
00173     ROS_DEBUG("Parsing robot urdf xml string");
00174     model = parseURDF(xml_string);
00175   }
00176 
00177   // copy data from model into this object
00178   if (model){
00179     this->links_ = model->links_;
00180     this->joints_ = model->joints_;
00181     this->materials_ = model->materials_;
00182     this->name_ = model->name_;
00183     this->root_link_ = model->root_link_;
00184     return true;
00185   }
00186   return false;
00187 }
00188 
00189 
00190 
00191 }// namespace


urdf
Author(s): Ioan Sucan , Jackie Kay
autogenerated on Tue Aug 15 2017 02:48:01