moveit2
The MoveIt Motion Planning Framework for ROS 2.
group_meta_config.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2022, Metro Robots
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 Metro Robots 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: David V. Lu!! */
36 
38 
39 namespace moveit_setup
40 {
41 namespace srdf_setup
42 {
44 {
45  return !group_meta_data_.empty();
46 }
47 
48 // ******************************************************************************************
49 // Input kinematics.yaml file
50 // ******************************************************************************************
51 void GroupMetaConfig::loadPrevious(const std::filesystem::path& package_path, const YAML::Node& /* node */)
52 {
53  std::filesystem::path file_path = package_path / KINEMATICS_FILE;
54  if (!inputKinematicsYAML(file_path))
55  {
56  throw std::runtime_error("Failed to parse kinematics yaml file. This file is not critical but any previous "
57  "kinematic solver settings have been lost. To re-populate this file edit each "
58  "existing planning group and choose a solver, then save each change.");
59  }
60 }
61 
62 void GroupMetaConfig::deleteGroup(const std::string& group_name)
63 {
64  group_meta_data_.erase(group_name);
65  changed_ = true;
66 }
67 
68 void GroupMetaConfig::renameGroup(const std::string& old_group_name, const std::string& new_group_name)
69 {
70  group_meta_data_[new_group_name] = group_meta_data_[old_group_name];
71  group_meta_data_.erase(old_group_name);
72  changed_ = true;
73 }
74 
75 const GroupMetaData& GroupMetaConfig::getMetaData(const std::string& group_name) const
76 {
77  const auto& match = group_meta_data_.find(group_name);
78  if (match != group_meta_data_.end())
79  {
80  return match->second;
81  }
82  else
83  {
84  return default_values_;
85  }
86 }
87 
88 void GroupMetaConfig::setMetaData(const std::string& group_name, const GroupMetaData& meta_data)
89 {
90  group_meta_data_[group_name] = meta_data;
91  changed_ = true;
92 }
93 
94 bool GroupMetaConfig::inputKinematicsYAML(const std::filesystem::path& file_path)
95 {
96  // Load file
97  std::ifstream input_stream(file_path);
98  if (!input_stream.good())
99  {
100  return false;
101  }
102 
103  // Begin parsing
104  try
105  {
106  YAML::Node doc = YAML::Load(input_stream);
107 
108  // Loop through all groups
109  for (YAML::const_iterator group_it = doc.begin(); group_it != doc.end(); ++group_it)
110  {
111  const std::string& group_name = group_it->first.as<std::string>();
112  const YAML::Node& group = group_it->second;
113 
114  // Create new meta data
115  GroupMetaData meta_data;
116 
117  getYamlProperty(group, "kinematics_solver", meta_data.kinematics_solver_);
118  getYamlProperty(group, "kinematics_solver_search_resolution", meta_data.kinematics_solver_search_resolution_,
119  DEFAULT_KIN_SOLVER_SEARCH_RESOLUTION);
120  getYamlProperty(group, "kinematics_solver_timeout", meta_data.kinematics_solver_timeout_,
121  DEFAULT_KIN_SOLVER_TIMEOUT);
122 
123  // Assign meta data to vector
124  group_meta_data_[group_name] = meta_data;
125  }
126 
127  return true;
128  }
129  catch (YAML::ParserException& e) // Catch errors
130  {
131  return false;
132  }
133 }
134 
135 // ******************************************************************************************
136 // Output kinematic config files
137 // ******************************************************************************************
139 {
140  emitter << YAML::BeginMap;
141 
142  // Output every group and the kinematic solver it can use ----------------------------------
143  for (const auto& meta_pair : parent_.group_meta_data_)
144  {
145  const std::string& group_name = meta_pair.first;
146  const GroupMetaData& meta_data = meta_pair.second;
147 
148  // Only save kinematic data if the solver is not "None"
149  if (meta_data.kinematics_solver_.empty() || meta_data.kinematics_solver_ == "None")
150  continue;
151 
152  emitter << YAML::Key << group_name;
153  emitter << YAML::Value << YAML::BeginMap;
154 
155  // Kinematic Solver
156  emitter << YAML::Key << "kinematics_solver";
157  emitter << YAML::Value << meta_data.kinematics_solver_;
158 
159  // Search Resolution
160  emitter << YAML::Key << "kinematics_solver_search_resolution";
161  emitter << YAML::Value << meta_data.kinematics_solver_search_resolution_;
162 
163  // Solver Timeout
164  emitter << YAML::Key << "kinematics_solver_timeout";
165  emitter << YAML::Value << meta_data.kinematics_solver_timeout_;
166 
167  emitter << YAML::EndMap;
168  }
169 
170  emitter << YAML::EndMap;
171  return true;
172 }
173 
174 void GroupMetaConfig::collectVariables(std::vector<TemplateVariable>& variables)
175 {
176  // TODO: Put any additional parameters files into the ROS 2 launch files where they can be read
177 
178  // Add parameter files for the kinematics solvers that should be loaded
179  // in addition to kinematics.yaml by planning_context.launch
180  std::string kinematics_parameters_files_block;
181  for (const auto& groups : group_meta_data_)
182  {
183  if (groups.second.kinematics_parameters_file_.empty())
184  continue;
185 
186  // add a linebreak if we have more than one entry
187  if (!kinematics_parameters_files_block.empty())
188  kinematics_parameters_files_block += "\n";
189 
190  std::string line = " <rosparam command=\"load\" ns=\"" + groups.first + "\" file=\"" +
191  groups.second.kinematics_parameters_file_ + "\"/>";
192  kinematics_parameters_files_block += line;
193  }
194  variables.push_back(TemplateVariable("KINEMATICS_PARAMETERS_FILE_NAMES_BLOCK", kinematics_parameters_files_block));
195 }
196 
197 } // namespace srdf_setup
198 } // namespace moveit_setup
199 
200 #include <pluginlib/class_list_macros.hpp> // NOLINT
PLUGINLIB_EXPORT_CLASS(cached_ik_kinematics_plugin::CachedIKKinematicsPlugin< kdl_kinematics_plugin::KDLKinematicsPlugin >, kinematics::KinematicsBase)
where all the data for each part of the configuration is stored.
Definition: config.hpp:58
void setMetaData(const std::string &group_name, const GroupMetaData &meta_data)
bool inputKinematicsYAML(const std::filesystem::path &file_path)
const GroupMetaData & getMetaData(const std::string &group_name) const
std::map< std::string, GroupMetaData > group_meta_data_
Planning groups extra data not found in srdf but used in config files.
void collectVariables(std::vector< TemplateVariable > &variables) override
Collect key/value pairs for use in templates.
void loadPrevious(const std::filesystem::path &package_path, const YAML::Node &node) override
Loads the configuration from an existing MoveIt configuration.
void renameGroup(const std::string &old_group_name, const std::string &new_group_name)
bool isConfigured() const override
Return true if this part of the configuration is completely set up.
void deleteGroup(const std::string &group_name)
bool getYamlProperty(const YAML::Node &node, const std::string &key, T &storage, const T &default_value=T())
Definition: utilities.hpp:110
Simple Key/value pair for templates.
Definition: templates.hpp:47