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 #include <memory>
18 
19 #include "nav2_rviz_plugins/utils.hpp"
20 #include "rclcpp/executors.hpp"
21 #include "rclcpp/future_return_code.hpp"
22 #include "rclcpp/logging.hpp"
23 #include "rclcpp/parameter_client.hpp"
24 #include "rclcpp/utilities.hpp"
25 
26 namespace nav2_rviz_plugins
27 {
28 
29 void pluginLoader(
30  // nosemgrep
31  rclcpp::Node::SharedPtr node, bool & server_failed, const std::string & server_name,
32  const std::string & plugin_type, QComboBox * combo_box, rclcpp::Executor::SharedPtr executor)
33 {
34  // Do not load the plugins if the combo box is already populated
35  if (combo_box->count() > 0) {
36  return;
37  }
38 
39  auto parameter_client = std::make_shared<rclcpp::AsyncParametersClient>(node, server_name);
40 
41  // Wait for the service to be available before calling it
42  bool server_unavailable = false;
43  while (!parameter_client->wait_for_service(std::chrono::seconds(1))) {
44  if (!rclcpp::ok()) {
45  RCLCPP_ERROR(node->get_logger(), "Interrupted while waiting for the service. Exiting.");
46  rclcpp::shutdown();
47  }
48  RCLCPP_INFO(node->get_logger(), "%s service not available", server_name.c_str());
49  server_unavailable = true;
50  server_failed = true;
51  break;
52  }
53 
54  // Loading the plugins into the combo box
55  // If server unavailable, let the combo box be empty
56  if (server_unavailable) {
57  return;
58  }
59  auto parameters = parameter_client->get_parameters({plugin_type});
60  if (executor) {
61  if (executor->spin_until_future_complete(parameters) != rclcpp::FutureReturnCode::SUCCESS) {
62  RCLCPP_ERROR(
63  node->get_logger(),
64  "Failed to get parameter '%s' from server '%s'",
65  plugin_type.c_str(), server_name.c_str());
66  return;
67  }
68  } else {
69  if (rclcpp::spin_until_future_complete(node, parameters) != rclcpp::FutureReturnCode::SUCCESS) {
70  RCLCPP_ERROR(
71  node->get_logger(),
72  "Failed to get parameter '%s' from server '%s'",
73  plugin_type.c_str(), server_name.c_str());
74  return;
75  }
76  }
77 
78  auto result = parameters.get();
79  if (result.empty()) {
80  RCLCPP_ERROR(node->get_logger(),
81  "Parameter '%s' not found on server '%s'",
82  plugin_type.c_str(), server_name.c_str());
83  return;
84  }
85  auto str_arr = result[0].as_string_array();
86  combo_box->addItem("Default");
87  for (auto str : str_arr) {
88  combo_box->addItem(QString::fromStdString(str));
89  }
90  combo_box->setCurrentText("Default");
91 }
92 
93 QString getGoalStatusLabel(std::string title, int8_t status)
94 {
95  std::string status_str;
96  switch (status) {
97  case action_msgs::msg::GoalStatus::STATUS_EXECUTING:
98  status_str = "<font color=green>active</color>";
99  break;
100 
101  case action_msgs::msg::GoalStatus::STATUS_SUCCEEDED:
102  status_str = "<font color=green>reached</color>";
103  break;
104 
105  case action_msgs::msg::GoalStatus::STATUS_CANCELED:
106  status_str = "<font color=orange>canceled</color>";
107  break;
108 
109  case action_msgs::msg::GoalStatus::STATUS_ABORTED:
110  status_str = "<font color=red>aborted</color>";
111  break;
112 
113  case action_msgs::msg::GoalStatus::STATUS_UNKNOWN:
114  status_str = "unknown";
115  break;
116 
117  default:
118  status_str = "inactive";
119  break;
120  }
121  return QString(
122  std::string(
123  "<table><tr><td width=150><b>" + title + ":</b></td><td>" +
124  status_str + "</td></tr></table>").c_str());
125 }
126 
127 } // namespace nav2_rviz_plugins