Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
utils.cpp
1 // Copyright (c) 2019 Intel Corporation
2 // Copyright (c) 2024 Neobotix GmbH
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #include <chrono>
17 
18 #include "nav2_rviz_plugins/utils.hpp"
19 
20 namespace nav2_rviz_plugins
21 {
22 
23 void pluginLoader(
24  // nosemgrep
25  rclcpp::Node::SharedPtr node, bool & server_failed, const std::string & server_name,
26  const std::string & plugin_type, QComboBox * combo_box, rclcpp::Executor::SharedPtr executor)
27 {
28  // Do not load the plugins if the combo box is already populated
29  if (combo_box->count() > 0) {
30  return;
31  }
32 
33  auto parameter_client = std::make_shared<rclcpp::AsyncParametersClient>(node, server_name);
34 
35  // Wait for the service to be available before calling it
36  bool server_unavailable = false;
37  while (!parameter_client->wait_for_service(std::chrono::seconds(1))) {
38  if (!rclcpp::ok()) {
39  RCLCPP_ERROR(node->get_logger(), "Interrupted while waiting for the service. Exiting.");
40  rclcpp::shutdown();
41  }
42  RCLCPP_INFO(node->get_logger(), "%s service not available", server_name.c_str());
43  server_unavailable = true;
44  server_failed = true;
45  break;
46  }
47 
48  // Loading the plugins into the combo box
49  // If server unavailable, let the combo box be empty
50  if (server_unavailable) {
51  return;
52  }
53  auto parameters = parameter_client->get_parameters({plugin_type});
54  if (executor) {
55  if (executor->spin_until_future_complete(parameters) != rclcpp::FutureReturnCode::SUCCESS) {
56  RCLCPP_ERROR(
57  node->get_logger(),
58  "Failed to get parameter '%s' from server '%s'",
59  plugin_type.c_str(), server_name.c_str());
60  return;
61  }
62  } else {
63  if (rclcpp::spin_until_future_complete(node, parameters) != rclcpp::FutureReturnCode::SUCCESS) {
64  RCLCPP_ERROR(
65  node->get_logger(),
66  "Failed to get parameter '%s' from server '%s'",
67  plugin_type.c_str(), server_name.c_str());
68  return;
69  }
70  }
71 
72  auto result = parameters.get();
73  if (result.empty()) {
74  RCLCPP_ERROR(node->get_logger(),
75  "Parameter '%s' not found on server '%s'",
76  plugin_type.c_str(), server_name.c_str());
77  return;
78  }
79  auto str_arr = result[0].as_string_array();
80  combo_box->addItem("Default");
81  for (auto str : str_arr) {
82  combo_box->addItem(QString::fromStdString(str));
83  }
84  combo_box->setCurrentText("Default");
85 }
86 
87 QString getGoalStatusLabel(std::string title, int8_t status)
88 {
89  std::string status_str;
90  switch (status) {
91  case action_msgs::msg::GoalStatus::STATUS_EXECUTING:
92  status_str = "<font color=green>active</color>";
93  break;
94 
95  case action_msgs::msg::GoalStatus::STATUS_SUCCEEDED:
96  status_str = "<font color=green>reached</color>";
97  break;
98 
99  case action_msgs::msg::GoalStatus::STATUS_CANCELED:
100  status_str = "<font color=orange>canceled</color>";
101  break;
102 
103  case action_msgs::msg::GoalStatus::STATUS_ABORTED:
104  status_str = "<font color=red>aborted</color>";
105  break;
106 
107  case action_msgs::msg::GoalStatus::STATUS_UNKNOWN:
108  status_str = "unknown";
109  break;
110 
111  default:
112  status_str = "inactive";
113  break;
114  }
115  return QString(
116  std::string(
117  "<table><tr><td width=150><b>" + title + ":</b></td><td>" +
118  status_str + "</td></tr></table>").c_str());
119 }
120 
121 } // namespace nav2_rviz_plugins