model.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 /* Author: Wim Meeussen */
36 
37 #include <fstream>
38 #include <iostream>
39 #include <string>
40 #include <vector>
41 
42 #include "urdf/model.h"
43 
44 /* we include the default parser for plain URDF files;
45  other parsers are loaded via plugins (if available) */
46 #include <urdf_parser/urdf_parser.h>
49 
50 #include <boost/algorithm/string.hpp>
51 #include <boost/scoped_ptr.hpp>
52 #include <boost/thread.hpp>
53 
54 #include <tinyxml.h>
55 #include <tinyxml2.h>
56 
57 namespace urdf
58 {
59 
60 static bool IsColladaData(const std::string & data)
61 {
62  return data.find("<COLLADA") != std::string::npos;
63 }
64 
65 bool Model::initFile(const std::string & filename)
66 {
67  // get the entire file
68  std::string xml_string;
69  std::fstream xml_file(filename.c_str(), std::fstream::in);
70  if (xml_file.is_open()) {
71  while (xml_file.good() ) {
72  std::string line;
73  std::getline(xml_file, line);
74  xml_string += (line + "\n");
75  }
76  xml_file.close();
77  return Model::initString(xml_string);
78  } else {
79  ROS_ERROR("Could not open file [%s] for parsing.",filename.c_str());
80  return false;
81  }
82 }
83 
84 
85 bool Model::initParam(const std::string & param)
86 {
88 }
89 
90 bool Model::initParamWithNodeHandle(const std::string & param, const ros::NodeHandle & nh)
91 {
92  std::string xml_string;
93 
94  // gets the location of the robot description on the parameter server
95  std::string full_param;
96  if (!nh.searchParam(param, full_param)){
97  ROS_ERROR("Could not find parameter %s on parameter server", param.c_str());
98  return false;
99  }
100 
101  // read the robot description from the parameter server
102  if (!nh.getParam(full_param, xml_string)){
103  ROS_ERROR("Could not read parameter %s on parameter server", full_param.c_str());
104  return false;
105  }
106  return Model::initString(xml_string);
107 }
108 
109 bool Model::initXml(TiXmlDocument * xml_doc)
110 {
111  if (!xml_doc) {
112  ROS_ERROR("Could not parse the xml document");
113  return false;
114  }
115 
116  std::stringstream ss;
117  ss << *xml_doc;
118 
119  return Model::initString(ss.str());
120 }
121 
122 bool Model::initXml(TiXmlElement * robot_xml)
123 {
124  if (!robot_xml) {
125  ROS_ERROR("Could not parse the xml element");
126  return false;
127  }
128 
129  std::stringstream ss;
130  ss << (*robot_xml);
131 
132  return Model::initString(ss.str());
133 }
134 
135 bool Model::initXml(const tinyxml2::XMLDocument *xml_doc)
136 {
137  if (!xml_doc) {
138  ROS_ERROR("Could not parse the xml document");
139  return false;
140  }
141 
142  tinyxml2::XMLPrinter printer;
143  xml_doc->Print(&printer);
144  std::string str(printer.CStr());
145 
146  return Model::initString(str);
147 }
148 
149 bool Model::initXml(const tinyxml2::XMLElement *robot_xml)
150 {
151  if (!robot_xml) {
152  ROS_ERROR("Could not parse the xml element");
153  return false;
154  }
155 
156  std::stringstream ss;
157  tinyxml2::XMLPrinter printer;
158  robot_xml->Accept(&printer);
159  ss << printer.CStr();
160 
161  return Model::initString(ss.str());
162 }
163 
164 bool Model::initString(const std::string & xml_string)
165 {
166  urdf::ModelInterfaceSharedPtr model;
167 
168  // necessary for COLLADA compatibility
169  if (IsColladaData(xml_string)) {
170  ROS_DEBUG("Parsing robot collada xml string");
171 
172  static boost::mutex PARSER_PLUGIN_LOCK;
173  static boost::scoped_ptr<pluginlib::ClassLoader<urdf::URDFParser> > PARSER_PLUGIN_LOADER;
174  boost::mutex::scoped_lock _(PARSER_PLUGIN_LOCK);
175 
176  try {
177  if (!PARSER_PLUGIN_LOADER) {
178  PARSER_PLUGIN_LOADER.reset(new pluginlib::ClassLoader<urdf::URDFParser>("urdf_parser_plugin", "urdf::URDFParser"));
179  }
180  const std::vector<std::string> &classes = PARSER_PLUGIN_LOADER->getDeclaredClasses();
181  bool found = false;
182  for (std::size_t i = 0 ; i < classes.size() ; ++i) {
183  if (classes[i].find("urdf/ColladaURDFParser") != std::string::npos) {
184  boost::shared_ptr<urdf::URDFParser> instance = PARSER_PLUGIN_LOADER->createInstance(classes[i]);
185  if (instance) {
186  model = instance->parse(xml_string);
187  }
188  found = true;
189  break;
190  }
191  }
192  if (!found) {
193  ROS_ERROR_STREAM("No URDF parser plugin found for Collada files. Did you install the corresponding package?");
194  }
195  }
197  ROS_ERROR_STREAM("Exception while creating planning plugin loader " << ex.what() << ". Will not parse Collada file.");
198  }
199  } else {
200  ROS_DEBUG("Parsing robot urdf xml string");
201  model = parseURDF(xml_string);
202  }
203 
204  // copy data from model into this object
205  if (model) {
206  this->links_ = model->links_;
207  this->joints_ = model->joints_;
208  this->materials_ = model->materials_;
209  this->name_ = model->name_;
210  this->root_link_ = model->root_link_;
211  return true;
212  }
213  return false;
214 }
215 } // namespace urdf
model.h
ROS_ERROR_STREAM
#define ROS_ERROR_STREAM(args)
boost::shared_ptr
ros::NodeHandle::getParam
bool getParam(const std::string &key, bool &b) const
pluginlib::PluginlibException
parser.h
ROS_DEBUG
#define ROS_DEBUG(...)
urdf::Model::initFile
URDF_EXPORT bool initFile(const std::string &filename)
Load Model given a filename.
Definition: model.cpp:97
ros::NodeHandle::searchParam
bool searchParam(const std::string &key, std::string &result) const
urdf::Model::initXml
URDF_EXPORT bool initXml(const tinyxml2::XMLElement *xml)
Load Model from tinyxml2::XMLElement.
Definition: model.cpp:181
urdf::Model::initParamWithNodeHandle
URDF_EXPORT bool initParamWithNodeHandle(const std::string &param, const ros::NodeHandle &nh=ros::NodeHandle())
Load Model given the name of parameter on parameter server using provided nodehandle.
Definition: model.cpp:122
pluginlib::ClassLoader
urdf::Model::initString
URDF_EXPORT bool initString(const std::string &xmlstring)
Load Model from a XML-string.
Definition: model.cpp:196
class_loader.hpp
urdf::IsColladaData
static bool IsColladaData(const std::string &data)
Definition: model.cpp:92
urdf
urdf::Model::initParam
URDF_EXPORT bool initParam(const std::string &param)
Load Model given the name of a parameter on the parameter server.
Definition: model.cpp:117
ROS_ERROR
#define ROS_ERROR(...)
param
T param(const std::string &param_name, const T &default_val)
ros::NodeHandle


urdf
Author(s): Ioan Sucan , Jackie Kay
autogenerated on Wed Jul 20 2022 02:43:43