moveit2
The MoveIt Motion Planning Framework for ROS 2.
perception_config.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2021, PickNik Robotics
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 PickNik Robotics 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 app
42 {
43 // ******************************************************************************************
44 // Loads sensors_3d yaml file
45 // ******************************************************************************************
46 void PerceptionConfig::loadPrevious(const std::filesystem::path& package_path, const YAML::Node& /*node*/)
47 {
48  // Loads parameters values from sensors_3d yaml file if available
49  std::filesystem::path sensors_3d_yaml_path = package_path / "config/sensors_3d.yaml";
50  if (!std::filesystem::is_regular_file(sensors_3d_yaml_path))
51  {
52  sensors_3d_yaml_path = getSharePath("moveit_setup_app_plugins") / "templates" / "config/sensors_3d.yaml";
53  }
54  try
55  {
57  }
58  catch (const std::runtime_error& e)
59  {
60  RCLCPP_ERROR_STREAM(*logger_, e.what());
61  }
62 }
63 
64 // ******************************************************************************************
65 // Input sensors_3d yaml file
66 // ******************************************************************************************
67 std::vector<SensorParameters> PerceptionConfig::load3DSensorsYAML(const std::filesystem::path& file_path)
68 {
69  std::vector<SensorParameters> config;
70  // Is there a sensors config in the package?
71  if (file_path.empty())
72  return config;
73 
74  // Load file
75  std::ifstream input_stream(file_path);
76  if (!input_stream.good())
77  {
78  throw std::runtime_error("Unable to open file for reading " + file_path.string());
79  }
80 
81  // Begin parsing
82  try
83  {
84  const YAML::Node& doc = YAML::Load(input_stream);
85  // Get sensors node
86  const YAML::Node& sensors_node = doc["sensors"];
87 
88  // Make sure that the sensors are written as a sequence
89  if (sensors_node && sensors_node.IsSequence())
90  {
91  // Loop over the sensors available in the file
92  for (const YAML::Node& sensor_name_node : sensors_node)
93  {
94  std::string sensor_name = sensor_name_node.as<std::string>();
95  const YAML::Node& sensor = doc[sensor_name];
96 
97  SensorParameters sensor_map;
98  sensor_map["name"] = sensor_name;
99  for (YAML::const_iterator sensor_it = sensor.begin(); sensor_it != sensor.end(); ++sensor_it)
100  {
101  sensor_map[sensor_it->first.as<std::string>()] = sensor_it->second.as<std::string>();
102  }
103 
104  config.push_back(sensor_map);
105  }
106  }
107  return config;
108  }
109  catch (YAML::ParserException& e) // Catch errors, rethrow as runtime_error
110  {
111  throw std::runtime_error(std::string("Error parsing sensors yaml: ") + e.what());
112  }
113 }
114 
115 // ******************************************************************************************
116 // Used to clear sensor plugin configuration parameter list
117 // ******************************************************************************************
119 {
121  {
122  return;
123  }
124  changed_ = true;
126 }
127 
128 // ******************************************************************************************
129 // Used to add a sensor plugin configuration parameter to the sensor plugin configuration parameter list
130 // ******************************************************************************************
132 {
133  changed_ = true;
135  {
136  sensors_plugin_config_parameter_list_.push_back(parameters);
137  }
138  else
139  {
140  // Right now, the widget only supports editing one plugin
142  }
143  sensors_plugin_config_parameter_list_[0]["name"] = "default_sensor";
144 }
145 
146 // ******************************************************************************************
147 // Output 3D Sensor configuration file
148 // ******************************************************************************************
150 {
151  emitter << YAML::BeginMap;
152  emitter << YAML::Key << "sensors";
153  emitter << YAML::BeginSeq;
154  for (auto& sensor_config : parent_.sensors_plugin_config_parameter_list_)
155  {
156  emitter << YAML::Value << sensor_config["name"];
157  }
158  emitter << YAML::EndSeq;
159  for (auto& sensor_config : parent_.sensors_plugin_config_parameter_list_)
160  {
161  emitter << YAML::Key << sensor_config["name"];
162  emitter << YAML::BeginMap;
163  for (auto& parameter : sensor_config)
164  {
165  if (parameter.first == "name")
166  {
167  continue;
168  }
169  emitter << YAML::Key << parameter.first;
170  emitter << YAML::Value << parameter.second;
171  }
172  emitter << YAML::EndMap;
173  }
174  emitter << YAML::EndMap;
175  return true;
176 }
177 } // namespace app
178 } // namespace moveit_setup
179 
180 #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
std::shared_ptr< rclcpp::Logger > logger_
Definition: config.hpp:164
void setConfig(const SensorParameters &parameters)
static std::vector< SensorParameters > load3DSensorsYAML(const std::filesystem::path &file_path)
Load perception sensor config.
std::vector< SensorParameters > sensors_plugin_config_parameter_list_
Sensor plugin configuration parameter list, each sensor plugin type is a map of string pairs.
void loadPrevious(const std::filesystem::path &package_path, const YAML::Node &node) override
Loads the configuration from an existing MoveIt configuration.
void clearSensorPluginConfig()
Clear the sensor plugin configuration parameter list.
std::map< std::string, std::string > SensorParameters
std::filesystem::path getSharePath(const std::string &package_name)
Return a path for the given package's share folder.
Definition: utilities.hpp:50