Skip to content

Instantly share code, notes, and snippets.

@plusangel
Created October 31, 2021 19:18
Show Gist options
  • Save plusangel/e07f16ab8f83673f9539df95f272b440 to your computer and use it in GitHub Desktop.
Save plusangel/e07f16ab8f83673f9539df95f272b440 to your computer and use it in GitHub Desktop.
rospylib qt app
import sys
from time import sleep
from PySide2.QtCore import Qt, QObject, QThread, Signal, Slot
from PySide2.QtWidgets import (
QApplication,
QLabel,
QMainWindow,
QPushButton,
QVBoxLayout,
QWidget,
)
import roslibpy
class Robot:
def __init__(self):
self.client = roslibpy.Ros(host='192.168.0.78', port=9090)
self.client.run()
print('Is ROS connected?', self.client.is_connected)
class Listener(QObject):
finished = Signal()
progress = Signal(str)
def __init__(self, client):
super().__init__()
listener = roslibpy.Topic(client, '/sensors', 'std_msgs/String')
listener.subscribe(self.run)
def run(self, message):
print(message['data'])
self.progress.emit(message['data'])
# for i in range(5):
# sleep(1)
# self.progress.emit(i + 1)
# self.finished.emit()
class Publisher(QObject):
# finished = Signal()
# progress = Signal(str)
def __init__(self, client):
super().__init__()
self.count = 0
self.publisher = roslibpy.Topic(client, '/cmd_vel', 'geometry_msgs/Twist')
@Slot()
def run(self):
print("sending message")
self.count += 1
self.publisher.publish(roslibpy.Message({'linear': {'x': self.count, 'y': 0.0, 'z': 0.0}, 'angular': {'x': 0.0,'y': 0.0,'z': 0.0}}))
# self.progress.emit(message['data'])
class Window(QMainWindow):
closed = Signal()
forward = Signal()
def __init__(self, parent=None):
super().__init__(parent)
self.robot = Robot()
self.clicksCount = 0
self.setupUi()
self.pub_thread = QThread()
self.publisher = Publisher(self.robot.client)
self.publisher.moveToThread(self.pub_thread)
self.pub_thread.start()
self.forward.connect(self.publisher.run)
def setupUi(self):
self.setWindowTitle("Freezing GUI")
self.resize(300, 150)
self.centralWidget = QWidget()
self.setCentralWidget(self.centralWidget)
# Create and connect widgets
self.clicksLabel = QLabel("Counting: 0 clicks", self)
self.clicksLabel.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
self.stepLabel = QLabel("Long-Running Step: 0")
self.stepLabel.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
self.countBtn = QPushButton("Publisher!", self)
self.countBtn.clicked.connect(self.countClicks)
self.longRunningBtn = QPushButton("Listener!", self)
self.longRunningBtn.clicked.connect(self.runLongTask)
# Set the layout
layout = QVBoxLayout()
layout.addWidget(self.clicksLabel)
layout.addWidget(self.countBtn)
layout.addStretch()
layout.addWidget(self.stepLabel)
layout.addWidget(self.longRunningBtn)
self.centralWidget.setLayout(layout)
def countClicks(self):
self.forward.emit()
# self.clicksCount += 1
# self.clicksLabel.setText(f"Counting: {self.clicksCount} clicks")
def reportProgress(self, n):
self.stepLabel.setText(n)
def runLongTask(self):
self.thread = QThread()
self.listener = Listener(self.robot.client)
self.listener.moveToThread(self.thread)
self.thread.started.connect(self.listener.run)
self.listener.finished.connect(self.thread.quit)
self.listener.finished.connect(self.listener.deleteLater)
self.thread.finished.connect(self.thread.deleteLater)
self.listener.progress.connect(self.reportProgress)
self.closed.connect(self.thread.quit)
self.thread.start()
self.longRunningBtn.setEnabled(False)
self.thread.finished.connect(
lambda: self.longRunningBtn.setEnabled(True)
)
self.thread.finished.connect(
lambda: self.stepLabel.setText("Long-Running Step: 0")
)
def closeEvent(self, event):
self.closed.emit()
event.accept()
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment