Nav2 Navigation Stack - rolling  main
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  rclcpp::Node::SharedPtr node, bool & server_failed, const std::string & server_name,
25  const std::string & plugin_type, QComboBox * combo_box)
26 {
27  // Do not load the plugins if the combo box is already populated
28  if (combo_box->count() > 0) {
29  return;
30  }
31 
32  auto parameter_client = std::make_shared<rclcpp::SyncParametersClient>(node, server_name);
33 
34  // Wait for the service to be available before calling it
35  bool server_unavailable = false;
36  while (!parameter_client->wait_for_service(std::chrono::seconds(1))) {
37  if (!rclcpp::ok()) {
38  RCLCPP_ERROR(node->get_logger(), "Interrupted while waiting for the service. Exiting.");
39  rclcpp::shutdown();
40  }
41  RCLCPP_INFO(node->get_logger(), "%s service not available", server_name.c_str());
42  server_unavailable = true;
43  server_failed = true;
44  break;
45  }
46 
47  // Loading the plugins into the combo box
48  // If server unavailable, let the combo box be empty
49  if (server_unavailable) {
50  return;
51  }
52  auto parameters = parameter_client->get_parameters({plugin_type});
53  auto str_arr = parameters[0].as_string_array();
54  combo_box->addItem("Default");
55  for (auto str : str_arr) {
56  combo_box->addItem(QString::fromStdString(str));
57  }
58  combo_box->setCurrentText("Default");
59 }
60 
61 QString getGoalStatusLabel(std::string title, int8_t status)
62 {
63  std::string status_str;
64  switch (status) {
65  case action_msgs::msg::GoalStatus::STATUS_EXECUTING:
66  status_str = "<font color=green>active</color>";
67  break;
68 
69  case action_msgs::msg::GoalStatus::STATUS_SUCCEEDED:
70  status_str = "<font color=green>reached</color>";
71  break;
72 
73  case action_msgs::msg::GoalStatus::STATUS_CANCELED:
74  status_str = "<font color=orange>canceled</color>";
75  break;
76 
77  case action_msgs::msg::GoalStatus::STATUS_ABORTED:
78  status_str = "<font color=red>aborted</color>";
79  break;
80 
81  case action_msgs::msg::GoalStatus::STATUS_UNKNOWN:
82  status_str = "unknown";
83  break;
84 
85  default:
86  status_str = "inactive";
87  break;
88  }
89  return QString(
90  std::string(
91  "<table><tr><td width=150><b>" + title + ":</b></td><td>" +
92  status_str + "</td></tr></table>").c_str());
93 }
94 
95 } // namespace nav2_rviz_plugins