I want to create a GUI for my project using ROS2. I do not have any experience in developing graphical applications, so I decided to use Qt Designer for a more "Drag and Drop" experience. I was able to export the contents of the
.ui file into a Python file, using
pyuic5. I present both files, the generated Python file, and the main file below:
generated python file
from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QMainWindow class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(230, 197) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget) self.verticalLayout.setObjectName("verticalLayout") self.dataLabel = QtWidgets.QLabel(self.centralwidget) self.dataLabel.setIndent(10) self.dataLabel.setObjectName("dataLabel") self.verticalLayout.addWidget(self.dataLabel) self.label = QtWidgets.QLabel(self.centralwidget) self.label.setAlignment(QtCore.Qt.AlignCenter) self.label.setObjectName("label") self.verticalLayout.addWidget(self.label) self.updateBtn = QtWidgets.QPushButton(self.centralwidget) self.updateBtn.setObjectName("updateBtn") self.verticalLayout.addWidget(self.updateBtn) MainWindow.setCentralWidget(self.centralwidget) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.dataLabel.setText(_translate("MainWindow", "Subscription to topic : chatter")) self.label.setText(_translate("MainWindow", "TextLabel")) self.updateBtn.setText(_translate("MainWindow", "Update")) class Window(QMainWindow, Ui_MainWindow): def __init__(self, parent=None): super().__init__(parent) self.setupUi(self) self.counter_value = 0 self.label.setText(str(self.counter_value)) self.updateBtn.clicked.connect(self.callback_updateBtn) def callback_updateBtn(self): self.counter_value = self.counter_value + 1 self.label.setText(str(self.counter_value))
As you can see, I have added a small class to initialise and mainly to implement the callback funciton. The
main.py is as follows:
#! /usr/bin/env python3 import sys from PyQt5.QtWidgets import QApplication from PyQt5.uic import loadUi from simple import Window def main(): app = QApplication(sys.argv) win = Window() win.show() sys.exit(app.exec()) if __name__ == "__main__": main()
Now I want to make this GUI function with ROS2. I understand that I will have to create a class for my ROS2 Node (I would like the
Window class to be my ROS2 node). I would also like to set up a subscriber in the constructor of the ROS2 Node, let us say that for the time being it listens to the
chatter topic. I would like to use the
callback_updateBtn function to act as the subscriber callback, and each time it receives a message on the topic, it would update the label with the value passed as the message.
My question is that, how do I go about defining them? I am quite confused, since the main function has the part
# rclpy.init(args=None) # node = Window() app = QApplication(sys.argv) win = Window() win.show() # rclpy.spin(node) sys.exit(app.exec()) # rclpy.shutdown()
which would need to be there, and for ROS2, I would also need to include the commented lines above. I looked at an approach online where they separated the execution on a different thread for ROS, but I do not understand how to go about that. In my mind, I think I should be able to inherit the properties of the
rclpy.Node class into
Window, set up the publishers and subscribers in the constructor, and simply modify the callback function so that now instead of looking at a counter value, I look at the data on the topic.
For my project, I would need to trigger some service calls with the use of Buttons as well. I am trying to get a basic idea of how I should go on about it.
Any help and guidance on this matter would be highly appreciated.
Post score: 4