15 #include "nav2_rviz_plugins/selector.hpp"
16 #include "nav2_rviz_plugins/utils.hpp"
17 #include "rviz_common/display_context.hpp"
19 using namespace std::chrono_literals;
21 namespace nav2_rviz_plugins
23 Selector::Selector(QWidget * parent)
26 client_node_ = std::make_shared<rclcpp::Node>(
"nav2_rviz_selector_node");
27 rclcpp::QoS qos(rclcpp::KeepLast(1));
28 qos.transient_local().reliable();
31 client_node_->create_publisher<std_msgs::msg::String>(
"controller_selector", qos);
32 pub_planner_ = client_node_->create_publisher<std_msgs::msg::String>(
"planner_selector", qos);
34 client_node_->create_publisher<std_msgs::msg::String>(
"goal_checker_selector", qos);
35 pub_smoother_ = client_node_->create_publisher<std_msgs::msg::String>(
"smoother_selector", qos);
36 pub_progress_checker_ =
37 client_node_->create_publisher<std_msgs::msg::String>(
"progress_checker_selector", qos);
39 client_node_->create_publisher<std_msgs::msg::String>(
"path_handler_selector", qos);
41 main_layout_ =
new QVBoxLayout;
42 row_1_label_layout_ =
new QHBoxLayout;
43 row_2_label_layout_ =
new QHBoxLayout;
44 row_3_label_layout_ =
new QHBoxLayout;
45 row_1_layout_ =
new QHBoxLayout;
46 row_2_layout_ =
new QHBoxLayout;
47 row_3_layout_ =
new QHBoxLayout;
48 controller_ =
new QComboBox;
49 planner_ =
new QComboBox;
50 goal_checker_ =
new QComboBox;
51 smoother_ =
new QComboBox;
52 progress_checker_ =
new QComboBox;
53 path_handler_ =
new QComboBox;
55 main_layout_->setContentsMargins(10, 10, 10, 10);
57 row_1_label_layout_->addWidget(
new QLabel(
"Controller"));
58 row_1_layout_->addWidget(controller_);
59 row_1_label_layout_->addWidget(
new QLabel(
"Planner"));
60 row_1_layout_->addWidget(planner_);
61 row_2_label_layout_->addWidget(
new QLabel(
"Goal Checker"));
62 row_2_layout_->addWidget(goal_checker_);
63 row_2_label_layout_->addWidget(
new QLabel(
"Smoother"));
64 row_2_layout_->addWidget(smoother_);
65 row_3_label_layout_->addWidget(
new QLabel(
"Progress Checker"));
66 row_3_layout_->addWidget(progress_checker_);
67 row_3_label_layout_->addWidget(
new QLabel(
"Path Handler"));
68 row_3_layout_->addWidget(path_handler_);
70 main_layout_->addLayout(row_1_label_layout_);
71 main_layout_->addLayout(row_1_layout_);
72 main_layout_->addLayout(row_2_label_layout_);
73 main_layout_->addLayout(row_2_layout_);
74 main_layout_->addLayout(row_3_label_layout_);
75 main_layout_->addLayout(row_3_layout_);
77 setLayout(main_layout_);
81 controller_, QOverload<int>::of(&QComboBox::activated),
this,
82 &Selector::setController);
85 planner_, QOverload<int>::of(&QComboBox::activated),
this,
86 &Selector::setPlanner);
89 goal_checker_, QOverload<int>::of(&QComboBox::activated),
this,
90 &Selector::setGoalChecker);
93 smoother_, QOverload<int>::of(&QComboBox::activated),
this,
94 &Selector::setSmoother);
97 progress_checker_, QOverload<int>::of(&QComboBox::activated),
this,
98 &Selector::setProgressChecker);
101 path_handler_, QOverload<int>::of(&QComboBox::activated),
this,
102 &Selector::setPathHandler);
105 Selector::~Selector()
107 if (load_plugins_thread_.joinable()) {
108 load_plugins_thread_.join();
113 void Selector::setSelection(
114 QComboBox * combo_box,
115 rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher)
118 if (combo_box->findText(
"Default") != -1) {
119 combo_box->removeItem(0);
123 if (combo_box->count() == 0) {
127 auto msg = std::make_unique<std_msgs::msg::String>();
128 msg->data = combo_box->currentText().toStdString();
130 publisher->publish(std::move(msg));
134 void Selector::setController()
136 setSelection(controller_, pub_controller_);
140 void Selector::setPlanner()
142 setSelection(planner_, pub_planner_);
146 void Selector::setGoalChecker()
148 setSelection(goal_checker_, pub_goal_checker_);
152 void Selector::setSmoother()
154 setSelection(smoother_, pub_smoother_);
157 void Selector::setProgressChecker()
159 setSelection(progress_checker_, pub_progress_checker_);
162 void Selector::setPathHandler()
164 setSelection(path_handler_, pub_path_handler_);
168 Selector::loadPlugins()
170 load_plugins_thread_ = std::thread(
172 rclcpp::Rate rate(0.2);
173 while (rclcpp::ok() && !plugins_loaded_) {
174 RCLCPP_INFO(client_node_->get_logger(),
"Trying to load plugins...");
175 nav2_rviz_plugins::pluginLoader(
176 client_node_, server_failed_,
"controller_server",
"controller_plugins", controller_);
177 nav2_rviz_plugins::pluginLoader(
178 client_node_, server_failed_,
"planner_server",
"planner_plugins", planner_);
179 nav2_rviz_plugins::pluginLoader(
180 client_node_, server_failed_,
"controller_server",
"goal_checker_plugins",
182 nav2_rviz_plugins::pluginLoader(
183 client_node_, server_failed_,
"smoother_server",
"smoother_plugins", smoother_);
184 nav2_rviz_plugins::pluginLoader(
185 client_node_, server_failed_,
"controller_server",
"progress_checker_plugins",
187 nav2_rviz_plugins::pluginLoader(
188 client_node_, server_failed_,
"controller_server",
"path_handler_plugins",
190 if (controller_->count() > 0 &&
191 planner_->count() > 0 &&
192 goal_checker_->count() > 0 &&
193 smoother_->count() > 0 &&
194 progress_checker_->count() > 0 &&
195 path_handler_->count() > 0)
197 plugins_loaded_ =
true;
199 RCLCPP_INFO(client_node_->get_logger(),
"Failed to load plugins. Retrying...");
207 #include "pluginlib/class_list_macros.hpp"