-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathdocking_panel.cpp
499 lines (442 loc) · 16.3 KB
/
docking_panel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// Copyright (c) 2024 Alberto J. Tudela Roldán
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// QT
#include <QLineEdit>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
// C++
#include <chrono>
#include <memory>
#include <sstream>
#include <string>
#include <rclcpp/rclcpp.hpp>
#include <rviz_common/display_context.hpp>
#include "nav2_util/geometry_utils.hpp"
#include "nav2_rviz_plugins/docking_panel.hpp"
#include "nav2_rviz_plugins/utils.hpp"
using namespace std::chrono_literals;
namespace nav2_rviz_plugins
{
DockingPanel::DockingPanel(QWidget * parent)
: Panel(parent),
server_timeout_(100)
{
client_node_ = std::make_shared<rclcpp::Node>("nav2_rviz_docking_panel_node");
main_layout_ = new QVBoxLayout;
info_layout_ = new QHBoxLayout;
feedback_layout_ = new QVBoxLayout;
dock_id_layout_ = new QHBoxLayout;
dock_type_layout_ = new QHBoxLayout;
dock_pose_layout_ = new QHBoxLayout;
nav_stage_layout_ = new QHBoxLayout;
dock_type_ = new QComboBox;
docking_button_ = new QPushButton("Dock robot");
undocking_button_ = new QPushButton("Undock robot");
docking_goal_status_indicator_ = new QLabel;
docking_feedback_indicator_ = new QLabel;
docking_result_indicator_ = new QLabel;
use_dock_id_checkbox_ = new QCheckBox;
nav_to_staging_checkbox_ = new QCheckBox;
dock_id_ = new QLineEdit;
dock_pose_x_ = new QLineEdit;
dock_pose_y_ = new QLineEdit;
dock_pose_yaw_ = new QLineEdit;
docking_button_->setEnabled(false);
undocking_button_->setEnabled(false);
use_dock_id_checkbox_->setEnabled(false);
nav_to_staging_checkbox_->setEnabled(false);
dock_id_->setEnabled(false);
dock_pose_x_->setEnabled(false);
dock_pose_y_->setEnabled(false);
dock_pose_yaw_->setEnabled(false);
docking_goal_status_indicator_->setText(nav2_rviz_plugins::getGoalStatusLabel());
docking_feedback_indicator_->setText(getDockFeedbackLabel());
docking_goal_status_indicator_->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
docking_feedback_indicator_->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
nav_to_staging_checkbox_->setFixedWidth(150);
info_layout_->addWidget(docking_goal_status_indicator_);
info_layout_->addWidget(docking_result_indicator_);
feedback_layout_->addWidget(docking_feedback_indicator_);
dock_id_layout_->addWidget(new QLabel("Dock id"));
dock_id_layout_->addWidget(use_dock_id_checkbox_);
dock_id_layout_->addWidget(dock_id_);
dock_type_layout_->addWidget(new QLabel("Dock type"));
dock_type_layout_->addWidget(dock_type_);
dock_pose_layout_->addWidget(new QLabel("Dock pose {X"));
dock_pose_layout_->addWidget(dock_pose_x_);
dock_pose_layout_->addWidget(new QLabel("Y"));
dock_pose_layout_->addWidget(dock_pose_y_);
dock_pose_layout_->addWidget(new QLabel("θ"));
dock_pose_layout_->addWidget(dock_pose_yaw_);
dock_pose_layout_->addWidget(new QLabel("}"));
nav_stage_layout_->addWidget(nav_to_staging_checkbox_);
nav_stage_layout_->addWidget(new QLabel("Navigate to staging pose"));
main_layout_->setContentsMargins(10, 10, 10, 10);
main_layout_->addLayout(info_layout_);
main_layout_->addLayout(feedback_layout_);
main_layout_->addLayout(nav_stage_layout_);
main_layout_->addLayout(dock_id_layout_);
main_layout_->addLayout(dock_type_layout_);
main_layout_->addLayout(dock_pose_layout_);
main_layout_->addWidget(docking_button_);
main_layout_->addWidget(undocking_button_);
setLayout(main_layout_);
timer_.start(200, this);
dock_client_ = rclcpp_action::create_client<Dock>(client_node_, "dock_robot");
undock_client_ = rclcpp_action::create_client<Undock>(client_node_, "undock_robot");
// Conect buttons with functions
QObject::connect(docking_button_, SIGNAL(clicked()), this, SLOT(onDockingButtonPressed()));
QObject::connect(undocking_button_, SIGNAL(clicked()), this, SLOT(onUndockingButtonPressed()));
QObject::connect(
use_dock_id_checkbox_, &QCheckBox::stateChanged, this, &DockingPanel::dockIdCheckbox);
}
void DockingPanel::onInitialize()
{
node_ptr_ = getDisplayContext()->getRosNodeAbstraction().lock();
if (node_ptr_ == nullptr) {
// The node no longer exists, so just don't initialize
RCLCPP_ERROR(
rclcpp::get_logger("docking_panel"),
"Underlying ROS node no longer exists, initialization failed");
return;
}
rclcpp::Node::SharedPtr node = node_ptr_->get_raw_node();
// Create action feedback subscriber
docking_feedback_sub_ = node->create_subscription<Dock::Impl::FeedbackMessage>(
"dock_robot/_action/feedback",
rclcpp::SystemDefaultsQoS(),
[this](const Dock::Impl::FeedbackMessage::SharedPtr msg) {
docking_feedback_indicator_->setText(getDockFeedbackLabel(msg->feedback));
docking_button_->setText("Cancel docking");
undocking_button_->setEnabled(false);
docking_in_progress_ = true;
});
undocking_feedback_sub_ = node->create_subscription<Undock::Impl::FeedbackMessage>(
"undock_robot/_action/feedback",
rclcpp::SystemDefaultsQoS(),
[this](const Undock::Impl::FeedbackMessage::SharedPtr /*msg*/) {
docking_button_->setEnabled(false);
undocking_button_->setText("Cancel undocking");
undocking_in_progress_ = true;
});
// Create action goal status subscribers
docking_goal_status_sub_ = node->create_subscription<action_msgs::msg::GoalStatusArray>(
"dock_robot/_action/status",
rclcpp::SystemDefaultsQoS(),
[this](const action_msgs::msg::GoalStatusArray::SharedPtr msg) {
docking_goal_status_indicator_->setText(
nav2_rviz_plugins::getGoalStatusLabel("Feedback", msg->status_list.back().status));
if (msg->status_list.back().status != action_msgs::msg::GoalStatus::STATUS_EXECUTING) {
docking_button_->setText("Dock robot");
undocking_button_->setEnabled(true);
docking_in_progress_ = false;
}
// Reset values when action is completed
if (msg->status_list.back().status == action_msgs::msg::GoalStatus::STATUS_SUCCEEDED) {
docking_feedback_indicator_->setText(getDockFeedbackLabel());
}
});
undocking_goal_status_sub_ = node->create_subscription<action_msgs::msg::GoalStatusArray>(
"undock_robot/_action/status",
rclcpp::SystemDefaultsQoS(),
[this](const action_msgs::msg::GoalStatusArray::SharedPtr msg) {
docking_goal_status_indicator_->setText(
nav2_rviz_plugins::getGoalStatusLabel("Feedback", msg->status_list.back().status));
if (msg->status_list.back().status != action_msgs::msg::GoalStatus::STATUS_EXECUTING) {
docking_button_->setEnabled(true);
undocking_button_->setText("Undock robot");
undocking_in_progress_ = false;
}
});
}
DockingPanel::~DockingPanel()
{
}
void DockingPanel::onDockingButtonPressed()
{
if (!docking_in_progress_) {
startDocking();
} else {
cancelDocking();
}
}
void DockingPanel::onUndockingButtonPressed()
{
if (!undocking_in_progress_) {
startUndocking();
} else {
cancelUndocking();
}
}
void DockingPanel::startDocking()
{
auto is_action_server_ready =
dock_client_->wait_for_action_server(std::chrono::seconds(5));
if (!is_action_server_ready) {
RCLCPP_ERROR(client_node_->get_logger(), "dock_robot action server is not available.");
return;
}
QComboBox * combo_box = dock_type_;
// If "default" option is selected, it gets removed and the next item is selected
if (combo_box->findText("Default") != -1) {
combo_box->removeItem(0);
}
// If there are no plugins available, return
if (combo_box->count() == 0) {
return;
}
// Send the goal to the action server
auto goal_msg = Dock::Goal();
goal_msg.use_dock_id = use_dock_id_;
goal_msg.navigate_to_staging_pose = nav_to_staging_checkbox_->isChecked();
if (use_dock_id_) {
if (dock_id_->text().isEmpty()) {
RCLCPP_ERROR(client_node_->get_logger(), "Dock id is empty.");
return;
}
goal_msg.dock_id = dock_id_->text().toStdString();
RCLCPP_INFO(
client_node_->get_logger(), "DockRobot will be called using dock id: %s",
goal_msg.dock_id.c_str());
} else {
if (dock_pose_x_->text().isEmpty() || dock_pose_y_->text().isEmpty() ||
dock_pose_yaw_->text().isEmpty())
{
RCLCPP_ERROR(client_node_->get_logger(), "Dock pose is empty.");
return;
}
goal_msg.dock_pose.header.frame_id = "map";
goal_msg.dock_pose.header.stamp = client_node_->now();
goal_msg.dock_pose.pose.position.x = dock_pose_x_->text().toDouble();
goal_msg.dock_pose.pose.position.y = dock_pose_y_->text().toDouble();
goal_msg.dock_pose.pose.orientation = nav2_util::geometry_utils::orientationAroundZAxis(
dock_pose_yaw_->text().toDouble());
goal_msg.dock_type = combo_box->currentText().toStdString();
RCLCPP_INFO(
client_node_->get_logger(), "DockRobot will be called using dock pose: (%f, %f) and type: %s",
goal_msg.dock_pose.pose.position.x, goal_msg.dock_pose.pose.position.y,
goal_msg.dock_type.c_str());
}
// Enable result awareness by providing an empty lambda function
auto send_goal_options = rclcpp_action::Client<Dock>::SendGoalOptions();
send_goal_options.result_callback = [this](const DockGoalHandle::WrappedResult & result) {
dock_goal_handle_.reset();
if (result.result->success) {
docking_result_indicator_->setText("");
} else {
docking_result_indicator_->setText(
QString(std::string("(" + dockErrorToString(result.result->error_code) + ")").c_str()));
}
};
auto future_goal_handle = dock_client_->async_send_goal(goal_msg, send_goal_options);
if (rclcpp::spin_until_future_complete(client_node_, future_goal_handle, server_timeout_) !=
rclcpp::FutureReturnCode::SUCCESS)
{
RCLCPP_ERROR(client_node_->get_logger(), "Send goal call failed");
return;
}
// Get the goal handle and save so that we can check on completion in the timer callback
dock_goal_handle_ = future_goal_handle.get();
if (!dock_goal_handle_) {
RCLCPP_ERROR(client_node_->get_logger(), "Goal was rejected by server");
return;
}
timer_.start(200, this);
}
void DockingPanel::startUndocking()
{
auto is_action_server_ready =
undock_client_->wait_for_action_server(std::chrono::seconds(5));
if (!is_action_server_ready) {
RCLCPP_ERROR(client_node_->get_logger(), "undock_robot action server is not available.");
return;
}
QComboBox * combo_box = dock_type_;
// If "default" option is selected, it gets removed and the next item is selected
if (combo_box->findText("Default") != -1) {
combo_box->removeItem(0);
}
// If there are no plugins available, return
if (combo_box->count() == 0) {
return;
}
// Send the goal to the action server
auto goal_msg = Undock::Goal();
goal_msg.dock_type = combo_box->currentText().toStdString();
RCLCPP_INFO(
client_node_->get_logger(), "UndockRobot will be called using dock type: %s",
goal_msg.dock_type.c_str());
// Enable result awareness by providing an empty lambda function
auto send_goal_options = rclcpp_action::Client<Undock>::SendGoalOptions();
send_goal_options.result_callback = [this](const UndockGoalHandle::WrappedResult & result) {
undock_goal_handle_.reset();
if (result.result->success) {
docking_result_indicator_->setText("");
} else {
docking_result_indicator_->setText(
QString(std::string("(" + dockErrorToString(result.result->error_code) + ")").c_str()));
}
};
auto future_goal_handle = undock_client_->async_send_goal(goal_msg, send_goal_options);
if (rclcpp::spin_until_future_complete(client_node_, future_goal_handle, server_timeout_) !=
rclcpp::FutureReturnCode::SUCCESS)
{
RCLCPP_ERROR(client_node_->get_logger(), "Send goal call failed");
return;
}
// Get the goal handle and save so that we can check on completion in the timer callback
undock_goal_handle_ = future_goal_handle.get();
if (!undock_goal_handle_) {
RCLCPP_ERROR(client_node_->get_logger(), "Goal was rejected by server");
return;
}
timer_.start(200, this);
}
void DockingPanel::dockIdCheckbox()
{
if (use_dock_id_checkbox_->isChecked()) {
use_dock_id_ = true;
dock_id_->setEnabled(true);
dock_pose_x_->setEnabled(false);
dock_pose_y_->setEnabled(false);
dock_pose_yaw_->setEnabled(false);
} else {
use_dock_id_ = false;
dock_id_->setEnabled(false);
dock_pose_x_->setEnabled(true);
dock_pose_y_->setEnabled(true);
dock_pose_yaw_->setEnabled(true);
}
}
void DockingPanel::cancelDocking()
{
if (dock_goal_handle_) {
auto future_cancel = dock_client_->async_cancel_goal(dock_goal_handle_);
if (rclcpp::spin_until_future_complete(client_node_, future_cancel, server_timeout_) !=
rclcpp::FutureReturnCode::SUCCESS)
{
RCLCPP_ERROR(client_node_->get_logger(), "Failed to cancel goal");
} else {
dock_goal_handle_.reset();
}
}
}
void DockingPanel::cancelUndocking()
{
if (undock_goal_handle_) {
auto future_cancel = undock_client_->async_cancel_goal(undock_goal_handle_);
if (rclcpp::spin_until_future_complete(client_node_, future_cancel, server_timeout_) !=
rclcpp::FutureReturnCode::SUCCESS)
{
RCLCPP_ERROR(client_node_->get_logger(), "Failed to cancel goal");
} else {
undock_goal_handle_.reset();
}
}
}
void DockingPanel::timerEvent(QTimerEvent * event)
{
if (event->timerId() == timer_.timerId()) {
if (!plugins_loaded_) {
nav2_rviz_plugins::pluginLoader(
client_node_, server_failed_, "docking_server", "dock_plugins", dock_type_);
plugins_loaded_ = true;
docking_button_->setEnabled(true);
undocking_button_->setEnabled(true);
use_dock_id_checkbox_->setEnabled(true);
use_dock_id_checkbox_->setChecked(true);
nav_to_staging_checkbox_->setEnabled(true);
nav_to_staging_checkbox_->setChecked(true);
dock_id_->setEnabled(true);
}
// Restart the timer if the one of the server fails
if (server_failed_ && !tried_once_) {
RCLCPP_INFO(client_node_->get_logger(), "Retrying to connect to the failed server.");
server_failed_ = false;
plugins_loaded_ = false;
tried_once_ = true;
timer_.start(200, this);
return;
}
timer_.stop();
}
}
inline QString DockingPanel::getDockFeedbackLabel(Dock::Feedback msg)
{
return QString(std::string("<table>" + toLabel(msg) + "</table>").c_str());
}
template<typename T>
inline std::string DockingPanel::toLabel(T & msg)
{
return std::string(
"</td></tr><tr><td width=150>State:</td><td>" +
dockStateToString(msg.state) +
"</td></tr><tr><td width=150>Time taken:</td><td>" +
toString(rclcpp::Duration(msg.docking_time).seconds(), 0) + " s"
"</td></tr><tr><td width=150>Retries:</td><td>" +
std::to_string(msg.num_retries) +
"</td></tr>");
}
inline std::string DockingPanel::toString(double val, int precision)
{
std::ostringstream out;
out.precision(precision);
out << std::fixed << val;
return out.str();
}
inline std::string DockingPanel::dockStateToString(int16_t state)
{
switch (state) {
case 0:
return "none";
case 1:
return "nav. to staging pose";
case 2:
return "initial perception";
case 3:
return "controlling";
case 4:
return "wait for charge";
case 5:
return "retry";
default:
return "none";
}
}
inline std::string DockingPanel::dockErrorToString(int16_t error_code)
{
switch (error_code) {
case 0:
return "none";
case 901:
return "dock not in database";
case 902:
return "dock not valid";
case 903:
return "failed to stage";
case 904:
return "failed to detect dock";
case 905:
return "failed to control";
case 906:
return "failed to charge";
case 999:
default:
return "unknown";
}
}
} // namespace nav2_rviz_plugins
#include <pluginlib/class_list_macros.hpp> // NOLINT
PLUGINLIB_EXPORT_CLASS(nav2_rviz_plugins::DockingPanel, rviz_common::Panel)