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 namespace urdf
55 {
56 
57 static bool IsColladaData(const std::string & data)
58 {
59  return data.find("<COLLADA") != std::string::npos;
60 }
61 
62 bool Model::initFile(const std::string & filename)
63 {
64  // get the entire file
65  std::string xml_string;
66  std::fstream xml_file(filename.c_str(), std::fstream::in);
67  if (xml_file.is_open()) {
68  while (xml_file.good() ) {
69  std::string line;
70  std::getline(xml_file, line);
71  xml_string += (line + "\n");
72  }
73  xml_file.close();
74  return Model::initString(xml_string);
75  } else {
76  ROS_ERROR("Could not open file [%s] for parsing.",filename.c_str());
77  return false;
78  }
79 }
80 
81 
82 bool Model::initParam(const std::string & param)
83 {
85 }
86 
87 bool Model::initParamWithNodeHandle(const std::string & param, const ros::NodeHandle & nh)
88 {
89  std::string xml_string;
90 
91  // gets the location of the robot description on the parameter server
92  std::string full_param;
93  if (!nh.searchParam(param, full_param)){
94  ROS_ERROR("Could not find parameter %s on parameter server", param.c_str());
95  return false;
96  }
97 
98  // read the robot description from the parameter server
99  if (!nh.getParam(full_param, xml_string)){
100  ROS_ERROR("Could not read parameter %s on parameter server", full_param.c_str());
101  return false;
102  }
103  return Model::initString(xml_string);
104 }
105 
106 bool Model::initXml(TiXmlDocument * xml_doc)
107 {
108  if (!xml_doc) {
109  ROS_ERROR("Could not parse the xml document");
110  return false;
111  }
112 
113  std::stringstream ss;
114  ss << *xml_doc;
115 
116  return Model::initString(ss.str());
117 }
118 
119 bool Model::initXml(TiXmlElement * robot_xml)
120 {
121  if (!robot_xml) {
122  ROS_ERROR("Could not parse the xml element");
123  return false;
124  }
125 
126  std::stringstream ss;
127  ss << (*robot_xml);
128 
129  return Model::initString(ss.str());
130 }
131 
132 bool Model::initString(const std::string & xml_string)
133 {
134  urdf::ModelInterfaceSharedPtr model;
135 
136  // necessary for COLLADA compatibility
137  if (IsColladaData(xml_string)) {
138  ROS_DEBUG("Parsing robot collada xml string");
139 
140  static boost::mutex PARSER_PLUGIN_LOCK;
141  static boost::scoped_ptr<pluginlib::ClassLoader<urdf::URDFParser> > PARSER_PLUGIN_LOADER;
142  boost::mutex::scoped_lock _(PARSER_PLUGIN_LOCK);
143 
144  try
145  {
146  if (!PARSER_PLUGIN_LOADER)
147  PARSER_PLUGIN_LOADER.reset(new pluginlib::ClassLoader<urdf::URDFParser>("urdf_parser_plugin", "urdf::URDFParser"));
148  const std::vector<std::string> &classes = PARSER_PLUGIN_LOADER->getDeclaredClasses();
149  bool found = false;
150  for (std::size_t i = 0 ; i < classes.size() ; ++i)
151  if (classes[i].find("urdf/ColladaURDFParser") != std::string::npos)
152  {
153  boost::shared_ptr<urdf::URDFParser> instance = PARSER_PLUGIN_LOADER->createInstance(classes[i]);
154  if (instance)
155  model = instance->parse(xml_string);
156  found = true;
157  break;
158  }
159  if (!found)
160  ROS_ERROR_STREAM("No URDF parser plugin found for Collada files. Did you install the corresponding package?");
161  }
163  {
164  ROS_ERROR_STREAM("Exception while creating planning plugin loader " << ex.what() << ". Will not parse Collada file.");
165  }
166  } else {
167  ROS_DEBUG("Parsing robot urdf xml string");
168  model = parseURDF(xml_string);
169  }
170 
171  // copy data from model into this object
172  if (model) {
173  this->links_ = model->links_;
174  this->joints_ = model->joints_;
175  this->materials_ = model->materials_;
176  this->name_ = model->name_;
177  this->root_link_ = model->root_link_;
178  return true;
179  }
180  return false;
181 }
182 } // namespace urdf
URDF_EXPORT bool initFile(const std::string &filename)
Load Model given a filename.
Definition: model.cpp:62
static bool IsColladaData(const std::string &data)
Definition: model.cpp:57
URDF_EXPORT bool initString(const std::string &xmlstring)
Load Model from a XML-string.
Definition: model.cpp:132
URDF_EXPORT bool initXml(TiXmlElement *xml)
Load Model from TiXMLElement.
Definition: model.cpp:119
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:87
bool searchParam(const std::string &key, std::string &result) const
URDF_EXPORT bool initParam(const std::string &param)
Load Model given the name of a parameter on the parameter server.
Definition: model.cpp:82
bool getParam(const std::string &key, std::string &s) const
#define ROS_ERROR_STREAM(args)
#define ROS_ERROR(...)
#define ROS_DEBUG(...)


urdf
Author(s): Ioan Sucan , Jackie Kay
autogenerated on Mon Jun 10 2019 15:38:18