-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoboWidget.py
80 lines (57 loc) · 1.84 KB
/
RoboWidget.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from QVariant import *
from RoboDiff import RoboDiff
ROBOT_UPDATE = 50 # Update time from robot device in millisec
class RoboWidget(QWidget):
def __init__(self, *args):
super(QWidget,self).__init__(*args)
self.robot = None
self.initWidget()
self.timer = QTimer()
self.timer.timeout.connect(self._update)
def initWidget(self):
self.mainLayout = QGridLayout()
self.stateLabel = QLabel("State:")
self.stateValue = QLabel()
self.powerLabel = QLabel("Power:")
self.powerValue = QLabel()
self.powerButton = QPushButton("Enable")
self.powerButton.clicked.connect(self.switchPower)
self.mainLayout.addWidget(self.stateLabel, 0, 0)
self.mainLayout.addWidget(self.stateValue, 0, 1)
self.mainLayout.addWidget(self.powerLabel, 1, 0)
self.mainLayout.addWidget(self.powerValue, 1, 1)
self.mainLayout.addWidget(self.powerButton, 2, 1)
self.setLayout(self.mainLayout)
def setRobot(self,device):
self.robot = device
self.startRobot()
def startRobot(self):
if self.robot:
self.robot.run()
self.timer.start(ROBOT_UPDATE)
def _update(self):
if self.robot:
self.robot.sync()
self.stateValue.setText( str(self.robot.get_state()) )
self.powerValue.setText( str(self.robot.get_power()) )
def switchPower(self):
self.robot.switchPower()
def test():
import signal
def sig_handler(*args):
print("Stopping robot.")
robot.stop()
sys.exit(0)
signal.signal(signal.SIGINT, sig_handler)
signal.signal(signal.SIGTERM, sig_handler)
app = QApplication([])
win = QMainWindow()
wid = RoboWidget()
#robot = RoboDiff(("160.103.50.80",9020,9100))
robot = RoboDiff()
wid.setRobot(robot)
win.setCentralWidget(wid)
win.show()
sys.exit(app.exec_())
if __name__ == '__main__':
test()