Skip to content

Commit ce644b6

Browse files
authored
feat(bag_time_manager_rviz_plugin): add bag time manager rviz plugin (#1776)
* feat(bag_timemanager): add bag time manager rviz plugin Signed-off-by: tanaka3 <ttatcoder@outlook.jp> * fix: fix change Signed-off-by: tanaka3 <ttatcoder@outlook.jp> Signed-off-by: tanaka3 <ttatcoder@outlook.jp>
1 parent 2f379a6 commit ce644b6

10 files changed

+280
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
project(bag_time_manager_rviz_plugin)
3+
4+
find_package(autoware_cmake REQUIRED)
5+
autoware_package()
6+
7+
find_package(Qt5 ${rviz_QT_VERSION} EXACT REQUIRED Core Widgets)
8+
set(QT_LIBRARIES Qt5::Widgets)
9+
set(CMAKE_AUTOMOC ON)
10+
set(CMAKE_INCLUDE_CURRENT_DIR ON)
11+
add_definitions(-DQT_NO_KEYWORDS)
12+
13+
ament_auto_add_library(${PROJECT_NAME} SHARED
14+
src/bag_time_manager_panel.cpp
15+
)
16+
17+
target_link_libraries(${PROJECT_NAME}
18+
${QT_LIBRARIES}
19+
)
20+
21+
# Export the plugin to be imported by rviz2
22+
pluginlib_export_plugin_description_file(rviz_common plugins/plugin_description.xml)
23+
24+
ament_auto_package(
25+
INSTALL_TO_SHARE
26+
icons
27+
plugins
28+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# bag_time_manager_rviz_plugin
2+
3+
## Purpose
4+
5+
This plugin allows publishing and controlling the ros bag time.
6+
7+
## Output
8+
9+
tbd.
10+
11+
## HowToUse
12+
13+
1. Start rviz and select panels/Add new panel.
14+
15+
![select_panel](./images/select_panels.png)
16+
17+
2. Select BagTimeManagerPanel and press OK.
18+
19+
![select_manager_plugin](./images/add_bag_time_manager_panel.png)
20+
21+
3. See bag_time_manager_rviz_plugin/BagTimeManagerPanel is added.
22+
23+
![manager_plugin](./images/bag_time_manager_panel.png)
24+
25+
- Pause/Resume: pause/resume the clock.
26+
- ApplyRate: apply rate of the clock.
Loading
Loading
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0"?>
2+
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
3+
<package format="3">
4+
<name>bag_time_manager_rviz_plugin</name>
5+
<version>0.0.1</version>
6+
<description>Rviz plugin to publish and control the ros bag</description>
7+
<maintainer email="taiki.tanaka@tier4.jp">Taiki Tanaka</maintainer>
8+
<license>Apache License 2.0</license>
9+
10+
<buildtool_depend>ament_cmake_auto</buildtool_depend>
11+
12+
<build_depend>autoware_cmake</build_depend>
13+
<depend>libqt5-core</depend>
14+
<depend>libqt5-gui</depend>
15+
<depend>libqt5-widgets</depend>
16+
<depend>qtbase5-dev</depend>
17+
<depend>rclcpp</depend>
18+
<depend>rosbag2_interfaces</depend>
19+
<depend>rviz_common</depend>
20+
21+
<test_depend>ament_lint_auto</test_depend>
22+
<test_depend>autoware_lint_common</test_depend>
23+
24+
<export>
25+
<build_type>ament_cmake</build_type>
26+
<rviz plugin="${prefix}/plugins/plugin_description.xml"/>
27+
</export>
28+
</package>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<library path="bag_time_manager_rviz_plugin">
2+
3+
<class
4+
type="rviz_plugins::BagTimeManagerPanel"
5+
base_class_type="rviz_common::Panel">
6+
<description>Panel that publishes a service to modify its speed.</description>
7+
</class>
8+
9+
</library>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
//
2+
// Copyright 2022 Tier IV, Inc. All rights reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
#include "bag_time_manager_panel.hpp"
18+
19+
#include <qt5/QtWidgets/QHBoxLayout>
20+
#include <qt5/QtWidgets/QLabel>
21+
#include <qt5/QtWidgets/QWidget>
22+
#include <rviz_common/display_context.hpp>
23+
24+
namespace rviz_plugins
25+
{
26+
BagTimeManagerPanel::BagTimeManagerPanel(QWidget * parent) : rviz_common::Panel(parent)
27+
{
28+
// pause / resume
29+
{
30+
pause_button_ = new QPushButton("Pause");
31+
pause_button_->setToolTip("Pause/Resume ROS time.");
32+
pause_button_->setStyleSheet("background-color: #00FF00;");
33+
pause_button_->setCheckable(true);
34+
}
35+
36+
// apply
37+
{
38+
apply_rate_button_ = new QPushButton("ApplyRate");
39+
apply_rate_button_->setToolTip("control ROS time rate.");
40+
}
41+
42+
// combo
43+
{
44+
rate_label_ = new QLabel("Rate:");
45+
rate_label_->setAlignment(Qt::AlignCenter);
46+
rate_combo_ = new QComboBox();
47+
rate_combo_->addItems({"0.01", "0.1", "0.5", "1.0", "2.0", "5.0", "10.0"});
48+
rate_combo_->setCurrentText(QString("1.0"));
49+
time_label_ = new QLabel("X real time ");
50+
rate_label_->setAlignment(Qt::AlignCenter);
51+
}
52+
53+
auto * layout = new QHBoxLayout();
54+
layout->addWidget(pause_button_);
55+
layout->addWidget(apply_rate_button_);
56+
layout->addWidget(rate_label_);
57+
layout->addWidget(rate_combo_);
58+
layout->addWidget(time_label_);
59+
setLayout(layout);
60+
61+
connect(pause_button_, SIGNAL(clicked()), this, SLOT(onPauseClicked()));
62+
connect(apply_rate_button_, SIGNAL(clicked()), this, SLOT(onApplyRateClicked()));
63+
connect(rate_combo_, SIGNAL(currentIndexChanged(int)), this, SLOT(onRateChanged()));
64+
}
65+
66+
void BagTimeManagerPanel::onInitialize()
67+
{
68+
raw_node_ = this->getDisplayContext()->getRosNodeAbstraction().lock()->get_raw_node();
69+
client_pause_ =
70+
raw_node_->create_client<Pause>("/rosbag2_player/pause", rmw_qos_profile_services_default);
71+
client_resume_ =
72+
raw_node_->create_client<Resume>("/rosbag2_player/resume", rmw_qos_profile_services_default);
73+
client_set_rate_ =
74+
raw_node_->create_client<SetRate>("/rosbag2_player/set_rate", rmw_qos_profile_services_default);
75+
}
76+
77+
void BagTimeManagerPanel::onPauseClicked()
78+
{
79+
if (current_state_ == STATE::PAUSE) {
80+
// do resume
81+
current_state_ = STATE::RESUME;
82+
pause_button_->setText(QString::fromStdString("Resume"));
83+
// green
84+
pause_button_->setStyleSheet("background-color: #00FF00;");
85+
auto req = std::make_shared<Resume::Request>();
86+
client_resume_->async_send_request(
87+
req, [this]([[maybe_unused]] rclcpp::Client<Resume>::SharedFuture result) {});
88+
} else {
89+
// do pause
90+
current_state_ = STATE::PAUSE;
91+
pause_button_->setText(QString::fromStdString("Pause"));
92+
// red
93+
pause_button_->setStyleSheet("background-color: #FF0000;");
94+
auto req = std::make_shared<Pause::Request>();
95+
client_pause_->async_send_request(
96+
req, [this]([[maybe_unused]] rclcpp::Client<Pause>::SharedFuture result) {});
97+
}
98+
}
99+
100+
void BagTimeManagerPanel::onApplyRateClicked()
101+
{
102+
auto request = std::make_shared<SetRate::Request>();
103+
request->rate = std::stod(rate_combo_->currentText().toStdString());
104+
client_set_rate_->async_send_request(
105+
request, [this, request](rclcpp::Client<SetRate>::SharedFuture result) {
106+
const auto & response = result.get();
107+
if (response->success) {
108+
RCLCPP_INFO(raw_node_->get_logger(), "set ros bag rate %f x real time", request->rate);
109+
} else {
110+
RCLCPP_WARN(raw_node_->get_logger(), "service failed");
111+
}
112+
});
113+
}
114+
} // namespace rviz_plugins
115+
116+
#include <pluginlib/class_list_macros.hpp>
117+
PLUGINLIB_EXPORT_CLASS(rviz_plugins::BagTimeManagerPanel, rviz_common::Panel)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//
2+
// Copyright 2022 Tier IV, Inc. All rights reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
#ifndef BAG_TIME_MANAGER_PANEL_HPP_
18+
#define BAG_TIME_MANAGER_PANEL_HPP_
19+
20+
#include <qt5/QtWidgets/QComboBox>
21+
#include <qt5/QtWidgets/QLabel>
22+
#include <qt5/QtWidgets/QPushButton>
23+
#include <rclcpp/rclcpp.hpp>
24+
#include <rosbag2_interfaces/srv/pause.hpp>
25+
#include <rosbag2_interfaces/srv/resume.hpp>
26+
#include <rosbag2_interfaces/srv/set_rate.hpp>
27+
#include <rviz_common/panel.hpp>
28+
29+
#include <memory>
30+
#include <string>
31+
32+
namespace rviz_plugins
33+
{
34+
using rosbag2_interfaces::srv::Pause;
35+
using rosbag2_interfaces::srv::Resume;
36+
using rosbag2_interfaces::srv::SetRate;
37+
class BagTimeManagerPanel : public rviz_common::Panel
38+
{
39+
Q_OBJECT
40+
public:
41+
explicit BagTimeManagerPanel(QWidget * parent = nullptr);
42+
void onInitialize() override;
43+
44+
protected Q_SLOTS:
45+
/// @brief callback for when the publishing rate is changed
46+
void onRateChanged() {}
47+
/// @brief callback for when the step button is clicked
48+
void onPauseClicked();
49+
void onApplyRateClicked();
50+
51+
protected:
52+
// ROS
53+
rclcpp::Node::SharedPtr raw_node_;
54+
rclcpp::Client<Pause>::SharedPtr client_pause_;
55+
rclcpp::Client<Resume>::SharedPtr client_resume_;
56+
rclcpp::Client<SetRate>::SharedPtr client_set_rate_;
57+
58+
// GUI
59+
QPushButton * pause_button_;
60+
QPushButton * apply_rate_button_;
61+
QLabel * rate_label_;
62+
QLabel * time_label_;
63+
QComboBox * rate_combo_;
64+
65+
private:
66+
enum STATE { PAUSE, RESUME };
67+
STATE current_state_{RESUME};
68+
};
69+
70+
} // namespace rviz_plugins
71+
72+
#endif // BAG_TIME_MANAGER_PANEL_HPP_

0 commit comments

Comments
 (0)