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 <boost/algorithm/string.hpp>
00038 #include <ros/ros.h>
00039 #include <vector>
00040 #include "urdf/model.h"
00041 #include <fstream>
00042 #include <iostream>
00043 
00044 namespace urdf{
00045 
00046 bool IsColladaData(const std::string& data)
00047 {
00048   return data.find("<COLLADA") != std::string::npos;
00049 }
00050 
00051 
00052 bool Model::initFile(const std::string& filename)
00053 {
00054 
00055   // get the entire file
00056   std::string xml_string;
00057   std::fstream xml_file(filename.c_str(), std::fstream::in);
00058   if (xml_file.is_open())
00059   {
00060     while ( xml_file.good() )
00061     {
00062       std::string line;
00063       std::getline( xml_file, line);
00064       xml_string += (line + "\n");
00065     }
00066     xml_file.close();
00067     return Model::initString(xml_string);
00068   }
00069   else
00070   {
00071     ROS_ERROR("Could not open file [%s] for parsing.",filename.c_str());
00072     return false;
00073   }
00074 
00075 }
00076 
00077 
00078 bool Model::initParam(const std::string& param)
00079 {
00080   ros::NodeHandle nh;
00081   std::string xml_string;
00082   
00083   // gets the location of the robot description on the parameter server
00084   std::string full_param;
00085   if (!nh.searchParam(param, full_param)){
00086     ROS_ERROR("Could not find parameter %s on parameter server", param.c_str());
00087     return false;
00088   }
00089 
00090   // read the robot description from the parameter server
00091   if (!nh.getParam(full_param, xml_string)){
00092     ROS_ERROR("Could not read parameter %s on parameter server", full_param.c_str());
00093     return false;
00094   }
00095   return Model::initString(xml_string);
00096 }
00097 
00098 bool Model::initXml(TiXmlDocument *xml_doc)
00099 {
00100   if (!xml_doc)
00101   {
00102     ROS_ERROR("Could not parse the xml document");
00103     return false;
00104   }
00105 
00106   std::stringstream ss;
00107   ss << *xml_doc;
00108 
00109   return Model::initString(ss.str());
00110 }
00111 
00112 bool Model::initXml(TiXmlElement *robot_xml)
00113 {
00114   if (!robot_xml)
00115   {
00116     ROS_ERROR("Could not parse the xml element");
00117     return false;
00118   }
00119 
00120   std::stringstream ss;
00121   ss << (*robot_xml);
00122 
00123   return Model::initString(ss.str());
00124 }
00125 
00126 bool Model::initString(const std::string& xml_string)
00127 {
00128   boost::shared_ptr<ModelInterface> model;
00129 
00130   // necessary for COLLADA compatibility
00131   if( IsColladaData(xml_string) ) {
00132     ROS_DEBUG("Parsing robot collada xml string");
00133     model = parseCollada(xml_string);
00134   }
00135   else {
00136     ROS_DEBUG("Parsing robot urdf xml string");
00137     model = parseURDF(xml_string);
00138   }
00139 
00140   // copy data from model into this object
00141   if (model){
00142     this->links_ = model->links_;
00143     this->joints_ = model->joints_;
00144     this->materials_ = model->materials_;
00145     this->name_ = model->name_;
00146     this->root_link_ = model->root_link_;
00147     return true;
00148   }
00149   else
00150     return false;
00151 }
00152 
00153 
00154 
00155 }// namespace
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends


urdf
Author(s): Wim Meeussen, John Hsu, Rosen Diankov
autogenerated on Mon Aug 19 2013 11:02:11