moveit2
The MoveIt Motion Planning Framework for ROS 2.
Loading...
Searching...
No Matches
rdf_loader.cpp
Go to the documentation of this file.
1/*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2011, 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 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: Ioan Sucan, Mathias Lüdtke, Dave Coleman */
36
37// MoveIt
39#include <std_msgs/msg/string.hpp>
40#include <ament_index_cpp/get_package_prefix.hpp>
41// ament_index_cpp's get_package_share_directory API changed twice.
42// Guard on ament_index_cpp's own version — not rclcpp — because the
43// changes live in ament_index_cpp and its release cadence is independent
44// of rclcpp's.
45// 1.14+ (Rolling on Resolute):
46// get_package_share_directory.hpp REMOVED, replaced with
47// get_package_share_path.hpp exposing get_package_share_path(pkg)
48// returning std::filesystem::path directly.
49// 1.5+ (Jazzy 1.8.4, Kilted 1.11.4, Lyrical 1.13.3, Rolling-on-Noble 1.13.1):
50// 2-arg out-param overload get_package_share_directory(pkg, path)
51// available; the 1-arg form is deprecated starting in 1.13.
52// Older (Humble 1.4.1):
53// only the non-deprecated 1-arg get_package_share_directory(pkg)
54// returning std::string is available.
55#include <ament_index_cpp/version.h>
56#if AMENT_INDEX_CPP_VERSION_GTE(1, 14, 0)
57#include <ament_index_cpp/get_package_share_path.hpp>
58#else
59#include <ament_index_cpp/get_package_share_directory.hpp>
60#endif
62
63#include <rclcpp/duration.hpp>
64#include <rclcpp/logger.hpp>
65#include <rclcpp/logging.hpp>
66#include <rclcpp/node.hpp>
67#include <rclcpp/time.hpp>
68
69// C++
70#include <fstream>
71#include <streambuf>
72#include <algorithm>
73#include <chrono>
74#include <filesystem>
75
76namespace rdf_loader
77{
78namespace
79{
80rclcpp::Logger getLogger()
81{
82 return moveit::getLogger("moveit.ros.rdf_loader");
83}
84} // namespace
85
86RDFLoader::RDFLoader(const std::shared_ptr<rclcpp::Node>& node, const std::string& ros_name,
87 bool default_continuous_value, double default_timeout)
88 : ros_name_(ros_name)
89{
90 auto start = node->now();
91
92 urdf_string_ = urdf_ssp_.loadInitialValue(
93 node, ros_name, [this](const std::string& new_urdf_string) { return urdfUpdateCallback(new_urdf_string); },
94 default_continuous_value, default_timeout);
95
96 const std::string srdf_name = ros_name + "_semantic";
97 srdf_string_ = srdf_ssp_.loadInitialValue(
98 node, srdf_name, [this](const std::string& new_srdf_string) { return srdfUpdateCallback(new_srdf_string); },
99 default_continuous_value, default_timeout);
100
101 if (!loadFromStrings())
102 {
103 return;
104 }
105
106 RCLCPP_INFO_STREAM(getLogger(), "Loaded robot model in " << (node->now() - start).seconds() << " seconds");
107}
108
109RDFLoader::RDFLoader(const std::string& urdf_string, const std::string& srdf_string)
110 : urdf_string_(urdf_string), srdf_string_(srdf_string)
111{
112 if (!loadFromStrings())
113 {
114 return;
115 }
116}
117
118bool RDFLoader::loadFromStrings()
119{
120 std::unique_ptr<urdf::Model> urdf = std::make_unique<urdf::Model>();
121 if (!urdf->initString(urdf_string_))
122 {
123 RCLCPP_INFO(getLogger(), "Unable to parse URDF");
124 return false;
125 }
126
127 srdf::ModelSharedPtr srdf = std::make_shared<srdf::Model>();
128 if (!srdf->initString(*urdf, srdf_string_))
129 {
130 RCLCPP_ERROR(getLogger(), "Unable to parse SRDF");
131 return false;
132 }
133
134 urdf_ = std::move(urdf);
135 srdf_ = std::move(srdf);
136 return true;
137}
138
139bool RDFLoader::isXacroFile(const std::string& path)
140{
141 std::string lower_path = path;
142 std::transform(lower_path.begin(), lower_path.end(), lower_path.begin(), ::tolower);
143
144 return lower_path.find(".xacro") != std::string::npos;
145}
146
147bool RDFLoader::loadFileToString(std::string& buffer, const std::string& path)
148{
149 if (path.empty())
150 {
151 RCLCPP_ERROR(getLogger(), "Path is empty");
152 return false;
153 }
154
155 if (!std::filesystem::exists(path))
156 {
157 RCLCPP_ERROR(getLogger(), "File does not exist");
158 return false;
159 }
160
161 std::ifstream stream(path.c_str());
162 if (!stream.good())
163 {
164 RCLCPP_ERROR(getLogger(), "Unable to load path");
165 return false;
166 }
167
168 // Load the file to a string using an efficient memory allocation technique
169 stream.seekg(0, std::ios::end);
170 buffer.reserve(stream.tellg());
171 stream.seekg(0, std::ios::beg);
172 buffer.assign((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());
173 stream.close();
174
175 return true;
176}
177
178bool RDFLoader::loadXacroFileToString(std::string& buffer, const std::string& path,
179 const std::vector<std::string>& xacro_args)
180{
181 buffer.clear();
182 if (path.empty())
183 {
184 RCLCPP_ERROR(getLogger(), "Path is empty");
185 return false;
186 }
187
188 if (!std::filesystem::exists(path))
189 {
190 RCLCPP_ERROR(getLogger(), "File does not exist");
191 return false;
192 }
193
194 std::string cmd = "ros2 run xacro xacro ";
195 for (const std::string& xacro_arg : xacro_args)
196 cmd += xacro_arg + " ";
197 cmd += path;
198
199#ifdef _WIN32
200 FILE* pipe = _popen(cmd.c_str(), "r");
201#else
202 FILE* pipe = popen(cmd.c_str(), "r");
203#endif
204 if (!pipe)
205 {
206 RCLCPP_ERROR(getLogger(), "Unable to load path");
207 return false;
208 }
209
210 char pipe_buffer[128];
211 while (!feof(pipe))
212 {
213 if (fgets(pipe_buffer, 128, pipe) != nullptr)
214 buffer += pipe_buffer;
215 }
216#ifdef _WIN32
217 _pclose(pipe);
218#else
219 pclose(pipe);
220#endif
221
222 return true;
223}
224
225bool RDFLoader::loadXmlFileToString(std::string& buffer, const std::string& path,
226 const std::vector<std::string>& xacro_args)
227{
228 if (isXacroFile(path))
229 {
230 return loadXacroFileToString(buffer, path, xacro_args);
231 }
232
233 return loadFileToString(buffer, path);
234}
235
236bool RDFLoader::loadPkgFileToString(std::string& buffer, const std::string& package_name,
237 const std::string& relative_path, const std::vector<std::string>& xacro_args)
238{
239 std::filesystem::path path;
240 try
241 {
242#if AMENT_INDEX_CPP_VERSION_GTE(1, 14, 0)
243 path = ament_index_cpp::get_package_share_path(package_name);
244#elif AMENT_INDEX_CPP_VERSION_GTE(1, 5, 0)
245 ament_index_cpp::get_package_share_directory(package_name, path);
246#else
247 path = ament_index_cpp::get_package_share_directory(package_name);
248#endif
249 }
250 catch (const ament_index_cpp::PackageNotFoundError& e)
251 {
252 RCLCPP_ERROR(getLogger(), "ament_index_cpp: %s", e.what());
253 return false;
254 }
255
256 path = path / relative_path;
257
258 return loadXmlFileToString(buffer, path.string(), xacro_args);
259}
260
261void RDFLoader::urdfUpdateCallback(const std::string& new_urdf_string)
262{
263 urdf_string_ = new_urdf_string;
264 if (!loadFromStrings())
265 {
266 return;
267 }
268 if (new_model_cb_)
269 {
270 new_model_cb_();
271 }
272}
273
274void RDFLoader::srdfUpdateCallback(const std::string& new_srdf_string)
275{
276 srdf_string_ = new_srdf_string;
277 if (!loadFromStrings())
278 {
279 return;
280 }
281 if (new_model_cb_)
282 {
283 new_model_cb_();
284 }
285}
286} // namespace rdf_loader
static bool loadXmlFileToString(std::string &buffer, const std::string &path, const std::vector< std::string > &xacro_args)
helper that branches between loadFileToString() and loadXacroFileToString() based on result of isXacr...
static bool isXacroFile(const std::string &path)
determine if given path points to a xacro file
static bool loadPkgFileToString(std::string &buffer, const std::string &package_name, const std::string &relative_path, const std::vector< std::string > &xacro_args)
helper that generates a file path based on package name and relative file path to package
RDFLoader(const std::shared_ptr< rclcpp::Node > &node, const std::string &ros_name="robot_description", bool default_continuous_value=false, double default_timeout=10.0)
Default constructor.
static bool loadFileToString(std::string &buffer, const std::string &path)
load file from given path into buffer
static bool loadXacroFileToString(std::string &buffer, const std::string &path, const std::vector< std::string > &xacro_args)
run xacro with the given args on the file, return result in buffer
rclcpp::Logger getLogger(const std::string &name)
Creates a namespaced logger.
Definition logger.cpp:79