15 #include "nav2_rviz_plugins/selector.hpp"
16 #include "nav2_rviz_plugins/utils.hpp"
17 #include "rviz_common/display_context.hpp"
18 #include "rclcpp/rate.hpp"
20 using namespace std::chrono_literals;
22 namespace nav2_rviz_plugins
24 Selector::Selector(QWidget * parent)
27 client_node_ = std::make_shared<rclcpp::Node>(
"nav2_rviz_selector_node");
28 rclcpp::QoS qos(rclcpp::KeepLast(1));
29 qos.transient_local().reliable();
32 client_node_->create_publisher<std_msgs::msg::String>(
"controller_selector", qos);
33 pub_planner_ = client_node_->create_publisher<std_msgs::msg::String>(
"planner_selector", qos);
35 client_node_->create_publisher<std_msgs::msg::String>(
"goal_checker_selector", qos);
36 pub_smoother_ = client_node_->create_publisher<std_msgs::msg::String>(
"smoother_selector", qos);
37 pub_progress_checker_ =
38 client_node_->create_publisher<std_msgs::msg::String>(
"progress_checker_selector", qos);
40 client_node_->create_publisher<std_msgs::msg::String>(
"path_handler_selector", qos);
42 main_layout_ =
new QVBoxLayout;
43 row_1_label_layout_ =
new QHBoxLayout;
44 row_2_label_layout_ =
new QHBoxLayout;
45 row_3_label_layout_ =
new QHBoxLayout;
46 row_1_layout_ =
new QHBoxLayout;
47 row_2_layout_ =
new QHBoxLayout;
48 row_3_layout_ =
new QHBoxLayout;
49 controller_ =
new QComboBox;
50 planner_ =
new QComboBox;
51 goal_checker_ =
new QComboBox;
52 smoother_ =
new QComboBox;
53 progress_checker_ =
new QComboBox;
54 path_handler_ =
new QComboBox;
56 main_layout_->setContentsMargins(10, 10, 10, 10);
58 row_1_label_layout_->addWidget(
new QLabel(
"Controller"));
59 row_1_layout_->addWidget(controller_);
60 row_1_label_layout_->addWidget(
new QLabel(
"Planner"));
61 row_1_layout_->addWidget(planner_);
62 row_2_label_layout_->addWidget(
new QLabel(
"Goal Checker"));
63 row_2_layout_->addWidget(goal_checker_);
64 row_2_label_layout_->addWidget(
new QLabel(
"Smoother"));
65 row_2_layout_->addWidget(smoother_);
66 row_3_label_layout_->addWidget(
new QLabel(
"Progress Checker"));
67 row_3_layout_->addWidget(progress_checker_);
68 row_3_label_layout_->addWidget(
new QLabel(
"Path Handler"));
69 row_3_layout_->addWidget(path_handler_);
71 main_layout_->addLayout(row_1_label_layout_);
72 main_layout_->addLayout(row_1_layout_);
73 main_layout_->addLayout(row_2_label_layout_);
74 main_layout_->addLayout(row_2_layout_);
75 main_layout_->addLayout(row_3_label_layout_);
76 main_layout_->addLayout(row_3_layout_);
78 setLayout(main_layout_);
82 controller_, QOverload<int>::of(&QComboBox::activated),
this,
83 &Selector::setController);
86 planner_, QOverload<int>::of(&QComboBox::activated),
this,
87 &Selector::setPlanner);
90 goal_checker_, QOverload<int>::of(&QComboBox::activated),
this,
91 &Selector::setGoalChecker);
94 smoother_, QOverload<int>::of(&QComboBox::activated),
this,
95 &Selector::setSmoother);
98 progress_checker_, QOverload<int>::of(&QComboBox::activated),
this,
99 &Selector::setProgressChecker);
102 path_handler_, QOverload<int>::of(&QComboBox::activated),
this,
103 &Selector::setPathHandler);
106 Selector::~Selector()
108 if (load_plugins_thread_.joinable()) {
109 load_plugins_thread_.join();
114 void Selector::setSelection(
115 QComboBox * combo_box,
116 rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher)
119 if (combo_box->findText(
"Default") != -1) {
120 combo_box->removeItem(0);
124 if (combo_box->count() == 0) {
128 auto msg = std::make_unique<std_msgs::msg::String>();
129 msg->data = combo_box->currentText().toStdString();
131 publisher->publish(std::move(msg));
135 void Selector::setController()
137 setSelection(controller_, pub_controller_);
141 void Selector::setPlanner()
143 setSelection(planner_, pub_planner_);
147 void Selector::setGoalChecker()
149 setSelection(goal_checker_, pub_goal_checker_);
153 void Selector::setSmoother()
155 setSelection(smoother_, pub_smoother_);
158 void Selector::setProgressChecker()
160 setSelection(progress_checker_, pub_progress_checker_);
163 void Selector::setPathHandler()
165 setSelection(path_handler_, pub_path_handler_);
169 Selector::loadPlugins()
171 load_plugins_thread_ = std::thread(
173 rclcpp::Rate rate(0.2);
174 while (rclcpp::ok() && !plugins_loaded_) {
175 RCLCPP_INFO(client_node_->get_logger(),
"Trying to load plugins...");
176 nav2_rviz_plugins::pluginLoader(
177 client_node_, server_failed_,
"controller_server",
"controller_plugins", controller_);
178 nav2_rviz_plugins::pluginLoader(
179 client_node_, server_failed_,
"planner_server",
"planner_plugins", planner_);
180 nav2_rviz_plugins::pluginLoader(
181 client_node_, server_failed_,
"controller_server",
"goal_checker_plugins",
183 nav2_rviz_plugins::pluginLoader(
184 client_node_, server_failed_,
"smoother_server",
"smoother_plugins", smoother_);
185 nav2_rviz_plugins::pluginLoader(
186 client_node_, server_failed_,
"controller_server",
"progress_checker_plugins",
188 nav2_rviz_plugins::pluginLoader(
189 client_node_, server_failed_,
"controller_server",
"path_handler_plugins",
191 if (controller_->count() > 0 &&
192 planner_->count() > 0 &&
193 goal_checker_->count() > 0 &&
194 smoother_->count() > 0 &&
195 progress_checker_->count() > 0 &&
196 path_handler_->count() > 0)
198 plugins_loaded_ =
true;
200 RCLCPP_INFO(client_node_->get_logger(),
"Failed to load plugins. Retrying...");
208 #include "pluginlib/class_list_macros.hpp"