Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update rviz plugin #162

Merged
merged 3 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions scenario_execution_rviz/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ The following image shows a snapshot of the rviz plugin during a run of the [exa

![tree_example.png](../docs/images/scenario_view_example.png)

### Known Issues

The Scenario View panel can not display the scenario's behavior tree if initialized while the robot navigation is already running.
The plugin subscribes to `/scenario_execution/snapshots` by default. Double-click on the header to modify the topic.

### Icon Licence

Expand Down
55 changes: 52 additions & 3 deletions scenario_execution_rviz/src/scenario_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@

#include "scenario_view.h"
#include "indicator_widget.h"

#include <QHeaderView>
#include <QInputDialog>
using std::placeholders::_1;

namespace scenario_execution_rviz {

ScenarioView::ScenarioView(QWidget *parent) : rviz_common::Panel(parent) {
ScenarioView::ScenarioView(QWidget *parent)
: rviz_common::Panel(parent), mInitTimer(this) {
mInitTimer.setSingleShot(true);
connect(&mInitTimer, &QTimer::timeout, this, &ScenarioView::setupConnection);

QVBoxLayout *layout = new QVBoxLayout;

QFormLayout *formLayout = new QFormLayout;
Expand Down Expand Up @@ -51,14 +56,58 @@ ScenarioView::ScenarioView(QWidget *parent) : rviz_common::Panel(parent) {
SLOT(handleItemCollapsed(QTreeWidgetItem *)));
connect(mScenarioView, SIGNAL(itemExpanded(QTreeWidgetItem *)), this,
SLOT(handleItemExpanded(QTreeWidgetItem *)));
connect(mScenarioView->header(), SIGNAL(sectionDoubleClicked(int)), this,
SLOT(onHeaderDoubleClicked(int)));
}

void ScenarioView::load(const rviz_common::Config &config) {
rviz_common::Panel::load(config);
QString topic;
if (config.mapGetString("snapshot_topic", &topic)) {

if (mSnapshotTopic != topic) {
mInitTimer.stop(); // stop init trigger
mSnapshotTopic = topic;
setupConnection();
}
}
}

void ScenarioView::save(rviz_common::Config config) const {
rviz_common::Panel::save(config);
config.mapSetValue("snapshot_topic", mSnapshotTopic);
}

void ScenarioView::onHeaderDoubleClicked(int idx) {
(void)idx;
bool ok;
QString text = QInputDialog::getText(this, "Configuration", "Snapshot Topic",
QLineEdit::Normal, mSnapshotTopic, &ok);
if (ok && !text.isEmpty() && (mSnapshotTopic != text)) {
mSnapshotTopic = text;
setupConnection();
Q_EMIT configChanged();
}
}

void ScenarioView::onInitialize() {
_node = getDisplayContext()->getRosNodeAbstraction().lock()->get_raw_node();
mSnapshotTopic = "/scenario_execution/snapshots";
mInitTimer.start(250); // only executed if no config-load gets triggered.
}

void ScenarioView::setupConnection() {
if (mSnapshotTopic.isEmpty()) {
return;
}
if (mBehaviorTreeSubscriber) {
mBehaviorTreeSubscriber.reset();
}

qDebug() << "Subscribing to" << mSnapshotTopic;
mBehaviorTreeSubscriber =
_node->create_subscription<py_trees_ros_interfaces::msg::BehaviourTree>(
"/scenario_execution/snapshots", 10,
mSnapshotTopic.toStdString().c_str(), 10,
std::bind(&ScenarioView::behaviorTreeChanged, this, _1));
}

Expand Down
10 changes: 10 additions & 0 deletions scenario_execution_rviz/src/scenario_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,17 @@ class ScenarioView : public rviz_common::Panel {
public:
ScenarioView(QWidget *parent = 0);

virtual void load(const rviz_common::Config &config) override;
virtual void save(rviz_common::Config config) const override;

Q_SIGNALS:
void doubleClickedA(int index);

protected Q_SLOTS:
void handleItemCollapsed(QTreeWidgetItem *collapsedItem);
void handleItemExpanded(QTreeWidgetItem *expandedItem);
void onHeaderDoubleClicked(int idx);
void setupConnection();

protected:
virtual void onInitialize() override;
Expand All @@ -93,6 +101,8 @@ protected Q_SLOTS:
TreeModel *mTreeModel;
bool treeWidgetBuilt = false;
QMap<QString, bool> *collapsedStates;
QString mSnapshotTopic;
QTimer mInitTimer;

// icons
QIcon runningIcon = QIcon(":/icons/chevron-right-o.png");
Expand Down