|
| 1 | +#!/bin/env python3 |
| 2 | + |
| 3 | +# Copyright 2024 TIER IV, Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | + |
| 18 | +import sys |
| 19 | +import time |
| 20 | + |
| 21 | +from PyQt5 import QtCore |
| 22 | +from PyQt5.QtWidgets import QApplication |
| 23 | +from PyQt5.QtWidgets import QGridLayout |
| 24 | +from PyQt5.QtWidgets import QMainWindow |
| 25 | +from PyQt5.QtWidgets import QPushButton |
| 26 | +from PyQt5.QtWidgets import QSizePolicy |
| 27 | +from PyQt5.QtWidgets import QSlider |
| 28 | +from PyQt5.QtWidgets import QWidget |
| 29 | +from autoware_auto_planning_msgs.msg import Trajectory |
| 30 | +import rclpy |
| 31 | +from rclpy.node import Node |
| 32 | +from rclpy.qos import QoSDurabilityPolicy |
| 33 | +from rclpy.qos import QoSProfile |
| 34 | +from std_msgs.msg import Bool |
| 35 | +from std_msgs.msg import Int32 |
| 36 | + |
| 37 | + |
| 38 | +class CenterlineUpdaterWidget(QMainWindow): |
| 39 | + def __init__(self): |
| 40 | + super(self.__class__, self).__init__() |
| 41 | + self.setupUI() |
| 42 | + |
| 43 | + def setupUI(self): |
| 44 | + self.setObjectName("MainWindow") |
| 45 | + self.resize(480, 120) |
| 46 | + self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint) |
| 47 | + |
| 48 | + central_widget = QWidget(self) |
| 49 | + central_widget.setObjectName("central_widget") |
| 50 | + |
| 51 | + self.grid_layout = QGridLayout(central_widget) |
| 52 | + self.grid_layout.setContentsMargins(10, 10, 10, 10) |
| 53 | + self.grid_layout.setObjectName("grid_layout") |
| 54 | + |
| 55 | + # button to update the trajectory's start and end index |
| 56 | + self.update_button = QPushButton("update slider") |
| 57 | + self.update_button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) |
| 58 | + self.update_button.clicked.connect(self.onUpdateButton) |
| 59 | + |
| 60 | + # button to reset the trajectory's start and end index |
| 61 | + self.reset_button = QPushButton("reset") |
| 62 | + self.reset_button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) |
| 63 | + self.reset_button.clicked.connect(self.onResetButton) |
| 64 | + |
| 65 | + # button to save map |
| 66 | + self.save_map_button = QPushButton("save map") |
| 67 | + self.save_map_button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) |
| 68 | + |
| 69 | + # slide of the trajectory's start and end index |
| 70 | + self.traj_start_index_slider = QSlider(QtCore.Qt.Horizontal) |
| 71 | + self.traj_end_index_slider = QSlider(QtCore.Qt.Horizontal) |
| 72 | + self.min_traj_start_index = 0 |
| 73 | + self.max_traj_end_index = None |
| 74 | + |
| 75 | + # set layout |
| 76 | + self.grid_layout.addWidget(self.update_button, 1, 0, 1, -1) |
| 77 | + self.grid_layout.addWidget(self.reset_button, 2, 0, 1, -1) |
| 78 | + self.grid_layout.addWidget(self.save_map_button, 3, 0, 1, -1) |
| 79 | + self.grid_layout.addWidget(self.traj_start_index_slider, 4, 0, 1, -1) |
| 80 | + self.grid_layout.addWidget(self.traj_end_index_slider, 5, 0, 1, -1) |
| 81 | + self.setCentralWidget(central_widget) |
| 82 | + |
| 83 | + def initWithEndIndex(self, max_traj_end_index): |
| 84 | + self.max_traj_end_index = max_traj_end_index |
| 85 | + |
| 86 | + # initialize slider |
| 87 | + self.displayed_min_traj_start_index = self.min_traj_start_index |
| 88 | + self.displayed_max_traj_end_index = self.max_traj_end_index |
| 89 | + self.initializeSlider() |
| 90 | + |
| 91 | + def initializeSlider(self, move_value_to_end=True): |
| 92 | + self.traj_start_index_slider.setMinimum(0) |
| 93 | + self.traj_end_index_slider.setMinimum(0) |
| 94 | + self.traj_start_index_slider.setMaximum( |
| 95 | + self.displayed_max_traj_end_index - self.displayed_min_traj_start_index |
| 96 | + ) |
| 97 | + self.traj_end_index_slider.setMaximum( |
| 98 | + self.displayed_max_traj_end_index - self.displayed_min_traj_start_index |
| 99 | + ) |
| 100 | + |
| 101 | + if move_value_to_end: |
| 102 | + self.traj_start_index_slider.setValue(0) |
| 103 | + self.traj_end_index_slider.setValue(self.traj_end_index_slider.maximum()) |
| 104 | + |
| 105 | + def onResetButton(self, event): |
| 106 | + current_traj_start_index = self.displayed_min_traj_start_index |
| 107 | + current_traj_end_index = self.displayed_max_traj_end_index |
| 108 | + |
| 109 | + self.displayed_min_traj_start_index = self.min_traj_start_index |
| 110 | + self.displayed_max_traj_end_index = self.max_traj_end_index |
| 111 | + |
| 112 | + self.initializeSlider(False) |
| 113 | + self.traj_start_index_slider.setValue(current_traj_start_index) |
| 114 | + if ( |
| 115 | + current_traj_start_index == self.min_traj_start_index |
| 116 | + and current_traj_end_index == self.max_traj_end_index |
| 117 | + ): |
| 118 | + self.traj_end_index_slider.setValue(self.displayed_max_traj_end_index) |
| 119 | + else: |
| 120 | + self.traj_end_index_slider.setValue( |
| 121 | + current_traj_start_index + self.traj_end_index_slider.value() |
| 122 | + ) |
| 123 | + |
| 124 | + def onUpdateButton(self, event): |
| 125 | + current_traj_start_index = self.getCurrentStartIndex() |
| 126 | + current_traj_end_index = self.getCurrentEndIndex() |
| 127 | + |
| 128 | + self.displayed_min_traj_start_index = current_traj_start_index |
| 129 | + self.displayed_max_traj_end_index = current_traj_end_index |
| 130 | + |
| 131 | + self.initializeSlider() |
| 132 | + |
| 133 | + def getCurrentStartIndex(self): |
| 134 | + return self.displayed_min_traj_start_index + self.traj_start_index_slider.value() |
| 135 | + |
| 136 | + def getCurrentEndIndex(self): |
| 137 | + return self.displayed_min_traj_start_index + self.traj_end_index_slider.value() |
| 138 | + |
| 139 | + |
| 140 | +class CenterlineUpdaterHelper(Node): |
| 141 | + def __init__(self): |
| 142 | + super().__init__("centerline_updater_helper") |
| 143 | + # Qt |
| 144 | + self.widget = CenterlineUpdaterWidget() |
| 145 | + self.widget.show() |
| 146 | + self.widget.save_map_button.clicked.connect(self.onSaveMapButtonPushed) |
| 147 | + self.widget.traj_start_index_slider.valueChanged.connect(self.onStartIndexChanged) |
| 148 | + self.widget.traj_end_index_slider.valueChanged.connect(self.onEndIndexChanged) |
| 149 | + |
| 150 | + # ROS |
| 151 | + self.pub_save_map = self.create_publisher(Bool, "~/save_map", 1) |
| 152 | + self.pub_traj_start_index = self.create_publisher(Int32, "~/traj_start_index", 1) |
| 153 | + self.pub_traj_end_index = self.create_publisher(Int32, "~/traj_end_index", 1) |
| 154 | + |
| 155 | + transient_local_qos = QoSProfile(depth=1, durability=QoSDurabilityPolicy.TRANSIENT_LOCAL) |
| 156 | + self.sub_whole_centerline = self.create_subscription( |
| 157 | + Trajectory, |
| 158 | + "/static_centerline_optimizer/output_whole_centerline", |
| 159 | + self.onWholeCenterline, |
| 160 | + transient_local_qos, |
| 161 | + ) |
| 162 | + |
| 163 | + while self.widget.max_traj_end_index is None: |
| 164 | + rclpy.spin_once(self, timeout_sec=0.01) |
| 165 | + time.sleep(0.1) |
| 166 | + |
| 167 | + def onWholeCenterline(self, whole_centerline): |
| 168 | + self.widget.initWithEndIndex(len(whole_centerline.points) - 1) |
| 169 | + |
| 170 | + def onSaveMapButtonPushed(self, event): |
| 171 | + msg = Bool() |
| 172 | + msg.data = True |
| 173 | + self.pub_save_map.publish(msg) |
| 174 | + |
| 175 | + def onStartIndexChanged(self, event): |
| 176 | + msg = Int32() |
| 177 | + msg.data = self.widget.getCurrentStartIndex() |
| 178 | + self.pub_traj_start_index.publish(msg) |
| 179 | + |
| 180 | + def onEndIndexChanged(self, event): |
| 181 | + msg = Int32() |
| 182 | + msg.data = self.widget.getCurrentEndIndex() |
| 183 | + self.pub_traj_end_index.publish(msg) |
| 184 | + |
| 185 | + |
| 186 | +def main(args=None): |
| 187 | + app = QApplication(sys.argv) |
| 188 | + |
| 189 | + rclpy.init(args=args) |
| 190 | + node = CenterlineUpdaterHelper() |
| 191 | + |
| 192 | + while True: |
| 193 | + app.processEvents() |
| 194 | + rclpy.spin_once(node, timeout_sec=0.01) |
| 195 | + |
| 196 | + |
| 197 | +if __name__ == "__main__": |
| 198 | + main() |
0 commit comments