-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpanel.cpp
More file actions
420 lines (362 loc) · 14.1 KB
/
panel.cpp
File metadata and controls
420 lines (362 loc) · 14.1 KB
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
#include "panel.hpp"
#include <pluginlib/class_list_macros.hpp>
using namespace std::chrono_literals;
namespace roadmap_explorer
{
ExplorationPanel::ExplorationPanel(QWidget * parent)
: rviz_common::Panel(parent),
rb_new_session_(new QRadioButton("New exploration session", this)),
rb_continue_terminated_(new QRadioButton("Continue from previous session", this)),
rb_continue_saved_(new QRadioButton("Continue from saved session", this)),
mode_group_(new QButtonGroup(this)),
start_button_(new QPushButton("Start exploration", this)),
stop_button_(new QPushButton("Stop exploration", this)),
node_(std::make_shared<rclcpp::Node>("roadmap_explorer_rviz_panel")),
action_client_(rclcpp_action::create_client<Explore>(node_, "roadmap_explorer")),
save_map_client_(node_->create_client<roadmap_explorer_msgs::srv::SaveMapAndRoadmap>("save_map_and_roadmap"))
{
// Basic UI
auto * layout = new QVBoxLayout;
{
// Put them all into a QButtonGroup so only one is checked at a time
mode_group_->addButton(
rb_new_session_,
static_cast<int>(Explore::Goal::NEW_EXPLORATION_SESSION));
mode_group_->addButton(
rb_continue_terminated_,
static_cast<int>(Explore::Goal::CONTINUE_FROM_TERMINATED_SESSION));
mode_group_->addButton(
rb_continue_saved_,
static_cast<int>(Explore::Goal::CONTINUE_FROM_SAVED_SESSION));
mode_group_->setExclusive(true);
// Check the “New session” one by default
rb_new_session_->setChecked(true);
// Add them to the layout (stacked vertically)
layout->addWidget(rb_new_session_);
layout->addWidget(rb_continue_terminated_);
layout->addWidget(rb_continue_saved_);
// load from saved session ui. This will work only if the “Continue from saved session” is checked
{
auto * load_layout = new QHBoxLayout;
auto default_path = QString::fromStdString(
std::string(
std::getenv(
"HOME")) + "/.ros/roadmap-explorer");
load_base_path_edit_ = new QLineEdit(default_path, this);
session_name_edit_ = new QLineEdit("my_session", this);
load_layout->addWidget(new QLabel("Folder:", this));
load_layout->addWidget(load_base_path_edit_);
load_layout->addWidget(new QLabel("Session name:", this));
load_layout->addWidget(session_name_edit_);
session_name_edit_->setEnabled(false);
layout->addLayout(load_layout);
/* disabled until “continue saved session” is chosen */
load_base_path_edit_->setEnabled(false);
session_name_edit_->setEnabled(false);
connect(
rb_continue_saved_, &QRadioButton::toggled, this,
[this](bool checked) {
load_base_path_edit_->setEnabled(checked);
session_name_edit_->setEnabled(checked);
});
}
}
layout->addWidget(start_button_);
connect(start_button_, &QPushButton::clicked, this, &ExplorationPanel::onStartClicked);
layout->addWidget(stop_button_);
connect(stop_button_, &QPushButton::clicked, this, &ExplorationPanel::onStopClicked);
auto * separator = new QFrame(this);
separator->setFrameShape(QFrame::HLine);
// separator->setFrameShadow(QFrame::Sunken);
separator->setStyleSheet("border: 1px solid green;");
layout->addWidget(separator);
// save-map UI
{
auto * save_layout = new QGridLayout;
auto default_path = QString::fromStdString(
std::string(
std::getenv(
"HOME")) + "/.ros/roadmap-explorer");
save_base_path_edit_ = new QLineEdit(default_path, this);
save_map_name_edit_ = new QLineEdit("my_session", this);
save_button_ = new QPushButton("Save session", this);
save_layout->addWidget(new QLabel("Save to folder:", this), 0, 0);
save_layout->addWidget(save_base_path_edit_, 0, 1);
save_layout->addWidget(new QLabel("Session name:", this), 1, 0);
save_layout->addWidget(save_map_name_edit_, 1, 1);
save_layout->addWidget(save_button_, 2, 0, 1, 2);
layout->addLayout(save_layout);
connect(
save_button_, &QPushButton::clicked,
this, &ExplorationPanel::onSaveClicked);
}
{
// Create a horizontal layout with a label and a read‐only QLineEdit
auto * log_layout = new QHBoxLayout;
log_box_ = new QLabel("Status: Startup...", this);
log_layout->addWidget(log_box_);
layout->addLayout(log_layout);
}
setLayout(layout);
// Initial state
updateButtons(ButtonSetting::IN_PROCESS);
// Start the ROS timer for spin. We use QTimer so that the GUI updates happen on the same thread.
panel_active_ = false;
ros_timer_ = new QTimer(this);
ros_timer_->setInterval(100); // 100 ms
connect(ros_timer_, &QTimer::timeout, this, &ExplorationPanel::spinnerOnGUI);
ros_timer_->start();
}
ExplorationPanel::~ExplorationPanel()
{
// Stop the timer first to prevent any further ROS calls
if (ros_timer_) {
ros_timer_->stop();
}
// Cancel any active goals before shutting down
if (goal_handle_) {
try {
// Don't wait for response during shutdown
action_client_->async_cancel_goal(goal_handle_);
} catch (const std::exception & e) {
// Ignore exceptions during shutdown
}
goal_handle_.reset();
}
// Clean up ROS clients before the node
if (action_client_) {
action_client_.reset();
}
if (save_map_client_) {
save_map_client_.reset();
}
// Finally, clean up the node
if (node_) {
node_.reset();
}
}
void ExplorationPanel::spinnerOnGUI()
{
// Safety check: if node is being destroyed, don't proceed
if (!node_ || !rclcpp::ok()) {
return;
}
rclcpp::spin_some(node_);
if (!action_client_->wait_for_action_server(0ms)) {
updateButtons(ButtonSetting::IN_PROCESS);
setLog("Exploration action server unavailable", true);
goal_handle_.reset();
panel_active_ = false;
}
if (!save_map_client_->wait_for_service(0ms)) {
updateButtons(ButtonSetting::IN_PROCESS);
setLog("Save roadmap service unavailable", true);
goal_handle_.reset();
panel_active_ = false;
}
/**
* this is set to GOAL_INACTIVE only if panel is not active because if it's active
* then the button states are handled based on action request / results.
*/
else if (!panel_active_) {
updateButtons(ButtonSetting::GOAL_INACTIVE);
panel_active_ = true;
setLog("Exploration server active", false);
}
}
// ───── Qt slots ──────────────────────────────────────────────────────────────
void ExplorationPanel::onStartClicked()
{
if (!action_client_->wait_for_action_server(0ms)) {
setLog("Action server unavailable: cannot send goal", true);
return;
}
sendGoal();
}
void ExplorationPanel::onStopClicked()
{
cancelGoal();
}
void ExplorationPanel::onSaveClicked()
{
auto req = std::make_shared<roadmap_explorer_msgs::srv::SaveMapAndRoadmap::Request>();
req->base_path = save_base_path_edit_->text().toStdString();
req->session_name = save_map_name_edit_->text().toStdString();
save_map_client_->async_send_request(req);
}
// ───── Internal ──────────────────────────────────────────────────────────────
void ExplorationPanel::sendGoal()
{
Explore::Goal goal_msg;
goal_msg.exploration_bringup_mode = this->currentMode();
if (this->currentMode() == Explore::Goal::CONTINUE_FROM_SAVED_SESSION) {
std_msgs::msg::String base_path;
base_path.data = load_base_path_edit_->text().toStdString();
goal_msg.load_from_folder = base_path;
std_msgs::msg::String session_name;
session_name.data = session_name_edit_->text().toStdString();
goal_msg.session_name = session_name;
}
auto goal_options = rclcpp_action::Client<Explore>::SendGoalOptions();
goal_options.goal_response_callback =
std::bind(&ExplorationPanel::actionGoalResponseCallback, this, std::placeholders::_1);
goal_options.feedback_callback =
std::bind(
&ExplorationPanel::actionFeedbackCallback, this,
std::placeholders::_1, std::placeholders::_2);
goal_options.result_callback =
std::bind(&ExplorationPanel::actionResultCallback, this, std::placeholders::_1);
updateButtons(ButtonSetting::IN_PROCESS);
action_client_->async_send_goal(goal_msg, goal_options);
}
void ExplorationPanel::cancelGoal()
{
if (goal_handle_) {
updateButtons(ButtonSetting::IN_PROCESS);
action_client_->async_cancel_goal(
goal_handle_,
std::bind(&ExplorationPanel::actionCancelCallback, this, std::placeholders::_1));
} else {
updateButtons(ButtonSetting::GOAL_INACTIVE);
}
}
void ExplorationPanel::updateButtons(ButtonSetting setting)
{
if (setting == ButtonSetting::IN_PROCESS) {
rb_new_session_->setEnabled(false);
rb_continue_terminated_->setEnabled(false);
rb_continue_saved_->setEnabled(false);
// this is disabled here because of the need to disable the entire panel.
load_base_path_edit_->setEnabled(false);
session_name_edit_->setEnabled(false);
start_button_->setEnabled(false);
stop_button_->setEnabled(false);
save_base_path_edit_->setEnabled(false);
save_map_name_edit_->setEnabled(false);
save_button_->setEnabled(false);
} else {
rb_new_session_->setEnabled(setting == ButtonSetting::GOAL_INACTIVE);
rb_continue_terminated_->setEnabled(setting == ButtonSetting::GOAL_INACTIVE);
rb_continue_saved_->setEnabled(setting == ButtonSetting::GOAL_INACTIVE);
// load_base_path_edit_ and load_folder_combo_ are not enabled here because they are automatically enabled when continue from saved session is selected
start_button_->setEnabled(setting == ButtonSetting::GOAL_INACTIVE);
stop_button_->setEnabled(setting == ButtonSetting::GOAL_ACTIVE);
save_base_path_edit_->setEnabled(setting == ButtonSetting::GOAL_INACTIVE);
save_map_name_edit_->setEnabled(setting == ButtonSetting::GOAL_INACTIVE);
save_button_->setEnabled(setting == ButtonSetting::GOAL_INACTIVE);
}
}
uint16_t ExplorationPanel::currentMode() const
{
// use with the same IDs as the constants in the action:
// 0 = NEW_EXPLORATION_SESSION
// 1 = CONTINUE_FROM_TERMINATED_SESSION
// 2 = CONTINUE_FROM_SAVED_SESSION
int id = mode_group_->checkedId();
if (id < 0) {
// Fallback: if somehow none is checked (should not happen), default to 0.
RCLCPP_WARN(
node_->get_logger(),
"No exploration bringup mode was specified, resorting to new exploration session");
return Explore::Goal::NEW_EXPLORATION_SESSION;
}
return static_cast<uint16_t>(id);
}
void ExplorationPanel::setLog(const QString & text, bool error)
{
if (error) {
QString html = QString("<span style=\"color:red;\">Status: %1</span>").arg(text);
log_box_->setText(html);
// log_box_->setTextFormat(Qt::RichText);
} else {
log_box_->setText("Status: " + text);
}
}
// ───── Action callbacks ──────────────────────────────────────────────────────
void ExplorationPanel::actionGoalResponseCallback(
const GoalHandle::SharedPtr & goal_handle)
{
goal_handle_ = goal_handle;
if (!goal_handle_) {
RCLCPP_ERROR(node_->get_logger(), "Goal was rejected by action server");
updateButtons(ButtonSetting::GOAL_INACTIVE);
return;
}
updateButtons(ButtonSetting::GOAL_ACTIVE);
}
void ExplorationPanel::actionFeedbackCallback(
GoalHandle::SharedPtr,
const std::shared_ptr<const Explore::Feedback> feedback)
{
RCLCPP_INFO_THROTTLE(
node_->get_logger(), *node_->get_clock(), 2000,
"Exploring for %.1fs, explored frontier count: %zu",
rclcpp::Duration(feedback->exploration_time).seconds(),
feedback->current_frontier.size());
}
void ExplorationPanel::actionResultCallback(const GoalHandle::WrappedResult & result)
{
switch (result.code) {
case rclcpp_action::ResultCode::SUCCEEDED:
RCLCPP_INFO(
node_->get_logger(), "Exploration finished successfully: %s",
result.result->success ? "true" : "false");
break;
case rclcpp_action::ResultCode::ABORTED:
RCLCPP_WARN(
node_->get_logger(),
"Exploration aborted (error code %u)", result.result->error_code);
break;
case rclcpp_action::ResultCode::CANCELED:
RCLCPP_INFO(node_->get_logger(), "Exploration goal canceled");
break;
default:
RCLCPP_ERROR(node_->get_logger(), "Unknown result code");
break;
}
goal_handle_.reset();
updateButtons(ButtonSetting::GOAL_INACTIVE);
rb_continue_terminated_->setChecked(true);
}
void ExplorationPanel::actionCancelCallback(
std::shared_ptr<action_msgs::srv::CancelGoal_Response> response)
{
using CancelResp = action_msgs::srv::CancelGoal_Response;
if (!goal_handle_) {
RCLCPP_INFO(node_->get_logger(), "Cancel response received, but no goal was active.");
return;
}
switch (response->return_code) {
case CancelResp::ERROR_NONE:
RCLCPP_INFO(
node_->get_logger(),
"Cancel request was ACCEPTED by the action server.");
// Now that cancellation succeeded, switch into GOAL_INACTIVE.
updateButtons(ButtonSetting::IN_PROCESS);
break;
case CancelResp::ERROR_REJECTED:
RCLCPP_WARN(
node_->get_logger(),
"Cancel request was REJECTED by the action server.");
// Keep GOAL_ACTIVE (since the server refused to cancel).
updateButtons(ButtonSetting::GOAL_ACTIVE);
break;
case CancelResp::ERROR_GOAL_TERMINATED:
updateButtons(ButtonSetting::GOAL_INACTIVE);
break;
case CancelResp::ERROR_UNKNOWN_GOAL_ID:
updateButtons(ButtonSetting::GOAL_INACTIVE);
break;
default:
RCLCPP_ERROR(
node_->get_logger(),
"Unexpected return_code %u from cancel service.",
response->return_code);
// Fallback: assume goal is still active
updateButtons(ButtonSetting::GOAL_ACTIVE);
break;
}
}
} // namespace roadmap_explorer
// ─── Pluginlib export ─────────────────────────────────────────────────────────
PLUGINLIB_EXPORT_CLASS(roadmap_explorer::ExplorationPanel, rviz_common::Panel)