Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
dock_database.cpp
1 // Copyright (c) 2024 Open Navigation LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "opennav_docking/dock_database.hpp"
16 
17 namespace opennav_docking
18 {
19 
20 DockDatabase::DockDatabase(std::shared_ptr<std::mutex> mutex)
21 : mutex_(mutex),
22  dock_loader_("opennav_docking_core", "opennav_docking_core::ChargingDock")
23 {}
24 
26 {
27  dock_instances_.clear();
28  dock_plugins_.clear();
29  reload_db_service_.reset();
30 }
31 
33  const nav2::LifecycleNode::WeakPtr & parent,
34  std::shared_ptr<tf2_ros::Buffer> tf)
35 {
36  node_ = parent;
37  auto node = node_.lock();
38 
39  if (!getDockPlugins(node, tf)) {
40  RCLCPP_ERROR(
41  node->get_logger(),
42  "An error occurred while getting the dock plugins!");
43  return false;
44  }
45 
46  if (!getDockInstances(node)) {
47  RCLCPP_ERROR(
48  node->get_logger(),
49  "An error occurred while getting the dock instances!");
50  return false;
51  }
52 
53  RCLCPP_INFO(
54  node->get_logger(),
55  "Docking Server has %u dock types and %u dock instances available.",
56  this->plugin_size(), this->instance_size());
57 
58  reload_db_service_ = node->create_service<nav2_msgs::srv::ReloadDockDatabase>(
59  "~/reload_database",
60  std::bind(
62  std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
63 
64  return true;
65 }
66 
68 {
69  DockPluginMap::iterator it;
70  for (it = dock_plugins_.begin(); it != dock_plugins_.end(); ++it) {
71  it->second->activate();
72  }
73 }
74 
76 {
77  DockPluginMap::iterator it;
78  for (it = dock_plugins_.begin(); it != dock_plugins_.end(); ++it) {
79  it->second->deactivate();
80  }
81 }
82 
84  const std::shared_ptr<rmw_request_id_t>/*request_header*/,
85  const std::shared_ptr<nav2_msgs::srv::ReloadDockDatabase::Request> request,
86  std::shared_ptr<nav2_msgs::srv::ReloadDockDatabase::Response> response)
87 {
88  if (!mutex_->try_lock()) {
89  RCLCPP_ERROR(node_.lock()->get_logger(), "Cannot reload database while docking!");
90  response->success = false;
91  return;
92  }
93 
94  auto node = node_.lock();
95  DockMap dock_instances;
96  if (utils::parseDockFile(request->filepath, node, dock_instances)) {
97  dock_instances_ = dock_instances;
98  response->success = true;
99  RCLCPP_INFO(
100  node->get_logger(),
101  "Dock database reloaded from file %s.", request->filepath.c_str());
102  mutex_->unlock();
103  return;
104  }
105  response->success = false;
106  mutex_->unlock();
107 }
108 
109 Dock * DockDatabase::findDock(const std::string & dock_id)
110 {
111  Dock * dock_instance = findDockInstance(dock_id);
112  ChargingDock::Ptr dock_plugin{nullptr};
113 
114  if (dock_instance) {
115  dock_plugin = findDockPlugin(dock_instance->type);
116  if (dock_plugin) {
117  // Populate the plugin shared pointer
118  dock_instance->plugin = dock_plugin;
119  return dock_instance;
120  }
121  throw opennav_docking_core::DockNotValid("Dock requested has no valid plugin!");
122  }
123  throw opennav_docking_core::DockNotInDB("Dock ID requested is not in database!");
124 }
125 
126 Dock * DockDatabase::findDockInstance(const std::string & dock_id)
127 {
128  auto it = dock_instances_.find(dock_id);
129  if (it != dock_instances_.end()) {
130  return &(it->second);
131  }
132  return nullptr;
133 }
134 
135 ChargingDock::Ptr DockDatabase::findDockPlugin(const std::string & type)
136 {
137  // If only one dock plugin and type not set, use the default dock
138  if (type.empty() && dock_plugins_.size() == 1) {
139  return dock_plugins_.begin()->second;
140  }
141 
142  auto it = dock_plugins_.find(type);
143  if (it != dock_plugins_.end()) {
144  return it->second;
145  }
146  return nullptr;
147 }
148 
150  const nav2::LifecycleNode::SharedPtr & node,
151  std::shared_ptr<tf2_ros::Buffer> tf)
152 {
153  std::vector<std::string> docks_plugins;
154  if (!node->has_parameter("dock_plugins")) {
155  node->declare_parameter("dock_plugins", rclcpp::ParameterType::PARAMETER_STRING_ARRAY);
156  }
157  if (!node->get_parameter("dock_plugins", docks_plugins)) {
158  RCLCPP_ERROR(node->get_logger(), "Charging dock plugins not given!");
159  return false;
160  }
161 
162  if (docks_plugins.size() < 1u) {
163  RCLCPP_ERROR(node->get_logger(), "Charging dock plugins empty! Must provide 1.");
164  return false;
165  }
166 
167  for (size_t i = 0; i != docks_plugins.size(); i++) {
168  try {
169  std::string plugin_type = nav2::get_plugin_type_param(
170  node, docks_plugins[i]);
171  opennav_docking_core::ChargingDock::Ptr dock =
172  dock_loader_.createUniqueInstance(plugin_type);
173  RCLCPP_INFO(
174  node->get_logger(), "Created charging dock plugin %s of type %s",
175  docks_plugins[i].c_str(), plugin_type.c_str());
176  dock->configure(node, docks_plugins[i], tf);
177  dock_plugins_.insert({docks_plugins[i], dock});
178  } catch (const std::exception & ex) {
179  RCLCPP_FATAL(
180  node->get_logger(), "Failed to create Charging Dock plugin. Exception: %s",
181  ex.what());
182  return false;
183  }
184  }
185 
186  return true;
187 }
188 
189 bool DockDatabase::getDockInstances(const nav2::LifecycleNode::SharedPtr & node)
190 {
191  using rclcpp::ParameterType::PARAMETER_STRING;
192  using rclcpp::ParameterType::PARAMETER_STRING_ARRAY;
193 
194  // Attempt to obtain docks from separate file
195  std::string dock_filepath;
196  if (!node->has_parameter("dock_database")) {
197  node->declare_parameter("dock_database", PARAMETER_STRING);
198  }
199  if (node->get_parameter("dock_database", dock_filepath)) {
200  RCLCPP_INFO(
201  node->get_logger(), "Loading dock from database file %s.", dock_filepath.c_str());
202  try {
203  return utils::parseDockFile(dock_filepath, node, dock_instances_);
204  } catch (YAML::BadConversion & e) {
205  RCLCPP_ERROR(
206  node->get_logger(),
207  "Dock database (%s) is malformed: %s.", dock_filepath.c_str(), e.what());
208  return false;
209  }
210  return true;
211  }
212 
213  // Attempt to obtain docks from parameter file
214  std::vector<std::string> docks_param;
215  if (!node->has_parameter("docks")) {
216  node->declare_parameter("docks", PARAMETER_STRING_ARRAY);
217  }
218  if (node->get_parameter("docks", docks_param)) {
219  RCLCPP_INFO(node->get_logger(), "Loading docks from parameter file.");
220  return utils::parseDockParams(docks_param, node, dock_instances_);
221  }
222 
223  RCLCPP_WARN(
224  node->get_logger(),
225  "Dock database filepath nor dock parameters set. "
226  "Docking actions can only be executed specifying the dock pose via the action request. "
227  "Or update the dock database via the reload_database service.");
228  return true;
229 }
230 
231 unsigned int DockDatabase::plugin_size() const
232 {
233  return dock_plugins_.size();
234 }
235 
236 unsigned int DockDatabase::instance_size() const
237 {
238  return dock_instances_.size();
239 }
240 
241 } // namespace opennav_docking
Dock * findDock(const std::string &dock_id)
Find a dock instance & plugin in the databases from ID.
Dock * findDockInstance(const std::string &dock_id)
Find a dock instance in the database from ID.
ChargingDock::Ptr findDockPlugin(const std::string &type)
Find a dock plugin to use for a given type.
void reloadDbCb(const std::shared_ptr< rmw_request_id_t > request_header, const std::shared_ptr< nav2_msgs::srv::ReloadDockDatabase::Request > request, std::shared_ptr< nav2_msgs::srv::ReloadDockDatabase::Response > response)
Service request to reload database of docks.
void deactivate()
An deactivation method.
unsigned int plugin_size() const
Get the number of dock types in the database.
~DockDatabase()
A destructor for opennav_docking::DockDatabase.
unsigned int instance_size() const
Get the number of docks in the database.
bool getDockInstances(const nav2::LifecycleNode::SharedPtr &node)
Populate database of dock instances.
bool getDockPlugins(const nav2::LifecycleNode::SharedPtr &node, std::shared_ptr< tf2_ros::Buffer > tf)
Populate database of dock type plugins.
DockDatabase(std::shared_ptr< std::mutex > mutex=std::make_shared< std::mutex >())
A constructor for opennav_docking::DockDatabase.
void activate()
An activation method.
bool initialize(const nav2::LifecycleNode::WeakPtr &parent, std::shared_ptr< tf2_ros::Buffer > tf)
A setup function to populate database.
Dock was not found in the provided dock database.
Dock plugin provided in the database or action was invalid.
Definition: types.hpp:33